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 4, 2024
1 parent 5d7be40 commit 9740a7b
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 52 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 from 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 configuration.
/// 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
59 changes: 32 additions & 27 deletions crates/uv/src/commands/project/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use owo_colors::OwoColorize;

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 @@ -484,7 +485,7 @@ async fn init_project(
&requires_python,
python_request.as_ref(),
vcs,
with_authors,
author_from,
no_readme,
package,
)
Expand Down Expand Up @@ -574,7 +575,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 @@ -586,7 +587,7 @@ impl InitProjectKind {
requires_python,
python_request,
vcs,
with_authors,
author_from,
no_readme,
package,
)
Expand All @@ -599,7 +600,7 @@ impl InitProjectKind {
requires_python,
python_request,
vcs,
with_authors,
author_from,
no_readme,
package,
)
Expand All @@ -616,18 +617,21 @@ 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 = author_from.unwrap_or_else(|| {
if package {
AuthorFrom::default()
} else {
AuthorFrom::None
}
});
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 @@ -705,7 +709,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 @@ -715,11 +719,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 @@ -880,10 +880,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 @@ -1195,7 +1195,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 @@ -8,7 +8,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
22 changes: 11 additions & 11 deletions crates/uv/tests/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2254,7 +2254,7 @@ fn init_git_not_installed() {
}

#[test]
fn init_with_author() -> Result<()> {
fn init_with_author() {
let context = TestContext::new("3.12");

// Create a Git repository and set the author.
Expand Down Expand Up @@ -2282,7 +2282,7 @@ fn init_with_author() -> Result<()> {

// `authors` is not filled for non-package application by default,
context.init().arg("foo").assert().success();
let pyproject = fs_err::read_to_string(context.temp_dir.join("foo/pyproject.toml"))?;
let pyproject = context.read("foo/pyproject.toml");
insta::with_settings!({
filters => context.filters(),
}, {
Expand All @@ -2299,14 +2299,15 @@ 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"))?;
let pyproject = context.read("bar/pyproject.toml");
insta::with_settings!({
filters => context.filters(),
}, {
Expand All @@ -2328,7 +2329,7 @@ fn init_with_author() -> Result<()> {

// Fill `authors` for library by default,
context.init().arg("baz").arg("--lib").assert().success();
let pyproject = fs_err::read_to_string(context.temp_dir.join("baz/pyproject.toml"))?;
let pyproject = context.read("baz/pyproject.toml");
insta::with_settings!({
filters => context.filters(),
}, {
Expand All @@ -2352,15 +2353,16 @@ 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"))?;
let pyproject = context.read("qux/pyproject.toml");
insta::with_settings!({
filters => context.filters(),
}, {
Expand All @@ -2380,6 +2382,4 @@ fn init_with_author() -> Result<()> {
"#
);
});

Ok(())
}
15 changes: 13 additions & 2 deletions docs/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,19 @@ uv init [OPTIONS] [PATH]

<p>By default, an application is not intended to be built and distributed as a Python package. The <code>--package</code> option can be used to create an application that is distributable, e.g., if you want to distribute a command-line interface via PyPI.</p>

</dd><dt><code>--author-from</code> <i>author-from</i></dt><dd><p>Fill in the <code>authors</code> field in the <code>pyproject.toml</code>.</p>

<p>By default, uv will attempt to infer the author information from some sources (e.g., Git) (<code>auto</code>). Use <code>--author-from git</code> to only infer from Git configuration. Use <code>--author-from none</code> to avoid inferring the author information.</p>

<p>Possible values:</p>

<ul>
<li><code>auto</code>: Fetch the author information from some sources (e.g., Git) automatically</li>

<li><code>git</code>: Fetch the author information from Git configuration only</li>

<li><code>none</code>: Do not infer the author information</li>
</ul>
</dd><dt><code>--cache-dir</code> <i>cache-dir</i></dt><dd><p>Path to the cache directory.</p>

<p>Defaults to <code>$HOME/Library/Caches/uv</code> on macOS, <code>$XDG_CACHE_HOME/uv</code> or <code>$HOME/.cache/uv</code> on Linux, and <code>%LOCALAPPDATA%\uv\cache</code> on Windows.</p>
Expand Down Expand Up @@ -484,8 +497,6 @@ uv init [OPTIONS] [PATH]
<p>However, in some cases, you may want to use the platform&#8217;s native certificate store, especially if you&#8217;re relying on a corporate trust root (e.g., for a mandatory proxy) that&#8217;s included in your system&#8217;s certificate store.</p>

<p>May also be set with the <code>UV_NATIVE_TLS</code> environment variable.</p>
</dd><dt><code>--no-authors</code></dt><dd><p>Do not fill in the <code>authors</code> field in the <code>pyproject.toml</code></p>

</dd><dt><code>--no-cache</code>, <code>-n</code></dt><dd><p>Avoid reading from or writing to the cache, instead using a temporary directory for the duration of the operation</p>

<p>May also be set with the <code>UV_NO_CACHE</code> environment variable.</p>
Expand Down

0 comments on commit 9740a7b

Please sign in to comment.