Skip to content

Commit a67e9ec

Browse files
committed
Apply suggestions from review
1 parent d41d177 commit a67e9ec

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

src/cargo/ops/cargo_package/vcs.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -189,21 +189,22 @@ fn git(
189189
// - untracked files (which are "new" worktree files)
190190
// - ignored (in case the user has an `include` directive that
191191
// conflicts with .gitignore).
192-
let mut dirty_files = Vec::new();
192+
let (mut dirty_files, mut dirty_files_outside_package_root) = (Vec::new(), Vec::new());
193193
let workdir = repo.workdir().unwrap();
194-
let mut dirty_files_outside_of_package_root = collect_statuses(
194+
collect_statuses(
195195
repo,
196196
workdir,
197197
relative_package_root(repo, pkg.root()).as_deref(),
198198
&mut dirty_files,
199+
&mut dirty_files_outside_package_root,
199200
)?;
200201

201202
// Include each submodule so that the error message can provide
202203
// specifically *which* files in a submodule are modified.
203204
status_submodules(
204205
repo,
205206
&mut dirty_files,
206-
&mut dirty_files_outside_of_package_root,
207+
&mut dirty_files_outside_package_root,
207208
)?;
208209

209210
// Find the intersection of dirty in git, and the src_files that would
@@ -230,7 +231,7 @@ fn git(
230231
})
231232
.map(|p| p.as_ref())
232233
.chain(
233-
dirty_files_outside_pkg_root(ws, pkg, &dirty_files_outside_of_package_root, src_files)?
234+
dirty_files_outside_pkg_root(ws, pkg, &dirty_files_outside_package_root, src_files)?
234235
.iter(),
235236
)
236237
.map(|path| {
@@ -243,8 +244,6 @@ fn git(
243244
.collect();
244245
let dirty = !dirty_src_files.is_empty();
245246
if !dirty || opts.allow_dirty {
246-
// Must check whether the repo has no commit firstly; otherwise `revparse_single` would fail on bare commit repo.
247-
// Due to lacking the `sha1` field, it's better not record the `GitVcsInfo` for consistency.
248247
let maybe_head_id = repo.head()?.try_peel_to_id_in_place()?;
249248
Ok(maybe_head_id.map(|id| GitVcsInfo {
250249
sha1: id.to_string(),
@@ -264,14 +263,16 @@ fn git(
264263

265264
/// Helper to collect dirty statuses for a single repo.
266265
/// `relative_package_root` is `Some` if the root is a sub-directory of the workdir.
267-
/// Returns the dirty files outside `relative_package_root`.
266+
/// Writes dirty files outside `relative_package_root` into `dirty_files_outside_package_root`,
267+
/// and all *everything else* into `dirty_files`.
268268
#[must_use]
269269
fn collect_statuses(
270270
repo: &gix::Repository,
271271
workdir: &Path,
272272
relative_package_root: Option<&Path>,
273273
dirty_files: &mut Vec<PathBuf>,
274-
) -> CargoResult<Vec<PathBuf>> {
274+
dirty_files_outside_package_root: &mut Vec<PathBuf>,
275+
) -> CargoResult<()> {
275276
let statuses = repo
276277
.status(gix::progress::Discard)?
277278
.dirwalk_options(|opts| {
@@ -296,7 +297,6 @@ fn collect_statuses(
296297
)
297298
})?;
298299

299-
let mut dirty_files_outside_of_package_root = Vec::new();
300300
for status in statuses {
301301
let status = status.with_context(|| {
302302
format!(
@@ -308,7 +308,7 @@ fn collect_statuses(
308308
let rel_path = gix::path::from_bstr(status.location());
309309
let path = workdir.join(&rel_path);
310310
if relative_package_root.is_some_and(|pkg_root| !rel_path.starts_with(pkg_root)) {
311-
dirty_files_outside_of_package_root.push(path);
311+
dirty_files_outside_package_root.push(path);
312312
continue;
313313
}
314314

@@ -326,14 +326,14 @@ fn collect_statuses(
326326

327327
dirty_files.push(path);
328328
}
329-
Ok(dirty_files_outside_of_package_root)
329+
Ok(())
330330
}
331331

332332
/// Helper to collect dirty statuses while recursing into submodules.
333333
fn status_submodules(
334334
repo: &gix::Repository,
335335
dirty_files: &mut Vec<PathBuf>,
336-
dirty_files_outside_of_package_root: &mut Vec<PathBuf>,
336+
dirty_files_outside_package_root: &mut Vec<PathBuf>,
337337
) -> CargoResult<()> {
338338
let Some(submodules) = repo.submodules()? else {
339339
return Ok(());
@@ -345,13 +345,14 @@ fn status_submodules(
345345
let Some(workdir) = sub_repo.workdir() else {
346346
continue;
347347
};
348-
status_submodules(&sub_repo, dirty_files, dirty_files_outside_of_package_root)?;
349-
dirty_files_outside_of_package_root.extend(collect_statuses(
348+
status_submodules(&sub_repo, dirty_files, dirty_files_outside_package_root)?;
349+
collect_statuses(
350350
&sub_repo,
351351
workdir,
352352
None,
353353
dirty_files,
354-
)?);
354+
dirty_files_outside_package_root,
355+
)?;
355356
}
356357
}
357358
Ok(())

tests/testsuite/package.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,11 +1470,9 @@ fn dirty_file_outside_pkg_root_inside_submodule() {
14701470
p.symlink("submodule/file.txt", "isengard/src/file.txt");
14711471
git::add(&repo);
14721472
git::commit(&repo);
1473-
// This dirtiness should be detected in the future.
14741473
p.change_file("submodule/file.txt", "changed");
14751474

1476-
let mut execs = p.cargo("package --workspace --no-verify");
1477-
execs
1475+
p.cargo("package --workspace --no-verify")
14781476
.with_status(101)
14791477
.with_stderr_data(str![[r#"
14801478
[ERROR] 1 files in the working directory contain changes that were not yet committed into git:

0 commit comments

Comments
 (0)