cargo-generate 0.24.0

cargo, make me a project
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//! gix-based replacement for the git2 clone path.
//!
//! Public API surface intentionally mirrors `crate::git::clone_tool::RepoCloneBuilder`
//! so migrating call sites is a type-swap.

use std::num::NonZeroU32;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use console::style;
use gix::prepare_clone;
use gix::remote::fetch::Shallow;
use gix::url;
use log::{debug, info};

use crate::emoji::WRENCH;
use crate::git::{gitconfig, remove_history, utils};

type BranchName = String;

/// Which target the checkout should point to after clone.
enum CheckoutTarget {
    /// A partial ref name like `main` or `feat/one` — resolved as a branch/tag on the remote.
    Ref(String),
    /// A full object ID or a fully-qualified reference (`refs/…`). Fetched as a detached HEAD.
    Revision(String),
}

pub struct RepoCloneBuilder {
    url: String,
    identity_file: Option<PathBuf>,
    target: Option<CheckoutTarget>,
    skip_submodules: bool,
    requires_full_history: bool,
    destination_path: Option<PathBuf>,
}

impl RepoCloneBuilder {
    pub fn new(url: &str) -> Self {
        Self {
            url: url.to_owned(),
            identity_file: None,
            target: None,
            skip_submodules: false,
            requires_full_history: false,
            destination_path: None,
        }
    }

    pub const fn with_submodules(mut self, with_submodules: bool) -> Self {
        self.skip_submodules = !with_submodules;
        self
    }

    /// Uses `gitcfg` (or auto-discovered `~/.gitconfig`) to rewrite the clone url
    /// through any matching `[url "…"] insteadOf` entries, and to bridge
    /// `http.proxy` / `http.noProxy` into the `ALL_PROXY` / `NO_PROXY` env vars
    /// that gix's reqwest transport reads. Env vars set by the shell always win.
    pub fn with_gitconfig(mut self, gitcfg: Option<&Path>) -> Result<Self> {
        if let Some(gitconfig) = gitcfg
            .map(|p| p.to_owned())
            .or_else(|| gitconfig::find_gitconfig().ok().flatten())
        {
            if let Some(url) = gitconfig::resolve_instead_url(&self.url, &gitconfig)? {
                debug!("{} gitconfig 'insteadOf' lead to this url: {}", WRENCH, url);
                self.url = url;
            }
            apply_proxy_from_gitconfig(&gitconfig)?;
        }
        Ok(self)
    }

    pub fn with_ssh_identity(mut self, identity_path: Option<&Path>) -> Result<Self> {
        if let Some(identity_path) = identity_path {
            let identity_path = utils::canonicalize_path(identity_path)?;
            info!(
                "{} `{}` {}",
                style("Using private key:").bold(),
                style(format_args!("{}", identity_path.display()))
                    .bold()
                    .yellow(),
                style("for git-ssh checkout").bold()
            );
            self.identity_file = Some(identity_path);
        }
        Ok(self)
    }

    pub fn with_branch(mut self, branch: Option<&str>) -> Self {
        if let Some(branch) = branch {
            self.target = Some(CheckoutTarget::Ref(branch.to_owned()));
        }
        self
    }

    /// Ensures a specific tag is cloned. Overrides a previously-set revision.
    pub fn with_tag(mut self, tag: Option<&str>) -> Self {
        if let Some(tag) = tag {
            self.target = Some(CheckoutTarget::Ref(tag.to_owned()));
            self.requires_full_history = false;
        }
        self
    }

    /// Ensures a specific revision is cloned. Overrides a previously-set tag.
    pub fn with_revision(mut self, revision: Option<&str>) -> Self {
        if let Some(revision) = revision {
            self.target = Some(CheckoutTarget::Revision(revision.to_owned()));
            self.requires_full_history = true;
        }
        self
    }

    pub fn with_destination(mut self, destination_path: impl AsRef<Path>) -> Result<Self> {
        self.destination_path = Some(utils::canonicalize_path(destination_path.as_ref())?);
        Ok(self)
    }

    pub fn build(self) -> Result<GitCloneCmd> {
        if self.destination_path.is_none() {
            anyhow::bail!("Destination path is not set");
        }
        Ok(GitCloneCmd { builder: self })
    }
}

