Repository Public
1import Foundation23/// What `HEAD` points at.4public enum GitHead: Sendable, Hashable, Codable {5 /// On a branch (possibly unborn, see ``GitStatus/headOID``).6 case branch(String)7 /// Detached `HEAD`.8 case detached9}1011/// Kind of change reported for the index or the work tree side of a status entry.12public enum GitChangeKind: String, Sendable, Hashable, Codable {13 case modified = "M"14 case typeChanged = "T"15 case added = "A"16 case deleted = "D"17 case renamed = "R"18 case copied = "C"19 case unmerged = "U"20}2122/// One path of `git status`.23public struct GitStatusEntry: Sendable, Hashable, Codable, Identifiable {24 /// Category of an entry.25 public enum Kind: String, Sendable, Hashable, Codable {26 /// Tracked path with staged and/or unstaged changes.27 case changed28 case untracked29 /// Unmerged path (merge conflict).30 case conflicted31 }3233 public var kind: Kind34 /// Path relative to the work tree root.35 public var path: String36 /// Previous path of a renamed or copied entry.37 public var originalPath: String?38 /// Change recorded in the index (staged), or one side of a conflict.39 public var index: GitChangeKind?40 /// Change in the work tree (unstaged), or the other side of a conflict.41 public var worktree: GitChangeKind?4243 public var id: String { path }44 /// The entry has a staged change.45 public var isStaged: Bool { kind == .changed && index != nil }46 /// The entry has an unstaged change to a tracked file.47 public var isUnstaged: Bool { kind == .changed && worktree != nil }4849 public init(kind: Kind, path: String, originalPath: String? = nil, index: GitChangeKind? = nil, worktree: GitChangeKind? = nil) {50 self.kind = kind51 self.path = path52 self.originalPath = originalPath53 self.index = index54 self.worktree = worktree55 }56}5758/// Parsed `git status --porcelain=v2 --branch`.59public struct GitStatus: Sendable, Hashable, Codable {60 public var head: GitHead61 /// Commit `HEAD` points at; `nil` before the first commit.62 public var headOID: String?63 /// Upstream branch, e.g. `origin/main`.64 public var upstream: String?65 /// Commits ahead of the upstream; `nil` without an upstream (or when it is gone).66 public var ahead: Int?67 /// Commits behind the upstream; `nil` without an upstream (or when it is gone).68 public var behind: Int?69 public var entries: [GitStatusEntry]70 /// `true` when the status output exceeded the size cap and `entries` is incomplete.71 public var isTruncated: Bool7273 public var staged: [GitStatusEntry] { entries.filter(\.isStaged) }74 public var unstaged: [GitStatusEntry] { entries.filter(\.isUnstaged) }75 public var untracked: [GitStatusEntry] { entries.filter { $0.kind == .untracked } }76 public var conflicted: [GitStatusEntry] { entries.filter { $0.kind == .conflicted } }77 /// No staged, unstaged, untracked or conflicted path.78 public var isClean: Bool { entries.isEmpty && !isTruncated }7980 public init(head: GitHead, headOID: String? = nil, upstream: String? = nil, ahead: Int? = nil, behind: Int? = nil, entries: [GitStatusEntry] = [], isTruncated: Bool = false) {81 self.head = head82 self.headOID = headOID83 self.upstream = upstream84 self.ahead = ahead85 self.behind = behind86 self.entries = entries87 self.isTruncated = isTruncated88 }8990 /// Parses NUL-separated porcelain v2 output.91 static func parse(porcelainV2 data: Data, truncated: Bool = false) -> GitStatus {92 var status = GitStatus(head: .detached, isTruncated: truncated)93 var records = String(decoding: data, as: UTF8.self).split(separator: "\0", omittingEmptySubsequences: false).map(String.init)94 // A truncated buffer ends with a partial record; a complete one ends with an empty element.95 if !records.isEmpty { records.removeLast() }96 func change(_ character: Character) -> GitChangeKind? { GitChangeKind(rawValue: String(character)) }9798 var index = 099 while index < records.count {100 let record = records[index]101 index += 1102 if record.hasPrefix("# ") {103 let parts = record.dropFirst(2).split(separator: " ", maxSplits: 1).map(String.init)104 guard parts.count == 2 else { continue }105 switch parts[0] {106 case "branch.oid": status.headOID = parts[1] == "(initial)" ? nil : parts[1]107 case "branch.head": status.head = parts[1] == "(detached)" ? .detached : .branch(parts[1])108 case "branch.upstream": status.upstream = parts[1]109 case "branch.ab":110 let counts = parts[1].split(separator: " ")111 if counts.count == 2 {112 status.ahead = Int(counts[0].dropFirst())113 status.behind = Int(counts[1].dropFirst())114 }115 default: break116 }117 continue118 }119 guard let type = record.first else { continue }120 switch type {121 case "1", "2", "u":122 let fieldsBeforePath = type == "1" ? 8 : (type == "2" ? 9 : 10)123 let fields = record.split(separator: " ", maxSplits: fieldsBeforePath, omittingEmptySubsequences: false)124 guard fields.count == fieldsBeforePath + 1, fields[1].count == 2 else { continue }125 let xy = Array(fields[1])126 var entry = GitStatusEntry(kind: type == "u" ? .conflicted : .changed, path: String(fields[fieldsBeforePath]), index: change(xy[0]), worktree: change(xy[1]))127 if type == "2" {128 guard index < records.count else { continue }129 entry.originalPath = records[index]130 index += 1131 }132 status.entries.append(entry)133 case "?":134 status.entries.append(GitStatusEntry(kind: .untracked, path: String(record.dropFirst(2))))135 default:136 break137 }138 }139 return status140 }141}142143/// One commit of `git log`.144public struct GitCommit: Sendable, Hashable, Codable, Identifiable {145 public var hash: String146 public var shortHash: String147 public var authorName: String148 public var authorEmail: String149 /// Author date.150 public var date: Date151 public var subject: String152153 public var id: String { hash }154155 public init(hash: String, shortHash: String, authorName: String, authorEmail: String, date: Date, subject: String) {156 self.hash = hash157 self.shortHash = shortHash158 self.authorName = authorName159 self.authorEmail = authorEmail160 self.date = date161 self.subject = subject162 }163}164165/// A unified diff, possibly cut at a size cap.166public struct GitDiff: Sendable, Hashable, Codable {167 /// Patch text (UTF-8, invalid bytes replaced).168 public var text: String169 /// `true` when the diff exceeded the cap and `text` holds only its beginning.170 public var truncated: Bool171172 public var isEmpty: Bool { text.isEmpty }173174 public init(text: String, truncated: Bool) {175 self.text = text176 self.truncated = truncated177 }178}179