adds claude-generated commit messages and a recent commits panel to the git view - adds log tailing in the explorer, branch merging and a clean up button

This commit is contained in:
2026-11-04 10:07:52 +01:00
parent 600d4874d2
commit cc6651c08a
49 changed files with 1944 additions and 180 deletions
@@ -0,0 +1,71 @@
import Foundation
/// The staged changes as `git diff --cached` prints them: the `--stat` summary and the full patch.
public struct StagedDiff: Sendable, Equatable {
public var stat: String
public var patch: String
public init(stat: String, patch: String) {
self.stat = stat
self.patch = patch
}
public var isEmpty: Bool {
patch.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
}
}
/// The prompt that asks the Claude CLI for one commit message, and the cleaning of its answer.
public enum CommitMessagePrompt {
public static let maxPatchCharacters = 60_000
public static let examples = [
"Adds a new button on the header to navigate to profile",
"Fixed security issue that made it possible to hack a file upload",
"Makes sure the register form does not accept R&B fans - Enforces password rules on login form",
]
/// The full prompt; a patch over `limit` characters is cut and the prompt says so, the stat always stays whole.
public static func make(_ diff: StagedDiff, limit: Int = maxPatchCharacters) -> String {
let isCut = diff.patch.count > limit
let patch = isCut ? String(diff.patch.prefix(limit)) : diff.patch
var lines = [
"Write ONE git commit message for the staged changes below.",
"",
"Rules:",
"- One single sentence on one line. No body, no quotes, no prefix such as \"feat:\". A trailing period is not needed.",
"- English. Start with a verb in the third person or in the past tense.",
"- When two separate things changed, join them with \" - \" in that one line.",
"- Reply with the message only, nothing else.",
"",
"Examples:",
"- \(examples[0])",
"- \(examples[1])",
"- \(examples[2]) (two things changed)",
"",
"Changed files (git diff --cached --stat):",
diff.stat.trimmingCharacters(in: .newlines),
"",
"Diff (git diff --cached):",
]
if isCut {
lines.append("(The diff is cut after \(limit) characters; the file list above is complete.)")
}
lines.append(patch.trimmingCharacters(in: .newlines))
return lines.joined(separator: "\n")
}
/// The first non-empty line, Markdown fence lines skipped, without surrounding quotes or backticks and with single spaces;
/// nil when nothing is left.
public static func clean(_ output: String) -> String? {
let wrappers: Set<Character> = ["\"", "'", "`", "“", "”", "‘", "’"]
guard var line = output.split(whereSeparator: \.isNewline)
.map({ $0.trimmingCharacters(in: .whitespaces) })
.first(where: { !$0.isEmpty && !$0.hasPrefix("```") }) else { return nil }
while let first = line.first, let last = line.last, line.count >= 2, wrappers.contains(first), wrappers.contains(last) {
line = String(line.dropFirst().dropLast()).trimmingCharacters(in: .whitespaces)
}
line = line.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
return line.isEmpty ? nil : line
}
}
+50
View File
@@ -0,0 +1,50 @@
import Foundation
/// One commit of HEAD's recent history, as the Git view's commit list shows it.
public struct GitCommitSummary: Sendable, Equatable {
public var hash: String
public var subject: String
public var author: String
public var date: Date
public init(hash: String, subject: String, author: String, date: Date) {
self.hash = hash
self.subject = subject
self.author = author
self.date = date
}
}
public enum GitCommitLogParsing {
public static let limit = 30
/// NUL between fields and a record separator after each commit, so no subject can split a record.
public static let arguments = ["log", "-n", "\(limit)", "--no-color", "--format=%h%x00%s%x00%an%x00%ct%x1e"]
/// Reads the records `arguments` prints, newest first; a malformed record is skipped.
public static func commits(_ raw: String) -> [GitCommitSummary] {
raw.split(separator: "\u{1e}").compactMap { record in
let text = record.trimmingCharacters(in: .newlines)
let fields = text.split(separator: "\0", omittingEmptySubsequences: false)
guard fields.count == 4, !fields[0].isEmpty, let seconds = TimeInterval(fields[3]) else { return nil }
return GitCommitSummary(hash: String(fields[0]), subject: String(fields[1]), author: String(fields[2]),
date: Date(timeIntervalSince1970: seconds))
}
}
}
public enum RelativeTime {
/// "just now", "5 min ago", "3 h ago", "2 d ago", "3 w ago", "4 mo ago", "2 y ago"; a future date reads "just now".
public static func text(_ date: Date, now: Date) -> String {
let seconds = Int(now.timeIntervalSince(date))
let minute = 60, hour = 3_600, day = 86_400
switch seconds {
case ..<minute: return "just now"
case ..<hour: return "\(seconds / minute) min ago"
case ..<day: return "\(seconds / hour) h ago"
case ..<(7 * day): return "\(seconds / day) d ago"
case ..<(30 * day): return "\(seconds / (7 * day)) w ago"
case ..<(365 * day): return "\(seconds / (30 * day)) mo ago"
default: return "\(seconds / (365 * day)) y ago"
}
}
}