Skip to content

Commit 59b2ddd

Browse files
authored
refactor(cargo-package): let-else to flatten code (#14959)
### 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.
2 parents 1296566 + 3c75c15 commit 59b2ddd

File tree

2 files changed

+59
-38
lines changed

2 files changed

+59
-38
lines changed

src/cargo/ops/cargo_package.rs

+58-37
Original file line numberDiff line numberDiff line change
@@ -740,48 +740,69 @@ fn check_repo_state(
740740
gctx: &GlobalContext,
741741
opts: &PackageOpts<'_>,
742742
) -> CargoResult<Option<VcsInfo>> {
743-
if let Ok(repo) = git2::Repository::discover(p.root()) {
744-
if let Some(workdir) = repo.workdir() {
745-
debug!("found a git repo at {:?}", workdir);
746-
let path = p.manifest_path();
747-
let path =
748-
paths::strip_prefix_canonical(path, workdir).unwrap_or_else(|_| path.to_path_buf());
749-
if let Ok(status) = repo.status_file(&path) {
750-
if (status & git2::Status::IGNORED).is_empty() {
751-
debug!(
752-
"found (git) Cargo.toml at {:?} in workdir {:?}",
753-
path, workdir
754-
);
755-
let path_in_vcs = path
756-
.parent()
757-
.and_then(|p| p.to_str())
758-
.unwrap_or("")
759-
.replace("\\", "/");
760-
let Some(git) = git(p, src_files, &repo, &opts)? else {
761-
// If the git repo lacks essensial field like `sha1`, and since this field exists from the beginning,
762-
// then don't generate the corresponding file in order to maintain consistency with past behavior.
763-
return Ok(None);
764-
};
765-
return Ok(Some(VcsInfo { git, path_in_vcs }));
766-
}
767-
}
768-
gctx.shell().verbose(|shell| {
769-
shell.warn(format!(
770-
"no (git) Cargo.toml found at `{}` in workdir `{}`",
771-
path.display(),
772-
workdir.display()
773-
))
774-
})?;
775-
}
776-
} else {
743+
let Ok(repo) = git2::Repository::discover(p.root()) else {
777744
gctx.shell().verbose(|shell| {
778745
shell.warn(format!("no (git) VCS found for `{}`", p.root().display()))
779746
})?;
747+
// No Git repo found. Have to assume it is clean.
748+
return Ok(None);
749+
};
750+
751+
let Some(workdir) = repo.workdir() else {
752+
debug!(
753+
"no (git) workdir found for repo at `{}`",
754+
repo.path().display()
755+
);
756+
// No git workdir. Have to assume it is clean.
757+
return Ok(None);
758+
};
759+
760+
debug!("found a git repo at `{}`", workdir.display());
761+
let path = p.manifest_path();
762+
let path = paths::strip_prefix_canonical(path, workdir).unwrap_or_else(|_| path.to_path_buf());
763+
let Ok(status) = repo.status_file(&path) else {
764+
gctx.shell().verbose(|shell| {
765+
shell.warn(format!(
766+
"no (git) Cargo.toml found at `{}` in workdir `{}`",
767+
path.display(),
768+
workdir.display()
769+
))
770+
})?;
771+
// No checked-in `Cargo.toml` found. This package may be irrelevant.
772+
// Have to assume it is clean.
773+
return Ok(None);
774+
};
775+
776+
if !(status & git2::Status::IGNORED).is_empty() {
777+
gctx.shell().verbose(|shell| {
778+
shell.warn(format!(
779+
"found (git) Cargo.toml ignored at `{}` in workdir `{}`",
780+
path.display(),
781+
workdir.display()
782+
))
783+
})?;
784+
// An ignored `Cargo.toml` found. This package may be irrelevant.
785+
// Have to assume it is clean.
786+
return Ok(None);
780787
}
781788

782-
// No VCS with a checked in `Cargo.toml` found, so we don't know if the
783-
// directory is dirty or not, thus we have to assume that it's clean.
784-
return Ok(None);
789+
debug!(
790+
"found (git) Cargo.toml at `{}` in workdir `{}`",
791+
path.display(),
792+
workdir.display(),
793+
);
794+
let path_in_vcs = path
795+
.parent()
796+
.and_then(|p| p.to_str())
797+
.unwrap_or("")
798+
.replace("\\", "/");
799+
let Some(git) = git(p, src_files, &repo, &opts)? else {
800+
// If the git repo lacks essensial field like `sha1`, and since this field exists from the beginning,
801+
// then don't generate the corresponding file in order to maintain consistency with past behavior.
802+
return Ok(None);
803+
};
804+
805+
return Ok(Some(VcsInfo { git, path_in_vcs }));
785806

786807
fn git(
787808
pkg: &Package,

tests/testsuite/publish_lockfile.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ fn note_resolve_changes() {
241241
[ARCHIVING] Cargo.toml.orig
242242
[ARCHIVING] src/main.rs
243243
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
244-
[WARNING] no (git) Cargo.toml found at `[..]/foo/Cargo.toml` in workdir `[..]`
244+
[WARNING] found (git) Cargo.toml ignored at `[..]/foo/Cargo.toml` in workdir `[..]`
245245
246246
"#]].unordered())
247247
.run();

0 commit comments

Comments
 (0)