Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
Keep the duplicate on the home folder's disk before an older one
Țugui Dragoș84094007e507
diff --git a/GUIDE.md b/GUIDE.mdindex 2b2a9e8d..5e80bf91 100644--- a/GUIDE.md+++ b/GUIDE.md@@ -111,16 +111,17 @@ Space shows what fills your disk, area by area, and what each area is.- **Build Artifacts:** what builds left in your projects, like `node_modules`, `DerivedData`, or `target`, each onlywhen its tool's own file proves it. Nothing is selected for you, and Select Recommended passes by a projectchanged in the last 7 days.-- **Duplicates:** files and whole folders with identical contents, compared with SHA-256. Unless you choose folders, it- looks in Desktop, Documents, Downloads, Pictures, Movies, and Music. Files under 100 KB, and folders holding less, are- left out unless you choose a smaller size, in the app and in `peel duplicates` alike. Copies whose Finder tags, Finder- comment, or resource fork differ aren't listed together, since moving the copy that has them would lose them. After a- scan you start, Peel selects every copy in each group but the first, which it suggests keeping, so look before you- move them: one copy is always kept. A copy in a folder that syncs to your other devices, such as iCloud Drive or what- Desktop and Documents hold when they are in iCloud, is listed and never moved, since the Trash would take it from your- other devices too: beside a copy on this Mac it is marked Stays, and a group whose every copy syncs is listed under- Synced to Other Devices. A chosen folder Peel doesn't scan, such as a repository, is named with why, in Scan Settings- and with the result.+- **Duplicates:** files and whole folders with identical contents, compared with SHA-256. Unless you choose folders,+ it looks in Desktop, Documents, Downloads, Pictures, Movies, and Music. Files under 100 KB, and folders holding+ less, are left out unless you choose a smaller size, in the app and in `peel duplicates` alike. Copies whose Finder+ tags, Finder comment, or resource fork differ aren't listed together, since moving the copy that has them would lose+ them. After a scan you start, Peel selects every copy in each group but the first, which it suggests keeping: one+ outside Downloads, then one whose name doesn't end like a copy's, such as `photo copy.jpg` or `photo (1).jpg`, then+ one on the disk your home folder is on, then the oldest. Look before you move them: one copy is always kept. A copy+ in a folder that syncs to your other devices, such as iCloud Drive or what Desktop and Documents hold when they are+ in iCloud, is listed and never moved, since the Trash would take it from your other devices too: beside a copy on+ this Mac it is marked Stays, and a group whose every copy syncs is listed under Synced to Other Devices. A chosen+ folder Peel doesn't scan, such as a repository, is named with why, in Scan Settings and with the result.- **Installers and Backups:** disk images and packages in Downloads, Desktop, Documents, Public, and Shared, and inthe folders directly inside them, where installers are usually left, with archives there that hold an app or aninstaller. It also lists packages an app keeps in Application Support, macOS installers there and in the foldersdiff --git a/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swift b/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swiftindex e67a22b3..91aebb1c 100644--- a/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swift+++ b/Packages/PeelCore/Sources/PeelCore/Duplicates/DuplicateFinder.swift@@ -184,7 +184,7 @@ public struct DuplicateFinder: Sendable {).groups(in: scannable,minimumSize: options.minimumSize,- downloads: Self.path(of: homeDirectory.appending(path: "Downloads")),+ keepOrder: KeepOrder(home: homeDirectory),removalGuard: removalGuard,known: known,progress: progress@@ -413,14 +413,9 @@ public struct DuplicateFinder: Sendable {}private func makeGroup(_ members: [HashedCandidate]) -> DuplicateGroup {- let downloads = Self.path(of: homeDirectory.appending(path: "Downloads"))+ let order = KeepOrder(home: homeDirectory)let files = members- .map {- (- $0.item,- Self.keepRank(of: $0.item.url, creationTime: $0.item.identity.creationTime, downloads: downloads)- )- }+ .map { ($0.item, order.rank(of: $0.item.url, identity: $0.item.identity)) }.sorted { $0.1 < $1.1 }.map { candidate, _ inDuplicateFile(@@ -435,19 +430,35 @@ public struct DuplicateFinder: Sendable {return DuplicateGroup(id: members[0].digest.hexadecimal, files: files)}- /// Ranks a copy for keeping, lower being better: outside Downloads, without a copy suffix, created earlier,- /// less deeply nested, then with a shorter name. The path breaks any tie that is left.- static func keepRank(of url: URL, creationTime: Int, downloads: String) -> (Int, Int, Int, Int, Int, String) {- let path = path(of: url)- let name = url.deletingPathExtension().lastPathComponent- return (- path.isInside(downloads) ? 1 : 0,- hasCopySuffix(name) ? 1 : 0,- creationTime,- url.pathComponents.count,- name.count,- path- )+ /// Which copy is kept, from the home folder: its Downloads, and the disk it is on.+ struct KeepOrder: Sendable {+ private let downloads: String+ private let homeDevice: dev_t?++ init(home: URL) {+ downloads = DuplicateFinder.path(of: home.appending(path: "Downloads"))+ var info = stat()+ homeDevice = stat(home.path(percentEncoded: false), &info) == 0 ? info.st_dev : nil+ }++ /// Ranks a copy for keeping, lower being better: outside Downloads, without a copy suffix, on the disk the+ /// home folder is on, created earlier, less deeply nested, then with a shorter name. The path breaks any tie+ /// that is left.+ func rank(of url: URL, identity: FileIdentity) -> (Int, Int, Int, Int, String) {+ let path = DuplicateFinder.path(of: url)+ let name = url.deletingPathExtension().lastPathComponent+ let flags = [+ path.isInside(downloads), DuplicateFinder.hasCopySuffix(name), identity.link.device != homeDevice,+ ]+ return (+ // Each flag outweighs those after it.+ flags.reduce(0) { $0 * 2 + ($1 ? 1 : 0) },+ identity.creationTime,+ url.pathComponents.count,+ name.count,+ path+ )+ }}/// Folders never treated as a project, even with a build file in them: the chosen folders, the home folder,diff --git a/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swift b/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swiftindex 8218a594..650d415f 100644--- a/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swift+++ b/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swift@@ -61,7 +61,7 @@ struct FolderDuplicates: Sendable {func groups(in roots: [URL],minimumSize: Int64,- downloads: String,+ keepOrder: DuplicateFinder.KeepOrder,removalGuard: RemovalGuard,known: KnownDigests? = nil,progress: @escaping @Sendable (DuplicateScanProgress) -> Void@@ -101,7 +101,7 @@ struct FolderDuplicates: Sendable {var contents = samples.filter { !FileDigest.isSampled(size: $0.key.size) }contents.merge(Self.table(read)) { _, verified in verified }- return build(grouped(alike, by: contents), downloads: downloads, removalGuard: removalGuard)+ return build(grouped(alike, by: contents), keepOrder: keepOrder, removalGuard: removalGuard)}/// Whether `folder` still holds exactly what the scan compared, listed again the same way. The exclusions and@@ -360,7 +360,7 @@ struct FolderDuplicates: Sendable {/// tree make one row, not a row for every folder in it.private func build(_ groups: [(digest: SHA256.Digest, folders: [Folder])],- downloads: String,+ keepOrder: DuplicateFinder.KeepOrder,removalGuard: RemovalGuard) -> [DuplicateFolderGroup] {// The shallowest groups go first. A group whose folders all sit inside offered ones can never be a row, nor@@ -380,8 +380,7 @@ struct FolderDuplicates: Sendable {let topmost = group.folders.filter { !DuplicateFinder.isInside(offered, $0.url) }guard topmost.count > 1 else { return nil }let sorted = topmost.sorted {- DuplicateFinder.keepRank(of: $0.url, creationTime: $0.identity.creationTime, downloads: downloads)- < DuplicateFinder.keepRank(of: $1.url, creationTime: $1.identity.creationTime, downloads: downloads)+ keepOrder.rank(of: $0.url, identity: $0.identity) < keepOrder.rank(of: $1.url, identity: $1.identity)}let folders = sorted.compactMap(duplicate)guard folders.count > 1 else { return nil }diff --git a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swiftindex 874f6465..95719e1d 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift@@ -693,6 +693,40 @@ struct DuplicateFinderTests {}}+ /// The copy on the disk the home folder is on is kept before an older one on another disk, such as photos that+ /// were on a card before they were on the Mac.+ @Test func keepsTheCopyOnTheHomeDisk() async throws {+ let directory = try TemporaryDirectory()+ let home = directory.url.appending(path: "home", directoryHint: .isDirectory)+ let volume = try ScratchVolume(+ fileSystem: "APFS", mountedAt: home.appending(path: "Card", directoryHint: .isDirectory)+ )+ let contents = Data(repeating: 7, count: 200_000)+ let onTheMac = try directory.file("home/Pictures/Holiday/photo.jpg", contents: contents)+ let onTheCard = volume.url.appending(path: "Holiday/photo.jpg")+ try FileManager.default.createDirectory(+ at: onTheCard.deletingLastPathComponent(), withIntermediateDirectories: true+ )+ try contents.write(to: onTheCard)+ let earlier = Date.now.addingTimeInterval(-2 * 24 * 3_600)+ for url in [onTheCard, onTheCard.deletingLastPathComponent()] {+ try FileManager.default.setAttributes(+ [.creationDate: earlier, .modificationDate: earlier], ofItemAtPath: url.path(percentEncoded: false)+ )+ }++ let pictures = home.appending(path: "Pictures", directoryHint: .isDirectory)+ let finder = DuplicateFinder(homeDirectory: home)+ let kept = { (url: URL?) in url.map { PathPattern.comparablePath(of: $0) } }++ let folders = try await finder.scan(.everySize(in: [pictures, volume.url]))+ #expect(kept(folders.folderGroups.first?.folders.first?.url) == kept(onTheMac.deletingLastPathComponent()))++ try directory.file("home/Pictures/Holiday/notes.txt", contents: Data("only here".utf8))+ let files = try await finder.scan(.everySize(in: [pictures, volume.url]))+ #expect(kept(files.groups.first?.files.first?.url) == kept(onTheMac))+ }+@Test func stopsWhenCanceled() async throws {let directory = try TemporaryDirectory()try directory.file("home/Documents/a.txt", contents: Data("same".utf8))