pub struct GitCloneCmd {
    builder: RepoCloneBuilder,
}

impl GitCloneCmd {
    /// Clones the configured repository and returns the checked-out branch's short name
    /// (or the object id, if HEAD ended up detached).
    pub fn do_clone(self) -> Result<BranchName> {
        let RepoCloneBuilder {
            url: url_str,
            identity_file,
            target,
            skip_submodules,
            requires_full_history,
            destination_path,
        } = self.builder;
        let dest = destination_path.expect("build() enforces destination is set");

        let url =
            url::parse(url_str.as_str()).with_context(|| format!("Invalid git url: {url_str}"))?;
        debug!("{WRENCH} cloning `{url_str}` into `{}`", dest.display());

        // gix's `with_revision` only accepts a full 40-char SHA (or a `refs/…`).
        // Short SHAs — which `git`, `git2`, and cargo-generate's users all accept —
        // are resolved here via a bare peek clone.
        let target = match target {
            Some(CheckoutTarget::Revision(rev)) if looks_like_short_sha(&rev) => {
                let full = peek_resolve_short_sha(&url, &rev, identity_file.as_deref())?;
                Some(CheckoutTarget::Revision(full))
            }
            other => other,
        };

        let mut prepare_clone = prepare_clone(url.clone(), &dest)
            .context("Please check if the Git user / repository exists.")?;

        prepare_clone = match target {
            Some(CheckoutTarget::Ref(name)) => prepare_clone.with_ref_name(Some(name.as_str()))?,
            Some(CheckoutTarget::Revision(rev)) => prepare_clone.with_revision(Some(rev))?,
            None => prepare_clone,
        };

        if should_limit_fetch_depth(&url_str, requires_full_history) {
            let depth = NonZeroU32::new(1).expect("1 is non-zero");
            prepare_clone = prepare_clone.with_shallow(Shallow::DepthAtRemote(depth));
        }

        if let Some(identity) = identity_file.as_deref() {
            // gix has no in-process ssh stack — it shells out to `ssh`.
            // Setting core.sshCommand mirrors `GIT_SSH_COMMAND=ssh -i <key>` for real git.
            prepare_clone = prepare_clone.with_in_memory_config_overrides([format!(
                "core.sshCommand=ssh -i {}",
                sh_single_quote(&identity.display().to_string())
            )]);
        }

        let (mut prepare_checkout, _) = prepare_clone
            .fetch_then_checkout(gix::progress::Discard, &gix::interrupt::IS_INTERRUPTED)
            .context("Please check if the Git user / repository exists.")?;

        let (repo, _) = prepare_checkout
            .main_worktree(gix::progress::Discard, &gix::interrupt::IS_INTERRUPTED)
            .context("Checkout of worktree failed")?;

        if !skip_submodules {
            init_and_update_submodules(&repo, &dest, &url, identity_file.as_deref())?;
        }

        let branch = match repo.head_name()? {
            Some(name) => name.shorten().to_string(),
            None => repo.head_id()?.to_string(),
        };

        // Templates must not carry the source repository's history.
        remove_history(&dest)?;

        Ok(branch)
    }
}

