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:
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user