Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start searching git config at new path #8886

Merged
merged 1 commit into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ fn mk(config: &Config, opts: &MkOptions<'_>) -> CargoResult<()> {
init_vcs(path, vcs, config)?;
write_ignore_file(path, &ignore, vcs)?;

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

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

let name_variables = [
Expand Down Expand Up @@ -833,10 +833,10 @@ fn discover_author() -> CargoResult<(String, Option<String>)> {
Ok((name, email))
}

fn find_git_config() -> Option<GitConfig> {
fn find_git_config(path: &Path) -> Option<GitConfig> {
match env::var("__CARGO_TEST_ROOT") {
Ok(test_root) => find_tests_git_config(test_root),
Err(_) => find_real_git_config(),
Err(_) => find_real_git_config(path),
}
}

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

fn find_real_git_config() -> Option<GitConfig> {
match env::current_dir() {
Ok(cwd) => GitRepository::discover(cwd)
.and_then(|repo| repo.config())
.or_else(|_| GitConfig::open_default())
.ok(),
Err(_) => GitConfig::open_default().ok(),
}
fn find_real_git_config(path: &Path) -> Option<GitConfig> {
GitRepository::discover(path)
.and_then(|repo| repo.config())
.or_else(|_| GitConfig::open_default())
.ok()
}
46 changes: 45 additions & 1 deletion tests/testsuite/new.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Tests for the `cargo new` command.

use cargo_test_support::paths;
use cargo_test_support::paths::{self, CargoPathExt};
use cargo_test_support::{cargo_process, git_process};
use std::env;
use std::fs::{self, File};
Expand Down Expand Up @@ -305,6 +305,50 @@ fn finds_git_author() {
assert!(contents.contains(r#"authors = ["foo <gitfoo>"]"#), contents);
}

#[cargo_test]
fn finds_git_author_in_included_config() {
let included_gitconfig = paths::root().join("foo").join(".gitconfig");
included_gitconfig.parent().unwrap().mkdir_p();
fs::write(
&included_gitconfig,
r#"
[user]
name = foo
email = bar
"#,
)
.unwrap();

let gitconfig = paths::home().join(".gitconfig");
fs::write(
&gitconfig,
format!(
r#"
[includeIf "gitdir/i:{}"]
path = {}
"#,
included_gitconfig
.parent()
.unwrap()
.join("")
.display()
.to_string()
.replace("\\", "/"),
included_gitconfig.display().to_string().replace("\\", "/"),
)
.as_bytes(),
)
.unwrap();

cargo_process("new foo/bar")
// Avoid the special treatment of tests to find git configuration
.env_remove("__CARGO_TEST_ROOT")
.run();
let toml = paths::root().join("foo/bar/Cargo.toml");
let contents = fs::read_to_string(&toml).unwrap();
assert!(contents.contains(r#"authors = ["foo <bar>"]"#), contents,);
}

#[cargo_test]
fn finds_git_committer() {
create_empty_gitconfig();
Expand Down