Files
helder/Sources/HelderCore/Git/GitDerivation.swift
T

94 lines
4.3 KiB
Swift

import Foundation
public enum GitDerivation {
/// Turns a snapshot into rows plus per-row and per-file diffs; nil means "not a repository".
///
/// Per file, HEAD comes from the staged row's original and disk from the unstaged row's
/// updated; with one row both come from it. The unstaged row owns the file-level status,
/// so a file staged as modified but deleted on disk reads as deleted.
public static func derive(_ snapshot: GitSnapshot?) -> GitWorkingState {
guard let snapshot else { return .empty }
var state = GitWorkingState(branch: snapshot.branch, ahead: snapshot.ahead, canPush: snapshot.canPush, isRepository: true)
var wholeFile: [String: (head: String, disk: String, status: GitStatus, rows: Int, rowID: String)] = [:]
var tooBig = Set<String>()
for change in snapshot.changes {
var diff = LineDiff.build(original: change.original, updated: change.updated, status: change.status)
diff.isTooBig = change.isTooBig
if change.isTooBig { tooBig.insert(change.path) }
let row = Change(path: change.path, status: change.status, added: diff.added, removed: diff.removed,
staged: change.staged, originalPath: change.originalPath)
state.rowDiffs[row.id] = diff
state.changes.append(row)
if change.staged { state.stagedPaths.insert(change.path) }
if let previous = wholeFile[change.path] {
wholeFile[change.path] = change.staged
? (change.original, previous.disk, previous.status, previous.rows + 1, previous.rowID)
: (previous.head, change.updated, change.status, previous.rows + 1, previous.rowID)
} else {
wholeFile[change.path] = (change.original, change.updated, change.status, 1, row.id)
}
}
for (path, whole) in wholeFile {
var diff = whole.rows == 1
? state.rowDiffs[whole.rowID]
: LineDiff.build(original: whole.head, updated: whole.disk, status: whole.status)
diff?.isTooBig = tooBig.contains(path)
state.fileDiffs[path] = diff
}
return state
}
}
/// Rows of one folder in the Source Control list.
public struct ChangeGroup: Sendable, Equatable {
/// Folder path; "" is the project root.
public var dir: String
public var rows: [Change]
public init(dir: String, rows: [Change]) {
self.dir = dir
self.rows = rows
}
}
public enum SourceControlText {
/// Groups rows by folder in order of first appearance; keyboard navigation walks this same order.
public static func groupByDir(_ list: [Change]) -> [ChangeGroup] {
var groups: [ChangeGroup] = []
var indexByDir: [String: Int] = [:]
for change in list {
let dir = PathUtils.dirname(change.path)
if let index = indexByDir[dir] {
groups[index].rows.append(change)
} else {
indexByDir[dir] = groups.count
groups.append(ChangeGroup(dir: dir, rows: [change]))
}
}
return groups
}
/// The row order of the list: staged groups first, then unstaged, each folder by folder.
public static func navigationOrder(_ changes: [Change]) -> [Change] {
let staged = groupByDir(changes.filter(\.staged)).flatMap(\.rows)
let unstaged = groupByDir(changes.filter { !$0.staged }).flatMap(\.rows)
return staged + unstaged
}
/// Dictation tools capitalise and add line breaks; a commit subject is one lowercase line.
public static func normalizeCommitMessage(_ value: String) -> String {
value.lowercased().replacingOccurrences(of: "[ \\t]*[\\r\\n]+[ \\t]*", with: " ", options: .regularExpression)
}
public static func pushTooltip(canPush: Bool, ahead: Int) -> String {
if !canPush { return "Nothing to push (⌘P)" }
if ahead > 0 { return "Push \(ahead) commit\(ahead > 1 ? "s" : "") to remote (⌘P)" }
return "Push the branch to remote (⌘P)"
}
/// Git rows under `tests/` or `cypress/` render dimmed.
public static func isTestFile(_ path: String) -> Bool {
path.hasPrefix("tests/") || path.hasPrefix("cypress/")
}
}