Skip to content

Commit 4e32fba

Browse files
committed
Auto merge of #8886 - justfortherec:fix/8882/gitconfig-includeif, r=alexcrichton
Start searching git config at new path This lets `cargo new` follow `includeIf` blocks inside .gitignore. Fixes #8882 I am not sure if removing the `__CARGO_TEST_ROOT` environment variable has any bad side effects. My quick grep of the repository didn't highlight anything in particular.
2 parents 4877f4a + 35b029c commit 4e32fba

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ fn mk(config: &Config, opts: &MkOptions<'_>) -> CargoResult<()> {
645645
init_vcs(path, vcs, config)?;
646646
write_ignore_file(path, &ignore, vcs)?;
647647

648-
let (author_name, email) = discover_author()?;
648+
let (author_name, email) = discover_author(path)?;
649649
let author = match (cfg.name, cfg.email, author_name, email) {
650650
(Some(name), Some(email), _, _)
651651
| (Some(name), None, _, Some(email))
@@ -781,8 +781,8 @@ fn get_environment_variable(variables: &[&str]) -> Option<String> {
781781
variables.iter().filter_map(|var| env::var(var).ok()).next()
782782
}
783783

784-
fn discover_author() -> CargoResult<(String, Option<String>)> {
785-
let git_config = find_git_config();
784+
fn discover_author(path: &Path) -> CargoResult<(String, Option<String>)> {
785+
let git_config = find_git_config(path);
786786
let git_config = git_config.as_ref();
787787

788788
let name_variables = [
@@ -833,10 +833,10 @@ fn discover_author() -> CargoResult<(String, Option<String>)> {
833833
Ok((name, email))
834834
}
835835

836-
fn find_git_config() -> Option<GitConfig> {
836+
fn find_git_config(path: &Path) -> Option<GitConfig> {
837837
match env::var("__CARGO_TEST_ROOT") {
838838
Ok(test_root) => find_tests_git_config(test_root),
839-
Err(_) => find_real_git_config(),
839+
Err(_) => find_real_git_config(path),
840840
}
841841
}
842842

@@ -851,12 +851,9 @@ fn find_tests_git_config(cargo_test_root: String) -> Option<GitConfig> {
851851
}
852852
}
853853

854-
fn find_real_git_config() -> Option<GitConfig> {
855-
match env::current_dir() {
856-
Ok(cwd) => GitRepository::discover(cwd)
857-
.and_then(|repo| repo.config())
858-
.or_else(|_| GitConfig::open_default())
859-
.ok(),
860-
Err(_) => GitConfig::open_default().ok(),
861-
}
854+
fn find_real_git_config(path: &Path) -> Option<GitConfig> {
855+
GitRepository::discover(path)
856+
.and_then(|repo| repo.config())
857+
.or_else(|_| GitConfig::open_default())
858+
.ok()
862859
}

tests/testsuite/new.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Tests for the `cargo new` command.
22
3-
use cargo_test_support::paths;
3+
use cargo_test_support::paths::{self, CargoPathExt};
44
use cargo_test_support::{cargo_process, git_process};
55
use std::env;
66
use std::fs::{self, File};
@@ -305,6 +305,50 @@ fn finds_git_author() {
305305
assert!(contents.contains(r#"authors = ["foo <gitfoo>"]"#), contents);
306306
}
307307

308+
#[cargo_test]
309+
fn finds_git_author_in_included_config() {
310+
let included_gitconfig = paths::root().join("foo").join(".gitconfig");
311+
included_gitconfig.parent().unwrap().mkdir_p();
312+
fs::write(
313+
&included_gitconfig,
314+
r#"
315+
[user]
316+
name = foo
317+
email = bar
318+
"#,
319+
)
320+
.unwrap();
321+
322+
let gitconfig = paths::home().join(".gitconfig");
323+
fs::write(
324+
&gitconfig,
325+
format!(
326+
r#"
327+
[includeIf "gitdir/i:{}"]
328+
path = {}
329+
"#,
330+
included_gitconfig
331+
.parent()
332+
.unwrap()
333+
.join("")
334+
.display()
335+
.to_string()
336+
.replace("\\", "/"),
337+
included_gitconfig.display().to_string().replace("\\", "/"),
338+
)
339+
.as_bytes(),
340+
)
341+
.unwrap();
342+
343+
cargo_process("new foo/bar")
344+
// Avoid the special treatment of tests to find git configuration
345+
.env_remove("__CARGO_TEST_ROOT")
346+
.run();
347+
let toml = paths::root().join("foo/bar/Cargo.toml");
348+
let contents = fs::read_to_string(&toml).unwrap();
349+
assert!(contents.contains(r#"authors = ["foo <bar>"]"#), contents,);
350+
}
351+
308352
#[cargo_test]
309353
fn finds_git_committer() {
310354
create_empty_gitconfig();

0 commit comments

Comments
 (0)