141 lines
4.9 KiB
Swift
141 lines
4.9 KiB
Swift
import Foundation
|
|
|
|
/// The status letter a git row shows. Copies and untracked files read as `A`, conflicts as `M`.
|
|
public enum GitStatus: String, Codable, Sendable, CaseIterable {
|
|
case added = "A"
|
|
case modified = "M"
|
|
case deleted = "D"
|
|
case renamed = "R"
|
|
case unmerged = "U"
|
|
}
|
|
|
|
/// One file half as `git load` reads it: the text on each side of that half.
|
|
public struct GitFileChange: Sendable, Equatable {
|
|
public var path: String
|
|
public var status: GitStatus
|
|
/// True for the HEAD→index half, false for the index→disk half.
|
|
public var staged: Bool
|
|
public var original: String
|
|
public var updated: String
|
|
/// The source path of a rename or copy; nil otherwise.
|
|
public var originalPath: String?
|
|
/// A side was over the diff cap, so both texts are left empty.
|
|
public var isTooBig: Bool
|
|
|
|
public init(path: String, status: GitStatus, staged: Bool, original: String, updated: String, originalPath: String? = nil,
|
|
isTooBig: Bool = false) {
|
|
self.path = path
|
|
self.status = status
|
|
self.staged = staged
|
|
self.original = original
|
|
self.updated = updated
|
|
self.originalPath = originalPath
|
|
self.isTooBig = isTooBig
|
|
}
|
|
}
|
|
|
|
/// What one `git status` read of the working tree returns.
|
|
public struct GitSnapshot: Sendable, Equatable {
|
|
public var branch: String
|
|
/// Commits the remote does not have yet; 0 when unknown.
|
|
public var ahead: Int
|
|
/// True when a push would send something, including a branch without an upstream.
|
|
public var canPush: Bool
|
|
public var changes: [GitFileChange]
|
|
|
|
public init(branch: String, ahead: Int, canPush: Bool, changes: [GitFileChange]) {
|
|
self.branch = branch
|
|
self.ahead = ahead
|
|
self.canPush = canPush
|
|
self.changes = changes
|
|
}
|
|
}
|
|
|
|
/// A row in the Source Control column. A file staged and then edited again has two rows.
|
|
public struct Change: Sendable, Equatable, Identifiable {
|
|
/// Row identity; the path alone is not unique (see `staged`).
|
|
public var id: String
|
|
public var path: String
|
|
public var status: GitStatus
|
|
public var added: Int
|
|
public var removed: Int
|
|
public var isDeleted: Bool
|
|
public var staged: Bool
|
|
/// The source path of a rename or copy; nil otherwise.
|
|
public var originalPath: String?
|
|
|
|
public init(path: String, status: GitStatus, added: Int, removed: Int, staged: Bool, originalPath: String? = nil) {
|
|
self.id = Change.rowID(path: path, staged: staged)
|
|
self.path = path
|
|
self.status = status
|
|
self.added = added
|
|
self.removed = removed
|
|
self.isDeleted = status == .deleted
|
|
self.staged = staged
|
|
self.originalPath = originalPath
|
|
}
|
|
|
|
/// `s:path` for the staged half, `w:path` for the working-tree half.
|
|
public static func rowID(path: String, staged: Bool) -> String {
|
|
(staged ? "s:" : "w:") + path
|
|
}
|
|
}
|
|
|
|
/// The git slice of the workspace, derived from one snapshot.
|
|
public struct GitWorkingState: Sendable, Equatable {
|
|
public var branch: String
|
|
public var ahead: Int
|
|
public var canPush: Bool
|
|
public var changes: [Change]
|
|
/// Per path: HEAD vs disk. Drives the Actual view.
|
|
public var fileDiffs: [String: Diff]
|
|
/// Per row id: that row's own pair. Drives the Diff overlay.
|
|
public var rowDiffs: [String: Diff]
|
|
/// Paths that have a staged row.
|
|
public var stagedPaths: Set<String>
|
|
/// False when the project is not a git repository.
|
|
public var isRepository: Bool
|
|
|
|
public init(
|
|
branch: String = "—", ahead: Int = 0, canPush: Bool = false, changes: [Change] = [],
|
|
fileDiffs: [String: Diff] = [:], rowDiffs: [String: Diff] = [:],
|
|
stagedPaths: Set<String> = [], isRepository: Bool = false
|
|
) {
|
|
self.branch = branch
|
|
self.ahead = ahead
|
|
self.canPush = canPush
|
|
self.changes = changes
|
|
self.fileDiffs = fileDiffs
|
|
self.rowDiffs = rowDiffs
|
|
self.stagedPaths = stagedPaths
|
|
self.isRepository = isRepository
|
|
}
|
|
|
|
public static let empty = GitWorkingState()
|
|
|
|
/// Whole-project line counts for the status bar.
|
|
public var totals: (added: Int, removed: Int) {
|
|
changes.reduce((0, 0)) { ($0.0 + $1.added, $0.1 + $1.removed) }
|
|
}
|
|
|
|
public var stagedCount: Int { changes.filter(\.staged).count }
|
|
public var unstagedCount: Int { changes.count - stagedCount }
|
|
|
|
/// Distinct paths with an unstaged row, in row order: what "stage all" sends to git.
|
|
public var unstagedPaths: [String] {
|
|
var seen = Set<String>()
|
|
return changes.filter { !$0.staged && seen.insert($0.path).inserted }.map(\.path)
|
|
}
|
|
}
|
|
|
|
/// The outcome of a push, shown in a toast.
|
|
public struct PushResult: Sendable, Equatable {
|
|
public var ok: Bool
|
|
public var message: String
|
|
|
|
public init(ok: Bool, message: String) {
|
|
self.ok = ok
|
|
self.message = message
|
|
}
|
|
}
|