/// Initialize + update submodules recursively using gix only.
///
/// For each active submodule we clone its url pinned at the SHA the superproject
/// recorded (via `Submodule::head_id()`), recurse into it, then strip its `.git`
/// dir — the template semantics of cargo-generate never want inner history.
///
/// gix 0.87 has no high-level submodule init/update API (see gitoxide#2387);
/// this is our own take. The parent's ssh identity override is inherited so
/// private submodules work when the parent clone did.
fn init_and_update_submodules(
    super_repo: &gix::Repository,
    super_worktree: &Path,
    super_url: &gix::Url,
    identity: Option<&Path>,
) -> Result<()> {
    let Some(submodules) = super_repo.submodules()? else {
        return Ok(());
    };

    for sub in submodules {
        // Deliberately skip `sub.is_active()` — that checks `.git/config`, which is
        // empty right after clone (`git submodule init` is what populates it).
        // Templates want every declared submodule initialized.
        let sub_name = sub.name().to_string();
        let sub_rel_path = gix::path::from_bstring(sub.path()?);
        let sub_abs_path = super_worktree.join(&sub_rel_path);
        let sub_url = resolve_submodule_url(super_url, sub.url()?);
        let sub_url_str = sub_url.to_bstring().to_string();
        let pinned = sub.head_id().ok().flatten();

        match &pinned {
            Some(sha) => {
                debug!("{WRENCH} submodule '{sub_name}' → {sub_url_str} @ {sha}");
            }
            None => debug!("{WRENCH} submodule '{sub_name}' → {sub_url_str}"),
        }

        let mut prepare = prepare_clone(sub_url.clone(), &sub_abs_path).with_context(|| {
            format!("Failed to prepare submodule '{sub_name}' from '{sub_url_str}'")
        })?;
        if let Some(sha) = pinned {
            prepare = prepare
                .with_revision(Some(sha.to_string()))
                .with_context(|| {
                    format!("Failed to pin submodule '{sub_name}' from '{sub_url_str}' to {sha}")
                })?;
        }
        if let Some(identity) = identity {
            prepare = prepare.with_in_memory_config_overrides([format!(
                "core.sshCommand=ssh -i {}",
                sh_single_quote(&identity.display().to_string())
            )]);
        }

        let (mut checkout, _) = prepare
            .fetch_then_checkout(gix::progress::Discard, &gix::interrupt::IS_INTERRUPTED)
            .with_context(|| {
                format!("Failed to fetch submodule '{sub_name}' from '{sub_url_str}'")
            })?;
        let (sub_repo, _) = checkout
            .main_worktree(gix::progress::Discard, &gix::interrupt::IS_INTERRUPTED)
            .with_context(|| {
                format!("Failed to checkout submodule '{sub_name}' from '{sub_url_str}'")
            })?;

        // Recurse *before* stripping .git so nested submodules can be read.
        init_and_update_submodules(&sub_repo, &sub_abs_path, &sub_url, identity)?;

        remove_history(&sub_abs_path).with_context(|| {
            format!(
                "Failed to strip .git from submodule '{sub_name}' at '{}'",
                sub_abs_path.display()
            )
        })?;
    }

    Ok(())
}

/// Resolve a submodule url written as `./foo` or `../bar` against the parent's origin.
/// Non-relative urls (http/ssh/git/file with an absolute path) are returned unchanged.
///
/// `gix_url` parses relative paths as `Scheme::File` with the raw path preserved, so we
/// detect the relative shape from there and manipulate the parent's serialized url form
/// with git's classic "pop last path component per `../`" rule.
fn resolve_submodule_url(parent: &gix::Url, sub: gix::Url) -> gix::Url {
    if sub.scheme != gix::url::Scheme::File {
        return sub;
    }
    let sub_path = sub.path.to_string();
    if !sub_path.starts_with("./") && !sub_path.starts_with("../") {
        return sub;
    }
    let parent_str = parent.to_bstring().to_string();
    let joined = join_relative_url(&parent_str, &sub_path);
    gix::url::parse(joined.as_str()).unwrap_or(sub)
}

fn join_relative_url(parent: &str, rel: &str) -> String {
    let mut base = parent.trim_end_matches('/').to_owned();
    let mut rest = rel;
    while let Some(stripped) = rest.strip_prefix("../") {
        if let Some(pos) = base.rfind('/') {
            base.truncate(pos);
        }
        rest = stripped;
    }
    if let Some(stripped) = rest.strip_prefix("./") {
        rest = stripped;
    }
    format!("{base}/{rest}")
}

/// A short SHA is 1..40 lowercase-hex chars — anything else (`HEAD`, `refs/…`,
/// full 40-char SHA) is passed straight to gix.
fn looks_like_short_sha(s: &str) -> bool {
    !s.is_empty() && s.len() < 40 && s.bytes().all(|b| b.is_ascii_hexdigit())
}

