restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
import Foundation
|
||||
|
||||
public struct StatusEntry: Sendable, Equatable {
|
||||
/// The index column of the porcelain XY code (" " when clean).
|
||||
public var index: Character
|
||||
/// The working-tree column of the porcelain XY code.
|
||||
public var working: Character
|
||||
/// The destination path for renames and copies.
|
||||
public var path: String
|
||||
/// The source path of a rename or copy; nil otherwise.
|
||||
public var originalPath: String?
|
||||
|
||||
public init(index: Character, working: Character, path: String, originalPath: String? = nil) {
|
||||
self.index = index
|
||||
self.working = working
|
||||
self.path = path
|
||||
self.originalPath = originalPath
|
||||
}
|
||||
|
||||
/// True when the index column records a rename, whose source is gone from the index.
|
||||
public var isStagedRename: Bool { index == "R" && originalPath != nil }
|
||||
}
|
||||
|
||||
public struct ParsedStatus: Sendable, Equatable {
|
||||
public var branch: String
|
||||
public var ahead: Int
|
||||
public var canPush: Bool
|
||||
public var entries: [StatusEntry]
|
||||
|
||||
public init(branch: String, ahead: Int, canPush: Bool, entries: [StatusEntry]) {
|
||||
self.branch = branch
|
||||
self.ahead = ahead
|
||||
self.canPush = canPush
|
||||
self.entries = entries
|
||||
}
|
||||
}
|
||||
|
||||
/// One row a porcelain entry turns into.
|
||||
public struct GitRowSpec: Sendable, Equatable {
|
||||
public var status: GitStatus
|
||||
public var staged: Bool
|
||||
|
||||
public init(status: GitStatus, staged: Bool) {
|
||||
self.status = status
|
||||
self.staged = staged
|
||||
}
|
||||
}
|
||||
|
||||
public enum Porcelain {
|
||||
/// The arguments `GitService.load` passes to git; `-z` keeps paths unquoted.
|
||||
public static let statusArguments = ["status", "--porcelain", "-b", "--untracked-files=all", "-z"]
|
||||
|
||||
/// Parses `git status --porcelain -b -z`: NUL-separated, the `## ` header first.
|
||||
///
|
||||
/// For a rename or copy (R or C in either column) the next NUL field is the source
|
||||
/// path, not an entry of its own. A missing header leaves the branch at "HEAD".
|
||||
public static func parseStatus(_ raw: String) -> ParsedStatus {
|
||||
let parts = raw.split(separator: "\0", omittingEmptySubsequences: false)
|
||||
var result = ParsedStatus(branch: "HEAD", ahead: 0, canPush: false, entries: [])
|
||||
var sourceFollows = false
|
||||
for part in parts {
|
||||
if sourceFollows {
|
||||
sourceFollows = false
|
||||
result.entries[result.entries.count - 1].originalPath = String(part)
|
||||
continue
|
||||
}
|
||||
if part.isEmpty { continue }
|
||||
if part.hasPrefix("## ") {
|
||||
let header = String(part.dropFirst(3))
|
||||
result.branch = parseBranch(header: header)
|
||||
(result.ahead, result.canPush) = parseAhead(header: header)
|
||||
continue
|
||||
}
|
||||
let chars = Array(part.prefix(2))
|
||||
let index = chars.first ?? " "
|
||||
let working = chars.count > 1 ? chars[1] : " "
|
||||
let path = String(part.dropFirst(3))
|
||||
if index == "R" || index == "C" || working == "R" || working == "C" { sourceFollows = true }
|
||||
result.entries.append(StatusEntry(index: index, working: working, path: path))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/// The display branch of a header such as `main...origin/main [ahead 1]`,
|
||||
/// `No commits yet on main` (→ "main") or `HEAD (no branch)` (→ "HEAD").
|
||||
public static func parseBranch(header: String) -> String {
|
||||
let noCommits = "No commits yet on "
|
||||
if header.hasPrefix(noCommits) { return header.dropFirst(noCommits.count).trimmingCharacters(in: .whitespaces) }
|
||||
if header.hasPrefix("HEAD ") { return "HEAD" }
|
||||
let head = header.range(of: "...").map { header[..<$0.lowerBound] } ?? Substring(header)
|
||||
let name = head.split(separator: " ", omittingEmptySubsequences: false).first.map(String.init) ?? ""
|
||||
let trimmed = name.trimmingCharacters(in: .whitespaces)
|
||||
return trimmed.isEmpty ? "HEAD" : trimmed
|
||||
}
|
||||
|
||||
/// Push state from a header. `[ahead N]` → (N, true); no `...` upstream → (0, true),
|
||||
/// because a first push still sends every commit; "No commits yet" or detached → (0, false).
|
||||
public static func parseAhead(header: String) -> (ahead: Int, canPush: Bool) {
|
||||
if header.hasPrefix("No commits yet on ") || header.hasPrefix("HEAD ") { return (0, false) }
|
||||
if let match = header.firstMatch(of: /\[[^\]]*\bahead (\d+)/), let count = Int(match.1) { return (count, true) }
|
||||
return (0, !header.contains("..."))
|
||||
}
|
||||
|
||||
/// Maps one porcelain column letter to the letter the row shows.
|
||||
public static func status(for code: Character) -> GitStatus {
|
||||
switch code {
|
||||
case "A", "C", "?": .added
|
||||
case "D": .deleted
|
||||
case "R": .renamed
|
||||
default: .modified
|
||||
}
|
||||
}
|
||||
|
||||
/// A merge conflict. Neither half can be staged on its own, so it stays one row.
|
||||
public static func isConflict(index: Character, working: Character) -> Bool {
|
||||
index == "U" || working == "U" || (index == "A" && working == "A") || (index == "D" && working == "D")
|
||||
}
|
||||
|
||||
/// Splits an XY pair into rows: "MM" is a staged row plus an unstaged row, never one folded row.
|
||||
public static func classify(index: Character, working: Character) -> [GitRowSpec] {
|
||||
if isConflict(index: index, working: working) { return [GitRowSpec(status: .modified, staged: true)] }
|
||||
var rows: [GitRowSpec] = []
|
||||
if index != " " && index != "?" { rows.append(GitRowSpec(status: status(for: index), staged: true)) }
|
||||
if working != " " { rows.append(GitRowSpec(status: status(for: working), staged: false)) }
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user