adds nice git interface

This commit is contained in:
2026-10-30 13:21:45 +01:00
parent b9c03a7637
commit caec81e762
107 changed files with 6466 additions and 3992 deletions
+21 -15
View File
@@ -25,12 +25,16 @@ public struct ParsedStatus: Sendable, Equatable {
public var branch: String
public var ahead: Int
public var canPush: Bool
public var behind: Int
public var upstream: String?
public var entries: [StatusEntry]
public init(branch: String, ahead: Int, canPush: Bool, entries: [StatusEntry]) {
public init(branch: String, ahead: Int, canPush: Bool, behind: Int = 0, upstream: String? = nil, entries: [StatusEntry]) {
self.branch = branch
self.ahead = ahead
self.canPush = canPush
self.behind = behind
self.upstream = upstream
self.entries = entries
}
}
@@ -69,6 +73,8 @@ public enum Porcelain {
let header = String(part.dropFirst(3))
result.branch = parseBranch(header: header)
(result.ahead, result.canPush) = parseAhead(header: header)
result.behind = parseBehind(header: header)
result.upstream = parseUpstream(header: header)
continue
}
let chars = Array(part.prefix(2))
@@ -101,6 +107,20 @@ public enum Porcelain {
return (0, !header.contains("..."))
}
/// The `[behind N]` count of a header; 0 without one.
public static func parseBehind(header: String) -> Int {
guard let match = header.firstMatch(of: /\[[^\]]*\bbehind (\d+)/) else { return 0 }
return Int(match.1) ?? 0
}
/// The tracked branch after `...` in a header (`main...origin/main [ahead 1]` → "origin/main"); nil without one.
public static func parseUpstream(header: String) -> String? {
if header.hasPrefix("HEAD ") { return nil }
guard let range = header.range(of: "...") else { return nil }
let name = header[range.upperBound...].split(separator: " ").first.map(String.init) ?? ""
return name.isEmpty ? nil : name
}
/// Maps one porcelain column letter to the letter the row shows.
public static func status(for code: Character) -> GitStatus {
switch code {
@@ -125,20 +145,6 @@ public enum Porcelain {
return rows.isEmpty ? [GitRowSpec(status: status(for: index), staged: true)] : rows
}
/// The toast line for a push: git reports on stderr, so the summary is pulled from there.
public static func pushSummary(exitCode: Int32, stdout: String, stderr: String) -> PushResult {
let source = stderr.isEmpty ? stdout : stderr
var lines = source.split(whereSeparator: \.isNewline)
.map { $0.trimmingCharacters(in: .whitespaces) }
.filter { !$0.isEmpty }
if exitCode != 0 { return PushResult(ok: false, message: lines.popLast() ?? "push failed") }
let summary = lines.first { line in
line.contains("->") || line.localizedCaseInsensitiveContains("up-to-date")
|| line.localizedCaseInsensitiveContains("new branch")
}
return PushResult(ok: true, message: summary ?? lines.popLast() ?? "Pushed")
}
/// True when a failed `git push` only needs `-u origin <branch>`.
public static func needsUpstream(stderr: String) -> Bool {
stderr.range(of: "no upstream branch|set-upstream", options: [.regularExpression, .caseInsensitive]) != nil