Skip to content

Commit bd32ba7

Browse files
committed
Auto merge of #6243 - btashton:strip_email_bracket, r=alexcrichton
Strip angle brackets from author email before passing to template. Some people already have angle brackets around their email in git settings or other author sources. Right now if you create a new project the Cargo.toml would render something like: `authors = ["bar <<foo@baz>>"]` instead of `authors = ["bar <foo@baz>"]` This detects the emails that start and end with `<>` and removes them.
2 parents fd9cb98 + 7d57948 commit bd32ba7

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,17 @@ fn discover_author() -> CargoResult<(String, Option<String>)> {
660660
.or_else(|| get_environment_variable(&email_variables[3..]));
661661

662662
let name = name.trim().to_string();
663-
let email = email.map(|s| s.trim().to_string());
663+
let email = email.map(|s| {
664+
let mut s = s.trim();
665+
666+
// In some cases emails will already have <> remove them since they
667+
// are already added when needed.
668+
if s.starts_with("<") && s.ends_with(">") {
669+
s = &s[1..s.len() - 1];
670+
}
671+
672+
s.to_string()
673+
});
664674

665675
Ok((name, email))
666676
}

tests/testsuite/new.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,23 @@ fn author_prefers_cargo() {
337337
assert!(!root.join("foo/.gitignore").exists());
338338
}
339339

340+
#[test]
341+
fn strip_angle_bracket_author_email() {
342+
create_empty_gitconfig();
343+
cargo_process("new foo")
344+
.env("USER", "bar")
345+
.env("EMAIL", "<baz>")
346+
.run();
347+
348+
let toml = paths::root().join("foo/Cargo.toml");
349+
let mut contents = String::new();
350+
File::open(&toml)
351+
.unwrap()
352+
.read_to_string(&mut contents)
353+
.unwrap();
354+
assert!(contents.contains(r#"authors = ["bar <baz>"]"#));
355+
}
356+
340357
#[test]
341358
fn git_prefers_command_line() {
342359
let root = paths::root();

0 commit comments

Comments
 (0)