Skip to content

Commit ab4ab66

Browse files
committed
fix: cargo package failed on bare commit git repo
1 parent a562387 commit ab4ab66

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 6 additions & 2 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,9 +800,12 @@ fn check_repo_state(
799800
.collect();
800801
let dirty = !dirty_src_files.is_empty();
801802
if !dirty || opts.allow_dirty {
803+
if repo.is_empty()? {
804+
return Ok(GitVcsInfo { sha1: None, dirty });
805+
}
802806
let rev_obj = repo.revparse_single("HEAD")?;
803807
Ok(GitVcsInfo {
804-
sha1: rev_obj.id().to_string(),
808+
sha1: Some(rev_obj.id().to_string()),
805809
dirty,
806810
})
807811
} else {

tests/testsuite/package.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,13 +1273,28 @@ fn issue_14354_allowing_dirty_bare_commit() {
12731273
)
12741274
.file("src/lib.rs", "");
12751275

1276-
p.cargo("package --allow-dirty")
1277-
.with_status(101)
1278-
.with_stderr_data(str![[r#"
1279-
[ERROR] revspec 'HEAD' not found; class=Reference (4); code=NotFound (-3)
1276+
p.cargo("package --allow-dirty").run();
12801277

1281-
"#]])
1282-
.run();
1278+
let f = File::open(&p.root().join("target/package/foo-0.1.0.crate")).unwrap();
1279+
validate_crate_contents(
1280+
f,
1281+
"foo-0.1.0.crate",
1282+
&[
1283+
".cargo_vcs_info.json",
1284+
"Cargo.toml",
1285+
"Cargo.toml.orig",
1286+
"src/lib.rs",
1287+
],
1288+
&[(
1289+
".cargo_vcs_info.json",
1290+
r#"{
1291+
"git": {
1292+
"dirty": true
1293+
},
1294+
"path_in_vcs": ""
1295+
}"#,
1296+
)],
1297+
);
12831298
}
12841299

12851300
#[cargo_test]

0 commit comments

Comments
 (0)