adds nice git interface
This commit is contained in:
@@ -8,7 +8,8 @@ public enum GitDerivation {
|
||||
/// 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 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 {
|
||||
@@ -39,55 +40,9 @@ public enum GitDerivation {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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/")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user