Skip to main content

gkit_core/
clone.rs

1//! Config-driven clone with explicit flag placement, built-in stateless steps, and
2//! pre/post-clone hooks.
3//!
4//! Per repo, in order: global `pre-clone` → repo `pre-clone` → `git <PRE> clone
5//! <POST> <url> <dir>` → built-ins (git identity + submodule branch-switch +
6//! `direnv allow`) → global `post-clone` → repo `post-clone`.
7//!
8//! Git identity (`user.name`/`user.email`) is **per-invocation, not in the conf**
9//! (the conf is shared across a team): it comes from `Opts` (the `clone`
10//! `--user-name`/`--user-email` flags, or an interactive prompt), and is stamped
11//! `git config` on each cloned repo right after clone so `post-clone` hooks see it.
12//!
13//! The `git clone` and built-ins are **captured** (clean status; an `.envrc` that
14//! runs `glow …` can't distort output — `direnv allow` only records trust). User
15//! hooks run via `sh -c` with their output **inherited** (explicit commands, shown
16//! live) and `$GKIT_REPO`/`GKIT_DIR`/`GKIT_URL`/`GKIT_HOST`/`GKIT_NAMESPACE` set
17//! (plus `GKIT_USER_NAME`/`GKIT_USER_EMAIL`, empty when no identity was given).
18
19use crate::conf::{expand_path, CloneConf};
20use crate::git::Git;
21use std::path::{Path, PathBuf};
22use std::process::Command;
23
24#[derive(Debug, PartialEq, Eq)]
25pub enum Outcome {
26    Cloned,
27    Skipped,
28    Failed(String),
29}
30
31#[derive(Debug)]
32pub struct CloneReport {
33    pub name: String,
34    pub dir: PathBuf,
35    pub outcome: Outcome,
36    pub command: String,
37}
38
39pub struct Opts {
40    pub submodule_branch: bool,
41    pub direnv: bool,
42    /// Git identity stamped on each cloned repo (`git config user.name`). Per
43    /// invocation, not from the conf — `None` leaves the repo's inherited identity.
44    pub user_name: Option<String>,
45    /// Git identity stamped on each cloned repo (`git config user.email`).
46    pub user_email: Option<String>,
47    /// Absolute path to the conf file driving this clone, stamped as `gkit.conf` on
48    /// each top-level repo so `gkit stamp` (run inside the repo, no arg) can later
49    /// resolve its own conf. `None` (e.g. tests) skips the stamp.
50    pub conf_path: Option<String>,
51}
52
53impl Default for Opts {
54    fn default() -> Self {
55        Self {
56            submodule_branch: true,
57            direnv: true,
58            user_name: None,
59            user_email: None,
60            conf_path: None,
61        }
62    }
63}
64
65// Also reused by `fixsub` (re-applies this branch-switch over an existing tree).
66pub(crate) const SUBMODULE_SWITCH: &str = "b=$(git config -f \"$toplevel/.gitmodules\" \"submodule.$name.branch\" 2>/dev/null || echo main); git switch \"$b\" 2>/dev/null || true";
67
68/// Single-quote a value for safe interpolation into an `sh -c` command line
69/// (each embedded `'` becomes `'\''`). Shared with `fixsub`.
70pub(crate) fn sh_squote(s: &str) -> String {
71    format!("'{}'", s.replace('\'', "'\\''"))
72}
73
74/// The `git submodule foreach --recursive` body that stamps the resolved identity
75/// into each submodule, values single-quoted for `sh`. `None` when no identity was
76/// given (so the caller skips the recursion entirely).
77fn submodule_identity_cmd(user_name: Option<&str>, user_email: Option<&str>) -> Option<String> {
78    let parts: Vec<String> = [("user.name", user_name), ("user.email", user_email)]
79        .into_iter()
80        .filter_map(|(k, v)| v.map(|v| format!("git config {k} {}", sh_squote(v))))
81        .collect();
82    (!parts.is_empty()).then(|| parts.join("; "))
83}
84
85/// The git-config `(key, value)` for the **namespace-scoped** `insteadOf` rewrite that
86/// lets a *canonical* submodule URL route through the alias's key:
87///   key   = `url.<alias>:<ns>/.insteadOf`   value = `git@<hostname>:<ns>/`
88/// so git rewrites `git@<hostname>:<ns>/repo.git` → `<alias>:<ns>/repo.git` → `id_<alias>`.
89/// The trailing `/` on both sides scopes the rule to the namespace (so multiple aliases
90/// on the same host — different clients — each keep their own key).
91pub fn insteadof_pair(alias: &str, hostname: &str, ns: &str) -> (String, String) {
92    (
93        format!("url.{alias}:{ns}/.insteadOf"),
94        format!("git@{hostname}:{ns}/"),
95    )
96}
97
98/// Distinct namespaces across a conf's repos (each repo's effective namespace), in
99/// conf order, deduplicated — one `insteadOf` rule is written per distinct namespace.
100pub fn distinct_namespaces(conf: &CloneConf) -> Vec<String> {
101    let mut out: Vec<String> = Vec::new();
102    for r in &conf.repo {
103        if let Some(ns) = conf.namespace_for(r) {
104            if !out.iter().any(|n| n == ns) {
105                out.push(ns.to_string());
106            }
107        }
108    }
109    out
110}
111
112/// Run hook commands via `sh -c` in `cwd` with `env` set; output inherited; each
113/// printed `+ <cmd>`. Stops at the first non-zero exit. Shared with `stamp`, which
114/// re-runs a conf's `post-clone` over an existing tree.
115pub(crate) fn run_hooks(cmds: &[String], cwd: &Path, env: &[(&str, &str)]) -> Result<(), String> {
116    for cmd in cmds {
117        println!("+ {cmd}");
118        let mut c = Command::new("sh");
119        c.arg("-c").arg(cmd).current_dir(cwd);
120        for (k, v) in env {
121            c.env(k, v);
122        }
123        match c.status() {
124            Ok(s) if s.success() => {}
125            Ok(s) => return Err(format!("hook `{cmd}` exited {}", s.code().unwrap_or(-1))),
126            Err(e) => return Err(format!("hook `{cmd}` failed to start: {e}")),
127        }
128    }
129    Ok(())
130}
131
132/// Clone every repo in `conf`, printing each step in order. Returns a report per
133/// repo (for the aggregate exit code).
134pub fn clone_all<G: Git>(git: &G, conf: &CloneConf, opts: &Opts) -> Vec<CloneReport> {
135    conf.repo
136        .iter()
137        .map(|r| {
138            let name = r.name();
139            let dir_s = expand_path(&r.dir, |k| std::env::var(k).ok());
140            let dir = PathBuf::from(&dir_s);
141            // Per-repo namespace overrides the global one; `clone_cmd` validates this
142            // up front, so `None` here is a defensive backstop, not a normal path.
143            let ns = match conf.namespace_for(r) {
144                Some(n) => n.to_string(),
145                None => {
146                    let e = format!("no namespace for {}", r.dir);
147                    println!("FAILED   {name:<28} {e}");
148                    return CloneReport {
149                        name,
150                        dir,
151                        outcome: Outcome::Failed(e),
152                        command: String::new(),
153                    };
154                }
155            };
156            let url = format!("{}:{}/{}.git", conf.host, ns, name);
157
158            // git <git-flags> clone <depth/branch> --recurse-submodules <clone-flags> <repo flags> <url> <dir>
159            let mut args: Vec<String> = Vec::new();
160            args.extend(conf.git_flags.iter().cloned());
161            args.push("clone".into());
162            if let Some(d) = r.depth {
163                args.push("--depth".into());
164                args.push(d.to_string());
165            }
166            if let Some(b) = &r.branch {
167                args.push("--branch".into());
168                args.push(b.clone());
169                args.push("--single-branch".into());
170            }
171            args.push("--recurse-submodules".into());
172            args.extend(conf.clone_flags.iter().cloned());
173            args.extend(r.clone_flags.iter().cloned());
174            args.push(url.clone());
175            args.push(dir_s.clone());
176            let command = format!("git {}", args.join(" "));
177
178            let mk = |outcome| CloneReport {
179                name: name.clone(),
180                dir: dir.clone(),
181                outcome,
182                command: command.clone(),
183            };
184
185            if dir.join(".git").exists() {
186                println!("+ {command}");
187                println!("skipped  {name:<28} {dir_s} (exists)");
188                return mk(Outcome::Skipped);
189            }
190
191            let env = [
192                ("GKIT_REPO", name.as_str()),
193                ("GKIT_DIR", dir_s.as_str()),
194                ("GKIT_URL", url.as_str()),
195                ("GKIT_HOST", conf.host.as_str()),
196                ("GKIT_NAMESPACE", ns.as_str()),
197                ("GKIT_USER_NAME", opts.user_name.as_deref().unwrap_or("")),
198                ("GKIT_USER_EMAIL", opts.user_email.as_deref().unwrap_or("")),
199            ];
200
201            // 1+2: pre-clone hooks (cwd = parent of target; create it first)
202            let parent = dir.parent().unwrap_or(Path::new("."));
203            let _ = std::fs::create_dir_all(parent);
204            let pre: Vec<String> = conf
205                .pre_clone
206                .0
207                .iter()
208                .chain(r.pre_clone.0.iter())
209                .cloned()
210                .collect();
211            if let Err(e) = run_hooks(&pre, parent, &env) {
212                println!("FAILED   {name:<28} {e}");
213                return mk(Outcome::Failed(e));
214            }
215
216            // 3: clone (printed; output captured)
217            println!("+ {command}");
218            let refs: Vec<&str> = args.iter().map(String::as_str).collect();
219            let out = git.run(Path::new("."), &refs);
220            if !out.success {
221                let e = out.stderr.trim().to_string();
222                println!("FAILED   {name:<28} {}", e.lines().next().unwrap_or(""));
223                return mk(Outcome::Failed(e));
224            }
225
226            // 4: built-ins. Identity first (printed; values are explicit user input)
227            // so post-clone hooks and direnv see it; a failure fails the repo.
228            let identity: Vec<(&str, &str)> = [
229                ("user.name", opts.user_name.as_deref()),
230                ("user.email", opts.user_email.as_deref()),
231            ]
232            .into_iter()
233            .filter_map(|(k, v)| Some((k, v?)))
234            .collect();
235            // 4a: the superproject (args passed straight to git — no shell).
236            for (key, val) in &identity {
237                println!("+ git config {key} {val}");
238                let out = git.run(&dir, &["config", key, val]);
239                if !out.success {
240                    let e = format!("git config {key} failed: {}", out.stderr.trim());
241                    println!("FAILED   {name:<28} {e}");
242                    return mk(Outcome::Failed(e));
243                }
244            }
245            // 4a': stamp gkit.conf (absolute conf path) on the superproject so
246            // `gkit stamp` (no arg, run inside this repo) can resolve its conf later.
247            if let Some(cp) = opts.conf_path.as_deref() {
248                println!("+ git config gkit.conf {cp}");
249                let out = git.run(&dir, &["config", "gkit.conf", cp]);
250                if !out.success {
251                    let e = format!("git config gkit.conf failed: {}", out.stderr.trim());
252                    println!("FAILED   {name:<28} {e}");
253                    return mk(Outcome::Failed(e));
254                }
255            }
256            // 4b: the same identity into every submodule (recursive) so commits there
257            // use it too — a submodule is its own repo with its own config. Runs via
258            // `sh -c`, so the values are single-quoted.
259            if let Some(body) =
260                submodule_identity_cmd(opts.user_name.as_deref(), opts.user_email.as_deref())
261            {
262                println!("+ git submodule foreach --recursive {body}");
263                let out = git.run(
264                    &dir,
265                    &["submodule", "foreach", "--recursive", body.as_str()],
266                );
267                if !out.success {
268                    let e = format!("submodule identity failed: {}", out.stderr.trim());
269                    println!("FAILED   {name:<28} {e}");
270                    return mk(Outcome::Failed(e));
271                }
272            }
273            // remaining built-ins (captured)
274            if opts.submodule_branch {
275                let _ = git.run(
276                    &dir,
277                    &["submodule", "foreach", "--recursive", SUBMODULE_SWITCH],
278                );
279            }
280            if opts.direnv && dir.join(".envrc").exists() {
281                let _ = Command::new("direnv").arg("allow").arg(&dir).output(); // trust-only, no eval
282            }
283
284            // 5+6: post-clone hooks (cwd = the cloned repo)
285            let post: Vec<String> = conf
286                .post_clone
287                .0
288                .iter()
289                .chain(r.post_clone.0.iter())
290                .cloned()
291                .collect();
292            if let Err(e) = run_hooks(&post, &dir, &env) {
293                println!("FAILED   {name:<28} {e}");
294                return mk(Outcome::Failed(e));
295            }
296
297            println!("cloned   {name:<28} {dir_s}");
298            mk(Outcome::Cloned)
299        })
300        .collect()
301}
302
303#[cfg(test)]
304mod tests {
305    use super::{sh_squote, submodule_identity_cmd};
306    use crate::conf;
307
308    #[test]
309    fn submodule_identity_cmd_quotes_and_skips() {
310        // both fields → two `git config`s, single-quoted, joined with `; `
311        assert_eq!(
312            submodule_identity_cmd(Some("Jane Dev"), Some("jane@acme.com")).as_deref(),
313            Some("git config user.name 'Jane Dev'; git config user.email 'jane@acme.com'")
314        );
315        // only one field set → just that one
316        assert_eq!(
317            submodule_identity_cmd(Some("Jane"), None).as_deref(),
318            Some("git config user.name 'Jane'")
319        );
320        // neither → None (caller skips the recursion)
321        assert_eq!(submodule_identity_cmd(None, None), None);
322        // an embedded single quote is escaped so `sh` can't break out
323        assert_eq!(
324            submodule_identity_cmd(Some("O'Brien"), None).as_deref(),
325            Some(r"git config user.name 'O'\''Brien'")
326        );
327        assert_eq!(sh_squote("a b"), "'a b'");
328    }
329
330    #[test]
331    fn insteadof_pair_is_namespace_scoped() {
332        // bitbucket client
333        assert_eq!(
334            super::insteadof_pair("tlbb", "bitbucket.org", "codogenics"),
335            (
336                "url.tlbb:codogenics/.insteadOf".to_string(),
337                "git@bitbucket.org:codogenics/".to_string()
338            )
339        );
340        // gitlab subgroup namespace keeps its slash
341        assert_eq!(
342            super::insteadof_pair("ctl", "gitlab.com", "grp/sub").1,
343            "git@gitlab.com:grp/sub/"
344        );
345    }
346
347    #[test]
348    fn distinct_namespaces_dedups_in_order() {
349        let c = conf::parse(
350            "host=\"h\"\nnamespace=\"glob\"\n\
351             [[repo]]\ndir=\"$H/a\"\n\
352             [[repo]]\ndir=\"$H/b\"\nnamespace=\"bob\"\n\
353             [[repo]]\ndir=\"$H/c\"\n",
354        )
355        .unwrap();
356        // glob (a), bob (b override), glob again (c) → [glob, bob], deduped, in order
357        assert_eq!(super::distinct_namespaces(&c), vec!["glob", "bob"]);
358    }
359
360    #[test]
361    fn opts_default_has_no_conf_path() {
362        // gkit.conf is opt-in: the default (used by tests / non-clone callers)
363        // leaves it unstamped.
364        assert_eq!(super::Opts::default().conf_path, None);
365    }
366
367    #[test]
368    fn builds_expected_url_shape() {
369        let c = conf::parse("host = \"tlbb\"\nnamespace = \"example-org\"\n[[repo]]\ndir = \"$HOME/x/cosp\"\ndepth = 1\n").unwrap();
370        assert_eq!(c.repo[0].name(), "cosp");
371        assert_eq!(c.repo[0].depth, Some(1));
372        let ns = c.namespace_for(&c.repo[0]).unwrap();
373        let url = format!("{}:{}/{}.git", c.host, ns, c.repo[0].name());
374        assert_eq!(url, "tlbb:example-org/cosp.git");
375    }
376
377    #[test]
378    fn per_repo_namespace_drives_url() {
379        let c = conf::parse("host=\"gh\"\n[[repo]]\ndir=\"$HOME/x/foo\"\nnamespace=\"alice\"\n")
380            .unwrap();
381        let ns = c.namespace_for(&c.repo[0]).unwrap();
382        let url = format!("{}:{}/{}.git", c.host, ns, c.repo[0].name());
383        assert_eq!(url, "gh:alice/foo.git");
384    }
385}