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 2b9eb5e commit c8f2734
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
15 changes: 9 additions & 6 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,11 +800,13 @@ fn check_repo_state(
.collect();
let dirty = !dirty_src_files.is_empty();
if !dirty || opts.allow_dirty {
let rev_obj = repo.revparse_single("HEAD")?;
Ok(GitVcsInfo {
sha1: rev_obj.id().to_string(),
dirty,
})
let sha1 = if let Ok(rev_obj) = repo.revparse_single("HEAD") {
Some(rev_obj.id().to_string())
} else {
None
};

Ok(GitVcsInfo { sha1, dirty })
} else {
anyhow::bail!(
"{} files in the working directory contain changes that were \
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_bare_git_repo() {
)
.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 c8f2734

Please sign in to comment.