Skip to content

Commit

Permalink
RFC 3052: Stop including authors field in manifests made by cargo new
Browse files Browse the repository at this point in the history
  • Loading branch information
lf- authored and ehuss committed Mar 22, 2021
1 parent 39d9413 commit c685eb3
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 372 deletions.
109 changes: 0 additions & 109 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ use crate::util::errors::{CargoResult, CargoResultExt};
use crate::util::{existing_vcs_repo, FossilRepo, GitRepo, HgRepo, PijulRepo};
use crate::util::{restricted_names, Config};
use cargo_util::paths;
use git2::Config as GitConfig;
use git2::Repository as GitRepository;
use serde::de;
use serde::Deserialize;
use std::collections::BTreeMap;
use std::env;
use std::fmt;
use std::io::{BufRead, BufReader, ErrorKind};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -129,8 +126,6 @@ impl NewOptions {

#[derive(Deserialize)]
struct CargoNewConfig {
name: Option<String>,
email: Option<String>,
#[serde(rename = "vcs")]
version_control: Option<VersionControl>,
}
Expand Down Expand Up @@ -666,32 +661,6 @@ fn mk(config: &Config, opts: &MkOptions<'_>) -> CargoResult<()> {
init_vcs(path, vcs, config)?;
write_ignore_file(path, &ignore, vcs)?;

let (discovered_name, discovered_email) = discover_author(path);

// "Name <email>" or "Name" or "<email>" or None if neither name nor email is obtained
// cfg takes priority over the discovered ones
let author_name = cfg.name.or(discovered_name);
let author_email = cfg.email.or(discovered_email);

let author = match (author_name, author_email) {
(Some(name), Some(email)) => {
if email.is_empty() {
Some(name)
} else {
Some(format!("{} <{}>", name, email))
}
}
(Some(name), None) => Some(name),
(None, Some(email)) => {
if email.is_empty() {
None
} else {
Some(format!("<{}>", email))
}
}
(None, None) => None,
};

let mut cargotoml_path_specifier = String::new();

// Calculate what `[lib]` and `[[bin]]`s we need to append to `Cargo.toml`.
Expand Down Expand Up @@ -730,18 +699,13 @@ path = {}
r#"[package]
name = "{}"
version = "0.1.0"
authors = [{}]
edition = {}
{}
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
{}"#,
name,
match author {
Some(value) => format!("{}", toml::Value::String(value)),
None => format!(""),
},
match opts.edition {
Some(edition) => toml::Value::String(edition.to_string()),
None => toml::Value::String(Edition::LATEST_STABLE.to_string()),
Expand Down Expand Up @@ -811,76 +775,3 @@ mod tests {

Ok(())
}

fn get_environment_variable(variables: &[&str]) -> Option<String> {
variables.iter().filter_map(|var| env::var(var).ok()).next()
}

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

let name_variables = [
"CARGO_NAME",
"GIT_AUTHOR_NAME",
"GIT_COMMITTER_NAME",
"USER",
"USERNAME",
"NAME",
];
let name = get_environment_variable(&name_variables[0..3])
.or_else(|| git_config.and_then(|g| g.get_string("user.name").ok()))
.or_else(|| get_environment_variable(&name_variables[3..]));

let name = name.map(|namestr| namestr.trim().to_string());

let email_variables = [
"CARGO_EMAIL",
"GIT_AUTHOR_EMAIL",
"GIT_COMMITTER_EMAIL",
"EMAIL",
];
let email = get_environment_variable(&email_variables[0..3])
.or_else(|| git_config.and_then(|g| g.get_string("user.email").ok()))
.or_else(|| get_environment_variable(&email_variables[3..]));

let email = email.map(|s| {
let mut s = s.trim();

// In some cases emails will already have <> remove them since they
// are already added when needed.
if s.starts_with('<') && s.ends_with('>') {
s = &s[1..s.len() - 1];
}

s.to_string()
});

(name, email)
}

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

fn find_tests_git_config(path: &Path) -> Option<GitConfig> {
// Don't escape the test sandbox when looking for a git repository.
// NOTE: libgit2 has support to define the path ceiling in
// git_repository_discover, but the git2 bindings do not expose that.
for path in paths::ancestors(path, None) {
if let Ok(repo) = GitRepository::open(path) {
return Some(repo.config().expect("test repo should have valid config"));
}
}
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()
}
Loading

0 comments on commit c685eb3

Please sign in to comment.