Skip to content

Commit 37cda2d

Browse files
committed
feat: don't generate .cargo_vcs_info.json if git repro has no commit
1 parent ab4ab66 commit 37cda2d

File tree

2 files changed

+16
-26
lines changed

2 files changed

+16
-26
lines changed

src/cargo/ops/cargo_package.rs

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

8888
#[derive(Serialize)]
8989
struct GitVcsInfo {
90-
#[serde(skip_serializing_if = "Option::is_none")]
91-
sha1: Option<String>,
90+
sha1: String,
9291
/// Indicate whether or not the Git worktree is dirty.
9392
#[serde(skip_serializing_if = "std::ops::Not::not")]
9493
dirty: bool,
@@ -744,10 +743,12 @@ fn check_repo_state(
744743
.and_then(|p| p.to_str())
745744
.unwrap_or("")
746745
.replace("\\", "/");
747-
return Ok(Some(VcsInfo {
748-
git: git(p, src_files, &repo, &opts)?,
749-
path_in_vcs,
750-
}));
746+
let Some(git) = git(p, src_files, &repo, &opts)? else {
747+
// If the git repo lacks essensial field like `sha1`, and since this field exists from the beginning,
748+
// then don't generate the corresponding file in order to maintain consistency with past behavior.
749+
return Ok(None);
750+
};
751+
return Ok(Some(VcsInfo { git, path_in_vcs }));
751752
}
752753
}
753754
gctx.shell().verbose(|shell| {
@@ -773,7 +774,7 @@ fn check_repo_state(
773774
src_files: &[PathBuf],
774775
repo: &git2::Repository,
775776
opts: &PackageOpts<'_>,
776-
) -> CargoResult<GitVcsInfo> {
777+
) -> CargoResult<Option<GitVcsInfo>> {
777778
// This is a collection of any dirty or untracked files. This covers:
778779
// - new/modified/deleted/renamed/type change (index or worktree)
779780
// - untracked files (which are "new" worktree files)
@@ -800,14 +801,16 @@ fn check_repo_state(
800801
.collect();
801802
let dirty = !dirty_src_files.is_empty();
802803
if !dirty || opts.allow_dirty {
804+
// Must check whetherthe repo has no commit firstly, otherwise `revparse_single` would fail on bare commit repo.
805+
// Due to lacking the `sha1` field, it's better not record the `GitVcsInfo` for consistency.
803806
if repo.is_empty()? {
804-
return Ok(GitVcsInfo { sha1: None, dirty });
807+
return Ok(None);
805808
}
806809
let rev_obj = repo.revparse_single("HEAD")?;
807-
Ok(GitVcsInfo {
808-
sha1: Some(rev_obj.id().to_string()),
810+
Ok(Some(GitVcsInfo {
811+
sha1: rev_obj.id().to_string(),
809812
dirty,
810-
})
813+
}))
811814
} else {
812815
anyhow::bail!(
813816
"{} files in the working directory contain changes that were \

tests/testsuite/package.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,21 +1279,8 @@ fn issue_14354_allowing_dirty_bare_commit() {
12791279
validate_crate_contents(
12801280
f,
12811281
"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-
)],
1282+
&["Cargo.toml", "Cargo.toml.orig", "src/lib.rs"],
1283+
&[],
12971284
);
12981285
}
12991286

0 commit comments

Comments
 (0)