Skip to content

Commit 143aaed

Browse files
committed
Auto merge of #7082 - ehuss:git-fetch-with-cli-env-clean, r=Eh2406
Clean environment when git-fetch-with-cli is used. When the GIT_DIR environment variable is set, git-the-cli will use that instead of looking at cwd. This can happen, for example, when using the `exec` command in `git rebase` to call cargo. This causes cargo to fetch into the wrong directory. Closes #7072
2 parents 83d086d + 00fd31d commit 143aaed

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/cargo/sources/git/utils.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,17 @@ fn fetch_with_cli(
770770
.arg("--update-head-ok") // see discussion in #2078
771771
.arg(url.to_string())
772772
.arg(refspec)
773+
// If cargo is run by git (for example, the `exec` command in `git
774+
// rebase`), the GIT_DIR is set by git and will point to the wrong
775+
// location (this takes precedence over the cwd). Make sure this is
776+
// unset so git will look at cwd for the repo.
777+
.env_remove("GIT_DIR")
778+
// The reset of these may not be necessary, but I'm including them
779+
// just to be extra paranoid and avoid any issues.
780+
.env_remove("GIT_WORK_TREE")
781+
.env_remove("GIT_INDEX_FILE")
782+
.env_remove("GIT_OBJECT_DIRECTORY")
783+
.env_remove("GIT_ALTERNATE_OBJECT_DIRECTORIES")
773784
.cwd(repo.path());
774785
config
775786
.shell()

tests/testsuite/git.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2694,3 +2694,52 @@ fn git_with_cli_force() {
26942694
p.cargo("build").run();
26952695
p.rename_run("foo", "foo2").with_stdout("two").run();
26962696
}
2697+
2698+
#[cargo_test]
2699+
fn git_fetch_cli_env_clean() {
2700+
if disable_git_cli() {
2701+
return;
2702+
}
2703+
// This tests that git-fetch-with-cli works when GIT_DIR environment
2704+
// variable is set (for whatever reason).
2705+
let git_dep = git::new("dep1", |project| {
2706+
project
2707+
.file("Cargo.toml", &basic_manifest("dep1", "0.5.0"))
2708+
.file("src/lib.rs", "")
2709+
})
2710+
.unwrap();
2711+
2712+
let git_proj = git::new("foo", |project| {
2713+
project
2714+
.file(
2715+
"Cargo.toml",
2716+
&format!(
2717+
r#"
2718+
[package]
2719+
name = "foo"
2720+
version = "0.1.0"
2721+
[dependencies]
2722+
dep1 = {{ git = '{}' }}
2723+
"#,
2724+
git_dep.url()
2725+
),
2726+
)
2727+
.file("src/lib.rs", "pub extern crate dep1;")
2728+
.file(
2729+
".cargo/config",
2730+
"
2731+
[net]
2732+
git-fetch-with-cli = true
2733+
",
2734+
)
2735+
})
2736+
.unwrap();
2737+
2738+
// The directory set here isn't too important. Pointing to our own git
2739+
// directory causes git to be confused and fail. Can also point to an
2740+
// empty directory, or a nonexistent one.
2741+
git_proj
2742+
.cargo("fetch")
2743+
.env("GIT_DIR", git_proj.root().join(".git"))
2744+
.run();
2745+
}

0 commit comments

Comments
 (0)