From 3c75c1532e6652058c7ee51cf21dd0bf4f0d2d4b Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Wed, 18 Dec 2024 18:43:42 -0500 Subject: [PATCH] refactor(cargo-package): let-else to flatten code This also adds some more debug logs --- src/cargo/ops/cargo_package.rs | 95 ++++++++++++++++++----------- tests/testsuite/publish_lockfile.rs | 2 +- 2 files changed, 59 insertions(+), 38 deletions(-) diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index 30445b8643f..866b5c5ac6d 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -736,48 +736,69 @@ fn check_repo_state( gctx: &GlobalContext, opts: &PackageOpts<'_>, ) -> CargoResult> { - 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( p: &Package, diff --git a/tests/testsuite/publish_lockfile.rs b/tests/testsuite/publish_lockfile.rs index b204b7cf3bb..51f376ef756 100644 --- a/tests/testsuite/publish_lockfile.rs +++ b/tests/testsuite/publish_lockfile.rs @@ -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();