Skip to content

Commit 652623b

Browse files
authored
fix(package): use relpath to cwd for vcs dirtiness report (#14970)
### What does this PR try to resolve? Address #14968 (comment) > I think the ideal solution is to be relative to the current directory but that takes more work and this incremental improvement is great! Sorry I should have noticed your comment earlier.
2 parents 081d7ba + d325ace commit 652623b

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ fn check_repo_state(
796796
.and_then(|p| p.to_str())
797797
.unwrap_or("")
798798
.replace("\\", "/");
799-
let Some(git) = git(src_files, &repo, &opts)? else {
799+
let Some(git) = git(gctx, src_files, &repo, &opts)? else {
800800
// If the git repo lacks essensial field like `sha1`, and since this field exists from the beginning,
801801
// then don't generate the corresponding file in order to maintain consistency with past behavior.
802802
return Ok(None);
@@ -805,6 +805,7 @@ fn check_repo_state(
805805
return Ok(Some(VcsInfo { git, path_in_vcs }));
806806

807807
fn git(
808+
gctx: &GlobalContext,
808809
src_files: &[PathBuf],
809810
repo: &git2::Repository,
810811
opts: &PackageOpts<'_>,
@@ -823,12 +824,13 @@ fn check_repo_state(
823824
// Find the intersection of dirty in git, and the src_files that would
824825
// be packaged. This is a lazy n^2 check, but seems fine with
825826
// thousands of files.
826-
let workdir = repo.workdir().unwrap();
827+
let cwd = gctx.cwd();
827828
let mut dirty_src_files: Vec<_> = src_files
828829
.iter()
829830
.filter(|src_file| dirty_files.iter().any(|path| src_file.starts_with(path)))
830831
.map(|path| {
831-
path.strip_prefix(workdir)
832+
pathdiff::diff_paths(path, cwd)
833+
.as_ref()
832834
.unwrap_or(path)
833835
.display()
834836
.to_string()

tests/testsuite/package.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,20 @@ Cargo.toml
10991099
11001100
to proceed despite this and include the uncommitted changes, pass the `--allow-dirty` flag
11011101
1102+
"#]])
1103+
.run();
1104+
1105+
// cd to `src` and cargo report relative paths.
1106+
p.cargo("package")
1107+
.cwd(p.root().join("src"))
1108+
.with_status(101)
1109+
.with_stderr_data(str![[r#"
1110+
[ERROR] 1 files in the working directory contain changes that were not yet committed into git:
1111+
1112+
../Cargo.toml
1113+
1114+
to proceed despite this and include the uncommitted changes, pass the `--allow-dirty` flag
1115+
11021116
"#]])
11031117
.run();
11041118
}

0 commit comments

Comments
 (0)