improves the git feature by adding a conflict status

This commit is contained in:
2026-10-30 16:47:34 +01:00
parent caec81e762
commit 0278d7fbd1
14 changed files with 10927 additions and 19 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
import Foundation import Foundation
/// The status letter a git row shows. Copies and untracked files read as `A`, conflicts as `M`. /// The status letter a git row shows. Copies and untracked files read as `A`, conflicts as `U`.
public enum GitStatus: String, Codable, Sendable, CaseIterable { public enum GitStatus: String, Codable, Sendable, CaseIterable {
case added = "A" case added = "A"
case modified = "M" case modified = "M"
+2 -2
View File
@@ -131,14 +131,14 @@ public enum Porcelain {
} }
} }
/// A merge conflict. Neither half can be staged on its own, so it stays one row. /// A merge conflict. Neither half can be staged on its own, so it stays one unstaged `U` row.
public static func isConflict(index: Character, working: Character) -> Bool { public static func isConflict(index: Character, working: Character) -> Bool {
index == "U" || working == "U" || (index == "A" && working == "A") || (index == "D" && working == "D") index == "U" || working == "U" || (index == "A" && working == "A") || (index == "D" && working == "D")
} }
/// Splits an XY pair into rows: "MM" is a staged row plus an unstaged row, never one folded row. /// Splits an XY pair into rows: "MM" is a staged row plus an unstaged row, never one folded row.
public static func classify(index: Character, working: Character) -> [GitRowSpec] { public static func classify(index: Character, working: Character) -> [GitRowSpec] {
if isConflict(index: index, working: working) { return [GitRowSpec(status: .modified, staged: true)] } if isConflict(index: index, working: working) { return [GitRowSpec(status: .unmerged, staged: false)] }
var rows: [GitRowSpec] = [] var rows: [GitRowSpec] = []
if index != " " && index != "?" { rows.append(GitRowSpec(status: status(for: index), staged: true)) } if index != " " && index != "?" { rows.append(GitRowSpec(status: status(for: index), staged: true)) }
if working != " " { rows.append(GitRowSpec(status: status(for: working), staged: false)) } if working != " " { rows.append(GitRowSpec(status: status(for: working), staged: false)) }
@@ -145,7 +145,7 @@ private struct TreeRowView: View {
if let status { if let status {
Text(status.rawValue) Text(status.rawValue)
.typeRole(RowStyle.changeLetterRole) .typeRole(RowStyle.changeLetterRole)
.foregroundStyle(status == .deleted ? Palette.amberDeep : Palette.addedText) .foregroundStyle(letterColor(status))
.padding(.leading, 8) .padding(.leading, 8)
} }
} }
@@ -164,6 +164,14 @@ private struct TreeRowView: View {
.help(isDirectory ? "" : row.path) .help(isDirectory ? "" : row.path)
} }
private func letterColor(_ status: GitStatus) -> Color {
switch status {
case .deleted: Palette.amberDeep
case .unmerged: Palette.gitConflict
default: Palette.addedText
}
}
private var labelColor: Color { private var labelColor: Color {
if status == .deleted { return Palette.text4 } if status == .deleted { return Palette.text4 }
return selected || (isDirectory && isOpen) ? Palette.text1 : Palette.text3 return selected || (isDirectory && isOpen) ? Palette.text1 : Palette.text3
+5 -2
View File
@@ -1,9 +1,9 @@
import SwiftUI import SwiftUI
/// The Git view's Lucide icons, drawn on Lucide's 24-unit grid at stroke 1.75. /// The Git view's icons, drawn on Lucide's 24-unit grid at stroke 1.75; the flag is a plain pennant.
/// Icons with arcs Lucide draws as curves (cloud, eraser) use the nearest SF Symbol. /// Icons with arcs Lucide draws as curves (cloud, eraser) use the nearest SF Symbol.
enum GitIcon { enum GitIcon {
case gitBranch, cloud, arrowDownToLine, arrowUpFromLine, minimize, chevronDown, chevronRight, eraser, check case gitBranch, cloud, arrowDownToLine, arrowUpFromLine, minimize, chevronDown, chevronRight, eraser, check, flag
fileprivate var symbol: String? { fileprivate var symbol: String? {
switch self { switch self {
@@ -46,6 +46,9 @@ enum GitIcon {
line((9, 18), (15, 12), (9, 6)) line((9, 18), (15, 12), (9, 6))
case .check: case .check:
line((20, 6), (9, 17), (4, 12)) line((20, 6), (9, 17), (4, 12))
case .flag:
line((5, 22), (5, 3))
line((5, 4), (19, 9), (5, 14))
case .cloud, .eraser: case .cloud, .eraser:
break break
} }
+19 -4
View File
@@ -233,7 +233,14 @@ private struct GitFileRow: View {
GitIconView(icon: expanded ? .chevronDown : .chevronRight, size: 13) GitIconView(icon: expanded ? .chevronDown : .chevronRight, size: 13)
.foregroundStyle(expanded ? Palette.amber : Palette.text4) .foregroundStyle(expanded ? Palette.amber : Palette.text4)
StatusBadge(status: row.status) StatusBadge(status: row.status)
HStack(spacing: 8) {
path path
if isConflict {
GitIconView(icon: .flag, size: 13)
.foregroundStyle(Palette.gitConflict)
.tip("Merge conflict — resolve, then Stage")
}
}
.frame(maxWidth: .infinity, alignment: .leading) .frame(maxWidth: .infinity, alignment: .leading)
HStack(spacing: 0) { HStack(spacing: 0) {
Text("+\(row.added)").foregroundStyle(Palette.gitNew) Text("+\(row.added)").foregroundStyle(Palette.gitNew)
@@ -248,9 +255,9 @@ private struct GitFileRow: View {
} }
.padding(.horizontal, 14) .padding(.horizontal, 14)
.frame(height: GitMetrics.fileRow) .frame(height: GitMetrics.fileRow)
.background(expanded ? Palette.panel : (isHovered ? Palette.gitRowHover : .clear)) .background(expanded ? Palette.panel : (isHovered ? Palette.gitRowHover : (isConflict ? Palette.gitConflictTint : .clear)))
.overlay(alignment: .leading) { .overlay(alignment: .leading) {
Rectangle().fill(kind == .staged ? Palette.amber : .clear).frame(width: GitMetrics.accentBar) Rectangle().fill(accentColor).frame(width: GitMetrics.accentBar)
} }
.hairline(.bottom) .hairline(.bottom)
.contentShape(Rectangle()) .contentShape(Rectangle())
@@ -262,6 +269,13 @@ private struct GitFileRow: View {
.tracksHover($isHovered) .tracksHover($isHovered)
} }
private var isConflict: Bool { row.status == .unmerged }
private var accentColor: Color {
if isConflict { return Palette.gitConflict }
return kind == .staged ? Palette.amber : .clear
}
/// The folder and slash muted, the name bright; a deleted file is struck through with a muted name. /// The folder and slash muted, the name bright; a deleted file is struck through with a muted name.
private var path: some View { private var path: some View {
let dir = PathUtils.dirname(row.path) let dir = PathUtils.dirname(row.path)
@@ -292,7 +306,8 @@ private struct StatusBadge: View {
switch status { switch status {
case .added: Palette.gitNew case .added: Palette.gitNew
case .deleted: Palette.gitDeleted case .deleted: Palette.gitDeleted
case .modified, .renamed, .unmerged: Palette.gitModified case .modified, .renamed: Palette.gitModified
case .unmerged: Palette.gitConflict
} }
} }
@@ -302,7 +317,7 @@ private struct StatusBadge: View {
case .deleted: "Deleted" case .deleted: "Deleted"
case .modified: "Modified" case .modified: "Modified"
case .renamed: "Renamed" case .renamed: "Renamed"
case .unmerged: "Conflict" case .unmerged: "Merge conflict"
} }
} }
} }
+4
View File
@@ -132,11 +132,15 @@ public enum Palette {
public static let gitModified = Color(hex: 0xF0B476) public static let gitModified = Color(hex: 0xF0B476)
public static let gitNew = Color(hex: 0x8FBFB4) public static let gitNew = Color(hex: 0x8FBFB4)
public static let gitDeleted = Color(hex: 0xC3A6CE) public static let gitDeleted = Color(hex: 0xC3A6CE)
/// The one red in the app: a merge conflict must not blend in with the soft status colours.
public static let gitConflict = Color(hex: 0xE5534B)
/// Section header fills. /// Section header fills.
public static let gitStagedTint = Color(hex: 0xE8913A, alpha: 0.08) public static let gitStagedTint = Color(hex: 0xE8913A, alpha: 0.08)
public static let gitModifiedTint = Color(hex: 0xF0B476, alpha: 0.06) public static let gitModifiedTint = Color(hex: 0xF0B476, alpha: 0.06)
public static let gitNewTint = Color(hex: 0x8FBFB4, alpha: 0.06) public static let gitNewTint = Color(hex: 0x8FBFB4, alpha: 0.06)
public static let gitDeletedTint = Color(hex: 0xC3A6CE, alpha: 0.06) public static let gitDeletedTint = Color(hex: 0xC3A6CE, alpha: 0.06)
/// The fill of a conflicted row.
public static let gitConflictTint = Color(hex: 0xE5534B, alpha: 0.08)
/// The current branch row. /// The current branch row.
public static let gitCurrentBranch = Color(hex: 0xE8913A, alpha: 0.08) public static let gitCurrentBranch = Color(hex: 0xE8913A, alpha: 0.08)
/// Inline diff: line fills, the differing middle of a paired line, and the side with no line. /// Inline diff: line fills, the differing middle of a paired line, and the side with no line.
@@ -21,6 +21,18 @@ import Testing
#expect(state.fileDiffs["a.txt"]?.isTooBig == false) #expect(state.fileDiffs["a.txt"]?.isTooBig == false)
} }
@Test("a merge conflict is not counted as staged")
func conflictIsNotStaged() {
let state = GitDerivation.derive(GitSnapshot(branch: "main", ahead: 0, canPush: false, changes: [
change("both.txt", .unmerged, staged: false, "a\n", "<<<<<<< HEAD\na\n=======\nb\n>>>>>>> topic\n"),
change("ready.txt", .modified, staged: true, "1\n", "2\n"),
]))
#expect(state.stagedCount == 1)
#expect(state.unstagedCount == 1)
#expect(state.stagedPaths == ["ready.txt"])
#expect(state.unstagedPaths == ["both.txt"])
}
@Test("behind and upstream pass through from the snapshot") @Test("behind and upstream pass through from the snapshot")
func behindAndUpstream() { func behindAndUpstream() {
let state = GitDerivation.derive(GitSnapshot(branch: "main", ahead: 1, canPush: true, behind: 2, upstream: "origin/main", changes: [])) let state = GitDerivation.derive(GitSnapshot(branch: "main", ahead: 1, canPush: true, behind: 2, upstream: "origin/main", changes: []))
@@ -32,6 +32,14 @@ import Testing
#expect(GitSections.build([row("x", status, staged: false)]).map(\.kind) == [.changed]) #expect(GitSections.build([row("x", status, staged: false)]).map(\.kind) == [.changed])
} }
@Test("a merge conflict lands in changed, not staged", arguments: [("U", "U"), ("A", "A"), ("D", "D")] as [(Character, Character)])
func conflictIsChanged(index: Character, working: Character) {
let rows = Porcelain.classify(index: index, working: working).map { row("both.txt", $0.status, staged: $0.staged) }
let sections = GitSections.build(rows)
#expect(sections.map(\.kind) == [.changed])
#expect(sections[0].rows.map(\.status) == [.unmerged])
}
@Test("empty sections are left out") @Test("empty sections are left out")
func emptySections() { func emptySections() {
#expect(GitSections.build([]).isEmpty) #expect(GitSections.build([]).isEmpty)
@@ -29,9 +29,9 @@ import Testing
]) ])
} }
@Test("a merge conflict stays one row", arguments: [("U", "U"), ("A", "A"), ("D", "D"), ("U", "D")] as [(Character, Character)]) @Test("a merge conflict stays one unstaged U row", arguments: [("U", "U"), ("A", "A"), ("D", "D"), ("U", "D")] as [(Character, Character)])
func conflict(index: Character, working: Character) { func conflict(index: Character, working: Character) {
#expect(Porcelain.classify(index: index, working: working) == [GitRowSpec(status: .modified, staged: true)]) #expect(Porcelain.classify(index: index, working: working) == [GitRowSpec(status: .unmerged, staged: false)])
} }
// MARK: headers // MARK: headers
@@ -188,6 +188,12 @@ import Testing
#expect(git.executed == [["restore", "--", "src/b.php"]]) #expect(git.executed == [["restore", "--", "src/b.php"]])
} }
@Test func aConflictIsNotAStagedFile() async {
let (store, _, _) = await makeStore(snapshot([("both.txt", .unmerged, false), ("config/app.json", .modified, true)]))
#expect(store.stagedPathCount == 1)
#expect(store.fileStatuses["both.txt"] == .unmerged)
}
@Test func unstagingLogsRestoreStaged() async { @Test func unstagingLogsRestoreStaged() async {
let (store, git, _) = await makeStore() let (store, git, _) = await makeStore()
await store.unstage(["config/app.json"]) await store.unstage(["config/app.json"])
+2 -1
View File
@@ -78,6 +78,7 @@ Four colour-coded sections, each only when it has rows, in this order:
- A header shows the name, a count badge and the bulk actions; a click folds the section. - A header shows the name, a count badge and the bulk actions; a click folds the section.
- A row shows a chevron, a status badge (`A`, `M`, `D`, `R`, `U`), the path (folder dim, name bright, struck through when deleted), `+N` / `−N`, and its actions. A click expands an inline side-by-side diff of that row's half, last commit (or index) left and working tree right, with 3 lines of context around each change and longer unchanged runs folded into `⋯ N unchanged lines`. A file over the diff cap shows `Too big.`. - A row shows a chevron, a status badge (`A`, `M`, `D`, `R`, `U`), the path (folder dim, name bright, struck through when deleted), `+N` / `−N`, and its actions. A click expands an inline side-by-side diff of that row's half, last commit (or index) left and working tree right, with 3 lines of context around each change and longer unchanged runs folded into `⋯ N unchanged lines`. A file over the diff cap shows `Too big.`.
- A merge conflict (`UU`, `AA`, `DD`, or any `U`) is one unstaged `U` row in **changed**, never a staged row, and does not count toward `Commit N files`. Its badge is red, a red flag after the path has the tooltip `Merge conflict — resolve, then Stage`, and the row has a red left bar and a faint red fill. Its inline diff is the last commit against the file on disk, conflict markers included.
- A conflicted row offers only **Stage**: staging marks the resolution. - A conflicted row offers only **Stage**: staging marks the resolution.
- **Discard** reverts unstaged changes (modified and deleted files from the index, new files removed). **Discard** and **Delete** ask first (`git.confirmDiscard`, default on); **Restore** never asks. Stage and unstage never ask. - **Discard** reverts unstaged changes (modified and deleted files from the index, new files removed). **Discard** and **Delete** ask first (`git.confirmDiscard`, default on); **Restore** never asks. Stage and unstage never ask.
- **Right-click** a row: **Open in editor** (left out for a deleted file), **Pass on reference**, **Copy reference**. Open in editor and Pass on reference close the view first. - **Right-click** a row: **Open in editor** (left out for a deleted file), **Pass on reference**, **Copy reference**. Open in editor and Pass on reference close the view first.
@@ -103,7 +104,7 @@ The second column. A standard project file tree.
- An `EXPLORER` header. - An `EXPLORER` header.
- Folders expand and collapse. Files and folders use type icons. - Folders expand and collapse. Files and folders use type icons.
- Files that have changes show their status letter (`A`, `M`, `D`) on the right of their row. - Files that have changes show their status letter (`A`, `M`, `R`, `D`, `U`) on the right of their row; a conflict's `U` is red.
- **Left-click** a file opens it in the editor. The selected file is highlighted. - **Left-click** a file opens it in the editor. The selected file is highlighted.
- The tree hides paths matched by `files.exclude` and, when `files.followGitignore` is on, paths ignored by git. - The tree hides paths matched by `files.exclude` and, when `files.followGitignore` is on, paths ignored by git.
- **Right-click** a file offers **Copy reference**, which copies the file's project-relative path to the clipboard (the file-level form of the editor's Copy reference). - **Right-click** a file offers **Copy reference**, which copies the file's project-relative path to the clipboard (the file-level form of the editor's Copy reference).
+1
View File
@@ -56,6 +56,7 @@ A file with a git change shows a letter on the right of its row:
| `M` | Modified | teal | | `M` | Modified | teal |
| `R` | Renamed | teal | | `R` | Renamed | teal |
| `D` | Deleted | amber-deep | | `D` | Deleted | amber-deep |
| `U` | Merge conflict | red |
The letter is the file's overall status. When a file has both a staged and an unstaged change, the unstaged one wins. Folders do not show a letter. The letter is the file's overall status. When a file has both a staged and an unstaged change, the unstaged one wins. Folders do not show a letter.
+4 -4
View File
@@ -72,18 +72,18 @@ The changes sit in up to four sections, in this order. A section with no rows is
- Click a section header to fold or unfold it. - Click a section header to fold or unfold it.
- One file can show up twice: once in staged and once in another section. That happens when you staged a file and then edited it again. Each row stands for its own half. - One file can show up twice: once in staged and once in another section. That happens when you staged a file and then edited it again. Each row stands for its own half.
- A conflicted row (`U`) only offers **Stage**. Staging it marks the conflict as resolved. - A file with a merge conflict is never staged on its own: it sits in **changed** as a `U` row, and it does not count as a staged file. That row only offers **Stage**. Staging it marks the conflict as resolved.
### File rows ### File rows
Every row, left to right: Every row, left to right:
- A chevron: click anywhere on the row to show or hide its inline diff. - A chevron: click anywhere on the row to show or hide its inline diff.
- A status badge: `A` new, `M` modified, `D` deleted, `R` renamed, `U` conflict. Hover it for the word. - A status badge: `A` new, `M` modified, `D` deleted, `R` renamed, `U` merge conflict (red). Hover it for the word.
- The path: the folder dim, the file name bright. A deleted file is struck through. - The path: the folder dim, the file name bright. A deleted file is struck through. A conflicted file has a red flag after its name; hover it for `Merge conflict — resolve, then Stage`.
- The line counts: `+N` in teal and `−N` in light amber. - The line counts: `+N` in teal and `−N` in light amber.
- The row actions, from the table above. - The row actions, from the table above.
Staged rows carry an amber bar on their left edge. Staged rows carry an amber bar on their left edge. A conflicted row carries a red bar and a faint red fill. Red is used for nothing else in the app.
### Row actions ### Row actions
- **Stage** and **Unstage** run at once. They never ask. - **Stage** and **Unstage** run at once. They never ask.
+10850
View File
File diff suppressed because it is too large Load Diff