Skip to content

Commit e33f540

Browse files
committed
fix: cargo package failed on bare commit git repo
1 parent 2b9eb5e commit e33f540

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ struct VcsInfo {
8787

8888
#[derive(Serialize)]
8989
struct GitVcsInfo {
90-
sha1: String,
90+
#[serde(skip_serializing_if = "Option::is_none")]
91+
sha1: Option<String>,
9192
/// Indicate whether or not the Git worktree is dirty.
9293
#[serde(skip_serializing_if = "std::ops::Not::not")]
9394
dirty: bool,
@@ -799,11 +800,13 @@ fn check_repo_state(
799800
.collect();
800801
let dirty = !dirty_src_files.is_empty();
801802
if !dirty || opts.allow_dirty {
802-
let rev_obj = repo.revparse_single("HEAD")?;
803-
Ok(GitVcsInfo {
804-
sha1: rev_obj.id().to_string(),
805-
dirty,
806-
})
803+
let sha1 = if let Ok(rev_obj) = repo.revparse_single("HEAD") {
804+
Some(rev_obj.id().to_string())
805+
} else {
806+
None
807+
};
808+
809+
Ok(GitVcsInfo { sha1: sha1, dirty })
807810
} else {
808811
anyhow::bail!(
809812
"{} files in the working directory contain changes that were \

tests/testsuite/package.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,12 +1274,36 @@ fn issue_14354_allowing_bare_git_repo() {
12741274
.file("src/lib.rs", "");
12751275

12761276
p.cargo("package --allow-dirty")
1277-
.with_status(101)
12781277
.with_stderr_data(str![[r#"
1279-
[ERROR] revspec 'HEAD' not found; class=Reference (4); code=NotFound (-3)
1278+
[PACKAGING] foo v0.1.0 ([ROOT]/foo)
1279+
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
1280+
[VERIFYING] foo v0.1.0 ([ROOT]/foo)
1281+
[COMPILING] foo v0.1.0 ([ROOT]/foo/target/package/foo-0.1.0)
1282+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
12801283
12811284
"#]])
12821285
.run();
1286+
1287+
let f = File::open(&p.root().join("target/package/foo-0.1.0.crate")).unwrap();
1288+
validate_crate_contents(
1289+
f,
1290+
"foo-0.1.0.crate",
1291+
&[
1292+
".cargo_vcs_info.json",
1293+
"Cargo.toml",
1294+
"Cargo.toml.orig",
1295+
"src/lib.rs",
1296+
],
1297+
&[(
1298+
".cargo_vcs_info.json",
1299+
r#"{
1300+
"git": {
1301+
"dirty": true
1302+
},
1303+
"path_in_vcs": ""
1304+
}"#,
1305+
)],
1306+
);
12831307
}
12841308

12851309
#[cargo_test]

0 commit comments

Comments
 (0)