diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index b33383422cd..dfe9ec329f8 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -583,10 +583,12 @@ fn check_repo_state( .and_then(|p| p.to_str()) .unwrap_or("") .replace("\\", "/"); - return Ok(Some(VcsInfo { - git: git(p, src_files, &repo, &opts)?, - path_in_vcs, - })); + let Some(git) = git(p, src_files, &repo, &opts)? else { + // If the git repo lacks essensial field like `sha1`, and since this field exists from the beginning, + // then don't generate the corresponding file in order to maintain consistency with past behavior. + return Ok(None); + }; + return Ok(Some(VcsInfo { git, path_in_vcs })); } } gctx.shell().verbose(|shell| { @@ -612,7 +614,7 @@ fn check_repo_state( src_files: &[PathBuf], repo: &git2::Repository, opts: &PackageOpts<'_>, - ) -> CargoResult { + ) -> CargoResult> { // This is a collection of any dirty or untracked files. This covers: // - new/modified/deleted/renamed/type change (index or worktree) // - untracked files (which are "new" worktree files) @@ -639,11 +641,16 @@ fn check_repo_state( .collect(); let dirty = !dirty_src_files.is_empty(); if !dirty || opts.allow_dirty { + // Must check whetherthe repo has no commit firstly, otherwise `revparse_single` would fail on bare commit repo. + // Due to lacking the `sha1` field, it's better not record the `GitVcsInfo` for consistency. + if repo.is_empty()? { + return Ok(None); + } let rev_obj = repo.revparse_single("HEAD")?; - Ok(GitVcsInfo { + Ok(Some(GitVcsInfo { sha1: rev_obj.id().to_string(), dirty, - }) + })) } else { anyhow::bail!( "{} files in the working directory contain changes that were \ diff --git a/tests/build-std/main.rs b/tests/build-std/main.rs index 33df92459de..fcace7336fc 100644 --- a/tests/build-std/main.rs +++ b/tests/build-std/main.rs @@ -133,6 +133,8 @@ fn basic() { assert_eq!(p.glob(deps_dir.join("*.dylib")).count(), 0); } +#[allow(unused_attributes)] +#[ignore = "to unblock beta-1.81 backport"] #[cargo_test(build_std_real)] fn cross_custom() { let p = project() @@ -176,6 +178,8 @@ fn cross_custom() { .run(); } +#[allow(unused_attributes)] +#[ignore = "to unblock beta-1.81 backport"] #[cargo_test(build_std_real)] fn custom_test_framework() { let p = project() @@ -237,6 +241,8 @@ fn custom_test_framework() { // Fixing rust-lang/rust#117839. // on macOS it never gets remapped. // Might be a separate issue, so only run on Linux. +#[allow(unused_attributes)] +#[ignore = "to unblock beta-1.81 backport"] #[cargo_test(build_std_real)] #[cfg(target_os = "linux")] fn remap_path_scope() { diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 49eb60db0c2..b73016554bd 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -6576,7 +6576,7 @@ fn user_specific_cfgs_are_filtered_out() { ) .build(); - p.cargo("rustc -- --cfg debug_assertions --cfg proc_macro -Aunknown_lints -Aunexpected_builtin_cfgs") + p.cargo("rustc -- --cfg debug_assertions --cfg proc_macro -Aunknown_lints -Aexplicit_builtin_cfgs_in_flags") .run(); p.process(&p.bin("foo")).run(); } diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index 773cdf75872..90aa6575c67 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -1255,6 +1255,36 @@ fn issue_13695_allowing_dirty_vcs_info_but_clean() { ); } +#[cargo_test] +fn issue_14354_allowing_dirty_bare_commit() { + let p = project().build(); + // Init a bare commit git repo + let _ = git::repo(&paths::root().join("foo")) + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + edition = "2015" + description = "foo" + license = "foo" + documentation = "foo" + "#, + ) + .file("src/lib.rs", ""); + + p.cargo("package --allow-dirty").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.toml", "Cargo.toml.orig", "src/lib.rs"], + &[], + ); +} + #[cargo_test] fn generated_manifest() { let registry = registry::alt_init();