/// Resolve a short SHA to its full 40-char form by doing a bare peek clone in a
/// tempdir. gix's `PrepareFetch::with_revision` rejects anything that isn't a
/// full SHA (or `refs/…`), so we can't ask it to expand for us.
fn peek_resolve_short_sha(url: &gix::Url, short: &str, identity: Option<&Path>) -> Result<String> {
    let scratch =
        tempfile::tempdir().context("Failed to create scratch dir for revision resolve")?;
    let mut prepare = gix::prepare_clone_bare(url.clone(), scratch.path())
        .context("Failed to prepare peek clone for short-SHA resolution")?;
    if let Some(identity) = identity {
        prepare = prepare.with_in_memory_config_overrides([format!(
            "core.sshCommand=ssh -i {}",
            sh_single_quote(&identity.display().to_string())
        )]);
    }
    let (repo, _) = prepare
        .fetch_only(gix::progress::Discard, &gix::interrupt::IS_INTERRUPTED)
        .with_context(|| format!("Peek clone to resolve revision '{short}' failed"))?;
    let id = repo
        .rev_parse_single(short)
        .with_context(|| format!("Revision '{short}' could not be resolved on the remote"))?;
    Ok(id.detach().to_string())
}

fn is_http_repo_url(url: &str) -> bool {
    url.starts_with("http://") || url.starts_with("https://")
}

fn should_limit_fetch_depth(url: &str, requires_full_history: bool) -> bool {
    is_http_repo_url(url) && !requires_full_history
}

/// Look up the branch of the git repo at `path`. Returns `None` if the path
/// is not a repository or HEAD is detached / unborn.
pub fn try_get_branch_from_path(path: impl AsRef<Path>) -> Option<String> {
    let repo = gix::open(path.as_ref()).ok()?;
    let name = repo.head_name().ok().flatten()?;
    Some(name.shorten().to_string())
}

/// POSIX single-quote escape: wrap in `'…'`, split-escape any embedded `'`.
/// The `sh -c` interpreter that gix uses for `core.sshCommand` will unquote this back.
fn sh_single_quote(s: &str) -> String {
    format!("'{}'", s.replace('\'', "'\\''"))
}

/// Bridge `http.proxy` / `http.noProxy` from `gitconfig` to the env vars reqwest
/// consults (`ALL_PROXY`, `NO_PROXY`). gix's reqwest transport does not read git
/// config directly, so without this shim these settings are silently ignored.
///
/// Shell env always wins: if any of `ALL_PROXY` / `HTTPS_PROXY` / `HTTP_PROXY`
/// is already set, the gitconfig `http.proxy` is left as-is. The reasoning is
/// that a user who exported an env var meant those to be used, overriding would surprise them.
fn apply_proxy_from_gitconfig(gitconfig: &Path) -> Result<()> {
    let cfg = gitconfig::resolve_http_proxy(gitconfig)?;

    if let Some(proxy) = cfg.proxy.as_deref().filter(|v| !v.is_empty()) {
        if any_proxy_env_var_set() {
            debug!("{WRENCH} skipping gitconfig 'http.proxy'; a proxy env var is already set");
        } else {
            debug!("{WRENCH} applying gitconfig 'http.proxy' -> ALL_PROXY = {proxy}");
            // SAFETY: cargo-generate is single-threaded at this call site — the
            // gix reqwest worker is only spawned on the first fetch, which
            // happens later in do_clone().
            std::env::set_var("ALL_PROXY", proxy);
        }
    }

    if let Some(no_proxy) = cfg.no_proxy.as_deref().filter(|v| !v.is_empty()) {
        if std::env::var_os("NO_PROXY").is_some() || std::env::var_os("no_proxy").is_some() {
            debug!("{WRENCH} skipping gitconfig 'http.noProxy'; NO_PROXY env var is already set");
        } else {
            debug!("{WRENCH} applying gitconfig 'http.noProxy' -> NO_PROXY = {no_proxy}");
            std::env::set_var("NO_PROXY", no_proxy);
        }
    }

    Ok(())
}

fn any_proxy_env_var_set() -> bool {
    [
        "ALL_PROXY",
        "all_proxy",
        "HTTPS_PROXY",
        "https_proxy",
        "HTTP_PROXY",
        "http_proxy",
    ]
    .iter()
    .any(|k| std::env::var_os(k).is_some())
}

