Skip to content

Commit 2a1299a

Browse files
committed
fix: Include vcs_info even if workspace is dirty.
1 parent a6ad3a3 commit 2a1299a

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,8 @@ fn prepare_archive(
235235
}
236236
let src_files = src.list_files(pkg)?;
237237

238-
// Check (git) repository state, getting the current commit hash if not
239-
// dirty.
240-
let vcs_info = if !opts.allow_dirty {
241-
// This will error if a dirty repo is found.
242-
check_repo_state(pkg, &src_files, gctx)?
243-
} else {
244-
None
245-
};
238+
// Check (git) repository state, getting the current commit hash.
239+
let vcs_info = check_repo_state(pkg, &src_files, gctx, &opts)?;
246240

247241
build_ar_list(ws, pkg, src_files, vcs_info)
248242
}
@@ -559,13 +553,15 @@ fn check_metadata(pkg: &Package, gctx: &GlobalContext) -> CargoResult<()> {
559553
}
560554

561555
/// Checks if the package source is in a *git* DVCS repository. If *git*, and
562-
/// the source is *dirty* (e.g., has uncommitted changes) then `bail!` with an
563-
/// informative message. Otherwise return the sha1 hash of the current *HEAD*
564-
/// commit, or `None` if no repo is found.
556+
/// the source is *dirty* (e.g., has uncommitted changes), and `--allow-dirty`
557+
/// has not been passed, then `bail!` with an informative message. Otherwise
558+
/// return the sha1 hash of the current *HEAD* commit, or `None` if no repo is
559+
/// found.
565560
fn check_repo_state(
566561
p: &Package,
567562
src_files: &[PathBuf],
568563
gctx: &GlobalContext,
564+
opts: &PackageOpts<'_>,
569565
) -> CargoResult<Option<VcsInfo>> {
570566
if let Ok(repo) = git2::Repository::discover(p.root()) {
571567
if let Some(workdir) = repo.workdir() {
@@ -585,7 +581,7 @@ fn check_repo_state(
585581
.unwrap_or("")
586582
.replace("\\", "/");
587583
return Ok(Some(VcsInfo {
588-
git: git(p, src_files, &repo)?,
584+
git: git(p, src_files, &repo, &opts)?,
589585
path_in_vcs,
590586
}));
591587
}
@@ -608,7 +604,12 @@ fn check_repo_state(
608604
// directory is dirty or not, thus we have to assume that it's clean.
609605
return Ok(None);
610606

611-
fn git(p: &Package, src_files: &[PathBuf], repo: &git2::Repository) -> CargoResult<GitVcsInfo> {
607+
fn git(
608+
p: &Package,
609+
src_files: &[PathBuf],
610+
repo: &git2::Repository,
611+
opts: &PackageOpts<'_>,
612+
) -> CargoResult<GitVcsInfo> {
612613
// This is a collection of any dirty or untracked files. This covers:
613614
// - new/modified/deleted/renamed/type change (index or worktree)
614615
// - untracked files (which are "new" worktree files)
@@ -633,7 +634,7 @@ fn check_repo_state(
633634
.to_string()
634635
})
635636
.collect();
636-
if dirty_src_files.is_empty() {
637+
if dirty_src_files.is_empty() || opts.allow_dirty {
637638
let rev_obj = repo.revparse_single("HEAD")?;
638639
Ok(GitVcsInfo {
639640
sha1: rev_obj.id().to_string(),

tests/testsuite/git.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,6 +2610,7 @@ fn include_overrides_gitignore() {
26102610
p.cargo("package --list --allow-dirty")
26112611
.with_stdout(
26122612
"\
2613+
.cargo_vcs_info.json
26132614
Cargo.toml
26142615
Cargo.toml.orig
26152616
ignored.txt

tests/testsuite/package.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,7 @@ fn no_duplicates_from_modified_tracked_files() {
703703
p.cargo("package --list --allow-dirty")
704704
.with_stdout(
705705
"\
706+
.cargo_vcs_info.json
706707
Cargo.lock
707708
Cargo.toml
708709
Cargo.toml.orig
@@ -1011,6 +1012,7 @@ src/main.rs
10111012
.with_stderr("")
10121013
.with_stdout(
10131014
"\
1015+
.cargo_vcs_info.json
10141016
.gitignore
10151017
Cargo.lock
10161018
Cargo.toml
@@ -1209,14 +1211,19 @@ fn issue_13695_dirty_vcs_info() {
12091211
)
12101212
.run();
12111213

1212-
// Allowing a dirty worktree results in the vcs file not being included.
1214+
// Allowing a dirty worktree results in the vcs file being included.
12131215
p.cargo("package --allow-dirty").run();
12141216

12151217
let f = File::open(&p.root().join("target/package/foo-0.1.0.crate")).unwrap();
12161218
validate_crate_contents(
12171219
f,
12181220
"foo-0.1.0.crate",
1219-
&["Cargo.toml", "Cargo.toml.orig", "src/lib.rs"],
1221+
&[
1222+
".cargo_vcs_info.json",
1223+
"Cargo.toml",
1224+
"Cargo.toml.orig",
1225+
"src/lib.rs",
1226+
],
12201227
&[],
12211228
);
12221229

@@ -1225,6 +1232,7 @@ fn issue_13695_dirty_vcs_info() {
12251232
.with_stderr("")
12261233
.with_stdout(
12271234
"\
1235+
.cargo_vcs_info.json
12281236
Cargo.toml
12291237
Cargo.toml.orig
12301238
src/lib.rs
@@ -2395,6 +2403,7 @@ fn finds_git_in_parent() {
23952403
p.cargo("package --list --allow-dirty")
23962404
.with_stdout(
23972405
"\
2406+
.cargo_vcs_info.json
23982407
Cargo.toml
23992408
Cargo.toml.orig
24002409
ignoreme
@@ -2408,6 +2417,7 @@ src/lib.rs
24082417
p.cargo("package --list --allow-dirty")
24092418
.with_stdout(
24102419
"\
2420+
.cargo_vcs_info.json
24112421
.gitignore
24122422
Cargo.toml
24132423
Cargo.toml.orig
@@ -2421,6 +2431,7 @@ src/lib.rs
24212431
p.cargo("package --list --allow-dirty")
24222432
.with_stdout(
24232433
"\
2434+
.cargo_vcs_info.json
24242435
.gitignore
24252436
Cargo.toml
24262437
Cargo.toml.orig
@@ -2683,6 +2694,7 @@ fn deleted_git_working_tree() {
26832694
p.cargo("package --allow-dirty --list")
26842695
.with_stdout(
26852696
"\
2697+
.cargo_vcs_info.json
26862698
Cargo.lock
26872699
Cargo.toml
26882700
Cargo.toml.orig
@@ -2697,6 +2709,7 @@ src/main.rs
26972709
p.cargo("package --allow-dirty --list")
26982710
.with_stdout(
26992711
"\
2712+
.cargo_vcs_info.json
27002713
Cargo.lock
27012714
Cargo.toml
27022715
Cargo.toml.orig

tests/testsuite/publish_lockfile.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ fn note_resolve_changes() {
249249
[NOTE] package `multi v0.1.0` added to the packaged Cargo.lock file, was originally sourced from `[..]/foo/multi`
250250
[NOTE] package `patched v1.0.0` added to the packaged Cargo.lock file, was originally sourced from `[..]/foo/patched`
251251
[PACKAGED] [..] files, [..] ([..] compressed)
252+
[WARNING] no (git) Cargo.toml found at `target/tmp/[..]/foo/Cargo.toml` in workdir `[..]`
252253
",
253254
)
254255
.run();

0 commit comments

Comments
 (0)