Skip to content

Commit

Permalink
Auto merge of #9282 - lf-:remove-author, r=ehuss
Browse files Browse the repository at this point in the history
RFC 3052: Stop including authors field in manifests made by cargo new

See rust-lang/rust#83227

~~This is definitely a draft as there are a couple of tests I'm still working on fixing.~~ One question I ran into while digging through these test failures is that there is a Cargo config option for author name, `cargo-new.name`. Should we keep supporting that? I feel like perhaps we should as the user has specified explicit intent that they want their name in the authors of a manifest as generated by `cargo new`, but it seems weird to support *just* that case. There is likewise an environment variable `CARGO_NAME` that signals similar intent.

Should we completely drop the feature of putting in author names, and require people to manually put them in if they wish, or allow these limited cases where the user is specifically instructing *cargo* to do it?
  • Loading branch information
bors committed Mar 22, 2021
2 parents 39d9413 + 160b7f1 commit 4751e5c
Show file tree
Hide file tree
Showing 25 changed files with 75 additions and 808 deletions.
113 changes: 6 additions & 107 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,14 @@ impl NewOptions {

#[derive(Deserialize)]
struct CargoNewConfig {
#[deprecated = "cargo-new no longer supports adding the authors field"]
#[allow(dead_code)]
name: Option<String>,

#[deprecated = "cargo-new no longer supports adding the authors field"]
#[allow(dead_code)]
email: Option<String>,

#[serde(rename = "vcs")]
version_control: Option<VersionControl>,
}
Expand Down Expand Up @@ -666,32 +669,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 +707,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 +783,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()
}
2 changes: 0 additions & 2 deletions src/doc/man/cargo-init.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ will be used. If not, then a sample `src/main.rs` file will be created, or
If the directory is not already in a VCS repository, then a new repository
is created (see `--vcs` below).

{{> description-new-authors }}

See {{man "cargo-new" 1}} for a similar command which will create a new package in
a new directory.

Expand Down
2 changes: 0 additions & 2 deletions src/doc/man/cargo-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ includes a simple template with a `Cargo.toml` manifest, sample source file,
and a VCS ignore file. If the directory is not already in a VCS repository,
then a new repository is created (see `--vcs` below).

{{> description-new-authors }}

See {{man "cargo-init" 1}} for a similar command which will create a new manifest
in an existing directory.

Expand Down
38 changes: 0 additions & 38 deletions src/doc/man/generated_txt/cargo-init.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,6 @@ DESCRIPTION
If the directory is not already in a VCS repository, then a new
repository is created (see --vcs below).

The "authors" field in the manifest is determined from the environment
or configuration settings. A name is required and is determined from
(first match wins):

o cargo-new.name Cargo config value

o CARGO_NAME environment variable

o GIT_AUTHOR_NAME environment variable

o GIT_COMMITTER_NAME environment variable

o user.name git configuration value

o USER environment variable

o USERNAME environment variable

o NAME environment variable

The email address is optional and is determined from:

o cargo-new.email Cargo config value

o CARGO_EMAIL environment variable

o GIT_AUTHOR_EMAIL environment variable

o GIT_COMMITTER_EMAIL environment variable

o user.email git configuration value

o EMAIL environment variable

See the reference
<https://doc.rust-lang.org/cargo/reference/config.html> for more
information about configuration files.

See cargo-new(1) for a similar command which will create a new package
in a new directory.

Expand Down
38 changes: 0 additions & 38 deletions src/doc/man/generated_txt/cargo-new.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,6 @@ DESCRIPTION
source file, and a VCS ignore file. If the directory is not already in a
VCS repository, then a new repository is created (see --vcs below).

The "authors" field in the manifest is determined from the environment
or configuration settings. A name is required and is determined from
(first match wins):

o cargo-new.name Cargo config value

o CARGO_NAME environment variable

o GIT_AUTHOR_NAME environment variable

o GIT_COMMITTER_NAME environment variable

o user.name git configuration value

o USER environment variable

o USERNAME environment variable

o NAME environment variable

The email address is optional and is determined from:

o cargo-new.email Cargo config value

o CARGO_EMAIL environment variable

o GIT_AUTHOR_EMAIL environment variable

o GIT_COMMITTER_EMAIL environment variable

