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,87 @@
|
||||
import Foundation
|
||||
import HelderCore
|
||||
|
||||
/// Why no commit message came back; `detail` is the toast's mono line.
|
||||
public enum CommitMessageError: Error, Equatable {
|
||||
case notFound
|
||||
case timedOut
|
||||
/// The CLI could not start or exited non-zero; the first stderr line, else the exit code.
|
||||
case failed(String)
|
||||
/// The CLI answered with nothing usable.
|
||||
case empty
|
||||
|
||||
public var detail: String {
|
||||
switch self {
|
||||
case .notFound: "claude not found"
|
||||
case .timedOut: "timed out"
|
||||
case let .failed(reason): reason
|
||||
case .empty: "claude returned no message"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes a commit message for a prompt built by `CommitMessagePrompt`.
|
||||
public protocol CommitMessageGenerating: Sendable {
|
||||
/// The cleaned one-line message; throws `CommitMessageError`, or `CancellationError` when the caller went away.
|
||||
func generate(prompt: String) async throws -> String
|
||||
}
|
||||
|
||||
/// Runs the Claude CLI in print mode, with no tools, in the project root.
|
||||
public struct ClaudeCommitMessageService: CommitMessageGenerating {
|
||||
public let root: URL
|
||||
private let runner: ProcessRunning
|
||||
private let locator: ExecutableLocator
|
||||
private let timeout: TimeInterval
|
||||
|
||||
/// No `--model`: the CLI's own default model writes the message.
|
||||
public static let arguments = [
|
||||
"-p", "--output-format", "text", "--no-session-persistence", "--strict-mcp-config", "--disable-slash-commands",
|
||||
"--tools", "",
|
||||
]
|
||||
|
||||
public init(root: URL, runner: ProcessRunning = ProcessRunner(), locator: ExecutableLocator = .agent, timeout: TimeInterval = 90) {
|
||||
self.root = root
|
||||
self.runner = runner
|
||||
self.locator = locator
|
||||
self.timeout = timeout
|
||||
}
|
||||
|
||||
/// The found binary's folder goes first on the child's PATH, so the CLI finds its own helpers.
|
||||
public func generate(prompt: String) async throws -> String {
|
||||
guard let binary = locator.locate("claude") else { throw CommitMessageError.notFound }
|
||||
var environment = ProcessInfo.processInfo.environment
|
||||
let folder = binary.deletingLastPathComponent().path
|
||||
environment["PATH"] = [folder, environment["PATH"]].compactMap { $0 }.filter { !$0.isEmpty }.joined(separator: ":")
|
||||
let runner = self.runner, root = self.root, timeout = self.timeout
|
||||
|
||||
let outcome = try await withThrowingTaskGroup(of: ProcessResult?.self) { group -> ProcessResult? in
|
||||
group.addTask {
|
||||
do {
|
||||
return try await runner.run(binary.path, arguments: Self.arguments, in: root, environment: environment, input: prompt)
|
||||
} catch ProcessRunnerError.executableNotFound {
|
||||
throw CommitMessageError.notFound
|
||||
} catch let ProcessRunnerError.launchFailed(reason) {
|
||||
throw CommitMessageError.failed(reason)
|
||||
}
|
||||
}
|
||||
group.addTask {
|
||||
try await Task.sleep(nanoseconds: UInt64(max(0, timeout) * 1_000_000_000))
|
||||
return nil
|
||||
}
|
||||
defer { group.cancelAll() }
|
||||
return try await group.next() ?? nil
|
||||
}
|
||||
guard let result = outcome else { throw CommitMessageError.timedOut }
|
||||
guard result.succeeded else {
|
||||
throw CommitMessageError.failed(Self.reason(stderr: result.stderr) ?? "claude exited with code \(result.exitCode)")
|
||||
}
|
||||
guard let message = CommitMessagePrompt.clean(result.stdout) else { throw CommitMessageError.empty }
|
||||
return message
|
||||
}
|
||||
|
||||
/// The first stderr line; the CLI prints notices such as a sign-in expiry as "Warning: …" before the real error.
|
||||
static func reason(stderr: String) -> String? {
|
||||
let lines = stderr.split(whereSeparator: \.isNewline).map { $0.trimmingCharacters(in: .whitespaces) }.filter { !$0.isEmpty }
|
||||
return lines.first { !$0.hasPrefix("Warning:") } ?? lines.first
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user