Skip to content

Commit

Permalink
Use --author-from
Browse files Browse the repository at this point in the history
  • Loading branch information
j178 committed Oct 3, 2024
1 parent ff7de89 commit 6757bfb
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 42 deletions.
23 changes: 17 additions & 6 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2314,6 +2314,17 @@ impl ExternalCommand {
}
}

#[derive(Debug, Default, Copy, Clone, clap::ValueEnum)]
pub enum AuthorFrom {
/// Fetch the author information from some sources (e.g., Git) automatically.
#[default]
Auto,
/// Fetch the author information for Git configuration only.
Git,
/// Do not infer the author information.
None,
}

#[derive(Args)]
#[allow(clippy::struct_excessive_bools)]
pub struct InitArgs {
Expand Down Expand Up @@ -2400,13 +2411,13 @@ pub struct InitArgs {
#[arg(long)]
pub no_readme: bool,

/// Do not fill in the `authors` field in the `pyproject.toml`.
#[arg(long)]
pub no_authors: bool,

/// Fill in the `authors` field in the `pyproject.toml`.
#[arg(long, overrides_with = "no_authors", hide = true)]
pub authors: bool,
///
/// By default, uv will attempt to infer the author information from some sources (e.g., Git) (`auto`).
/// Use `--author-from git` to only infer from Git.
/// Use `--author-from none` to avoid inferring the author information.
#[arg(long, value_enum)]
pub author_from: Option<AuthorFrom>,

/// Do not create a `.python-version` file for the project.
///
Expand Down
60 changes: 34 additions & 26 deletions crates/uv/src/commands/project/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use pep440_rs::Version;
use pep508_rs::PackageName;
use tracing::{debug, warn};
use uv_cache::Cache;
use uv_cli::AuthorFrom;
use uv_client::{BaseClientBuilder, Connectivity};
use uv_configuration::{VersionControlError, VersionControlSystem};
use uv_fs::{Simplified, CWD};
Expand Down Expand Up @@ -37,7 +38,7 @@ pub(crate) async fn init(
init_kind: InitKind,
vcs: Option<VersionControlSystem>,
no_readme: bool,
with_authors: Option<bool>,
author_from: Option<AuthorFrom>,
no_pin_python: bool,
python: Option<String>,
no_workspace: bool,
Expand All @@ -64,7 +65,7 @@ pub(crate) async fn init(
printer,
no_workspace,
no_readme,
with_authors,
author_from,
no_pin_python,
package,
native_tls,
Expand Down Expand Up @@ -114,7 +115,7 @@ pub(crate) async fn init(
project_kind,
vcs,
no_readme,
with_authors,
author_from,
no_pin_python,
python,
no_workspace,
Expand Down Expand Up @@ -169,7 +170,7 @@ async fn init_script(
printer: Printer,
no_workspace: bool,
no_readme: bool,
with_authors: Option<bool>,
author_from: Option<AuthorFrom>,
no_pin_python: bool,
package: bool,
native_tls: bool,
Expand All @@ -180,8 +181,8 @@ async fn init_script(
if no_readme {
warn_user_once!("`--no_readme` is a no-op for Python scripts, which are standalone");
}
if with_authors.is_some() {
warn_user_once!("`--no_authors` is a no-op for Python scripts, which are standalone");
if author_from.is_some() {
warn_user_once!("`--author-from` is a no-op for Python scripts, which are standalone");
}
if package {
warn_user_once!("`--package` is a no-op for Python scripts, which are standalone");
Expand Down Expand Up @@ -245,7 +246,7 @@ async fn init_project(
project_kind: InitProjectKind,
vcs: Option<VersionControlSystem>,
no_readme: bool,
with_authors: Option<bool>,
author_from: Option<AuthorFrom>,
no_pin_python: bool,
python: Option<String>,
no_workspace: bool,
Expand Down Expand Up @@ -470,7 +471,7 @@ async fn init_project(
&requires_python,
python_request.as_ref(),
vcs,
with_authors,
author_from,
no_readme,
package,
)
Expand Down Expand Up @@ -560,7 +561,7 @@ impl InitProjectKind {
requires_python: &RequiresPython,
python_request: Option<&PythonRequest>,
vcs: Option<VersionControlSystem>,
with_authors: Option<bool>,
author_from: Option<AuthorFrom>,
no_readme: bool,
package: bool,
) -> Result<()> {
Expand All @@ -572,7 +573,7 @@ impl InitProjectKind {
requires_python,
python_request,
vcs,
with_authors,
author_from,
no_readme,
package,
)
Expand All @@ -585,7 +586,7 @@ impl InitProjectKind {
requires_python,
python_request,
vcs,
with_authors,
author_from,
no_readme,
package,
)
Expand All @@ -602,18 +603,24 @@ impl InitProjectKind {
requires_python: &RequiresPython,
python_request: Option<&PythonRequest>,
vcs: Option<VersionControlSystem>,
with_authors: Option<bool>,
author_from: Option<AuthorFrom>,
no_readme: bool,
package: bool,
) -> Result<()> {
fs_err::create_dir_all(path)?;

// Do no fill in `authors` for non-packaged applications unless explicitly requested.
let author = if with_authors.unwrap_or(package) {
get_author_info(path)
} else {
None
let author_from = match author_from {
None => {
if package {
AuthorFrom::default()
} else {
AuthorFrom::None
}
}
Some(author_from) => author_from,
};
let author = get_author_info(path, author_from);

// Create the `pyproject.toml`
let mut pyproject = pyproject_project(name, requires_python, author.as_ref(), no_readme);
Expand Down Expand Up @@ -691,7 +698,7 @@ impl InitProjectKind {
requires_python: &RequiresPython,
python_request: Option<&PythonRequest>,
vcs: Option<VersionControlSystem>,
with_authors: Option<bool>,
author_from: Option<AuthorFrom>,
no_readme: bool,
package: bool,
) -> Result<()> {
Expand All @@ -701,11 +708,7 @@ impl InitProjectKind {

fs_err::create_dir_all(path)?;

let author = if with_authors.unwrap_or(true) {
get_author_info(path)
} else {
None
};
let author = get_author_info(path, author_from.unwrap_or_default());

// Create the `pyproject.toml`
let mut pyproject = pyproject_project(name, requires_python, author.as_ref(), no_readme);
Expand Down Expand Up @@ -866,10 +869,15 @@ fn init_vcs(path: &Path, vcs: Option<VersionControlSystem>) -> Result<()> {
/// Try to get the author information.
///
/// Currently, this only tries to get the author information from git.
fn get_author_info(path: &Path) -> Option<Author> {
match get_author_from_git(path) {
Ok(author) => return Some(author),
Err(err) => warn!("Failed to get author from git: {err}"),
fn get_author_info(path: &Path, author_from: AuthorFrom) -> Option<Author> {
if matches!(author_from, AuthorFrom::None) {
return None;
}
if matches!(author_from, AuthorFrom::Auto | AuthorFrom::Git) {
match get_author_from_git(path) {
Ok(author) => return Some(author),
Err(err) => warn!("Failed to get author from git: {err}"),
}
}

None
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,7 @@ async fn run_project(
args.kind,
args.vcs,
args.no_readme,
args.with_authors,
args.author_from,
args.no_pin_python,
args.python,
args.no_workspace,
Expand Down
9 changes: 4 additions & 5 deletions crates/uv/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use url::Url;
use uv_cache::{CacheArgs, Refresh};
use uv_cli::{
options::{flag, resolver_installer_options, resolver_options},
BuildArgs, ExportArgs, PublishArgs, ToolUpgradeArgs,
AuthorFrom, BuildArgs, ExportArgs, PublishArgs, ToolUpgradeArgs,
};
use uv_cli::{
AddArgs, ColorChoice, ExternalCommand, GlobalArgs, InitArgs, ListFormat, LockArgs, Maybe,
Expand Down Expand Up @@ -164,7 +164,7 @@ pub(crate) struct InitSettings {
pub(crate) kind: InitKind,
pub(crate) vcs: Option<VersionControlSystem>,
pub(crate) no_readme: bool,
pub(crate) with_authors: Option<bool>,
pub(crate) author_from: Option<AuthorFrom>,
pub(crate) no_pin_python: bool,
pub(crate) no_workspace: bool,
pub(crate) python: Option<String>,
Expand All @@ -185,8 +185,7 @@ impl InitSettings {
script,
vcs,
no_readme,
no_authors,
authors,
author_from,
no_pin_python,
no_workspace,
python,
Expand All @@ -209,7 +208,7 @@ impl InitSettings {
kind,
vcs,
no_readme,
with_authors: flag(authors, no_authors),
author_from,
no_pin_python,
no_workspace,
python,
Expand Down
10 changes: 6 additions & 4 deletions crates/uv/tests/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2307,11 +2307,12 @@ fn init_with_author() -> Result<()> {
);
});

// use `--authors` to explicitly fill it.
// use `--author-from auto` to explicitly fill it.
context
.init()
.arg("bar")
.arg("--authors")
.arg("--author-from")
.arg("auto")
.assert()
.success();
let pyproject = fs_err::read_to_string(context.temp_dir.join("bar/pyproject.toml"))?;
Expand Down Expand Up @@ -2360,12 +2361,13 @@ fn init_with_author() -> Result<()> {
);
});

// use `--no-authors` to prevent it.
// use `--authors-from none` to prevent it.
context
.init()
.arg("qux")
.arg("--lib")
.arg("--no-authors")
.arg("--author-from")
.arg("none")
.assert()
.success();
let pyproject = fs_err::read_to_string(context.temp_dir.join("qux/pyproject.toml"))?;
Expand Down

0 comments on commit 6757bfb

Please sign in to comment.