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.

Offer the Git clone of CocoaPods' public specs

Țugui Dragoș2c113ee92809
diff --git a/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift b/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift
index 36b6bd24..3e27584c 100644
--- a/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Developer/DeveloperCaches.swift
@@ -158,12 +158,15 @@ public enum DeveloperCaches {
/// The links, relative to the home folder, a tool's installer puts on the path to the version it runs. When
/// set, the folder's rows are the versions it keeps beside that one (`olderVersions(in:home:)`).
let launchers: [String]
+ /// Where a Git clone here comes from when it is the tool's own, as `ToolSettings.gitAddress` writes it. A clone
+ /// of anything else, which the person may have added under the same name, is listed and never selected.
+ let clonedFrom: String?
init(
_ path: String, _ kind: DeveloperEnvironment.ContentKind, source: String, storeInside: String? = nil,
rowsDepth: Int = 0, rowEnding: String? = nil, rowsAreDerivedData: Bool = false,
rowsAreProjectState: Bool = false, movedBy: Relocation? = nil, base: Base = .home,
- launchers: [String] = []
+ launchers: [String] = [], clonedFrom: String? = nil
) {
self.path = path
self.kind = kind
@@ -176,6 +179,12 @@ public enum DeveloperCaches {
self.movedBy = movedBy
self.base = base
self.launchers = launchers
+ self.clonedFrom = clonedFrom
+ }
+
+ /// Whether what is at `url` is the tool's own, as far as where it was cloned from says.
+ func isTheTools(at url: URL) -> Bool {
+ clonedFrom.map { ToolSettings.cloneAddress(of: url) == $0 } ?? true
}
/// The versions in `folder` beside the ones its launchers run, or none when a launcher is not the installer's
@@ -383,6 +392,11 @@ public enum DeveloperCaches {
Folder("Library/Caches/CocoaPods", .downloads, source: "https://github.com/CocoaPods/CocoaPods/blob/b80e113e28a7e23cd6dbc169f0c923f6b48bf7a2/lib/cocoapods/config.rb#L23"),
// The CDN copy of the public spec index. The spec repositories a person added sit beside it and stay.
Folder(".cocoapods/repos/trunk", .cache, source: "https://github.com/CocoaPods/Core/blob/4c1beb2e068e1055d8ed9fa079f94e7817157ba3/lib/cocoapods-core/trunk_source.rb#L2-L7"),
+ // The Git clone of the same index, which CocoaPods made as `master` before it read the CDN, and which its
+ // 1.8 notes say may go once projects read the CDN; added now, it is named `cocoapods`
+ // (`Source::Manager#name_for_url`).
+ Folder(".cocoapods/repos/master", .downloads, source: "https://blog.cocoapods.org/CocoaPods-1.8.0-beta/", clonedFrom: "github.com/cocoapods/specs"),
+ Folder(".cocoapods/repos/cocoapods", .downloads, source: "https://github.com/CocoaPods/Core/blob/4c1beb2e068e1055d8ed9fa079f94e7817157ba3/lib/cocoapods-core/source/manager.rb#L426-L478", clonedFrom: "github.com/cocoapods/specs"),
]),
Definition(id: "carthage", name: "Carthage", systemImage: "shippingbox", appBundleIdentifiers: [], folders: [
Folder("Library/Caches/org.carthage.CarthageKit", .downloads, source: "https://github.com/Carthage/Carthage/blob/e33e133a5427129b38bfb1ae18d8f56b29a93204/Source/CarthageKit/Constants.swift#L31"),
@@ -1256,7 +1270,7 @@ public enum DeveloperCaches {
archive: folder.kind == .archives ? Self.archive(at: url) : nil,
workspace: derived?.workspace,
project: project,
- isTheTools: derived?.isXcodes ?? true,
+ isTheTools: derived?.isXcodes ?? folder.isTheTools(at: url),
lastWritten: contents.flatMap { $0.couldNotBeRead ? nil : $0.newestChange },
heldBack: contents.flatMap(HoldBack.secret(in:))
))
diff --git a/Packages/PeelCore/Sources/PeelCore/Developer/ToolSettings.swift b/Packages/PeelCore/Sources/PeelCore/Developer/ToolSettings.swift
index 6de92cd9..416ad9b3 100644
--- a/Packages/PeelCore/Sources/PeelCore/Developer/ToolSettings.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Developer/ToolSettings.swift
@@ -1,8 +1,9 @@
import Foundation
-/// Where the tools' own configuration files say a cache Developer lists has moved: the files each tool reads, which
-/// its own `config set` writes, read here and never by running a tool. A value counts only when it names a place
-/// without the environment the tool runs in, such as an absolute path or one from the home folder.
+/// Where the tools' own configuration files say a cache Developer lists has moved, and where a Git clone it lists
+/// comes from: the files each tool reads, which its own `config set` writes, read here and never by running a tool. A
+/// value counts only when it names a place without the environment the tool runs in, such as an absolute path or one
+/// from the home folder.
struct ToolSettings: Sendable {
/// npm's `cache` in `~/.npmrc`.
let npmCache: URL?
@@ -108,8 +109,76 @@ struct ToolSettings: Sendable {
return result + rest
}
+ /// Where the Git clone in `folder` comes from, as `gitAddress` writes it: the `url` of its `origin` remote.
+ static func cloneAddress(of folder: URL) -> String? {
+ text(at: folder.appending(path: ".git/config")).flatMap { gitValue(of: "url", inRemote: "origin", in: $0) }
+ .map(gitAddress)
+ }
+
// MARK: - The formats
+ /// The value of `key` in a remote's section of a Git configuration file, as `git-config(1)` describes it: the
+ /// section's and the key's names without case, the remote's name with it, a value quoted in parts or ending at a
+ /// comment. The last one counts.
+ static func gitValue(of key: String, inRemote remote: String, in text: String) -> String? {
+ var isInTheRemote = false
+ var found: String?
+ for line in text.split(whereSeparator: { $0 == "\n" || $0 == "\r" }) {
+ let content = line.trimmingCharacters(in: .whitespaces)
+ if content.isEmpty || content.hasPrefix("#") || content.hasPrefix(";") { continue }
+ if content.hasPrefix("["), content.hasSuffix("]") {
+ let header = content.dropFirst().dropLast().split(separator: " ", maxSplits: 1)
+ isInTheRemote = header.count == 2 && header[0].lowercased() == "remote"
+ && header[1].trimmingCharacters(in: .whitespaces) == "\"\(remote)\""
+ continue
+ }
+ guard isInTheRemote, let equals = content.firstIndex(of: "="),
+ content[..<equals].trimmingCharacters(in: .whitespaces).lowercased() == key
+ else { continue }
+ found = gitUnquoted(content[content.index(after: equals)...])
+ }
+ return found
+ }
+
+ private static func gitUnquoted(_ raw: Substring) -> String {
+ var result = ""
+ var isQuoted = false
+ var isEscaped = false
+ for character in raw.trimmingCharacters(in: .whitespaces) {
+ if isEscaped {
+ result.append(character == "n" ? "\n" : character == "t" ? "\t" : character)
+ isEscaped = false
+ } else if character == "\\" {
+ isEscaped = true
+ } else if character == "\"" {
+ isQuoted.toggle()
+ } else if !isQuoted, character == "#" || character == ";" {
+ break
+ } else {
+ result.append(character)
+ }
+ }
+ return result.trimmingCharacters(in: .whitespaces)
+ }
+
+ /// A Git remote's address as host and path, so `https://github.com/CocoaPods/Specs.git` and
+ /// `[email protected]:CocoaPods/Specs` read alike: without case, scheme, user, a trailing slash or `.git`.
+ static func gitAddress(_ url: String) -> String {
+ var address = Substring(url.lowercased())
+ let hasScheme = address.range(of: "://").map { address = address[$0.upperBound...] } != nil
+ let firstSlash = address.firstIndex(of: "/") ?? address.endIndex
+ if let at = address[..<firstSlash].lastIndex(of: "@") { address = address[address.index(after: at)...] }
+ // The short form `host:path` names its path after a colon.
+ if !hasScheme, let colon = address.firstIndex(of: ":"),
+ colon < (address.firstIndex(of: "/") ?? address.endIndex) {
+ address = Substring(address[..<colon] + "/" + address[address.index(after: colon)...])
+ }
+ while address.hasSuffix("/") { address = address.dropLast() }
+ if address.hasSuffix(".git") { address = address.dropLast(4) }
+ while address.hasSuffix("/") { address = address.dropLast() }
+ return String(address)
+ }
+
/// The value of a top-level `key` in an INI file as npm reads `.npmrc` (`ini` 7, `decode`): the last one before
/// any `[section]`. A quoted value is decoded, and an unquoted one ends at the first `;` or `#` not escaped.
static func iniValue(of key: String, in text: String) -> String? {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift
index f2ac9366..6a684df0 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/DeveloperCachesTests.swift
@@ -358,12 +358,14 @@ struct DeveloperCachesTests {
// Claude Code and Codex keep every conversation and prompt beside their caches and logs.
".claude", ".codex",
]
- // CocoaPods' spec repositories hold the ones a person added, which can carry unpushed work: only the CDN copy
- // of the public index, `trunk`, is a cache. nvm's, pyenv's, rbenv's and rustup's folders hold every version
- // they installed; only their download caches are caches.
- let onlyThisPart = [
- ".cocoapods/repos": ".cocoapods/repos/trunk", ".nvm": ".nvm/.cache", ".pyenv": ".pyenv/cache",
- ".rbenv": ".rbenv/cache", ".rustup": ".rustup/downloads",
+ // CocoaPods' spec repositories hold the ones a person added, which can carry unpushed work: only the copies of
+ // the public index are listed, the CDN's `trunk` and the Git clone CocoaPods names `master` or `cocoapods`,
+ // which is the tool's only when its origin is that index. nvm's, pyenv's, rbenv's and rustup's folders hold
+ // every version they installed; only their download caches are caches.
+ let onlyTheseParts: [String: Set<String>] = [
+ ".cocoapods/repos": [".cocoapods/repos/trunk", ".cocoapods/repos/master", ".cocoapods/repos/cocoapods"],
+ ".nvm": [".nvm/.cache"], ".pyenv": [".pyenv/cache"], ".rbenv": [".rbenv/cache"],
+ ".rustup": [".rustup/downloads"],
]
for definition in DeveloperCaches.definitions {
for path in definition.folders.map(\.path) {
@@ -373,8 +375,8 @@ struct DeveloperCachesTests {
for folder in onlyInParts {
#expect(!PathComponents.isPath(folder, atOrInside: path), "\(definition.id) lists the whole of \(folder)")
}
- for (folder, part) in onlyThisPart where PathComponents.isPath(path, atOrInside: folder) {
- #expect(path == part, "\(definition.id) lists \(path)")
+ for (folder, parts) in onlyTheseParts where PathComponents.isPath(path, atOrInside: folder) {
+ #expect(parts.contains(path), "\(definition.id) lists \(path)")
}
}
}
@@ -1039,6 +1041,30 @@ struct DeveloperCachesTests {
#expect(googleUpdater.first?.appBundleIdentifiers == ["com.google.GoogleUpdater"])
}
+ /// A spec repository is named by whoever adds it (`pod repo add <name> <url>`), so only the clone of CocoaPods'
+ /// own public specs, as its Git configuration says, is offered under the names CocoaPods gives it.
+ @Test func offersTheCloneOfCocoaPodsPublicSpecsAndNeverARepositoryThePersonAdded() async throws {
+ let directory = try TemporaryDirectory()
+ func clone(_ name: String, from origin: String) throws {
+ try directory.file(".cocoapods/repos/\(name)/Specs/0/0/0/Example/1.0/Example.podspec.json", bytes: 400_000)
+ try directory.file(
+ ".cocoapods/repos/\(name)/.git/config",
+ contents: Data("[core]\n\tbare = false\n[remote \"origin\"]\n\turl = \(origin)\n".utf8)
+ )
+ }
+ try clone("master", from: "https://github.com/CocoaPods/Specs.git")
+ try clone("cocoapods", from: "https://github.com/example/private-specs.git")
+ try clone("example", from: "https://github.com/example/specs.git")
+ let cocoaPods = DeveloperCaches.definitions.filter { $0.id == "cocoapods" }
+
+ let locations = await DeveloperCaches.scan(cocoaPods, homeDirectory: directory.url).flatMap(\.locations)
+
+ let byName = Dictionary(uniqueKeysWithValues: locations.map { ($0.url.lastPathComponent, $0) })
+ #expect(Set(byName.keys) == ["master", "cocoapods"])
+ #expect(byName["master"]?.isRecommended == true)
+ #expect(byName["cocoapods"]?.isRecommended == false)
+ }
+
@Test func offersTheImagesTartKeepsAndNeverItsMachines() async throws {
let directory = try TemporaryDirectory()
try directory.file(".tart/cache/OCIs/ghcr.io/cirruslabs/macos/disk.img", bytes: 400_000)
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ToolSettingsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ToolSettingsTests.swift
index 3ba5d1ef..cf7f106d 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ToolSettingsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ToolSettingsTests.swift
@@ -67,6 +67,28 @@ struct ToolSettingsTests {
}
/// npm and pnpm 10 read `${HOME}` and `~/`, Yarn its file's folder and a fallback, pnpm 11 `~/`, Go a full path.
+ @Test func readsARemotesAddressAsGitDoes() {
+ let config = """
+ [core]
+ \turl = https://example.org/not-a-remote.git
+ [Remote "origin"]
+ \tfetch = +refs/heads/*:refs/remotes/origin/*
+ \tURL = "https://github.com/CocoaPods/Specs.git" ; the public index
+ [remote "Origin"]
+ \turl = https://example.org/another.git
+ """
+
+ #expect(ToolSettings.gitValue(of: "url", inRemote: "origin", in: config) == "https://github.com/CocoaPods/Specs.git")
+ #expect(ToolSettings.gitValue(of: "url", inRemote: "upstream", in: config) == nil)
+ for address in [
+ "https://github.com/CocoaPods/Specs.git", "https://github.com/cocoapods/specs/", "[email protected]:CocoaPods/Specs.git",
+ "ssh://[email protected]/CocoaPods/Specs",
+ ] {
+ #expect(ToolSettings.gitAddress(address) == "github.com/cocoapods/specs", "\(address)")
+ }
+ #expect(ToolSettings.gitAddress("https://github.com/CocoaPods/Specs-mirror.git") != "github.com/cocoapods/specs")
+ }
+
@Test func takesEachToolsPathAsTheToolDoes() throws {
let directory = try TemporaryDirectory()
try directory.file("home/.npmrc", contents: Data("cache=${HOME}/npm-cache\nstore-dir=~/pnpm-ten\n".utf8))