Skip to content

Commit c9ce257

Browse files
committed
fix: Include vcs_info even if workspace is dirty.
1 parent 0c02803 commit c9ce257

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

src/cargo/ops/cargo_package.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,8 @@ pub fn package_one(
103103
}
104104
let src_files = src.list_files(pkg)?;
105105

106-
// Check (git) repository state, getting the current commit hash if not
107-
// dirty.
108-
let vcs_info = if !opts.allow_dirty {
109-
// This will error if a dirty repo is found.
110-
check_repo_state(pkg, &src_files, gctx)?
111-
} else {
112-
None
113-
};
106+
// Check (git) repository state, getting the current commit hash.
107+
let vcs_info = check_repo_state(pkg, &src_files, gctx, &opts)?;
114108

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

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

528522
/// Checks if the package source is in a *git* DVCS repository. If *git*, and
529-
/// the source is *dirty* (e.g., has uncommitted changes) then `bail!` with an
530-
/// informative message. Otherwise return the sha1 hash of the current *HEAD*
531-
/// commit, or `None` if no repo is found.
523+
/// the source is *dirty* (e.g., has uncommitted changes), and `--allow-dirty`
524+
/// has not been passed, then `bail!` with an informative message. Otherwise
525+
/// return the sha1 hash of the current *HEAD* commit, or `None` if no repo is
526+
/// found.
532527
fn check_repo_state(
533528
p: &Package,
534529
src_files: &[PathBuf],
535530
gctx: &GlobalContext,
531+
opts: &PackageOpts<'_>,
536532
) -> CargoResult<Option<VcsInfo>> {
537533
if let Ok(repo) = git2::Repository::discover(p.root()) {
538534
if let Some(workdir) = repo.workdir() {
@@ -552,7 +548,7 @@ fn check_repo_state(
552548
.unwrap_or("")
553549
.replace("\\", "/");
554550
return Ok(Some(VcsInfo {
555-
git: git(p, src_files, &repo)?,
551+
git: git(p, src_files, &repo, &opts)?,
556552
path_in_vcs,
557553
}));
558554
}
@@ -575,7 +571,12 @@ fn check_repo_state(
575571
// directory is dirty or not, thus we have to assume that it's clean.
576572
return Ok(None);
577573

578-
fn git(p: &Package, src_files: &[PathBuf], repo: &git2::Repository) -> CargoResult<GitVcsInfo> {
574+
fn git(
575+
p: &Package,
576+
src_files: &[PathBuf],
577+
repo: &git2::Repository,
578+
opts: &PackageOpts<'_>,
579+
) -> CargoResult<GitVcsInfo> {
579580
// This is a collection of any dirty or untracked files. This covers:
580581
// - new/modified/deleted/renamed/type change (index or worktree)
581582
// - untracked files (which are "new" worktree files)
@@ -600,7 +601,7 @@ fn check_repo_state(
600601
.to_string()
601602
})
602603
.collect();
603-
if dirty_src_files.is_empty() {
604+
if dirty_src_files.is_empty() || opts.allow_dirty {
604605
let rev_obj = repo.revparse_single("HEAD")?;
605606
Ok(GitVcsInfo {
606607
sha1: rev_obj.id().to_string(),

tests/testsuite/git.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2608,6 +2608,7 @@ fn include_overrides_gitignore() {
26082608
p.cargo("package --list --allow-dirty")
26092609
.with_stdout(
26102610
"\
2611+
.cargo_vcs_info.json
26112612
Cargo.toml
26122613
Cargo.toml.orig
26132614
ignored.txt

tests/testsuite/package.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ fn no_duplicates_from_modified_tracked_files() {
701701
p.cargo("package --list --allow-dirty")
702702
.with_stdout(
703703
"\
704+
.cargo_vcs_info.json
704705
Cargo.lock
705706
Cargo.toml
706707
Cargo.toml.orig
@@ -1009,6 +1010,7 @@ src/main.rs
10091010
.with_stderr("")
10101011
.with_stdout(
10111012
"\
1013+
.cargo_vcs_info.json
10121014
.gitignore
10131015
Cargo.lock
10141016
Cargo.toml
@@ -1207,14 +1209,19 @@ fn issue_13695_dirty_vcs_info() {
12071209
)
12081210
.run();
12091211

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

12131215
let f = File::open(&p.root().join("target/package/foo-0.1.0.crate")).unwrap();
12141216
validate_crate_contents(
12151217
f,
12161218
"foo-0.1.0.crate",
1217-
&["Cargo.toml", "Cargo.toml.orig", "src/lib.rs"],
1219+
&[
1220+
".cargo_vcs_info.json",
1221+
"Cargo.toml",
1222+
"Cargo.toml.orig",
1223+
"src/lib.rs",
1224+
],
12181225
&[],
12191226
);
12201227

@@ -1223,6 +1230,7 @@ fn issue_13695_dirty_vcs_info() {
12231230
.with_stderr("")
12241231
.with_stdout(
12251232
"\
1233+
.cargo_vcs_info.json
12261234
Cargo.toml
12271235
Cargo.toml.orig
12281236
src/lib.rs
@@ -2408,6 +2416,7 @@ fn finds_git_in_parent() {
24082416
p.cargo("package --list --allow-dirty")
24092417
.with_stdout(
24102418
"\
2419+
.cargo_vcs_info.json
24112420
Cargo.toml
24122421
Cargo.toml.orig
24132422
ignoreme
@@ -2421,6 +2430,7 @@ src/lib.rs
24212430
p.cargo("package --list --allow-dirty")
24222431
.with_stdout(
24232432
"\
2433+
.cargo_vcs_info.json
24242434
.gitignore
24252435
Cargo.toml
24262436
Cargo.toml.orig
@@ -2434,6 +2444,7 @@ src/lib.rs
24342444
p.cargo("package --list --allow-dirty")
24352445
.with_stdout(
24362446
"\
2447+
.cargo_vcs_info.json
24372448
.gitignore
24382449
Cargo.toml
24392450
Cargo.toml.orig
@@ -2696,6 +2707,7 @@ fn deleted_git_working_tree() {
26962707
p.cargo("package --allow-dirty --list")
26972708
.with_stdout(
26982709
"\
2710+
.cargo_vcs_info.json
26992711
Cargo.lock
27002712
Cargo.toml
27012713
Cargo.toml.orig
@@ -2710,6 +2722,7 @@ src/main.rs
27102722
p.cargo("package --allow-dirty --list")
27112723
.with_stdout(
27122724
"\
2725+
.cargo_vcs_info.json
27132726
Cargo.lock
27142727
Cargo.toml
27152728
Cargo.toml.orig

tests/testsuite/publish_lockfile.rs

+1
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ fn note_resolve_changes() {
247247
[NOTE] package `multi v0.1.0` added to the packaged Cargo.lock file, was originally sourced from `[..]/foo/multi`
248248
[NOTE] package `patched v1.0.0` added to the packaged Cargo.lock file, was originally sourced from `[..]/foo/patched`
249249
[PACKAGED] [..] files, [..] ([..] compressed)
250+
[WARNING] no (git) Cargo.toml found at `target/tmp/[..]/foo/Cargo.toml` in workdir `[..]`
250251
",
251252
)
252253
.run();

0 commit comments

Comments
 (0)