o user.email git configuration value

o EMAIL environment variable

See the reference
<https://doc.rust-lang.org/cargo/reference/config.html> for more
information about configuration files.

See cargo-init(1) for a similar command which will create a new manifest
in an existing directory.

Expand Down
24 changes: 0 additions & 24 deletions src/doc/man/includes/description-new-authors.md

This file was deleted.

26 changes: 0 additions & 26 deletions src/doc/src/commands/cargo-init.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,6 @@ will be used. If not, then a sample `src/main.rs` file will be created, or
If the directory is not already in a VCS repository, then a new repository
is created (see `--vcs` below).

The "authors" field in the manifest is determined from the environment or
configuration settings. A name is required and is determined from (first match
wins):

- `cargo-new.name` Cargo config value
- `CARGO_NAME` environment variable
- `GIT_AUTHOR_NAME` environment variable
- `GIT_COMMITTER_NAME` environment variable
- `user.name` git configuration value
- `USER` environment variable
- `USERNAME` environment variable
- `NAME` environment variable

The email address is optional and is determined from:

- `cargo-new.email` Cargo config value
- `CARGO_EMAIL` environment variable
- `GIT_AUTHOR_EMAIL` environment variable
- `GIT_COMMITTER_EMAIL` environment variable
- `user.email` git configuration value
- `EMAIL` environment variable

See [the reference](../reference/config.html) for more information about
configuration files.


See [cargo-new(1)](cargo-new.html) for a similar command which will create a new package in
a new directory.

Expand Down
26 changes: 0 additions & 26 deletions src/doc/src/commands/cargo-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,6 @@ includes a simple template with a `Cargo.toml` manifest, sample source file,
and a VCS ignore file. If the directory is not already in a VCS repository,
then a new repository is created (see `--vcs` below).

The "authors" field in the manifest is determined from the environment or
configuration settings. A name is required and is determined from (first match
wins):

- `cargo-new.name` Cargo config value
- `CARGO_NAME` environment variable
- `GIT_AUTHOR_NAME` environment variable
- `GIT_COMMITTER_NAME` environment variable
- `user.name` git configuration value
- `USER` environment variable
- `USERNAME` environment variable
- `NAME` environment variable

The email address is optional and is determined from:

- `cargo-new.email` Cargo config value
- `CARGO_EMAIL` environment variable
- `GIT_AUTHOR_EMAIL` environment variable
- `GIT_COMMITTER_EMAIL` environment variable
- `user.email` git configuration value
- `EMAIL` environment variable

See [the reference](../reference/config.html) for more information about
configuration files.


See [cargo-init(1)](cargo-init.html) for a similar command which will create a new manifest
in an existing directory.

Expand Down
1 change: 0 additions & 1 deletion src/doc/src/getting-started/first-steps.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ This is all we need to get started. First, let’s check out `Cargo.toml`:
[package]
name = "hello_world"
version = "0.1.0"
authors = ["Your Name <you@example.com>"]
edition = "2018"

[dependencies]
Expand Down
2 changes: 0 additions & 2 deletions src/doc/src/guide/cargo-toml-vs-cargo-lock.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ depend on another package:
[package]
name = "hello_world"
version = "0.1.0"
authors = ["Your Name <you@example.com>"]

[dependencies]
rand = { git = "https://github.com/rust-lang-nursery/rand.git" }
Expand Down Expand Up @@ -63,7 +62,6 @@ manifest like this:
[package]
name = "hello_world"
version = "0.1.0"
authors = ["Your Name <you@example.com>"]

[dependencies]
rand = { git = "https://github.com/rust-lang-nursery/rand.git" }
Expand Down
1 change: 0 additions & 1 deletion src/doc/src/guide/creating-a-new-project.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ Let’s take a closer look at `Cargo.toml`:
[package]
name = "hello_world"
version = "0.1.0"
authors = ["Your Name <you@example.com>"]
edition = "2018"

[dependencies]
Expand Down
1 change: 0 additions & 1 deletion src/doc/src/guide/dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ crates:
[package]
name = "hello_world"
version = "0.1.0"
authors = ["Your Name <you@example.com>"]
edition = "2018"

[dependencies]
Expand Down
Loading

0 comments on commit 4751e5c

Please sign in to comment.