Skip to content

Commit 3c75c15

Browse files
committed
refactor(cargo-package): let-else to flatten code
This also adds some more debug logs
1 parent 58b2d60 commit 3c75c15

File tree

2 files changed

+59
-38
lines changed

2 files changed

+59
-38
lines changed

src/cargo/ops/cargo_package.rs

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

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

782803
fn git(
783804
p: &Package,

tests/testsuite/publish_lockfile.rs

Lines changed: 1 addition & 1 deletion
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)