Skip to content

Commit

Permalink
fix(package): use relpath to cwd for vcs dirtiness report (#14970)
Browse files Browse the repository at this point in the history
### 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.
  • Loading branch information
weihanglo authored Dec 20, 2024
2 parents 081d7ba + d325ace commit 652623b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ fn check_repo_state(
.and_then(|p| p.to_str())
.unwrap_or("")
.replace("\\", "/");
let Some(git) = git(src_files, &repo, &opts)? else {
let Some(git) = git(gctx, 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);
Expand All @@ -805,6 +805,7 @@ fn check_repo_state(
return Ok(Some(VcsInfo { git, path_in_vcs }));

fn git(
gctx: &GlobalContext,
src_files: &[PathBuf],
repo: &git2::Repository,
opts: &PackageOpts<'_>,
Expand All @@ -823,12 +824,13 @@ fn check_repo_state(
// Find the intersection of dirty in git, and the src_files that would
// be packaged. This is a lazy n^2 check, but seems fine with
// thousands of files.
let workdir = repo.workdir().unwrap();
let cwd = gctx.cwd();
let mut dirty_src_files: Vec<_> = src_files
.iter()
.filter(|src_file| dirty_files.iter().any(|path| src_file.starts_with(path)))
.map(|path| {
path.strip_prefix(workdir)
pathdiff::diff_paths(path, cwd)
.as_ref()
.unwrap_or(path)
.display()
.to_string()
Expand Down
14 changes: 14 additions & 0 deletions tests/testsuite/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,20 @@ Cargo.toml
to proceed despite this and include the uncommitted changes, pass the `--allow-dirty` flag
"#]])
.run();

// cd to `src` and cargo report relative paths.
p.cargo("package")
.cwd(p.root().join("src"))
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] 1 files in the working directory contain changes that were not yet committed into git:
../Cargo.toml
to proceed despite this and include the uncommitted changes, pass the `--allow-dirty` flag
"#]])
.run();
}
Expand Down

0 comments on commit 652623b

Please sign in to comment.