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.

Say which folders Build Artifacts didn't search, and search one project once

Țugui Dragoșfa494ce994bb
diff --git a/Localization/Peel/Localizable.xcstrings b/Localization/Peel/Localizable.xcstrings
index 83160eb7..045bf230 100644
--- a/Localization/Peel/Localizable.xcstrings
+++ b/Localization/Peel/Localizable.xcstrings
@@ -70935,6 +70935,112 @@
}
}
},
+ "It holds %@, which is already on the list. Remove that one first." : {
+ "localizations" : {
+ "cs" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Obsahuje složku %@, která už je na seznamu. Nejdřív ji odeberte."
+ }
+ },
+ "de" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Dieser Ordner enthält den Ordner %@, der bereits in der Liste steht. Entferne zuerst diesen."
+ }
+ },
+ "es" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Contiene %@, que ya está en la lista. Quita primero esa carpeta."
+ }
+ },
+ "fr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Il contient %@, qui figure déjà dans la liste. Retirez d’abord celui-ci."
+ }
+ },
+ "it" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Contiene %@, che è già nell’elenco. Rimuovi prima quella cartella."
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "すでにリストにある%@を含んでいます。先にそちらを削除してください。"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "이미 목록에 있는 %@을(를) 포함하고 있습니다. 먼저 그 폴더를 목록에서 제거하십시오."
+ }
+ },
+ "nl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Het bevat %@, en die map staat al in de lijst. Verwijder die map eerst."
+ }
+ },
+ "pl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Zawiera folder %@, który jest już na liście. Najpierw usuń tamten folder z listy."
+ }
+ },
+ "pt" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Contém %@, que já está na lista. Remova essa pasta primeiro."
+ }
+ },
+ "ro" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Conține %@, care este deja în listă. Eliminați mai întâi acel dosar din listă."
+ }
+ },
+ "ru" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "В этой папке находится папка %@, которая уже есть в списке. Сначала удалите ту папку из списка."
+ }
+ },
+ "sv" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Den innehåller %@, som redan finns i listan. Ta bort den mappen först."
+ }
+ },
+ "tr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Zaten listede olan %@ klasörünü içeriyor. Önce onu listeden çıkarın."
+ }
+ },
+ "uk" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Папка містить папку %@, яка вже є в списку. Спершу вилучіть ту папку зі списку."
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "它包含%@,而该文件夹已在列表中。请先将其移除。"
+ }
+ },
+ "zh-Hant" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "它包含%@,而該檔案夾已在列表中。請先移除該檔案夾。"
+ }
+ }
+ }
+ },
"It holds something nothing could bring back, so Peel never moves it." : {
"localizations" : {
"cs" : {
@@ -73179,6 +73285,112 @@
}
}
},
+ "It is inside %@, which is already on the list." : {
+ "localizations" : {
+ "cs" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Je uvnitř složky %@, která už je na seznamu."
+ }
+ },
+ "de" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Dieser Ordner liegt im Ordner %@, der bereits in der Liste steht."
+ }
+ },
+ "es" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Está dentro de %@, que ya está en la lista."
+ }
+ },
+ "fr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Il se trouve dans %@, qui figure déjà dans la liste."
+ }
+ },
+ "it" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Si trova in %@, che è già nell’elenco."
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "すでにリストにある%@の中にあります。"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "이미 목록에 있는 %@ 안에 있습니다."
+ }
+ },
+ "nl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Het staat in %@, en die map staat al in de lijst."
+ }
+ },
+ "pl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Znajduje się w folderze %@, który jest już na liście."
+ }
+ },
+ "pt" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Está dentro de %@, que já está na lista."
+ }
+ },
+ "ro" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Se află în %@, care este deja în listă."
+ }
+ },
+ "ru" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Эта папка находится в папке %@, которая уже есть в списке."
+ }
+ },
+ "sv" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Den ligger i %@, som redan finns i listan."
+ }
+ },
+ "tr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Zaten listede olan %@ klasörünün içinde."
+ }
+ },
+ "uk" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Папка лежить усередині папки %@, яка вже є в списку."
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "它位于%@内,而该文件夹已在列表中。"
+ }
+ },
+ "zh-Hant" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "它位於%@內,而該檔案夾已在列表中。"
+ }
+ }
+ }
+ },
"It is locked. In Finder, choose File > Get Info and deselect Locked, then try again." : {
"localizations" : {
"cs" : {
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
index b1b622ed..c05c94cc 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
@@ -620,7 +620,7 @@ struct ProjectsCommand: AsyncParsableCommand {
struct Report: Encodable {
let artifacts: [Record]
let unreadableLocations: [String]
- /// Chosen folders the scan didn't search.
+ /// Folders the scan didn't search: chosen ones, and ones inside them that another app keeps in step.
let skippedLocations: [String]
/// True when there were more folders than Peel looks at in one go, so the list isn't all of them.
let cutShort: Bool
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Support/Output.swift b/Packages/PeelCore/Sources/PeelCommandLine/Support/Output.swift
index 72155785..1e3a889a 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Support/Output.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Support/Output.swift
@@ -469,6 +469,8 @@ extension ProjectArtifacts.Refusal {
case .notAFolder: "it isn't a folder Peel can reach"
case .inAPackage: "it is an app or another package, or inside one, and what is inside belongs to it"
case .neverSearched: "it is a hidden folder, a Library, or a folder a tool fills, such as node_modules, or it sits inside one, where apps and tools keep what they install"
+ case .insideAnotherFolder(let folder): "it is inside \(Output.path(folder)), which is already on the list"
+ case .holdsAnotherFolder(let folder): "it holds \(Output.path(folder)), which is already on the list"
}
}
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift b/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift
index fe5033e6..02382c21 100644
--- a/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift
@@ -875,7 +875,8 @@ public enum ProjectArtifacts {
public var unreadableLocations: [URL] = []
/// True when macOS kept Peel out of one of them for want of Full Disk Access.
public var needsFullDiskAccess = false
- /// The chosen folders that were not searched at all, each with the reason, in the order they were given.
+ /// The folders not searched, each with the reason: chosen ones in the order they were given, then one gone
+ /// by the end of the scan, then one inside them that another app keeps in step.
public var refusedRoots: [(url: URL, reason: Refusal)] = []
}
@@ -886,37 +887,52 @@ public enum ProjectArtifacts {
@concurrent
static func scan(roots: [URL], exclusions: Exclusions, measure: @escaping LeftoverScanner.Measure) async -> Scan {
- await withTaskGroup(of: (artifacts: [ProjectArtifact], wasCutShort: Bool, unreadable: [URL]).self) { group in
+ await withTaskGroup(of: Walked.self) { group in
var refused: [(url: URL, reason: Refusal)] = []
+ var searched: [(root: URL, folder: URL)] = []
for root in roots {
if let reason = refusal(for: root) {
refused.append((root, reason))
continue
}
// Listing a link as a URL answers "Not a directory", so the walk starts from where it leads.
- let folder = root.isSymbolicLink ? PathPattern.canonical(root) : root
- group.addTask { await artifacts(in: folder, exclusions: exclusions, measure: measure) }
+ searched.append((root, root.isSymbolicLink ? PathPattern.canonical(root) : root))
+ }
+ let folders = searched.map(\.folder)
+ for folder in folders {
+ // The highest chosen folder holding this one, so a project is the same whichever walk finds it first.
+ let top = folders.filter { PathComponents.isPath(key(of: folder), atOrInside: key(of: $0)) }
+ .min { key(of: $0).count < key(of: $1).count }
+ group.addTask { await artifacts(in: folder, within: top, exclusions: exclusions, measure: measure) }
}
var found: [ProjectArtifact] = []
var seen: Set<URL> = []
var wasCutShort = false
var unreadable: [URL] = []
+ var keptInStep: [(url: URL, reason: Refusal)] = []
for await result in group {
wasCutShort = wasCutShort || result.wasCutShort
unreadable += result.unreadable
+ keptInStep += result.keptInStep.map { ($0.url, .inTheCloud($0.keeper)) }
for artifact in result.artifacts where seen.insert(artifact.url).inserted {
found.append(artifact)
}
}
+ // A disk taken away during the scan: what it held can't be moved, and the walk said nothing of it.
+ let gone = searched.filter { $0.root.isMissing }
+ let kept = found.filter { artifact in
+ !gone.contains { PathComponents.isPath(key(of: artifact.url), atOrInside: key(of: $0.folder)) }
+ }
return Scan(
- artifacts: holdingBackWhatIsShared(await holdingBackWhatGitTracks(found))
+ artifacts: holdingBackWhatIsShared(await holdingBackWhatGitTracks(kept))
.sorted { SizeTotal([$0.size]) > SizeTotal([$1.size]) },
wasCutShort: wasCutShort,
unreadableLocations: unreadable.sorted {
PathPattern.comparablePath(of: $0) < PathPattern.comparablePath(of: $1)
},
needsFullDiskAccess: unreadable.contains { FullDiskAccess.canList($0) == .missing },
- refusedRoots: refused
+ refusedRoots: refused + gone.map { ($0.root, .notAFolder) }
+ + keptInStep.sorted { key(of: $0.url) < key(of: $1.url) }
)
}
}
@@ -994,6 +1010,19 @@ public enum ProjectArtifacts {
case inAPackage
/// A folder the walk never enters (`isSkipped`), or one inside it: apps and tools keep what they install there.
case neverSearched
+ case insideAnotherFolder(URL)
+ case holdsAnotherFolder(URL)
+ }
+
+ /// Why `root` can't join `listed`: what a folder inside another finds would be searched twice.
+ static func overlap(of root: URL, with listed: [URL]) -> Refusal? {
+ let path = PathPattern.comparablePath(of: PathPattern.canonical(root))
+ for folder in listed {
+ let other = PathPattern.comparablePath(of: PathPattern.canonical(folder))
+ if PathComponents.isPath(path, atOrInside: other) { return .insideAnotherFolder(folder) }
+ if PathComponents.isPath(other, atOrInside: path) { return .holdsAnotherFolder(folder) }
+ }
+ return nil
}
/// The folders in the home folder where people usually keep projects, suggested until one is chosen: each is
@@ -1006,15 +1035,23 @@ public enum ProjectArtifacts {
}
}
- /// Why each of `roots` cannot be searched, in their order. Each is judged within `FileSize.budget`, since a
+ /// Why each of `roots` cannot join `listed`, in their order. Each is judged within `FileSize.budget`, since a
/// folder on a network volume that stopped answering would hold the thread, and one that doesn't answer in time
/// can't be reached.
@concurrent
- public static func refusals(of roots: [URL], home: URL = .homeDirectory) async -> [Refusal?] {
+ public static func refusals(
+ of roots: [URL], besides listed: [URL] = [], home: URL = .homeDirectory
+ ) async -> [Refusal?] {
+ var taken = listed
var reasons: [Refusal?] = []
for root in roots {
- let reason = await SlowRead.answer(within: FileSize.budget) { _ in refusal(for: root, home: home) }
- reasons.append(reason ?? .notAFolder)
+ let others = taken
+ let answer = await SlowRead.answer(within: FileSize.budget) { _ in
+ refusal(for: root, home: home) ?? overlap(of: root, with: others)
+ }
+ let reason = answer ?? .notAFolder
+ reasons.append(reason)
+ if reason == nil { taken.append(root) }
}
return reasons
}
@@ -1063,15 +1100,19 @@ public enum ProjectArtifacts {
/// Walks `root` for artifacts. Each folder is read on a thread of its own, within `FileSize`'s budget, since a
/// folder on a network volume that stopped answering would otherwise hold one of the threads every scan
- /// shares, where a Stop cannot reach it. `listing` stands in for the listing in a test.
+ /// shares, where a Stop cannot reach it. A project is looked for up to `top`, by default `root`. `listing` stands
+ /// in for the listing in a test.
static func artifacts(
in root: URL,
+ within top: URL? = nil,
exclusions: Exclusions,
measure: LeftoverScanner.Measure,
listing: @escaping @Sendable (URL) -> [URL]? = contents(of:)
- ) async -> (artifacts: [ProjectArtifact], wasCutShort: Bool, unreadable: [URL]) {
+ ) async -> Walked {
var pending: [Pending] = []
var unreadable: [URL] = []
+ var keptInStep: [(url: URL, keeper: SyncKeeper)] = []
+ let keepers = SyncedFolders()
var queue = [root]
// An index, since `removeFirst` would make the walk quadratic.
var next = 0
@@ -1097,6 +1138,9 @@ public enum ProjectArtifacts {
continue
}
ScanCount.current?.add(look.entries.count)
+ if look.isKeptInStep, let keeper = keepers.keeper(of: folder) {
+ keptInStep.append((folder, keeper))
+ }
for artifact in look.found {
reached.insert(Self.key(of: folder.appending(path: artifact.name)))
}
@@ -1105,7 +1149,7 @@ public enum ProjectArtifacts {
let url = folder.appending(path: artifact.name)
let project = artifact.isBesideItsToolsFile
? folder
- : await project(above: folder, within: root, listing: listing, known: &projectFolders)
+ : await project(above: folder, within: top ?? root, listing: listing, known: &projectFolders)
pending.append(Pending(url: url, project: project, found: artifact, contents: await measure(url)))
}
@@ -1116,7 +1160,18 @@ public enum ProjectArtifacts {
queue.append(entry)
}
}
- return (await withLastActivity(pending), next < queue.count, unreadable)
+ return Walked(
+ artifacts: await withLastActivity(pending), wasCutShort: next < queue.count, unreadable: unreadable,
+ keptInStep: keptInStep
+ )
+ }
+
+ /// What one walk found and what it couldn't look at.
+ struct Walked: Sendable {
+ let artifacts: [ProjectArtifact]
+ let wasCutShort: Bool
+ let unreadable: [URL]
+ let keptInStep: [(url: URL, keeper: SyncKeeper)]
}
private struct Pending: Sendable {
@@ -1216,6 +1271,7 @@ public enum ProjectArtifacts {
private struct Look: Sendable {
let entries: [URL]
let found: [Found]
+ var isKeptInStep = false
}
private struct Found: Sendable {
@@ -1231,7 +1287,7 @@ public enum ProjectArtifacts {
private static func look(in folder: URL, exclusions: Exclusions, listing: (URL) -> [URL]?) -> Look? {
guard let entries = listing(folder) else { return nil }
// What a folder another app keeps in step holds would go from the other devices too.
- guard !SyncedFolders.marksAFolder(entries) else { return Look(entries: [], found: []) }
+ guard !SyncedFolders.marksAFolder(entries) else { return Look(entries: [], found: [], isKeptInStep: true) }
let names = Set(entries.map(\.lastPathComponent))
var artifactNames: Set<String> = []
var found: [Found] = []
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ProjectArtifactsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ProjectArtifactsTests.swift
index 3c900f52..754ebcfb 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ProjectArtifactsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ProjectArtifactsTests.swift
@@ -1497,7 +1497,57 @@ struct ProjectArtifactsTests {
#expect(scan.artifacts.map(\.project.lastPathComponent) == ["kept"])
let mega = SyncKeeper.app(.mega, marker: directory.url.appending(path: "Shared/.megaignore"))
- #expect(scan.refusedRoots.map { $0.reason } == [.inTheCloud(mega)])
+ #expect(scan.refusedRoots.first?.reason == .inTheCloud(mega))
+ #expect(scan.refusedRoots.map(\.url.lastPathComponent) == ["app", "synced"])
+ guard case .inTheCloud(.app(let app, let marker))? = scan.refusedRoots.last?.reason else {
+ Issue.record("Syncthing's folder is named with its app and its marker")
+ return
+ }
+ #expect(app == .syncthing)
+ #expect(marker.path(percentEncoded: false).hasSuffix("Code/synced/.stfolder"))
+ }
+
+ @Test func aFolderGoneDuringTheScanIsNamedAsOneThatCouldNotBeSearched() async throws {
+ let directory = try TemporaryDirectory()
+ try directory.file("Disk/app/package.json")
+ try directory.file("Disk/app/node_modules/left/index.js")
+ try directory.file("Code/app/package.json")
+ try directory.file("Code/app/node_modules/left/index.js")
+ let disk = directory.url.appending(path: "Disk", directoryHint: .isDirectory)
+ let code = directory.url.appending(path: "Code", directoryHint: .isDirectory)
+
+ let scan = await ProjectArtifacts.scan(roots: [disk, code], exclusions: .none) { url in
+ let contents = await LeftoverScanner.walk(url)
+ if url.path(percentEncoded: false).contains("/Disk/") { try? FileManager.default.removeItem(at: disk) }
+ return contents
+ }
+
+ #expect(scan.refusedRoots.map(\.url) == [disk])
+ #expect(scan.refusedRoots.map(\.reason) == [.notAFolder])
+ }
+
+ @Test func oneChosenFolderInsideAnotherNamesTheSameProjectEveryTime() async throws {
+ let directory = try TemporaryDirectory()
+ try directory.directory("root/repo/.git")
+ try directory.file("root/repo/pkg/app/main.py")
+ try directory.file("root/repo/pkg/app/__pycache__/main.cpython-314.pyc")
+ let root = directory.url.appending(path: "root", directoryHint: .isDirectory)
+ let inner = directory.url.appending(path: "root/repo/pkg", directoryHint: .isDirectory)
+
+ for roots in [[root, inner], [inner, root]] {
+ let scan = await ProjectArtifacts.scan(roots: roots)
+ #expect(scan.artifacts.map(\.project.lastPathComponent) == ["repo"])
+ }
+ }
+
+ @Test func aChosenFolderCannotHoldOrSitInsideOneOnTheList() {
+ let code = URL(filePath: "/Users/me/Code", directoryHint: .isDirectory)
+ let inner = URL(filePath: "/Users/me/Code/app", directoryHint: .isDirectory)
+ let other = URL(filePath: "/Users/me/Codes", directoryHint: .isDirectory)
+
+ #expect(ProjectArtifacts.overlap(of: inner, with: [code]) == .insideAnotherFolder(code))
+ #expect(ProjectArtifacts.overlap(of: code, with: [inner]) == .holdsAnotherFolder(inner))
+ #expect(ProjectArtifacts.overlap(of: other, with: [code, inner]) == nil)
}
@Test func findsEveryKindItKnowsInAProjectOfItsOwn() async throws {
diff --git a/Peel/Model/ProjectLibrary.swift b/Peel/Model/ProjectLibrary.swift
index a0a2f281..1ac2338f 100644
--- a/Peel/Model/ProjectLibrary.swift
+++ b/Peel/Model/ProjectLibrary.swift
@@ -39,7 +39,7 @@ final class ProjectLibrary {
private(set) var needsFullDiskAccess = false
/// The folders the last scan could not read, so what they hold isn't known.
private(set) var unreadable: [URL] = []
- /// The chosen folders the last scan didn't search at all, each with the reason.
+ /// The folders the last scan didn't search, each with the reason.
private(set) var unsearchable: [(url: URL, reason: ProjectArtifacts.Refusal)] = []
/// Folders the last Add refused, with the reason, until the next one.
private(set) var refused: [(url: URL, reason: ProjectArtifacts.Refusal)] = []
@@ -131,7 +131,7 @@ final class ProjectLibrary {
func add(_ urls: [URL]) async {
let chosen = urls.filter { !isListed($0) }
- let reasons = await ProjectArtifacts.refusals(of: chosen)
+ let reasons = await ProjectArtifacts.refusals(of: chosen, besides: folders)
// A folder that cannot be scanned is refused here, rather than saved and then silently skipped.
refused = zip(chosen, reasons).compactMap { url, reason in reason.map { (url, $0) } }
// Another Add may have listed one of them while these were judged.
diff --git a/Peel/Views/Presentation.swift b/Peel/Views/Presentation.swift
index b6e6cc3c..153f7099 100644
--- a/Peel/Views/Presentation.swift
+++ b/Peel/Views/Presentation.swift
@@ -235,6 +235,8 @@ extension ProjectArtifacts.Refusal {
case .notAFolder: String(localized: "It isn’t a folder Peel can reach. A disk that isn’t connected looks like this too.")
case .inAPackage: String(localized: "It is an app or another package, or inside one, and what is inside belongs to it.")
case .neverSearched: String(localized: "It is a hidden folder, a Library, or a folder a tool fills, such as node_modules, or it sits inside one. Apps and tools keep what they install there, so Peel doesn’t look for projects in it.")
+ case .insideAnotherFolder(let folder): String(localized: "It is inside \(folder.abbreviatedPath), which is already on the list.")
+ case .holdsAnotherFolder(let folder): String(localized: "It holds \(folder.abbreviatedPath), which is already on the list. Remove that one first.")
}
}
}