From 1b636855a9405b7745cea608df8eace0cd4139fd Mon Sep 17 00:00:00 2001 From: Tor Hovland <55164+torhovland@users.noreply.github.com> Date: Wed, 29 May 2024 09:13:26 +0200 Subject: [PATCH] Add a dirty flag to the vcs_info file. --- src/cargo/ops/cargo_package.rs | 6 ++- tests/testsuite/package.rs | 82 +++++++++++++++++++++++++--------- 2 files changed, 66 insertions(+), 22 deletions(-) diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index 0128473bc..26934de50 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -81,6 +81,8 @@ struct VcsInfo { #[derive(Serialize)] struct GitVcsInfo { sha1: String, + /// Indicate whether or not the Git worktree is dirty. + dirty: bool, } /// Packages a single package in a workspace, returning the resulting tar file. @@ -634,10 +636,12 @@ fn check_repo_state( .to_string() }) .collect(); - if dirty_src_files.is_empty() || opts.allow_dirty { + 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, }) } else { anyhow::bail!( diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index 79d84c141..163ce0188 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -189,7 +189,8 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for let vcs_contents = format!( r#"{{ "git": {{ - "sha1": "{}" + "sha1": "{}", + "dirty": false }}, "path_in_vcs": "" }} @@ -230,7 +231,8 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for let vcs_contents = format!( r#"{{ "git": {{ - "sha1": "{}" + "sha1": "{}", + "dirty": false }}, "path_in_vcs": "a/a" }} @@ -1174,7 +1176,7 @@ src/lib.rs } #[cargo_test] -fn issue_13695_dirty_vcs_info() { +fn issue_13695_allow_dirty_vcs_info() { let p = project() .file( "Cargo.toml", @@ -1195,23 +1197,7 @@ fn issue_13695_dirty_vcs_info() { // Initial commit, with no files added. git::commit(&repo); - // Fail because worktree is dirty. - p.cargo("package") - .with_status(101) - .with_stderr_contains( - "[ERROR] 2 files in the working directory contain changes that were not yet committed into git:", - ) - .run(); - - // Listing fails too. - p.cargo("package --list") - .with_status(101) - .with_stderr_contains( - "[ERROR] 2 files in the working directory contain changes that were not yet committed into git:", - ) - .run(); - - // Allowing a dirty worktree results in the vcs file being included. + // Allowing a dirty worktree results in the vcs file still being included. p.cargo("package --allow-dirty").run(); let f = File::open(&p.root().join("target/package/foo-0.1.0.crate")).unwrap(); @@ -1224,7 +1210,16 @@ fn issue_13695_dirty_vcs_info() { "Cargo.toml.orig", "src/lib.rs", ], - &[], + &[( + ".cargo_vcs_info.json", + r#"{ + "git": { + "sha1": "[..]", + "dirty": true + }, + "path_in_vcs": "" +}"#, + )], ); // Listing provides a consistent result. @@ -1241,6 +1236,51 @@ src/lib.rs .run(); } +#[cargo_test] +fn issue_13695_allowing_dirty_vcs_info_but_clean() { + let p = project().build(); + 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", "") + .build(); + + // Allowing a dirty worktree despite it being clean. + 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_vcs_info.json", + "Cargo.toml", + "Cargo.toml.orig", + "src/lib.rs", + ], + &[( + ".cargo_vcs_info.json", + r#"{ + "git": { + "sha1": "[..]", + "dirty": false + }, + "path_in_vcs": "" +}"#, + )], + ); +} + #[cargo_test] fn generated_manifest() { let registry = registry::alt_init();