1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//! Removing the folders that a deletion of untracked files left empty.
//!
//! Shared by both backends' `discard` and by `snapshot_restore` (redo of a
//! discard), so they all apply one rule.
use std::path::{Component, Path};
/// After `removed` files (paths relative to `root`) were deleted from disk,
/// removes each directory that is now completely empty, walking up from
/// every file's parent and stopping at the first directory that is not.
///
/// A directory is only ever removed when it is a real directory (never a
/// symlink, which is not followed), has no entry at all (so no hidden or
/// ignored file, no sub-folder, no `.git` of a submodule or worktree), and
/// `tracked_under(dir)` says the index holds nothing beneath it. The root
/// itself is never touched, and a directory that no removed file lived in is
/// never visited, so a folder that was already empty stays. Returns the
/// removed directories (forward slashes, relative to `root`).
pub fn prune_empty_parents(
root: &Path,
removed: &[String],
tracked_under: &dyn Fn(&str) -> bool,
) -> Vec<String> {
let mut gone: Vec<String> = Vec::new();
for file in removed {
let rel = file.replace('\\', "/");
let mut parts: Vec<&str> = rel
.split('/')
.filter(|p| !p.is_empty() && *p != ".")
.collect();
if parts.iter().any(|p| *p == ".." || *p == ".git") {
continue;
}
parts.pop();
while !parts.is_empty() {
let dir_rel = parts.join("/");
let full = root.join(&dir_rel);
if full.components().any(|c| matches!(c, Component::ParentDir)) {
break;
}
let Ok(meta) = std::fs::symlink_metadata(&full) else {
// Already gone (another file's walk removed it): go on up.
parts.pop();
continue;
};
if !meta.is_dir() {
break;
}
let empty = std::fs::read_dir(&full).is_ok_and(|mut d| d.next().is_none());
if !empty || tracked_under(&dir_rel) {
break;
}
if std::fs::remove_dir(&full).is_err() {
break;
}
gone.push(dir_rel);
parts.pop();
}
}
gone.sort();
gone.dedup();
gone
}