Files

46 lines
1.8 KiB
Swift

import Foundation
import HelderCore
public enum GitError: Error, Equatable {
/// A git command exited non-zero; `stderr` is trimmed.
case commandFailed(arguments: [String], exitCode: Int32, stderr: String)
}
/// What one `execute` run of git ended with.
public struct GitCommandResult: Sendable, Equatable {
public var exitCode: Int32
public var stdout: String
public var stderr: String
public init(exitCode: Int32, stdout: String, stderr: String) {
self.exitCode = exitCode
self.stdout = stdout
self.stderr = stderr
}
public var succeeded: Bool { exitCode == 0 }
}
public protocol GitServicing: Sendable {
/// The working state, or nil when the project is not a git repository.
func load() async throws -> GitSnapshot?
/// Runs `git <arguments>` in the project and streams every output line; never throws (a launch failure is exit code -1 with the reason on stderr).
func execute(_ arguments: [String], literalPathspecs: Bool, timeout: TimeInterval?,
onLine: @escaping @Sendable (String, Bool) -> Void) async -> GitCommandResult
/// Local and origin branches.
func branches() async -> GitBranchList
/// `user.name <user.email>` from git config, nil when unset.
func author() async -> String?
/// The last 30 commits of HEAD, newest first; empty without commits or outside a repository.
func recentCommits() async -> [GitCommitSummary]
/// `git diff --cached` as stat and patch; nil when git fails, e.g. outside a repository.
func stagedDiff() async -> StagedDiff?
}
extension GitServicing {
/// Runs with literal pathspecs and no timeout.
public func execute(_ arguments: [String], onLine: @escaping @Sendable (String, Bool) -> Void) async -> GitCommandResult {
await execute(arguments, literalPathspecs: true, timeout: nil, onLine: onLine)
}
}