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.

Leave a Git setting to the file the person's settings include

Țugui Dragoș7c8dc96c797f
diff --git a/Localization/Peel/Localizable.xcstrings b/Localization/Peel/Localizable.xcstrings
index cf5be839..40d572f2 100644
--- a/Localization/Peel/Localizable.xcstrings
+++ b/Localization/Peel/Localizable.xcstrings
@@ -131689,6 +131689,112 @@
}
}
},
+ "Set in a file your Git settings include" : {
+ "localizations" : {
+ "cs" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Nastaveno v souboru zahrnutém do vašeho nastavení Gitu"
+ }
+ },
+ "de" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "In einer Datei festgelegt, die deine Git-Einstellungen einbinden"
+ }
+ },
+ "es" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Definido en un archivo que incluyen tus ajustes de Git"
+ }
+ },
+ "fr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Défini dans un fichier inclus par vos réglages Git"
+ }
+ },
+ "it" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Definita in un file incluso nelle tue impostazioni di Git"
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Gitの設定が読み込むファイルで設定済み"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Git 설정에 포함된 파일에서 지정됨"
+ }
+ },
+ "nl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Ingesteld in een bestand dat in je Git-instellingen is opgenomen"
+ }
+ },
+ "pl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Ustawione w pliku dołączanym przez Twoje ustawienia Gita"
+ }
+ },
+ "pt" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Definido em um arquivo incluído nos seus ajustes do Git"
+ }
+ },
+ "ro" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Stabilită într‑un fișier inclus în configurările dvs. Git"
+ }
+ },
+ "ru" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Задано в файле, включенном в Ваши настройки Git"
+ }
+ },
+ "sv" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Anges i en fil som dina Git-inställningar inkluderar"
+ }
+ },
+ "tr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Git ayarlarınızın dahil ettiği bir dosyada ayarlandı"
+ }
+ },
+ "uk" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Задано у файлі, який підключають ваші налаштування Git"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "已在你的Git设置所包含的文件中设定"
+ }
+ },
+ "zh-Hant" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "已在你的Git設定所包含的檔案中設定"
+ }
+ }
+ }
+ },
"Set up" : {
"localizations" : {
"cs" : {
diff --git a/Packages/PeelCore/Sources/PeelCore/Terminal/GitConfig.swift b/Packages/PeelCore/Sources/PeelCore/Terminal/GitConfig.swift
index ef6e430a..4ce3d824 100644
--- a/Packages/PeelCore/Sources/PeelCore/Terminal/GitConfig.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Terminal/GitConfig.swift
@@ -43,6 +43,62 @@ public struct GitConfig: Sendable {
return settings
}
+ /// The keys, in lowercase, that the files the global settings include set, through `include.path` or
+ /// `includeIf.<condition>.path`, and the files those include. Git reads an included file where its include
+ /// stands and the last value wins (`git help config`), so a key written at the end of the global file would win
+ /// over the person's own in every folder their condition names.
+ public func keysSetByIncludedFiles() async -> Set<String> {
+ var keys: Set<String> = []
+ var read: Set<String> = []
+ var pending = Self.includes(in: await entries(["--global"]), home: home)
+ while let file = pending.popLast(), read.count < 64 {
+ let path = file.path(percentEncoded: false)
+ guard read.insert(path).inserted, !file.isMissing else { continue }
+ let entries = await entries(["--file", path])
+ keys.formUnion(entries.filter { !$0.isAnInclude }.map(\.key))
+ pending += Self.includes(in: entries, home: home)
+ }
+ return keys
+ }
+
+ private struct Entry {
+ let file: String
+ let key: String
+ let value: String?
+
+ var isAnInclude: Bool { key == "include.path" || key.hasPrefix("includeif.") && key.hasSuffix(".path") }
+ }
+
+ /// Each setting of `scope`, its key in lowercase, with the file it comes from.
+ private func entries(_ scope: [String]) async -> [Entry] {
+ guard case .success(let output) = await run(["config"] + scope + ["--list", "-z", "--show-origin"]),
+ output.status == 0
+ else { return [] }
+ let fields = output.standardOutput.split(separator: 0, omittingEmptySubsequences: false)
+ .map { String(decoding: $0, as: UTF8.self) }
+ return stride(from: 0, to: fields.count - 1, by: 2).compactMap { index in
+ guard fields[index].hasPrefix("file:") else { return nil }
+ let entry = fields[index + 1]
+ let newline = entry.firstIndex(of: "\n")
+ return Entry(
+ file: String(fields[index].dropFirst("file:".count)),
+ key: String(entry[..<(newline ?? entry.endIndex)]).lowercased(),
+ value: newline.map { String(entry[entry.index(after: $0)...]) }
+ )
+ }
+ }
+
+ /// The files `entries` include: a path beginning with `~/` is taken from the home and a relative one from the
+ /// file its include is in, as Git takes them.
+ private static func includes(in entries: [Entry], home: URL) -> [URL] {
+ entries.filter(\.isAnInclude).compactMap { entry in
+ guard let path = entry.value, !path.isEmpty else { return nil }
+ if path.hasPrefix("~/") { return home.appending(path: String(path.dropFirst(2))) }
+ if path.hasPrefix("/") { return URL(filePath: path) }
+ return URL(filePath: entry.file).deletingLastPathComponent().appending(path: path)
+ }
+ }
+
/// Every setting this Git knows, in lowercase, as `git help --config` lists them. A setting whose keys are not
/// all known is never offered, so a Git that renames or drops one never gets a key it would ignore.
public func knownKeys() async -> Set<String>? {
diff --git a/Packages/PeelCore/Sources/PeelCore/Terminal/GitLedger.swift b/Packages/PeelCore/Sources/PeelCore/Terminal/GitLedger.swift
index 4a444f22..e0498ff2 100644
--- a/Packages/PeelCore/Sources/PeelCore/Terminal/GitLedger.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Terminal/GitLedger.swift
@@ -22,6 +22,7 @@ public struct GitLedger {
public mutating func turnOn(_ setting: GitSetting, signingKey: String?, in config: GitConfig) async -> Outcome {
let values = setting.values(signingKey: signingKey)
guard !values.isEmpty, let settings = await config.settings() else { return .refused }
+ guard !setting.isSetInIncludedFiles(await config.keysSetByIncludedFiles()) else { return .unchanged }
var outcome = Outcome.unchanged
for wanted in values {
let name = wanted.key.lowercased()
diff --git a/Packages/PeelCore/Sources/PeelCore/Terminal/GitSetting.swift b/Packages/PeelCore/Sources/PeelCore/Terminal/GitSetting.swift
index 36827f21..b2d0858c 100644
--- a/Packages/PeelCore/Sources/PeelCore/Terminal/GitSetting.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Terminal/GitSetting.swift
@@ -80,6 +80,10 @@ public enum GitSetting: CaseIterable, Sendable {
}
}
+ public func isSetInIncludedFiles(_ includedKeys: Set<String>) -> Bool {
+ keys.contains { includedKeys.contains($0.lowercased()) }
+ }
+
public func isKnown(by keys: Set<String>) -> Bool {
self.keys.allSatisfy { keys.contains($0.lowercased()) }
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/GitSettingsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/GitSettingsTests.swift
index 98e2bb3c..a5790e79 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/GitSettingsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/GitSettingsTests.swift
@@ -59,6 +59,38 @@ struct GitSettingsTests {
#expect(settings["merge.conflictstyle"] == "diff3")
}
+ @Test func aKeyAFileTheGlobalSettingsIncludeSetsIsLeftToThatFile() async throws {
+ let git = try await GitHome()
+ let global = "[user]\n\temail = [email protected]\n[includeIf \"gitdir:~/work/\"]\n\tpath = ~/.gitconfig-work\n"
+ try global.write(to: git.home.appending(path: ".gitconfig"), atomically: true, encoding: .utf8)
+ try "[pull]\n\trebase = false\n".write(to: git.home.appending(path: ".gitconfig-work"), atomically: true, encoding: .utf8)
+ var ledger = GitLedger()
+
+ #expect(await ledger.turnOn(.rebaseOnPull, signingKey: nil, in: git.config) == .unchanged)
+ #expect(try await git.settings()["pull.rebase"] == nil)
+ #expect(await ledger.turnOn(.pruneOnFetch, signingKey: nil, in: git.config) == .changed)
+ }
+
+ @Test func readsEveryFileTheGlobalSettingsIncludeAsGitDoes() async throws {
+ let git = try await GitHome()
+ let files = [
+ ".gitconfig": "[user]\n\temail = [email protected]\n[include]\n\tpath = conf/local.conf\n\tpath = ~/missing.conf\n"
+ + "[includeIf \"gitdir:~/work/\"]\n\tpath = ~/.gitconfig-work\n",
+ "conf/local.conf": "[diff]\n\talgorithm = patience\n[include]\n\tpath = deeper.conf\n",
+ "conf/deeper.conf": "[fetch]\n\tprune = false\n",
+ ".gitconfig-work": "[pull]\n\trebase = false\n",
+ ]
+ for (name, text) in files {
+ let url = git.home.appending(path: name)
+ try FileManager.default.createDirectory(
+ at: url.deletingLastPathComponent(), withIntermediateDirectories: true
+ )
+ try text.write(to: url, atomically: true, encoding: .utf8)
+ }
+
+ #expect(await git.config.keysSetByIncludedFiles() == ["diff.algorithm", "fetch.prune", "pull.rebase"])
+ }
+
@Test func aValueThePersonChangedAfterPeelStaysTheirs() async throws {
let git = try await GitHome()
var ledger = GitLedger()
diff --git a/Peel/Model/GitLibrary.swift b/Peel/Model/GitLibrary.swift
index 13b0b59f..801bda2d 100644
--- a/Peel/Model/GitLibrary.swift
+++ b/Peel/Model/GitLibrary.swift
@@ -9,6 +9,7 @@ final class GitLibrary {
var settings: [String: String?]?
var known: Set<String>?
var signingKey: String?
+ var setInIncludedFiles: Set<String> = []
}
private static let ledgerKey = "terminalGit"
@@ -43,7 +44,8 @@ final class GitLibrary {
isInstalled: git != nil,
settings: settings,
known: known,
- signingKey: GitSetting.signingKey(in: .homeDirectory)
+ signingKey: GitSetting.signingKey(in: .homeDirectory),
+ setInIncludedFiles: await git?.keysSetByIncludedFiles() ?? []
)
}
@@ -53,9 +55,17 @@ final class GitLibrary {
func isOffered(_ setting: GitSetting) -> Bool {
guard let state, state.settings != nil, state.known.map(setting.isKnown(by:)) == true else { return false }
+ guard !isSetInIncludedFiles(setting) else { return false }
return setting != .signCommits || state.signingKey != nil || isOn(setting)
}
+ /// Whether a file the person's global settings include sets the setting, so Peel leaves it to them. One already on
+ /// can still be turned off.
+ func isSetInIncludedFiles(_ setting: GitSetting) -> Bool {
+ guard let state, !isOn(setting) else { return false }
+ return setting.isSetInIncludedFiles(state.setInIncludedFiles)
+ }
+
func set(_ setting: GitSetting, to isOn: Bool) {
ask([setting], isOn: isOn)
afterTheOthers {
diff --git a/Peel/Views/Terminal/GitTab.swift b/Peel/Views/Terminal/GitTab.swift
index 15945fb9..07390bb6 100644
--- a/Peel/Views/Terminal/GitTab.swift
+++ b/Peel/Views/Terminal/GitTab.swift
@@ -79,6 +79,7 @@ private struct GitSettingRow: View {
}
private var caption: LocalizedStringResource? {
+ if git.isSetInIncludedFiles(setting) { return "Set in a file your Git settings include" }
guard setting == .signCommits else { return nil }
guard let key = git.state?.signingKey else { return "Needs an SSH key in ~/.ssh" }
return "With \(URL(filePath: key).abbreviatedPath)"
diff --git a/TERMINAL.md b/TERMINAL.md
index c19c82bd..2465fb5b 100644
--- a/TERMINAL.md
+++ b/TERMINAL.md
@@ -125,7 +125,9 @@ its two files included:
of `~/.ssh/config`. What you set for a server earlier in that file stays in force, since ssh uses the first value
it finds.
- **Git**'s are changed with `git config --global`, Git's own command, and turning one off puts back what you had
- before Peel set it; a setting you made yourself goes back to Git's default.
+ before Peel set it; a setting you made yourself goes back to Git's default. A setting that a file your Git
+ settings include already sets, through `include` or `includeIf`, is left to that file: Git reads it where it is
+ included and the last value wins, so a value Peel added would take its place in every folder it serves.
The line that makes zsh read Peel's file: