Skip to content

Commit 1b63685

Browse files
committed
Add a dirty flag to the vcs_info file.
1 parent 2a1299a commit 1b63685

File tree

2 files changed

+66
-22
lines changed

2 files changed

+66
-22
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ struct VcsInfo {
8181
#[derive(Serialize)]
8282
struct GitVcsInfo {
8383
sha1: String,
84+
/// Indicate whether or not the Git worktree is dirty.
85+
dirty: bool,
8486
}
8587

8688
/// Packages a single package in a workspace, returning the resulting tar file.
@@ -634,10 +636,12 @@ fn check_repo_state(
634636
.to_string()
635637
})
636638
.collect();
637-
if dirty_src_files.is_empty() || opts.allow_dirty {
639+
let dirty = !dirty_src_files.is_empty();
640+
if !dirty || opts.allow_dirty {
638641
let rev_obj = repo.revparse_single("HEAD")?;
639642
Ok(GitVcsInfo {
640643
sha1: rev_obj.id().to_string(),
644+
dirty,
641645
})
642646
} else {
643647
anyhow::bail!(

tests/testsuite/package.rs

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for
189189
let vcs_contents = format!(
190190
r#"{{
191191
"git": {{
192-
"sha1": "{}"
192+
"sha1": "{}",
193+
"dirty": false
193194
}},
194195
"path_in_vcs": ""
195196
}}
@@ -230,7 +231,8 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for
230231
let vcs_contents = format!(
231232
r#"{{
232233
"git": {{
233-
"sha1": "{}"
234+
"sha1": "{}",
235+
"dirty": false
234236
}},
235237
"path_in_vcs": "a/a"
236238
}}
@@ -1174,7 +1176,7 @@ src/lib.rs
11741176
}
11751177

11761178
#[cargo_test]
1177-
fn issue_13695_dirty_vcs_info() {
1179+
fn issue_13695_allow_dirty_vcs_info() {
11781180
let p = project()
11791181
.file(
11801182
"Cargo.toml",
@@ -1195,23 +1197,7 @@ fn issue_13695_dirty_vcs_info() {
11951197
// Initial commit, with no files added.
11961198
git::commit(&repo);
11971199

1198-
// Fail because worktree is dirty.
1199-
p.cargo("package")
1200-
.with_status(101)
1201-
.with_stderr_contains(
1202-
"[ERROR] 2 files in the working directory contain changes that were not yet committed into git:",
1203-
)
1204-
.run();
1205-
1206-
// Listing fails too.
1207-
p.cargo("package --list")
1208-
.with_status(101)
1209-
.with_stderr_contains(
1210-
"[ERROR] 2 files in the working directory contain changes that were not yet committed into git:",
1211-
)
1212-
.run();
1213-
1214-
// Allowing a dirty worktree results in the vcs file being included.
1200+
// Allowing a dirty worktree results in the vcs file still being included.
12151201
p.cargo("package --allow-dirty").run();
12161202

12171203
let f = File::open(&p.root().join("target/package/foo-0.1.0.crate")).unwrap();
@@ -1224,7 +1210,16 @@ fn issue_13695_dirty_vcs_info() {
12241210
"Cargo.toml.orig",
12251211
"src/lib.rs",
12261212
],
1227-
&[],
1213+
&[(
1214+
".cargo_vcs_info.json",
1215+
r#"{
1216+
"git": {
1217+
"sha1": "[..]",
1218+
"dirty": true
1219+
},
1220+
"path_in_vcs": ""
1221+
}"#,
1222+
)],
12281223
);
12291224

12301225
// Listing provides a consistent result.
@@ -1241,6 +1236,51 @@ src/lib.rs
12411236
.run();
12421237
}
12431238

1239+
#[cargo_test]
1240+
fn issue_13695_allowing_dirty_vcs_info_but_clean() {
1241+
let p = project().build();
1242+
let _ = git::repo(&paths::root().join("foo"))
1243+
.file(
1244+
"Cargo.toml",
1245+
r#"
1246+
[package]
1247+
name = "foo"
1248+
version = "0.1.0"
1249+
edition = "2015"
1250+
description = "foo"
1251+
license = "foo"
1252+
documentation = "foo"
1253+
"#,
1254+
)
1255+
.file("src/lib.rs", "")
1256+
.build();
1257+
1258+
// Allowing a dirty worktree despite it being clean.
1259+
p.cargo("package --allow-dirty").run();
1260+
1261+
let f = File::open(&p.root().join("target/package/foo-0.1.0.crate")).unwrap();
1262+
validate_crate_contents(
1263+
f,
1264+
"foo-0.1.0.crate",
1265+
&[
1266+
".cargo_vcs_info.json",
1267+
"Cargo.toml",
1268+
"Cargo.toml.orig",
1269+
"src/lib.rs",
1270+
],
1271+
&[(
1272+
".cargo_vcs_info.json",
1273+
r#"{
1274+
"git": {
1275+
"sha1": "[..]",
1276+
"dirty": false
1277+
},
1278+
"path_in_vcs": ""
1279+
}"#,
1280+
)],
1281+
);
1282+
}
1283+
12441284
#[cargo_test]
12451285
fn generated_manifest() {
12461286
let registry = registry::alt_init();

0 commit comments

Comments
 (0)