Skip to content

Commit 97046d0

Browse files
committed
Auto merge of #6252 - alexcrichton:clone-and-templtes, r=dwijnand
Never use templates when managing git repos This commit disables usage of git templates whenever Cargo manages repositories in its internal git database. Templates don't want to be used at all in these situations and have been known to cause usability bugs. Closes #6240
2 parents 6026fc3 + 2a4cdc6 commit 97046d0

File tree

2 files changed

+59
-8
lines changed

2 files changed

+59
-8
lines changed

src/cargo/sources/git/utils.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl GitRemote {
146146
paths::remove_dir_all(dst)?;
147147
}
148148
fs::create_dir_all(dst)?;
149-
let mut repo = git2::Repository::init_bare(dst)?;
149+
let mut repo = init(dst, true)?;
150150
fetch(
151151
&mut repo,
152152
&self.url,
@@ -385,7 +385,7 @@ impl<'a> GitCheckout<'a> {
385385
Err(..) => {
386386
let path = parent.workdir().unwrap().join(child.path());
387387
let _ = paths::remove_dir_all(&path);
388-
git2::Repository::init(&path)?
388+
init(&path, false)?
389389
}
390390
};
391391

@@ -833,7 +833,7 @@ fn reinitialize(repo: &mut git2::Repository) -> CargoResult<()> {
833833
debug!("reinitializing git repo at {:?}", path);
834834
let tmp = path.join("tmp");
835835
let bare = !repo.path().ends_with(".git");
836-
*repo = git2::Repository::init(&tmp)?;
836+
*repo = init(&tmp, false)?;
837837
for entry in path.read_dir()? {
838838
let entry = entry?;
839839
if entry.file_name().to_str() == Some("tmp") {
@@ -842,15 +842,21 @@ fn reinitialize(repo: &mut git2::Repository) -> CargoResult<()> {
842842
let path = entry.path();
843843
drop(paths::remove_file(&path).or_else(|_| paths::remove_dir_all(&path)));
844844
}
845-
if bare {
846-
*repo = git2::Repository::init_bare(path)?;
847-
} else {
848-
*repo = git2::Repository::init(path)?;
849-
}
845+
*repo = init(&path, bare)?;
850846
paths::remove_dir_all(&tmp)?;
851847
Ok(())
852848
}
853849

850+
fn init(path: &Path, bare: bool) -> CargoResult<git2::Repository> {
851+
let mut opts = git2::RepositoryInitOptions::new();
852+
// Skip anyting related to templates, they just call all sorts of issues as
853+
// we really don't want to use them yet they insist on being used. See #6240
854+
// for an example issue that comes up.
855+
opts.external_template(false);
856+
opts.bare(bare);
857+
Ok(git2::Repository::init_opts(&path, &opts)?)
858+
}
859+
854860
/// Updating the index is done pretty regularly so we want it to be as fast as
855861
/// possible. For registries hosted on GitHub (like the crates.io index) there's
856862
/// a fast path available to use [1] to tell us that there's no updates to be

tests/testsuite/git.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2625,3 +2625,48 @@ fn use_the_cli() {
26252625

26262626
project.cargo("build -v").with_stderr(stderr).run();
26272627
}
2628+
2629+
#[test]
2630+
fn templatedir_doesnt_cause_problems() {
2631+
let git_project2 = git::new("dep2", |project| {
2632+
project
2633+
.file("Cargo.toml", &basic_manifest("dep2", "0.5.0"))
2634+
.file("src/lib.rs", "")
2635+
}).unwrap();
2636+
let git_project = git::new("dep1", |project| {
2637+
project
2638+
.file("Cargo.toml", &basic_manifest("dep1", "0.5.0"))
2639+
.file("src/lib.rs", "")
2640+
}).unwrap();
2641+
let p = project()
2642+
.file(
2643+
"Cargo.toml",
2644+
&format!(r#"
2645+
[project]
2646+
name = "fo"
2647+
version = "0.5.0"
2648+
authors = []
2649+
2650+
[dependencies]
2651+
dep1 = {{ git = '{}' }}
2652+
"#, git_project.url()),
2653+
).file("src/main.rs", "fn main() {}")
2654+
.build();
2655+
2656+
File::create(paths::home().join(".gitconfig"))
2657+
.unwrap()
2658+
.write_all(
2659+
&format!(r#"
2660+
[init]
2661+
templatedir = {}
2662+
"#, git_project2.url()
2663+
.to_file_path()
2664+
.unwrap()
2665+
.to_str()
2666+
.unwrap()
2667+
.replace("\\", "/")
2668+
).as_bytes(),
2669+
).unwrap();
2670+
2671+
p.cargo("build").run();
2672+
}

0 commit comments

Comments
 (0)