Skip to content

Commit 4e009a6

Browse files
committed
Auto merge of #2696 - sbeckeriv:an-author-by-another-name-1213, r=alexcrichton
An author by another name Dearest Reviewer This is to support environment variables for name and email more like git. This closes #1213 . I did look in to using libgit2 but I did not see a clear way to get the author ident. This just moves from or_else chaining to map_filter with env checks and getting the nth(0) result. The docs I found https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables#Committing do not really indicate an order of preference to the variables. I am more then happy to rearrange the arrays in anyway. I also did not add in a new test for the positive case because the test for the current variables cover that path. Thanks for reviewing, Becker
2 parents ba840ca + 91e4065 commit 4e009a6

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -435,12 +435,20 @@ mod tests {
435435
Ok(())
436436
}
437437

438+
fn get_environment_variable(variables: &[&str] ) -> Option<String>{
439+
variables.iter()
440+
.filter_map(|var| env::var(var).ok())
441+
.next()
442+
}
443+
438444
fn discover_author() -> CargoResult<(String, Option<String>)> {
439445
let git_config = GitConfig::open_default().ok();
440446
let git_config = git_config.as_ref();
447+
let name_variables = ["NAME", "GIT_AUTHOR_NAME", "GIT_COMMITTER_NAME",
448+
"USER", "USERNAME"];
441449
let name = git_config.and_then(|g| g.get_string("user.name").ok())
442-
.or_else(|| env::var("USER").ok()) // unix
443-
.or_else(|| env::var("USERNAME").ok()); // windows
450+
.or_else(|| get_environment_variable(&name_variables));
451+
444452
let name = match name {
445453
Some(name) => name,
446454
None => {
@@ -449,8 +457,9 @@ fn discover_author() -> CargoResult<(String, Option<String>)> {
449457
username_var)
450458
}
451459
};
460+
let email_variables = ["EMAIL", "GIT_AUTHOR_EMAIL", "GIT_COMMITTER_EMAIL"];
452461
let email = git_config.and_then(|g| g.get_string("user.email").ok())
453-
.or_else(|| env::var("EMAIL").ok());
462+
.or_else(|| get_environment_variable(&email_variables) );
454463

455464
let name = name.trim().to_string();
456465
let email = email.map(|s| s.trim().to_string());

tests/test_cargo_new.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,37 @@ test!(finds_author_git {
203203
assert!(contents.contains(r#"authors = ["bar <baz>"]"#));
204204
});
205205

206+
test!(finds_git_email{
207+
let td = TempDir::new("cargo").unwrap();
208+
assert_that(cargo_process("new").arg("foo")
209+
.env("GIT_AUTHOR_NAME", "foo")
210+
.env("GIT_AUTHOR_EMAIL", "gitfoo")
211+
.cwd(td.path().clone()),
212+
execs().with_status(0));
213+
214+
let toml = td.path().join("foo/Cargo.toml");
215+
let mut contents = String::new();
216+
File::open(&toml).unwrap().read_to_string(&mut contents).unwrap();
217+
assert!(contents.contains(r#"authors = ["foo <gitfoo>"]"#), contents);
218+
});
219+
220+
221+
test!(finds_git_author{
222+
// Use a temp dir to make sure we don't pick up .cargo/config somewhere in
223+
// the hierarchy
224+
let td = TempDir::new("cargo").unwrap();
225+
assert_that(cargo_process("new").arg("foo")
226+
.env_remove("USER")
227+
.env("GIT_COMMITTER_NAME", "gitfoo")
228+
.cwd(td.path().clone()),
229+
execs().with_status(0));
230+
231+
let toml = td.path().join("foo/Cargo.toml");
232+
let mut contents = String::new();
233+
File::open(&toml).unwrap().read_to_string(&mut contents).unwrap();
234+
assert!(contents.contains(r#"authors = ["gitfoo"]"#));
235+
});
236+
206237
test!(author_prefers_cargo {
207238
::process("git").args(&["config", "--global", "user.name", "foo"])
208239
.exec().unwrap();

0 commit comments

Comments
 (0)