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.

Date Homebrew's last check by its definitions file too

Țugui Dragoșb2d24792ecd4
diff --git a/AGENTS.md b/AGENTS.md
index cf55bf34..aa919371 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -297,8 +297,9 @@ If you are unsure whether something belongs in the helper, it doesn't.
- **Never run `brew` by hand with `HOMEBREW_NO_INSTALL_FROM_API=1`**: Homebrew then clones its whole core tap, over
a gigabyte. Peel sets it for one question only, whether Homebrew's copy of its definitions is on the Mac.
- **What Homebrew says of versions is as new as its last check for them.** `brew update` touches `.git/FETCH_HEAD` in
- Homebrew's repository, the mark Homebrew compares with its own day, so what it says counts for a day after that
- check (`Homebrew.lastUpdateCheck()`, `Homebrew.autoUpdateInterval`), while an update it knows of always shows.
+ Homebrew's repository, the mark Homebrew compares with its own day, and while it reads its definitions from its API
+ it dates their file at every download or confirmation (`api.rb`), so what it says counts for a day after the later
+ of the two (`Homebrew.lastUpdateCheck()`, `Homebrew.autoUpdateInterval`), while an update it knows of always shows.
- **Apple's lookup is limited and lags a release.** Apple limits `itunes.apple.com/lookup` to about 20 calls a
minute, so a process asks no faster (`RequestWindow`), and a copy of its catalog can name an older version for
hours after a release, so an answer older than an update already found counts as a check that failed.
diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md
index a20f37b9..74013363 100644
--- a/ARCHITECTURE.md
+++ b/ARCHITECTURE.md
@@ -348,8 +348,8 @@ macOS asks the user to approve it once. It talks to the app over XPC.
- **Updates of other apps.** `UpdateChecker` asks each app's own feed, or the App Store about an app from the App Store,
and uses what Homebrew knows for an app a cask installed: first with Automatic or Homebrew preferred, and under any
choice once the app's own source can't answer, an "up to date" only for a day after Homebrew last checked for new
- versions (`Homebrew.lastUpdateCheck`, the time of `.git/FETCH_HEAD` in its repository, which Homebrew itself compares
- before updating on its own). An answer Homebrew gives in place of the app's own source carries what that source
+ versions (`Homebrew.lastUpdateCheck`: the time of `.git/FETCH_HEAD` in its repository, which Homebrew itself compares
+ before updating on its own, or, while it reads its API, the later date of the definitions file it confirms). An answer Homebrew gives in place of the app's own source carries what that source
answered (`UpdateAnswer.standsInFor`, kept in `UpdateMemory`): the source's answer stands again once Homebrew's no
longer counts, the next check follows the source's own count, and an update the source found earlier stays. What
Homebrew's casks and the app's own files decide without a server, and what Homebrew knows where the app's own source
diff --git a/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift b/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift
index 4e43ff87..979035b1 100644
--- a/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Homebrew/Homebrew.swift
@@ -661,13 +661,37 @@ public enum Homebrew {
}
/// When Homebrew last checked for new versions: when `brew update` last touched `.git/FETCH_HEAD` in its
- /// repository, which Homebrew compares with `HOMEBREW_AUTO_UPDATE_SECS`. Nil when that file can't be found.
+ /// repository, which Homebrew compares with `HOMEBREW_AUTO_UPDATE_SECS`, or, while it reads its definitions from
+ /// its API, when it last downloaded or confirmed them, whichever is later. Nil when neither can be found.
@concurrent
public static func lastUpdateCheck() async -> Date? {
guard let answer = try? await answer(["--repository"]), let repository = folder(answering: answer) else {
return nil
}
- return lastUpdateCheck(inRepository: repository)
+ guard let executable = executableURL, let cache = (try? await self.answer(["--cache"])).flatMap(folder(answering:))
+ else { return lastUpdateCheck(inRepository: repository) }
+ let files = settingsFiles(
+ system: URL(filePath: "/etc/homebrew/brew.env"),
+ prefix: executable.deletingLastPathComponent().deletingLastPathComponent(),
+ userConfiguration: URL.homeDirectory.appending(path: ".homebrew", directoryHint: .isDirectory)
+ )
+ return lastUpdateCheck(inRepository: repository, cache: cache, readsTheTaps: readsTheTaps(settingsIn: files))
+ }
+
+ static func lastUpdateCheck(inRepository repository: URL, cache: URL, readsTheTaps: Bool) -> Date? {
+ let fetched = lastUpdateCheck(inRepository: repository)
+ guard !readsTheTaps else { return fetched }
+ return ([fetched] + definitionsChecks(inCache: cache)).compactMap(\.self).max()
+ }
+
+ /// The dates of the packages files in Homebrew's API cache, which it sets at every download or revalidation
+ /// (`api.rb`). One it pins to 1970, after a download it couldn't verify, says nothing.
+ static func definitionsChecks(inCache cache: URL) -> [Date] {
+ let folder = cache.appending(path: "api/internal", directoryHint: .isDirectory)
+ let names = (try? FileManager.default.contentsOfDirectory(atPath: folder.path(percentEncoded: false))) ?? []
+ return names.filter { $0.hasPrefix("packages.") && $0.hasSuffix(".jws.json") }
+ .compactMap { FileIdentity.of(folder.appending(path: $0))?.modificationDate }
+ .filter { $0 > Date(timeIntervalSince1970: 2 * 24 * 60 * 60) }
}
/// Homebrew reads the file only as a regular file (`find -type f` in `utils/auto-update.sh`), never through a link.
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift
index e1ceb2c3..9160692f 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/HomebrewTests.swift
@@ -175,6 +175,30 @@ struct HomebrewTests {
#expect(Homebrew.lastUpdateCheck(inRepository: empty.url) == nil)
}
+ @Test func takesTheLaterOfAnUpdateAndAConfirmationOfTheDefinitions() throws {
+ let repository = try TemporaryDirectory()
+ let fetchHead = try repository.file(".git/FETCH_HEAD", contents: Data("main\n".utf8))
+ let fetched = Date(timeIntervalSince1970: 1_792_315_250)
+ let date = { (when: Date, file: URL) in
+ let path = file.path(percentEncoded: false)
+ try FileManager.default.setAttributes([.modificationDate: when], ofItemAtPath: path)
+ }
+ try date(fetched, fetchHead)
+ let cache = try TemporaryDirectory()
+ let packages = try cache.file("api/internal/packages.arm64_tahoe.jws.json", bytes: 64)
+ let confirmed = fetched.addingTimeInterval(7 * 24 * 60 * 60)
+ try date(confirmed, packages)
+ let check = { (readsTheTaps: Bool) in
+ Homebrew.lastUpdateCheck(inRepository: repository.url, cache: cache.url, readsTheTaps: readsTheTaps)
+ }
+
+ #expect(check(false) == confirmed)
+ #expect(check(true) == fetched)
+ let pinned = Calendar(identifier: .gregorian).date(from: DateComponents(year: 1970, month: 1, day: 1))
+ try date(try #require(pinned), packages)
+ #expect(check(false) == fetched, "a download Homebrew couldn't verify")
+ }
+
@Test func readsTheLastCheckOfTheHomebrewOnThisMac() async throws {
guard Homebrew.executableURL != nil else {
#expect(await Homebrew.lastUpdateCheck() == nil)
@@ -185,12 +209,15 @@ struct HomebrewTests {
let fetchHead = repository.appending(path: ".git/FETCH_HEAD").path(percentEncoded: false)
let attributes = try? FileManager.default.attributesOfItem(atPath: fetchHead)
let isAFile = attributes?[.type] as? FileAttributeType == .typeRegular
- let expected = isAFile ? attributes?[.modificationDate] as? Date : nil
+ let fetched = isAFile ? attributes?[.modificationDate] as? Date : nil
+ let cache = try await Homebrew.execute(["--cache"], autoUpdate: false, timeout: 30).answer()
+ let confirmed = Homebrew.folder(answering: cache).map(Homebrew.definitionsChecks(inCache:)) ?? []
let read = await Homebrew.lastUpdateCheck()
- #expect((read == nil) == (expected == nil))
- if let read, let expected {
- #expect(abs(read.timeIntervalSince(expected)) < 0.001)
+ #expect((read == nil) == (fetched == nil && confirmed.isEmpty))
+ if let read {
+ #expect(([fetched] + confirmed).compactMap(\.self).contains { abs(read.timeIntervalSince($0)) < 0.001 })
+ #expect(fetched.map { read >= $0.addingTimeInterval(-0.001) } ?? true)
}
guard await Homebrew.hasLocalDefinitions() else { return }
#expect(try await Homebrew.installedPackages().allSatisfy { $0.lastUpdateCheck == read })