Skip to content

Commit

Permalink
Include vcs_info even if workspace is dirty.
Browse files Browse the repository at this point in the history
  • Loading branch information
torhovland committed May 24, 2024
1 parent a8d72c6 commit fc15233
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,8 @@ pub fn package_one(
}
let src_files = src.list_files(pkg)?;

// Check (git) repository state, getting the current commit hash if not
// dirty.
let vcs_info = if !opts.allow_dirty {
// This will error if a dirty repo is found.
check_repo_state(pkg, &src_files, gctx)?
} else {
None
};
// Check (git) repository state, getting the current commit hash.
let vcs_info = check_repo_state(pkg, &src_files, gctx, &opts)?;

let ar_files = build_ar_list(ws, pkg, src_files, vcs_info)?;

Expand Down Expand Up @@ -526,13 +520,15 @@ fn check_metadata(pkg: &Package, gctx: &GlobalContext) -> CargoResult<()> {
}

/// Checks if the package source is in a *git* DVCS repository. If *git*, and
/// the source is *dirty* (e.g., has uncommitted changes) then `bail!` with an
/// informative message. Otherwise return the sha1 hash of the current *HEAD*
/// commit, or `None` if no repo is found.
/// the source is *dirty* (e.g., has uncommitted changes), and `--allow-dirty`
/// has not been passed, then `bail!` with an informative message. Otherwise
/// return the sha1 hash of the current *HEAD* commit, or `None` if no repo is
/// found.
fn check_repo_state(
p: &Package,
src_files: &[PathBuf],
gctx: &GlobalContext,
opts: &PackageOpts<'_>,
) -> CargoResult<Option<VcsInfo>> {
if let Ok(repo) = git2::Repository::discover(p.root()) {
if let Some(workdir) = repo.workdir() {
Expand All @@ -552,7 +548,7 @@ fn check_repo_state(
.unwrap_or("")
.replace("\\", "/");
return Ok(Some(VcsInfo {
git: git(p, src_files, &repo)?,
git: git(p, src_files, &repo, &opts)?,
path_in_vcs,
}));
}
Expand All @@ -575,7 +571,12 @@ fn check_repo_state(
// directory is dirty or not, thus we have to assume that it's clean.
return Ok(None);

fn git(p: &Package, src_files: &[PathBuf], repo: &git2::Repository) -> CargoResult<GitVcsInfo> {
fn git(
p: &Package,
src_files: &[PathBuf],
repo: &git2::Repository,
opts: &PackageOpts<'_>,
) -> CargoResult<GitVcsInfo> {
// This is a collection of any dirty or untracked files. This covers:
// - new/modified/deleted/renamed/type change (index or worktree)
// - untracked files (which are "new" worktree files)
Expand All @@ -600,7 +601,7 @@ fn check_repo_state(
.to_string()
})
.collect();
if dirty_src_files.is_empty() {
if dirty_src_files.is_empty() || opts.allow_dirty {
let rev_obj = repo.revparse_single("HEAD")?;
Ok(GitVcsInfo {
sha1: rev_obj.id().to_string(),
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2608,6 +2608,7 @@ fn include_overrides_gitignore() {
p.cargo("package --list --allow-dirty")
.with_stdout(
"\
.cargo_vcs_info.json
Cargo.toml
Cargo.toml.orig
ignored.txt
Expand Down
7 changes: 7 additions & 0 deletions tests/testsuite/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ fn no_duplicates_from_modified_tracked_files() {
p.cargo("package --list --allow-dirty")
.with_stdout(
"\
.cargo_vcs_info.json
Cargo.lock
Cargo.toml
Cargo.toml.orig
Expand Down Expand Up @@ -1009,6 +1010,7 @@ src/main.rs
.with_stderr("")
.with_stdout(
"\
.cargo_vcs_info.json
.gitignore
Cargo.lock
Cargo.toml
Expand Down Expand Up @@ -2346,6 +2348,7 @@ fn finds_git_in_parent() {
p.cargo("package --list --allow-dirty")
.with_stdout(
"\
.cargo_vcs_info.json
Cargo.toml
Cargo.toml.orig
ignoreme
Expand All @@ -2359,6 +2362,7 @@ src/lib.rs
p.cargo("package --list --allow-dirty")
.with_stdout(
"\
.cargo_vcs_info.json
.gitignore
Cargo.toml
Cargo.toml.orig
Expand All @@ -2372,6 +2376,7 @@ src/lib.rs
p.cargo("package --list --allow-dirty")
.with_stdout(
"\
.cargo_vcs_info.json
.gitignore
Cargo.toml
Cargo.toml.orig
Expand Down Expand Up @@ -2634,6 +2639,7 @@ fn deleted_git_working_tree() {
p.cargo("package --allow-dirty --list")
.with_stdout(
"\
.cargo_vcs_info.json
Cargo.lock
Cargo.toml
Cargo.toml.orig
Expand All @@ -2648,6 +2654,7 @@ src/main.rs
p.cargo("package --allow-dirty --list")
.with_stdout(
"\
.cargo_vcs_info.json
Cargo.lock
Cargo.toml
Cargo.toml.orig
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/publish_lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ fn note_resolve_changes() {
[NOTE] package `multi v0.1.0` added to the packaged Cargo.lock file, was originally sourced from `[..]/foo/multi`
[NOTE] package `patched v1.0.0` added to the packaged Cargo.lock file, was originally sourced from `[..]/foo/patched`
[PACKAGED] [..] files, [..] ([..] compressed)
[WARNING] no (git) Cargo.toml found at `target/tmp/[..]/foo/Cargo.toml` in workdir `[..]`
",
)
.run();
Expand Down

0 comments on commit fc15233

Please sign in to comment.