Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Ask the guard only about folders Duplicates could offer

Țugui Dragoș30e098078caf
diff --git a/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swift b/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swift
index b2db42e9..1ffed652 100644
--- a/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swift
@@ -331,10 +331,18 @@ struct FolderDuplicates: Sendable {
downloads: String,
removalGuard: RemovalGuard
) -> [DuplicateFolderGroup] {
- let allowed = groups
- .map { (digest: $0.digest, folders: $0.folders.filter { removalGuard.allowsRemoval(of: $0.url) }) }
- .filter { $0.folders.count > 1 }
- let offered = Set(allowed.flatMap { $0.folders.map { DuplicateFinder.path(of: $0.url) } })
+ // The shallowest groups go first. A group whose folders all sit inside offered ones can never be a row, nor
+ // change which folders are inside an offered one, so the guard is not asked about it.
+ let shallowestFirst = groups.map { (depth: Self.depth(of: $0.folders), group: $0) }
+ .sorted { $0.depth < $1.depth }.map(\.group)
+ var allowed: [(digest: SHA256.Digest, folders: [Folder])] = []
+ var offered: Set<String> = []
+ for group in shallowestFirst where !group.folders.allSatisfy({ DuplicateFinder.isInside(offered, $0.url) }) {
+ let folders = group.folders.filter { removalGuard.allowsRemoval(of: $0.url) }
+ guard folders.count > 1 else { continue }
+ allowed.append((group.digest, folders))
+ offered.formUnion(folders.map { DuplicateFinder.path(of: $0.url) })
+ }
return allowed.compactMap { group -> DuplicateFolderGroup? in
let topmost = group.folders.filter { !DuplicateFinder.isInside(offered, $0.url) }
@@ -396,6 +404,10 @@ struct FolderDuplicates: Sendable {
Dictionary(digests.map { ($0.item.identity, $0.digest) }, uniquingKeysWith: { first, _ in first })
}
+ private static func depth(of folders: [Folder]) -> Int {
+ folders.map { PathComponents.of(DuplicateFinder.path(of: $0.url)).count }.min() ?? 0
+ }
+
/// Encodes one entry as a tag, the name's length, the name, and `tail`. The length comes first so a name
/// cannot be misread as the end of one entry and the start of the next.
private static func field(_ tag: String, _ name: String, _ tail: [UInt8]) -> [UInt8] {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift
index 9a1885d8..f8ff47b5 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/FolderDuplicateTests.swift
@@ -68,6 +68,33 @@ struct FolderDuplicateTests {
#expect(result.groups.isEmpty)
}
+ /// Documents itself is never moved, so its copy's row is the identical folder inside each of them.
+ @Test func offersTheFoldersInsideAPairTheGuardRefuses() async throws {
+ let directory = try TemporaryDirectory()
+ let contents = randomData(count: 4_000)
+ for folder in ["home/Documents/Trip", "home/Archive/Documents/Trip"] {
+ try directory.file("\(folder)/photo.jpg", contents: contents)
+ }
+
+ let result = try await scan(directory)
+
+ #expect(names(result) == [["Documents/Trip", "Archive/Documents/Trip"]])
+ }
+
+ @Test func offersTheCopiesOfAFolderThatSitOutsideAnOfferedPair() async throws {
+ let directory = try TemporaryDirectory()
+ let contents = randomData(count: 4_000)
+ for folder in ["home/Documents/Trip/Album", "home/Pictures/Trip/Album", "home/Desktop/Album", "home/Movies/Album"] {
+ try directory.file("\(folder)/photo.jpg", contents: contents)
+ }
+
+ let result = try await scan(directory)
+
+ #expect(Set(names(result).map(Set.init)) == [
+ ["Documents/Trip", "Pictures/Trip"], ["Desktop/Album", "Movies/Album"],
+ ])
+ }
+
@Test func groupsFoldersWithIdenticalContents() async throws {
let directory = try TemporaryDirectory()
try trip(in: directory)