Skip to content

Commit b66cad8

Browse files
committed
Auto merge of #14359 - linyihai:issue-14354, r=weihanglo
Fix: `cargo package` failed on bare commit git repo. ### What does this PR try to resolve? Fixes #14354 This approach chose to not generate a `.cargo_vcs_info.json` for bare commit git repo. ### How should we test and review this PR? Compare the test changes before and after the two commits ### Additional information
2 parents 0d8d22f + 37cda2d commit b66cad8

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -775,10 +775,12 @@ fn check_repo_state(
775775
.and_then(|p| p.to_str())
776776
.unwrap_or("")
777777
.replace("\\", "/");
778-
return Ok(Some(VcsInfo {
779-
git: git(p, src_files, &repo, &opts)?,
780-
path_in_vcs,
781-
}));
778+
let Some(git) = git(p, src_files, &repo, &opts)? else {
779+
// If the git repo lacks essensial field like `sha1`, and since this field exists from the beginning,
780+
// then don't generate the corresponding file in order to maintain consistency with past behavior.
781+
return Ok(None);
782+
};
783+
return Ok(Some(VcsInfo { git, path_in_vcs }));
782784
}
783785
}
784786
gctx.shell().verbose(|shell| {
@@ -804,7 +806,7 @@ fn check_repo_state(
804806
src_files: &[PathBuf],
805807
repo: &git2::Repository,
806808
opts: &PackageOpts<'_>,
807-
) -> CargoResult<GitVcsInfo> {
809+
) -> CargoResult<Option<GitVcsInfo>> {
808810
// This is a collection of any dirty or untracked files. This covers:
809811
// - new/modified/deleted/renamed/type change (index or worktree)
810812
// - untracked files (which are "new" worktree files)
@@ -831,11 +833,16 @@ fn check_repo_state(
831833
.collect();
832834
let dirty = !dirty_src_files.is_empty();
833835
if !dirty || opts.allow_dirty {
836+
// Must check whetherthe repo has no commit firstly, otherwise `revparse_single` would fail on bare commit repo.
837+
// Due to lacking the `sha1` field, it's better not record the `GitVcsInfo` for consistency.
838+
if repo.is_empty()? {
839+
return Ok(None);
840+
}
834841
let rev_obj = repo.revparse_single("HEAD")?;
835-
Ok(GitVcsInfo {
842+
Ok(Some(GitVcsInfo {
836843
sha1: rev_obj.id().to_string(),
837844
dirty,
838-
})
845+
}))
839846
} else {
840847
anyhow::bail!(
841848
"{} files in the working directory contain changes that were \

tests/testsuite/package.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,36 @@ fn issue_13695_allowing_dirty_vcs_info_but_clean() {
12541254
);
12551255
}
12561256

1257+
#[cargo_test]
1258+
fn issue_14354_allowing_dirty_bare_commit() {
1259+
let p = project().build();
1260+
// Init a bare commit git repo
1261+
let _ = git::repo(&paths::root().join("foo"))
1262+
.file(
1263+
"Cargo.toml",
1264+
r#"
1265+
[package]
1266+
name = "foo"
1267+
version = "0.1.0"
1268+
edition = "2015"
1269+
description = "foo"
1270+
license = "foo"
1271+
documentation = "foo"
1272+
"#,
1273+
)
1274+
.file("src/lib.rs", "");
1275+
1276+
p.cargo("package --allow-dirty").run();
1277+
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+
&["Cargo.toml", "Cargo.toml.orig", "src/lib.rs"],
1283+
&[],
1284+
);
1285+
}
1286+
12571287
#[cargo_test]
12581288
fn generated_manifest() {
12591289
let registry = registry::alt_init();

0 commit comments

Comments
 (0)