Skip to content

Commit

Permalink
refactor(cargo-package): let-else to flatten code (#14959)
Browse files Browse the repository at this point in the history
### What does this PR try to resolve?

Flatten code with let-else for easier reasoning of exit of
`check_repo_state` function.

### How should we test and review this PR?

This also adds some more debug logs.
Shouldn't affect any real production behavior.
  • Loading branch information
epage authored Dec 19, 2024
2 parents 1296566 + 3c75c15 commit 59b2ddd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 38 deletions.
95 changes: 58 additions & 37 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,48 +740,69 @@ fn check_repo_state(
gctx: &GlobalContext,
opts: &PackageOpts<'_>,
) -> CargoResult<Option<VcsInfo>> {
if let Ok(repo) = git2::Repository::discover(p.root()) {
if let Some(workdir) = repo.workdir() {
debug!("found a git repo at {:?}", workdir);
let path = p.manifest_path();
let path =
paths::strip_prefix_canonical(path, workdir).unwrap_or_else(|_| path.to_path_buf());
if let Ok(status) = repo.status_file(&path) {
if (status & git2::Status::IGNORED).is_empty() {
debug!(
"found (git) Cargo.toml at {:?} in workdir {:?}",
path, workdir
);
let path_in_vcs = path
.parent()
.and_then(|p| p.to_str())
.unwrap_or("")
.replace("\\", "/");
let Some(git) = git(p, src_files, &repo, &opts)? else {
// If the git repo lacks essensial field like `sha1`, and since this field exists from the beginning,
// then don't generate the corresponding file in order to maintain consistency with past behavior.
return Ok(None);
};
return Ok(Some(VcsInfo { git, path_in_vcs }));
}
}
gctx.shell().verbose(|shell| {
shell.warn(format!(
"no (git) Cargo.toml found at `{}` in workdir `{}`",
path.display(),
workdir.display()
))
})?;
}
} else {
let Ok(repo) = git2::Repository::discover(p.root()) else {
gctx.shell().verbose(|shell| {
shell.warn(format!("no (git) VCS found for `{}`", p.root().display()))
})?;
// No Git repo found. Have to assume it is clean.
return Ok(None);
};

let Some(workdir) = repo.workdir() else {
debug!(
"no (git) workdir found for repo at `{}`",
repo.path().display()
);
// No git workdir. Have to assume it is clean.
return Ok(None);
};

debug!("found a git repo at `{}`", workdir.display());
let path = p.manifest_path();
let path = paths::strip_prefix_canonical(path, workdir).unwrap_or_else(|_| path.to_path_buf());
let Ok(status) = repo.status_file(&path) else {
gctx.shell().verbose(|shell| {
shell.warn(format!(
"no (git) Cargo.toml found at `{}` in workdir `{}`",
path.display(),
workdir.display()
))
})?;
// No checked-in `Cargo.toml` found. This package may be irrelevant.
// Have to assume it is clean.
return Ok(None);
};

if !(status & git2::Status::IGNORED).is_empty() {
gctx.shell().verbose(|shell| {
shell.warn(format!(
"found (git) Cargo.toml ignored at `{}` in workdir `{}`",
path.display(),
workdir.display()
))
})?;
// An ignored `Cargo.toml` found. This package may be irrelevant.
// Have to assume it is clean.
return Ok(None);
}

// No VCS with a checked in `Cargo.toml` found, so we don't know if the
// directory is dirty or not, thus we have to assume that it's clean.
return Ok(None);
debug!(
"found (git) Cargo.toml at `{}` in workdir `{}`",
path.display(),
workdir.display(),
);
let path_in_vcs = path
.parent()
.and_then(|p| p.to_str())
.unwrap_or("")
.replace("\\", "/");
let Some(git) = git(p, src_files, &repo, &opts)? else {
// If the git repo lacks essensial field like `sha1`, and since this field exists from the beginning,
// then don't generate the corresponding file in order to maintain consistency with past behavior.
return Ok(None);
};

return Ok(Some(VcsInfo { git, path_in_vcs }));

fn git(
pkg: &Package,
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/publish_lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ fn note_resolve_changes() {
[ARCHIVING] Cargo.toml.orig
[ARCHIVING] src/main.rs
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
[WARNING] no (git) Cargo.toml found at `[..]/foo/Cargo.toml` in workdir `[..]`
[WARNING] found (git) Cargo.toml ignored at `[..]/foo/Cargo.toml` in workdir `[..]`
"#]].unordered())
.run();
Expand Down

0 comments on commit 59b2ddd

Please sign in to comment.