49 lines
2.6 KiB
Swift
49 lines
2.6 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,
|
|
behind: snapshot.behind, upstream: snapshot.upstream, 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
|
|
}
|
|
}
|
|
|
|
public enum SourceControlText {
|
|
/// 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)
|
|
}
|
|
}
|