adds claude-generated commit messages and a recent commits panel to the git view - adds log tailing in the explorer, branch merging and a clean up button

This commit is contained in:
2026-11-04 10:07:52 +01:00
parent 600d4874d2
commit cc6651c08a
49 changed files with 1944 additions and 180 deletions
@@ -0,0 +1,26 @@
import CoreGraphics
@testable import HelderUI
import Testing
@Suite struct GitStripLayoutTests {
@Test func aNarrowStripSplitsEvenlyWithoutTheCommitList() {
let layout = GitStripLayout(width: 1201)
#expect(layout.console == 600)
#expect(layout.branches == 600)
#expect(layout.commits == nil)
}
@Test func bothPanelsStopAt820AndASmallRestStaysEmpty() {
let layout = GitStripLayout(width: 1800)
#expect(layout.console == GitMetrics.consoleMaxWidth)
#expect(layout.branches == GitMetrics.branchesMaxWidth)
#expect(layout.commits == nil)
}
@Test func theCommitListTakesTheRestFrom260() {
let edge = GitMetrics.consoleMaxWidth + GitMetrics.branchesMaxWidth + 2 + GitMetrics.commitsMinWidth
#expect(GitStripLayout(width: edge - 1).commits == nil)
#expect(GitStripLayout(width: edge).commits == GitMetrics.commitsMinWidth)
#expect(GitStripLayout(width: 2560).commits == CGFloat(2560 - 1640 - 2))
}
}
@@ -274,6 +274,73 @@ import Testing
#expect(texts(store) == ["$ git restore -- src/b.php src/c.php", "$ git clean -f -- new.txt", "Removing new.txt"])
}
// MARK: Clean up
@Test func cleanUpRevertsTheWorkingTreeAndRemovesNewFiles() async {
let (store, git, recorder) = await makeStore()
#expect(store.cleanUpPathCount == 5)
#expect(store.canCleanUp)
await store.cleanUp()
#expect(git.executed == [["restore", "--worktree", "--", "."], ["clean", "-fd"]])
#expect(recorder.toastTitles == ["Cleaned up 5 files"])
}
@Test func cleanUpRunsOnlyWhatTheTreeNeeds() async {
let (onlyNew, newGit, _) = await makeStore(snapshot([("new.txt", .added, false), ("a.php", .modified, true)]))
await onlyNew.cleanUp()
#expect(newGit.executed == [["clean", "-fd"]])
let (onlyTracked, trackedGit, recorder) = await makeStore(snapshot([("a.php", .modified, false)]))
await onlyTracked.cleanUp()
#expect(trackedGit.executed == [["restore", "--worktree", "--", "."]])
#expect(recorder.toastTitles == ["Cleaned up 1 file"])
}
@Test func cleanUpDoesNothingWhenOnlyStagedChangesAreLeft() async {
let (store, git, recorder) = await makeStore(snapshot([("a.php", .modified, true), ("b.php", .added, true)]))
#expect(!store.canCleanUp)
#expect(store.confirmation(for: .gitCleanUp) == nil)
await store.cleanUp()
#expect(git.executed.isEmpty)
#expect(recorder.toastTitles.isEmpty)
}
@Test func cleanUpIsRefusedDuringAConflict() async {
let (store, git, _) = await makeStore(snapshot([("both.txt", .unmerged, false), ("src/b.php", .modified, false)]))
#expect(!store.canCleanUp)
await store.cleanUp()
#expect(git.executed.isEmpty)
}
@Test func cleanUpIsRefusedWhileAPushRuns() async {
let (store, git, _) = await makeStore()
git.setRun(FakeGitRun(delay: .milliseconds(300)), forCommand: "push")
let push = Task { await store.push() }
#expect(await eventually { store.busy == .push })
#expect(!store.canCleanUp)
await store.cleanUp()
await push.value
#expect(git.executed == [["push"]])
}
@Test func aFailedCleanUpToastsGitsError() async {
let (store, git, recorder) = await makeStore(snapshot([("new.txt", .added, false)]))
git.setRun(FakeGitRun(exitCode: 1, lines: [("warning: failed to remove new.txt: Permission denied", true)]), forCommand: "clean")
await store.cleanUp()
#expect(recorder.toastTitles == ["Clean up failed"])
#expect(recorder.toasts.first?.1 == "warning: failed to remove new.txt: Permission denied")
}
@Test func cleanUpAlwaysAsksWithTheCount() async {
let (store, _, _) = await makeStore()
store.config.confirmDiscard = false
let request = store.confirmation(for: .gitCleanUp)
#expect(request?.title == "Clean up 5 files?")
#expect(request?.body == "New files are removed from disk and unstaged changes are reverted. Staged changes stay. This cannot be undone.")
#expect(request?.confirmLabel == "Clean up")
#expect(request?.action == .gitCleanUp)
}
// MARK: Commit
@Test func commitAlwaysRunsGitEvenWithNothingStagedOrNoMessage() async {
@@ -350,6 +417,54 @@ import Testing
#expect(failingGit.executed == [["commit", "-m", "ship it"]])
}
@Test func stageAllAndCommitStagesEveryUnstagedPathThenCommits() async {
let (store, git, recorder) = await makeStore()
#expect(store.canStageAllAndCommit)
store.setCommitMessage("ship it")
await store.stageAllAndCommit()
#expect(git.executed == [
["add", "--", "src/b.php", "README.md", "src/a.php", "src/c.php", "new.txt"],
["commit", "-m", "ship it"],
])
#expect(store.commitMessage.isEmpty)
#expect(recorder.toastTitles.first?.hasPrefix("Committed") == true)
}
@Test func stageAllAndCommitWithoutAMessageRunsNothingAndFocusesTheMessage() async {
let (store, git, recorder) = await makeStore()
var focused = 0
store.focusMessage = { focused += 1 }
store.setCommitMessage(" ")
await store.stageAllAndCommit()
#expect(git.executed.isEmpty)
#expect(focused == 1)
#expect(recorder.toastTitles.isEmpty)
}
@Test func stageAllAndCommitIsRefusedDuringAConflict() async {
let (store, git, _) = await makeStore(snapshot([("both.txt", .unmerged, false), ("src/b.php", .modified, false)]))
#expect(!store.canStageAllAndCommit)
store.setCommitMessage("ship it")
await store.stageAllAndCommit()
#expect(git.executed.isEmpty)
}
@Test func stageAllAndCommitIsOffWithoutChanges() async {
let (store, git, _) = await makeStore(snapshot([]))
#expect(!store.canStageAllAndCommit)
store.setCommitMessage("ship it")
await store.stageAllAndCommit()
#expect(git.executed.isEmpty)
}
@Test func stageAllAndCommitDoesNotCommitWhenStagingFails() async {
let (store, git, _) = await makeStore()
git.setRun(FakeGitRun(exitCode: 128, lines: [("fatal: Unable to create '.git/index.lock': File exists.", true)]), forCommand: "add")
store.setCommitMessage("ship it")
await store.stageAllAndCommit()
#expect(git.executed.map(\.first) == ["add"])
}
// MARK: Push and pull
@Test func aPushWithoutUpstreamRetriesWithTheBranch() async {
@@ -441,6 +556,60 @@ import Testing
#expect(git.executed.isEmpty)
}
@Test func mergingIsRefusedWhileDirtyWithoutAskingGit() async {
let (store, git, recorder) = await makeStore()
#expect(await !store.mergeBranch("feature"))
#expect(git.executed.isEmpty)
#expect(recorder.toastTitles == ["— Commit or discard 6 files before you merge."])
}
@Test func aMergeRunsNoEditIntoTheCurrentBranchAndToasts() async {
let (store, git, recorder) = await makeStore(snapshot([]))
#expect(await store.mergeBranch("feature"))
#expect(git.executed == [["merge", "--no-edit", "feature"]])
#expect(texts(store).first == "$ git merge --no-edit feature")
#expect(recorder.toastTitles == ["Merged feature into main"])
}
@Test func theCurrentBranchIsNoMerge() async {
let (store, git, _) = await makeStore(snapshot([]))
#expect(await !store.mergeBranch("main"))
#expect(git.executed.isEmpty)
}
@Test func aMergeOnACleanTreeAsksFirst() async {
let (store, git, _) = await makeStore(snapshot([]))
let request = store.confirmation(for: .mergeBranch("feature"))
#expect(request?.title == "Merge feature into main?")
#expect(request?.body == "Git merges the commits of feature into main. A conflict stops the merge for you to resolve.")
#expect(request?.confirmLabel == "Merge")
#expect(request?.action == .mergeBranch("feature"))
#expect(git.executed.isEmpty)
}
@Test func aMergeThatWouldBeRefusedAsksNothing() async {
let (dirty, _, _) = await makeStore()
#expect(dirty.confirmation(for: .mergeBranch("feature")) == nil)
let (clean, _, _) = await makeStore(snapshot([]))
#expect(clean.confirmation(for: .mergeBranch("main")) == nil)
}
@Test func aMergeThatLeavesConflictsSaysSo() async {
let (store, git, recorder) = await makeStore(snapshot([]))
git.setRun(FakeGitRun(exitCode: 1, lines: [("CONFLICT (content): Merge conflict in a.txt", false)]), forCommand: "merge")
git.set(snapshot([("a.txt", .unmerged, false)]))
#expect(await !store.mergeBranch("feature"))
#expect(recorder.toastTitles == ["Merge conflict — resolve, then commit"])
}
@Test func aFailedMergeWithoutConflictsShowsGitsError() async {
let (store, git, recorder) = await makeStore(snapshot([]))
git.setRun(FakeGitRun(exitCode: 1, lines: [("merge: nope - not something we can merge", true)]), forCommand: "merge")
#expect(await !store.mergeBranch("nope"))
#expect(recorder.toasts.last?.0 == "Merge failed")
#expect(recorder.toasts.last?.1 == "merge: nope - not something we can merge")
}
// MARK: Console
@Test func clearEmptiesTheLogWithoutGit() async {
@@ -505,6 +674,23 @@ import Testing
#expect(texts(store).first == "$ git fetch origin")
}
@Test func openingTheViewAndEveryCommandReadTheRecentCommits() async {
let (store, git, _) = await makeStore()
let first = [GitCommitSummary(hash: "a1b2c3d", subject: "first", author: "Jonathan",
date: Date(timeIntervalSince1970: 1_700_000_000))]
git.setRecentCommits(first)
#expect(store.recentCommits.isEmpty)
await store.viewOpened()
#expect(store.recentCommits == first)
let second = [GitCommitSummary(hash: "b2c3d4e", subject: "second", author: "Jonathan",
date: Date(timeIntervalSince1970: 1_700_000_100))] + first
git.setRecentCommits(second)
await store.stage(["README.md"])
#expect(store.recentCommits == second)
#expect(git.recentCommitsReads == 2)
}
@Test func anUpstreamOnOriginIsEnoughToFetch() async {
let tracked = GitSnapshot(branch: "main", ahead: 1, canPush: true, upstream: "origin/main", changes: [])
let (store, git, _) = await makeStore(tracked)
@@ -536,3 +722,81 @@ import Testing
#expect(!store.isPolling)
}
}
@MainActor
@Suite struct SourceControlGenerateMessageTests {
private func makeStore(_ rows: [(String, GitStatus, Bool)] = [("a.txt", .modified, true)])
async -> (SourceControlStore, FakeGit, FakeCommitMessages, Recorder, () -> Int) {
let git = FakeGit(snapshot(rows))
let generator = FakeCommitMessages()
let store = SourceControlStore(git: git, commitMessages: generator, derive: TestDerivation.derive)
let recorder = Recorder()
store.notify = recorder.notify
var focused = 0
store.focusMessage = { focused += 1 }
await store.refresh()
return (store, git, generator, recorder, { focused })
}
@Test func successReplacesTheMessageAndFocusesTheField() async {
let (store, git, generator, recorder, focused) = await makeStore()
git.setStagedDiff(StagedDiff(stat: " a.txt | 1 +\n", patch: "+the change\n"))
generator.setResult(.success("Adds the change"))
store.setCommitMessage("old draft")
await store.generateCommitMessage()
#expect(store.commitMessage == "adds the change")
#expect(focused() == 1)
#expect(recorder.toasts.isEmpty)
#expect(!store.isGeneratingMessage)
let prompt = generator.prompts.first ?? ""
#expect(prompt.contains(" a.txt | 1 +"))
#expect(prompt.contains("+the change"))
}
@Test func failureToastsWithTheDetailAndKeepsTheDraft() async {
let (store, _, generator, recorder, focused) = await makeStore()
generator.setResult(.failure(.notFound))
store.setCommitMessage("my draft")
await store.generateCommitMessage()
#expect(store.commitMessage == "my draft")
#expect(recorder.toasts.map(\.0) == ["Couldn't generate a commit message"])
#expect(recorder.toasts.first?.1 == "claude not found")
#expect(focused() == 0)
#expect(!store.isGeneratingMessage)
}
@Test func nothingStagedDoesNothing() async {
let (store, _, generator, recorder, _) = await makeStore([("a.txt", .modified, false)])
#expect(!store.canGenerateMessage)
await store.generateCommitMessage()
#expect(generator.prompts.isEmpty)
#expect(recorder.toasts.isEmpty)
#expect(store.commitMessage == "")
}
@Test func anEmptyStagedDiffToastsWithoutAskingClaude() async {
let (store, git, generator, recorder, _) = await makeStore()
git.setStagedDiff(StagedDiff(stat: "", patch: ""))
await store.generateCommitMessage()
#expect(generator.prompts.isEmpty)
#expect(recorder.toasts.map(\.0) == ["Couldn't generate a commit message"])
}
@Test func aClickWhileGeneratingIsIgnored() async {
let (store, _, generator, _, _) = await makeStore()
generator.setDelay(.milliseconds(200))
let first = Task { await store.generateCommitMessage() }
#expect(await eventually { store.isGeneratingMessage })
#expect(!store.canGenerateMessage)
await store.generateCommitMessage()
await first.value
#expect(generator.prompts.count == 1)
#expect(!store.isGeneratingMessage)
}
@Test func withoutAGeneratorTheButtonStaysOff() async {
let store = SourceControlStore(git: FakeGit(snapshot([("a.txt", .modified, true)])), derive: TestDerivation.derive)
await store.refresh()
#expect(!store.canGenerateMessage)
}
}