#[cfg(test)]
mod tests {
    use crate::git::tmp_dir;

    use super::*;
    use std::fs::metadata;

    #[test]
    fn test_cloning_a_repo() {
        let dst = tmp_dir().unwrap();

        let branch = RepoCloneBuilder::new("https://github.com/cargo-generate/cargo-generate.git")
            .with_destination(dst.path())
            .unwrap()
            .build()
            .unwrap()
            .do_clone()
            .unwrap();

        assert_eq!(branch, "main");
        assert!(metadata(dst.path().join(".git")).is_err());
    }

    #[test]
    fn test_cloning_a_repo_at_revision() {
        let dst = tmp_dir().unwrap();

        let branch = RepoCloneBuilder::new("https://github.com/cargo-generate/cargo-generate.git")
            .with_revision(Some("65748e97b43a5aadd4b34042881c80637c97a30b"))
            .with_destination(dst.path())
            .unwrap()
            .build()
            .unwrap()
            .do_clone()
            .unwrap();

        assert_eq!(branch, "65748e97b43a5aadd4b34042881c80637c97a30b");
        assert!(metadata(dst.path().join(".git")).is_err());
    }

    #[test]
    fn test_cloning_a_repo_with_a_specific_branch() {
        let dst = tmp_dir().unwrap();

        let branch = RepoCloneBuilder::new("https://github.com/cargo-generate/cargo-generate.git")
            .with_branch(Some("feat/1037-gix-as-git2-successor"))
            .with_destination(dst.path())
            .unwrap()
            .build()
            .unwrap()
            .do_clone()
            .unwrap();

        assert_eq!(branch, "feat/1037-gix-as-git2-successor");
        assert!(metadata(dst.path().join(".git")).is_err());
    }

    #[test]
    fn build_requires_destination() {
        let err = match RepoCloneBuilder::new("https://github.com/example/template.git").build() {
            Ok(_) => panic!("expected build() to fail without a destination"),
            Err(err) => err,
        };
        assert!(err.to_string().contains("Destination"));
    }

    #[test]
    fn sh_single_quote_wraps_plain_paths() {
        assert_eq!(
            sh_single_quote("/home/user/.ssh/id_ed25519"),
            "'/home/user/.ssh/id_ed25519'"
        );
    }

    #[test]
    fn sh_single_quote_escapes_embedded_single_quote() {
        assert_eq!(sh_single_quote("/path/it's/key"), "'/path/it'\\''s/key'");
    }

    #[test]
    fn sh_single_quote_preserves_spaces() {
        assert_eq!(
            sh_single_quote("/home/some user/id_rsa"),
            "'/home/some user/id_rsa'"
        );
    }

    #[test]
    fn http_clones_are_shallow_by_default() {
        assert!(should_limit_fetch_depth(
            "https://github.com/example/template",
            false
        ));
    }

    #[test]
    fn revision_clones_skip_shallow_http_fetch() {
        assert!(!should_limit_fetch_depth(
            "https://github.com/example/template",
            true
        ));
    }

    #[test]
    fn non_http_clones_do_not_set_fetch_depth() {
        assert!(!should_limit_fetch_depth("git@example.com:repo.git", false));
    }

    #[test]
    fn join_relative_url_appends_dot_slash() {
        assert_eq!(
            join_relative_url("https://github.com/foo/bar.git", "./baz.git"),
            "https://github.com/foo/bar.git/baz.git"
        );
    }

    #[test]
    fn join_relative_url_walks_up_with_dot_dot_slash() {
        assert_eq!(
            join_relative_url("https://github.com/foo/bar.git", "../baz.git"),
            "https://github.com/foo/baz.git"
        );
    }

    #[test]
    fn join_relative_url_walks_up_twice() {
        assert_eq!(
            join_relative_url("https://github.com/foo/bar/child.git", "../../shared.git"),
            "https://github.com/foo/shared.git"
        );
    }

    #[test]
    fn join_relative_url_handles_file_path_parents() {
        assert_eq!(
            join_relative_url("/tmp/parent-repo", "../shared.git"),
            "/tmp/shared.git"
        );
    }
}