fix: Include vcs_info even if workspace is dirty.

This commit is contained in:
Tor Hovland 2024-05-27 10:18:44 +02:00
parent a6ad3a3ed5
commit 2a1299a878
4 changed files with 32 additions and 16 deletions

View file

@ -235,14 +235,8 @@ fn prepare_archive(
}
let src_files = src.list_files(pkg)?;
// Check (git) repository state, getting the current commit hash if not
// dirty.
let vcs_info = if !opts.allow_dirty {
// This will error if a dirty repo is found.
check_repo_state(pkg, &src_files, gctx)?
} else {
None
};
// Check (git) repository state, getting the current commit hash.
let vcs_info = check_repo_state(pkg, &src_files, gctx, &opts)?;
build_ar_list(ws, pkg, src_files, vcs_info)
}
@ -559,13 +553,15 @@ fn check_metadata(pkg: &Package, gctx: &GlobalContext) -> CargoResult<()> {
}
/// Checks if the package source is in a *git* DVCS repository. If *git*, and
/// the source is *dirty* (e.g., has uncommitted changes) then `bail!` with an
/// informative message. Otherwise return the sha1 hash of the current *HEAD*
/// commit, or `None` if no repo is found.
/// the source is *dirty* (e.g., has uncommitted changes), and `--allow-dirty`
/// has not been passed, then `bail!` with an informative message. Otherwise
/// return the sha1 hash of the current *HEAD* commit, or `None` if no repo is
/// found.
fn check_repo_state(
p: &Package,
src_files: &[PathBuf],
gctx: &GlobalContext,
opts: &PackageOpts<'_>,
) -> CargoResult<Option<VcsInfo>> {
if let Ok(repo) = git2::Repository::discover(p.root()) {
if let Some(workdir) = repo.workdir() {
@ -585,7 +581,7 @@ fn check_repo_state(
.unwrap_or("")
.replace("\\", "/");
return Ok(Some(VcsInfo {
git: git(p, src_files, &repo)?,
git: git(p, src_files, &repo, &opts)?,
path_in_vcs,
}));
}
@ -608,7 +604,12 @@ fn check_repo_state(
// directory is dirty or not, thus we have to assume that it's clean.
return Ok(None);
fn git(p: &Package, src_files: &[PathBuf], repo: &git2::Repository) -> CargoResult<GitVcsInfo> {
fn git(
p: &Package,
src_files: &[PathBuf],
repo: &git2::Repository,
opts: &PackageOpts<'_>,
) -> CargoResult<GitVcsInfo> {
// 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)
@ -633,7 +634,7 @@ fn check_repo_state(
.to_string()
})
.collect();
if dirty_src_files.is_empty() {
if dirty_src_files.is_empty() || opts.allow_dirty {
let rev_obj = repo.revparse_single("HEAD")?;
Ok(GitVcsInfo {
sha1: rev_obj.id().to_string(),

View file

@ -2610,6 +2610,7 @@ fn include_overrides_gitignore() {
p.cargo("package --list --allow-dirty")
.with_stdout(
"\
.cargo_vcs_info.json
Cargo.toml
Cargo.toml.orig
ignored.txt

View file

@ -703,6 +703,7 @@ fn no_duplicates_from_modified_tracked_files() {
p.cargo("package --list --allow-dirty")
.with_stdout(
"\
.cargo_vcs_info.json
Cargo.lock
Cargo.toml
Cargo.toml.orig
@ -1011,6 +1012,7 @@ src/main.rs
.with_stderr("")
.with_stdout(
"\
.cargo_vcs_info.json
.gitignore
Cargo.lock
Cargo.toml
@ -1209,14 +1211,19 @@ fn issue_13695_dirty_vcs_info() {
)
.run();
// Allowing a dirty worktree results in the vcs file not being included.
// Allowing a dirty worktree results in the vcs file being included.
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_vcs_info.json",
"Cargo.toml",
"Cargo.toml.orig",
"src/lib.rs",
],
&[],
);
@ -1225,6 +1232,7 @@ fn issue_13695_dirty_vcs_info() {
.with_stderr("")
.with_stdout(
"\
.cargo_vcs_info.json
Cargo.toml
Cargo.toml.orig
src/lib.rs
@ -2395,6 +2403,7 @@ fn finds_git_in_parent() {
p.cargo("package --list --allow-dirty")
.with_stdout(
"\
.cargo_vcs_info.json
Cargo.toml
Cargo.toml.orig
ignoreme
@ -2408,6 +2417,7 @@ src/lib.rs
p.cargo("package --list --allow-dirty")
.with_stdout(
"\
.cargo_vcs_info.json
.gitignore
Cargo.toml
Cargo.toml.orig
@ -2421,6 +2431,7 @@ src/lib.rs
p.cargo("package --list --allow-dirty")
.with_stdout(
"\
.cargo_vcs_info.json
.gitignore
Cargo.toml
Cargo.toml.orig
@ -2683,6 +2694,7 @@ fn deleted_git_working_tree() {
p.cargo("package --allow-dirty --list")
.with_stdout(
"\
.cargo_vcs_info.json
Cargo.lock
Cargo.toml
Cargo.toml.orig
@ -2697,6 +2709,7 @@ src/main.rs
p.cargo("package --allow-dirty --list")
.with_stdout(
"\
.cargo_vcs_info.json
Cargo.lock
Cargo.toml
Cargo.toml.orig

View file

@ -249,6 +249,7 @@ fn note_resolve_changes() {
[NOTE] package `multi v0.1.0` added to the packaged Cargo.lock file, was originally sourced from `[..]/foo/multi`
[NOTE] package `patched v1.0.0` added to the packaged Cargo.lock file, was originally sourced from `[..]/foo/patched`
[PACKAGED] [..] files, [..] ([..] compressed)
[WARNING] no (git) Cargo.toml found at `target/tmp/[..]/foo/Cargo.toml` in workdir `[..]`
",
)
.run();