Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve author detection when includeIf is used #443

Merged
merged 5 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ that were not yet released.

_Unreleased_

- The order of git submodule initialization was changed. This improves the
automatic author detection when `includeIf` is used. #443

- The linux shim installer code will no longer fall back to symlinks when a
hardlink cannot be created. This is done as a symlinked shim will not
ever function correctly on Linux. This prevents the shim executables like
Expand Down
66 changes: 37 additions & 29 deletions rye/src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
.unwrap_or_else(|| "unknown".into())
}));
let version = "0.1.0";
let author = get_default_author_with_fallback();
let author = get_default_author_with_fallback(&dir);
let license = match cmd.license {
Some(license) => Some(license),
None => cfg.default_license(),
Expand Down Expand Up @@ -336,6 +336,7 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
}

let imported_something = metadata.name.is_some() || metadata.dependencies.is_some();
let mut is_metadata_author_none = false;

// if we're missing metadata after the import we update it with what's found from normal initialization.
if metadata.name.is_none() {
Expand All @@ -348,7 +349,8 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
metadata.description = Some("Add your description here".to_string())
}
if metadata.author.is_none() {
metadata.author = author;
is_metadata_author_none = true;
metadata.author = author.clone();
}
if metadata.requires_python.is_none() {
metadata.requires_python = Some(requires_python);
Expand Down Expand Up @@ -391,6 +393,39 @@ pub fn execute(cmd: Args) -> Result<(), Error> {

let private = cmd.private;
let name_safe = metadata.name.as_ref().unwrap().replace('-', "_");
let is_rust = build_system == BuildSystem::Maturin;

// if git init is successful prepare the local git repository
if !is_inside_git_work_tree(&dir)
&& Command::new("git")
.arg("init")
.current_dir(&dir)
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.map(|status| status.success())
.unwrap_or(false)
{
let gitignore = dir.join(".gitignore");

// create a .gitignore if one is missing
if !gitignore.is_file() {
let rv = env.render_named_str(
"gitignore.txt",
GITIGNORE_TEMPLATE,
context! {
is_rust
},
)?;
fs::write(&gitignore, rv).context("failed to write .gitignore")?;
}
if is_metadata_author_none {
let new_author = get_default_author_with_fallback(&dir);
if author != new_author {
metadata.author = new_author;
}
}
}

let rv = env.render_named_str(
"pyproject.json",
Expand All @@ -410,7 +445,6 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
private,
},
)?;
let is_rust = build_system == BuildSystem::Maturin;
fs::write(&toml, rv).context("failed to write pyproject.toml")?;

let src_dir = dir.join("src");
Expand Down Expand Up @@ -449,32 +483,6 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
}
}

// if git init is successful prepare the local git repository
if !is_inside_git_work_tree(&dir)
&& Command::new("git")
.arg("init")
.current_dir(&dir)
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.map(|status| status.success())
.unwrap_or(false)
{
let gitignore = dir.join(".gitignore");

// create a .gitignore if one is missing
if !gitignore.is_file() {
let rv = env.render_named_str(
"gitignore.txt",
GITIGNORE_TEMPLATE,
context! {
is_rust
},
)?;
fs::write(&gitignore, rv).context("failed to write .gitignore")?;
}
}

echo!(
"{} Initialized project in {}",
style("success:").green(),
Expand Down
9 changes: 6 additions & 3 deletions rye/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,22 +171,25 @@ pub fn list_known_toolchains() -> Result<Vec<(PythonVersion, PathBuf)>, Error> {
}

/// Returns the default author from git or the config.
pub fn get_default_author_with_fallback() -> Option<(String, String)> {
pub fn get_default_author_with_fallback(dir: &PathBuf) -> Option<(String, String)> {
let (mut name, mut email) = Config::current().default_author();
let is_name_none = name.is_none();
let is_email_none = email.is_none();

if let Ok(rv) = Command::new("git")
.arg("config")
.arg("--get-regexp")
.current_dir(dir)
.arg("^user.(name|email)$")
.stdout(Stdio::piped())
.output()
{
for line in std::str::from_utf8(&rv.stdout).ok()?.lines() {
match line.split_once(' ') {
Some((key, value)) if key == "user.email" && email.is_none() => {
Some((key, value)) if key == "user.email" && is_email_none => {
email = Some(value.to_string());
}
Some((key, value)) if key == "user.name" && name.is_none() => {
Some((key, value)) if key == "user.name" && is_name_none => {
name = Some(value.to_string());
}
_ => {}
Expand Down