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.

Build Artifacts never selects a folder holding a repository or a wallet

Țugui Dragoșf5a66693c30b
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index fcf526c4..c03eb9f2 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -126,7 +126,9 @@ Tests enforce these. If your change makes one of them fail, the rule is right an
Anything shared is shown and left unselected, and nothing is selected for an app that would stay.
- **What may exist nowhere else is never selected for the user.** A folder with a wallet, a signing key, or a
repository inside, or one Peel could not finish reading, is shown with its reason and moves only when the user
- selects it: Select All, `peel uninstall`, and `peel orphans --remove` pass it by.
+ selects it: Select All, `peel uninstall`, `peel orphans --remove`, and `peel projects --remove` pass it by. The
+ one exception is a repository inside a folder its tool tags as a cache (`CACHEDIR.TAG`), such as Swift Package
+ Manager's `.build`, whose clones the tool makes again.
- **The user's exclusions reach every scanner**, so an excluded item never appears in the first place.
- **Resetting an app clears its settings, never the app.** It takes the app's own data only when the user
selects it, needs the app to be quit, and inside a sandboxed app's container never touches `Documents`,
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
index f9b1c799..16e243f7 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/FileCommands.swift
@@ -297,6 +297,30 @@ struct ProjectsCommand: AsyncParsableCommand {
let size: MeasuredSize
let suggested: Bool
let recentlyActive: Bool
+ /// Why measuring the folder left it to be chosen by hand, or `null` when nothing did.
+ let heldBack: String?
+
+ private enum CodingKeys: String, CodingKey {
+ case path
+ case project
+ case tool
+ case size
+ case suggested
+ case recentlyActive
+ case heldBack
+ }
+
+ /// Encodes a missing `heldBack` as `null`, for the reason `AppRecord` gives.
+ func encode(to encoder: any Encoder) throws {
+ var container = encoder.container(keyedBy: CodingKeys.self)
+ try container.encode(path, forKey: .path)
+ try container.encode(project, forKey: .project)
+ try container.encode(tool, forKey: .tool)
+ try container.encode(size, forKey: .size)
+ try container.encode(suggested, forKey: .suggested)
+ try container.encode(recentlyActive, forKey: .recentlyActive)
+ try container.encode(heldBack, forKey: .heldBack)
+ }
}
/// Fails when none of the folders can be searched, giving the reason for each, rather than report that
@@ -364,7 +388,8 @@ struct ProjectsCommand: AsyncParsableCommand {
tool: $0.tool,
size: MeasuredSize($0.size),
suggested: $0.isRecommended,
- recentlyActive: $0.isRecentlyActive
+ recentlyActive: $0.isRecentlyActive,
+ heldBack: $0.heldBack?.rawValue
)
},
unreadableLocations: scan.unreadableLocations.map(Output.path)
@@ -407,11 +432,11 @@ struct ProjectsCommand: AsyncParsableCommand {
/// Returns why `artifact` is kept, or "suggested". It checks the same conditions as `isRecommended`, and
/// the two must never disagree.
static func note(for artifact: ProjectArtifact) -> String {
+ if let heldBack = artifact.heldBack { return "\(heldBack.summary), kept" }
if artifact.isRecentlyActive { return "changed in the last 7 days, kept" }
if !artifact.lastActivityIsCertain { return "too large to tell when it last changed, kept" }
if artifact.isEnvironment { return "installed packages, kept" }
if artifact.hasGenericName { return "name could mean anything, kept" }
- if artifact.size == nil { return "not measured in time, kept" }
return "suggested"
}
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift b/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift
index 4b1dcc55..40ed404e 100644
--- a/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift
@@ -21,6 +21,9 @@ public struct ProjectArtifact: Sendable, Hashable, Identifiable {
public let hasGenericName: Bool
/// True for an installed set of packages, which a build doesn't make again on its own.
public let isEnvironment: Bool
+ /// Why what measuring the folder saw leaves it to be chosen by hand: it was not measured or not read, or a
+ /// wallet or a repository is inside. Nil when nothing did.
+ public var heldBack: HoldBack?
public var id: URL { url }
@@ -30,9 +33,10 @@ public struct ProjectArtifact: Sendable, Hashable, Identifiable {
}
/// Whether Peel selects this artifact for the user: build output with a name that can't mean anything
- /// else, in a project known not to have changed lately, and only once measured.
+ /// else, in a project known not to have changed lately, measured, and holding nothing that may exist nowhere
+ /// else.
public var isRecommended: Bool {
- !hasGenericName && !isEnvironment && !isRecentlyActive && lastActivityIsCertain && size != nil
+ !hasGenericName && !isEnvironment && !isRecentlyActive && lastActivityIsCertain && heldBack == nil
}
}
@@ -109,11 +113,11 @@ public enum ProjectArtifacts {
@concurrent
public static func scan(roots: [URL], exclusions: Exclusions = .none) async -> Scan {
- await scan(roots: roots, exclusions: exclusions, measure: FileSize.measure)
+ await scan(roots: roots, exclusions: exclusions, measure: LeftoverScanner.walk)
}
@concurrent
- static func scan(roots: [URL], exclusions: Exclusions, measure: @escaping FileSize.Measure) async -> Scan {
+ static func scan(roots: [URL], exclusions: Exclusions, measure: @escaping LeftoverScanner.Measure) async -> Scan {
await withTaskGroup(of: (artifacts: [ProjectArtifact], wasCutShort: Bool).self) { group in
for root in roots where isSearchable(root) {
group.addTask { await artifacts(in: root, exclusions: exclusions, measure: measure) }
@@ -178,7 +182,7 @@ public enum ProjectArtifacts {
refusal(for: root, home: home) == nil
}
- static func artifacts(in root: URL, exclusions: Exclusions, measure: FileSize.Measure) async -> (artifacts: [ProjectArtifact], wasCutShort: Bool) {
+ static func artifacts(in root: URL, exclusions: Exclusions, measure: LeftoverScanner.Measure) async -> (artifacts: [ProjectArtifact], wasCutShort: Bool) {
var found: [ProjectArtifact] = []
var queue: [(url: URL, depth: Int)] = [(root, 0)]
// An index rather than `removeFirst`, which shifts every waiting folder on each call and makes the
@@ -205,16 +209,21 @@ public enum ProjectArtifacts {
// Read once per project, after all of its artifacts are found, so the walk skips every one of them.
let activity = matching.isEmpty ? (date: nil, isCertain: true) : lastActivity(in: folder, ignoring: artifactNames)
for definition in matching {
+ let url = folder.appending(path: definition.name)
+ let contents = await measure(url)
+ let heldBack = HoldBack.seen(in: contents)
found.append(ProjectArtifact(
- url: folder.appending(path: definition.name),
+ url: url,
project: folder,
name: definition.name,
tool: definition.tool,
- size: await measure(folder.appending(path: definition.name)),
+ size: contents.flatMap { $0.couldNotBeRead ? nil : $0.size },
lastActivity: activity.date,
lastActivityIsCertain: activity.isCertain,
hasGenericName: definition.isGeneric,
- isEnvironment: definition.isEnvironment
+ isEnvironment: definition.isEnvironment,
+ // A tool that tags its folder as a cache makes everything in it again, its own clones included.
+ heldBack: heldBack == .holdsRepository && isTaggedAsACache(url) ? nil : heldBack
))
}
@@ -287,6 +296,15 @@ public enum ProjectArtifacts {
return (newest, newest != nil)
}
+ /// Whether `folder` carries a cache directory tag, with which its tool says that everything inside can be made
+ /// again. The Cache Directory Tagging Specification has the file begin with this signature. Swift Package
+ /// Manager tags `.build`, which `swift package reset` deletes whole and `swift package resolve` clones again.
+ static func isTaggedAsACache(_ folder: URL) -> Bool {
+ let signature = Data("Signature: 8a477f597d28d172789f06886806bc55".utf8)
+ guard let tag = BoundedRead.data(at: folder.appending(path: "CACHEDIR.TAG"), maximum: 4_096) else { return false }
+ return tag.starts(with: signature)
+ }
+
/// Returns whether `url` is a plain folder: not a link, a package, or a cloud item. A cloud placeholder
/// looks like a folder but holds nothing until it is downloaded. A package can be an app: an Electron app
/// ships `package.json` beside `node_modules` inside its bundle, and nothing may be removed from there.
diff --git a/Packages/PeelCore/Sources/PeelCore/Leftovers/Leftover.swift b/Packages/PeelCore/Sources/PeelCore/Leftovers/Leftover.swift
index 230fcf3b..db7f4409 100644
--- a/Packages/PeelCore/Sources/PeelCore/Leftovers/Leftover.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Leftovers/Leftover.swift
@@ -65,6 +65,14 @@ public enum HoldBack: String, Sendable, Hashable {
self == .holdsDocuments || self == .holdsALibrary || self == .holdsKeys || self == .beyondTheHelper
}
+ /// Why what a walk saw leaves a folder to be chosen by hand: a wallet, a signing key, or a repository inside may
+ /// exist nowhere else, and a folder the walk could not see into is not known to be empty.
+ static func seen(in contents: FolderContents?) -> HoldBack? {
+ guard let contents else { return .notMeasured }
+ if contents.couldNotBeRead { return .couldNotBeRead }
+ if contents.holdsWallet { return .holdsAWallet }
+ return contents.holdsRepository ? .holdsRepository : nil
+ }
}
public struct LeftoverMatch: Sendable, Hashable {
diff --git a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift
index dff3523c..56ad644f 100644
--- a/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Orphans/OrphanScanner.swift
@@ -252,15 +252,6 @@ public struct OrphanScanner: Sendable {
}
}
- /// Why what the walk saw leaves an item to be chosen by hand: a wallet, a signing key, or a repository inside
- /// may exist nowhere else, and a folder the walk could not see into is not known to be empty.
- private static func heldBack(by contents: FolderContents?) -> HoldBack? {
- guard let contents else { return .notMeasured }
- if contents.couldNotBeRead { return .couldNotBeRead }
- if contents.holdsWallet { return .holdsAWallet }
- return contents.holdsRepository ? .holdsRepository : nil
- }
-
private enum LocationResult: Sendable {
case found([(identifier: String, item: OrphanItem)])
case unreadable(SearchLocation)
@@ -309,7 +300,7 @@ public struct OrphanScanner: Sendable {
.holdsALibrary
} else {
// `/Users/Shared` belongs to every account on the Mac, and the other accounts' apps are not known here.
- Self.heldBack(by: contents) ?? (location.kind == .sharedFolder ? .sharedWithEveryone : nil)
+ HoldBack.seen(in: contents) ?? (location.kind == .sharedFolder ? .sharedWithEveryone : nil)
}
let item = OrphanItem(
url: url,
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift
index 6bba6034..a688a80e 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/FileCommandTests.swift
@@ -285,7 +285,8 @@ struct FileCommandTests {
lastActivity: Date? = .now.addingTimeInterval(-ProjectArtifacts.recentlyActive * 2),
certain: Bool = true,
generic: Bool = false,
- environment: Bool = false
+ environment: Bool = false,
+ heldBack: HoldBack? = nil
) -> ProjectArtifact {
var artifact = ProjectArtifact(
url: project.appending(path: name),
@@ -295,7 +296,8 @@ struct FileCommandTests {
size: size,
lastActivity: lastActivity,
hasGenericName: generic,
- isEnvironment: environment
+ isEnvironment: environment,
+ heldBack: heldBack
)
artifact.lastActivityIsCertain = certain
return artifact
@@ -306,7 +308,8 @@ struct FileCommandTests {
(artifact("huge", certain: false), "too large to tell when it last changed, kept"),
(artifact("venv", environment: true), "installed packages, kept"),
(artifact("build", generic: true), "name could mean anything, kept"),
- (artifact("slow", size: nil), "not measured in time, kept"),
+ (artifact("slow", size: nil, heldBack: .notMeasured), "not measured in time, kept"),
+ (artifact("Carthage", heldBack: .holdsRepository), "holds a repository, kept"),
]
for (artifact, note) in cases {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ProjectArtifactsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ProjectArtifactsTests.swift
index 7a6bd283..cd0d8e39 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ProjectArtifactsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ProjectArtifactsTests.swift
@@ -12,7 +12,7 @@ struct ProjectArtifactsTests {
let unanswered = Unanswered()
let stop = try await unanswered.stop {
- _ = await ProjectArtifacts.scan(roots: [directory.url.appending(path: "Code", directoryHint: .isDirectory)], exclusions: .none, measure: unanswered.measure)
+ _ = await ProjectArtifacts.scan(roots: [directory.url.appending(path: "Code", directoryHint: .isDirectory)], exclusions: .none, measure: unanswered.walk)
}
#expect(stop.took < .seconds(1))
@@ -77,6 +77,31 @@ struct ProjectArtifactsTests {
#expect(found.first?.isRecommended == true)
}
+ /// What may exist nowhere else is never selected: a clone someone commits in, as Carthage's checkouts are with
+ /// `--use-submodules`, or a wallet. A repository inside a folder its tool tags as a cache is the tool's own
+ /// clone, which it makes again, as Swift Package Manager does for `.build`.
+ @Test func anArtifactHoldingARepositoryOrAWalletIsNotSelected() async throws {
+ let directory = try TemporaryDirectory()
+ let tag = Data("Signature: 8a477f597d28d172789f06886806bc55\n# a cache directory tag\n".utf8)
+ try directory.file("Projects/app/Cartfile", bytes: 16)
+ try directory.file("Projects/app/Carthage/Checkouts/Dep/.git/HEAD", bytes: 16)
+ try directory.file("Projects/site/package.json", bytes: 16)
+ try directory.file("Projects/site/node_modules/coin/wallet.dat", bytes: 16)
+ try directory.file("Projects/tool/Package.swift", bytes: 16)
+ try directory.file("Projects/tool/.build/CACHEDIR.TAG", contents: tag)
+ try directory.file("Projects/tool/.build/checkouts/dep/.git/HEAD", bytes: 16)
+ try directory.file("Projects/old/Package.swift", bytes: 16)
+ try directory.file("Projects/old/.build/CACHEDIR.TAG", contents: Data("not a tag".utf8))
+ try directory.file("Projects/old/.build/checkouts/dep/.git/HEAD", bytes: 16)
+ try age(directory.url, days: 60)
+
+ let found = await ProjectArtifacts.scan(roots: [directory.url.appending(path: "Projects")]).artifacts
+
+ let reasons = found.map { "\($0.project.lastPathComponent): \($0.heldBack.map(\.rawValue) ?? "none")" }.sorted()
+ #expect(reasons == ["app: holdsRepository", "old: holdsRepository", "site: holdsAWallet", "tool: none"])
+ #expect(found.filter(\.isRecommended).map(\.project.lastPathComponent) == ["tool"])
+ }
+
/// A large `node_modules` is the folder most likely to run out of time. Its size is then unknown, not zero,
/// so it is listed first and never selected for the user.
@Test func anArtifactThatDidNotAnswerInTimeIsNotReadAsEmpty() async throws {
@@ -88,7 +113,7 @@ struct ProjectArtifactsTests {
try age(directory.url, days: 60)
let found = await ProjectArtifacts.scan(roots: [directory.url.appending(path: "Projects")], exclusions: .none) { url in
- url.lastPathComponent == "node_modules" ? nil : await FileSize.allocatedSize(of: url, within: FileSize.budget)
+ url.lastPathComponent == "node_modules" ? nil : await FileSize.contents(of: url)
}.artifacts
#expect(found.map(\.name) == ["node_modules", ".build"])
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ScanCountTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ScanCountTests.swift
index 6a7f8b95..55bcca6c 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ScanCountTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ScanCountTests.swift
@@ -48,7 +48,7 @@ struct ScanCountTests {
let count = ScanCount()
_ = await ScanCount.$current.withValue(count) {
- await ProjectArtifacts.scan(roots: [directory.url.appending(path: "Code", directoryHint: .isDirectory)], exclusions: .none) { _ in 0 }
+ await ProjectArtifacts.scan(roots: [directory.url.appending(path: "Code", directoryHint: .isDirectory)], exclusions: .none) { _ in FolderContents(size: 0, holdsRepository: false) }
}
#expect(count.value >= 3, "the project, its build file and its node_modules at least")
diff --git a/Peel/Views/Projects/ProjectDetailView.swift b/Peel/Views/Projects/ProjectDetailView.swift
index 01dd02c7..6bd58327 100644
--- a/Peel/Views/Projects/ProjectDetailView.swift
+++ b/Peel/Views/Projects/ProjectDetailView.swift
@@ -107,6 +107,9 @@ struct ProjectDetailView: View {
}
private func warning(for artifact: ProjectArtifact) -> String? {
+ if let heldBack = artifact.heldBack {
+ return String(localized: heldBack.explanation)
+ }
if artifact.isRecentlyActive {
return String(localized: "Not selected: something in this project changed in the last 7 days.")
}
@@ -118,9 +121,6 @@ struct ProjectDetailView: View {
if artifact.hasGenericName {
return String(localized: "Not selected: a folder called \(artifact.name) could be anyone’s.")
}
- if artifact.size == nil {
- return String(localized: "Not selected: this folder took too long to measure, so Peel doesn’t know how big it is or what is inside.")
- }
if !artifact.lastActivityIsCertain {
return String(localized: "Not selected: this project is too large for Peel to tell when it last changed.")
}
diff --git a/SAFETY.md b/SAFETY.md
index 50a6632c..78751241 100644
--- a/SAFETY.md
+++ b/SAFETY.md
@@ -153,6 +153,11 @@ not let Peel read, and a folder Peel could not measure in time. Those last two a
exactly the one with work inside, and nothing is selected for you without saying how much it is. A total that leaves
such a folder out reads "Over" what is known.
+A repository inside a folder its tool tags as a cache (`CACHEDIR.TAG`) is the one exception: the tool says it makes
+everything in there again. Swift Package Manager tags `.build`, where it clones a package's dependencies, and
+`swift package reset` deletes that folder whole, so Build Artifacts can still select it. Carthage's checkouts, which
+people commit in, carry no such tag and are never selected.
+
Orphaned Files selects nothing for you, and its Select All and `peel orphans --remove` leave these folders out as
well: one in `/Users/Shared`, one with a repository, a wallet, or a signing key inside, or one Peel could not read
or measure in time, moves only when you select it yourself.