Skip to content

Commit

Permalink
fix: cargo package failed on bare commit git repo
Browse files Browse the repository at this point in the history
  • Loading branch information
linyihai committed Aug 6, 2024
1 parent c7f2325 commit d465dd7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ struct VcsInfo {

#[derive(Serialize)]
struct GitVcsInfo {
sha1: String,
#[serde(skip_serializing_if = "Option::is_none")]
sha1: Option<String>,
/// Indicate whether or not the Git worktree is dirty.
#[serde(skip_serializing_if = "std::ops::Not::not")]
dirty: bool,
Expand Down Expand Up @@ -799,9 +800,12 @@ fn check_repo_state(
.collect();
let dirty = !dirty_src_files.is_empty();
if !dirty || opts.allow_dirty {
if repo.is_empty()? {
return Ok(GitVcsInfo { sha1: None, dirty });
}
let rev_obj = repo.revparse_single("HEAD")?;
Ok(GitVcsInfo {
sha1: rev_obj.id().to_string(),
sha1: Some(rev_obj.id().to_string()),
dirty,
})
} else {
Expand Down
27 changes: 21 additions & 6 deletions tests/testsuite/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1273,13 +1273,28 @@ fn issue_14354_allowing_dirty_bare_commit() {
)
.file("src/lib.rs", "");

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

"#]])
.run();
let f = File::open(&p.root().join("target/package/foo-0.1.0.crate")).unwrap();
validate_crate_contents(
f,
"foo-0.1.0.crate",
&[
".cargo_vcs_info.json",
"Cargo.toml",
"Cargo.toml.orig",
"src/lib.rs",
],
&[(
".cargo_vcs_info.json",
r#"{
"git": {
"dirty": true
},
"path_in_vcs": ""
}"#,
)],
);
}

#[cargo_test]
Expand Down

0 comments on commit d465dd7

Please sign in to comment.