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.

Sign with the key Git already signs with, and say which one

Țugui Dragoș0d5f8764d4c4
diff --git a/Packages/PeelCore/Sources/PeelCore/Terminal/GitSetting.swift b/Packages/PeelCore/Sources/PeelCore/Terminal/GitSetting.swift
index b2d0858c..9d999041 100644
--- a/Packages/PeelCore/Sources/PeelCore/Terminal/GitSetting.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Terminal/GitSetting.swift
@@ -32,9 +32,13 @@ public enum GitSetting: CaseIterable, Sendable {
}
}
- /// The public key to sign commits with: the first default key `ssh-keygen` makes that is in `~/.ssh`.
- public static func signingKey(in home: URL) -> String? {
- ["id_ed25519.pub", "id_ecdsa.pub", "id_rsa.pub"]
+ /// The public key to sign commits with: the one Git already signs with, when `gpg.format` is `ssh` and
+ /// `user.signingKey` names one, and otherwise the first default key `ssh-keygen` makes that is in `~/.ssh`.
+ public static func signingKey(in settings: [String: String?], home: URL) -> String? {
+ if value(settings["gpg.format"]) == "ssh", let key = value(settings["user.signingkey"]), !key.isEmpty {
+ return key
+ }
+ return ["id_ed25519.pub", "id_ecdsa.pub", "id_rsa.pub"]
.map { home.appending(path: ".ssh/\($0)").path(percentEncoded: false) }
.first { FileManager.default.fileExists(atPath: $0) }
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/GitSettingsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/GitSettingsTests.swift
index 39cd8d48..58af6393 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/GitSettingsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/GitSettingsTests.swift
@@ -167,15 +167,38 @@ struct GitSettingsTests {
#expect(git.file == "[pull\n\trebase = false\n")
}
- @Test func signingUsesTheFirstDefaultKeyAndNeedsOne() async throws {
+ @Test func signingUsesTheKeyGitSignsWithOrElseTheFirstDefaultKeyAndNeedsOne() async throws {
let git = try await GitHome()
- #expect(GitSetting.signingKey(in: git.home) == nil)
+ #expect(GitSetting.signingKey(in: [:], home: git.home) == nil)
var ledger = GitLedger()
#expect(await ledger.turnOn(.signCommits, signingKey: nil, in: git.config) == .refused)
let rsa = try git.publicKey("id_rsa.pub")
- #expect(GitSetting.signingKey(in: git.home) == rsa)
+ #expect(GitSetting.signingKey(in: [:], home: git.home) == rsa)
let ed25519 = try git.publicKey("id_ed25519.pub")
- #expect(GitSetting.signingKey(in: git.home) == ed25519)
+ #expect(GitSetting.signingKey(in: [:], home: git.home) == ed25519)
+ let work = try git.publicKey("work.pub")
+ #expect(GitSetting.signingKey(in: ["gpg.format": "ssh", "user.signingkey": work], home: git.home) == work)
+ #expect(GitSetting.signingKey(in: ["gpg.format": "openpgp", "user.signingkey": "ABCDEF0123"], home: git.home) == ed25519)
+ #expect(GitSetting.signingKey(in: ["user.signingkey": work], home: git.home) == ed25519)
+ #expect(GitSetting.signingKey(in: ["gpg.format": "ssh", "user.signingkey": ""], home: git.home) == ed25519)
+ }
+
+ @Test func turningOnSigningKeepsTheKeyThePersonSignsWith() async throws {
+ let git = try await GitHome()
+ _ = try git.publicKey("id_ed25519.pub")
+ let work = try git.publicKey("work.pub")
+ #expect(await git.config.set("gpg.format", to: "ssh"))
+ #expect(await git.config.set("user.signingKey", to: work))
+ let settings = try await git.settings()
+ let key = try #require(GitSetting.signingKey(in: settings, home: git.home))
+ var ledger = GitLedger()
+ #expect(await ledger.turnOn(.signCommits, signingKey: key, in: git.config) == .changed)
+ #expect(try await git.settings()["user.signingkey"] == .some(work))
+ #expect(await ledger.turnOff(.signCommits, in: git.config) == .changed)
+ let after = try await git.settings()
+ #expect(after["user.signingkey"] == .some(work))
+ #expect(after["gpg.format"] == "ssh")
+ #expect(after["commit.gpgsign"] == nil)
}
@Test func theGitOnThisMacKnowsEverySettingAndASettingItDoesNotKnowIsNotOffered() async throws {
diff --git a/Peel/Model/GitLibrary.swift b/Peel/Model/GitLibrary.swift
index 82e16d3f..8ea4e652 100644
--- a/Peel/Model/GitLibrary.swift
+++ b/Peel/Model/GitLibrary.swift
@@ -44,7 +44,7 @@ final class GitLibrary {
isInstalled: git != nil,
settings: settings,
known: known,
- signingKey: GitSetting.signingKey(in: .homeDirectory),
+ signingKey: GitSetting.signingKey(in: settings ?? [:], home: .homeDirectory),
setInIncludedFiles: await git?.keysSetByIncludedFiles() ?? []
)
}
diff --git a/TERMINAL.md b/TERMINAL.md
index 5c96c8eb..4d922a33 100644
--- a/TERMINAL.md
+++ b/TERMINAL.md
@@ -396,7 +396,7 @@ git config --global column.ui auto
| Setting | What it does |
| --- | --- |
-| [Sign commits with your SSH key](https://git-scm.com/docs/git-config#Documentation/git-config.txt-commitgpgSign) | Every commit is signed with your SSH key, and GitHub shows it as Verified once you add the key there as a signing key. Peel offers it when one of `id_ed25519.pub`, `id_ecdsa.pub`, and `id_rsa.pub` is in `~/.ssh`, or when signing is already on, and uses the first of them. Turning it off puts the three keys back as they were before Peel set them: a key Peel found already set stays, so signing you had with another format, such as GPG, goes on as before, and signing you set up yourself with an SSH key stops, as a setting you made yourself goes back to Git's default. |
+| [Sign commits with your SSH key](https://git-scm.com/docs/git-config#Documentation/git-config.txt-commitgpgSign) | Every commit is signed with your SSH key, and GitHub shows it as Verified once you add the key there as a signing key. Peel signs with the key Git already signs with when `gpg.format` is `ssh`, and otherwise with the first of `id_ed25519.pub`, `id_ecdsa.pub`, and `id_rsa.pub` in `~/.ssh`; it offers the setting when it has such a key, or when signing is already on. Turning it off puts the three keys back as they were before Peel set them: a key Peel found already set stays, so signing you had with another format, such as GPG, goes on as before, and signing you set up yourself with an SSH key stops, as a setting you made yourself goes back to Git's default. |
```sh
# Sign commits with your SSH key