Skip to content

Commit 6b81fc3

Browse files
committed
Preserve SHAs in jaspr nav up/top when no amend has occurred
navigateUp and navigateToTop used to cherry-pick every entry above the cursor unconditionally, even when nothing had changed. The result: every commit visited got a new SHA, the stored stack drifted from the user's branch, and reviewing a stack via nav was effectively unusable. The replay path now checks the entry's git parent against HEAD: - Match -> plain checkout (SHA preserved). - Mismatch -> existing cherry-pick path (SHA rewritten, stored state updated). Comparing against the entry's actual git parent (rather than against the previous stored SHA in the in-memory stack) matters once a rewrite has occurred: the previous stored SHA is the rewritten one, but the next entry has not been touched yet and still carries the original commit, whose git parent is the unmodified prior SHA. Refactored the per-entry loop out of navigateUp/navigateToTop into a private replayEntries(stack, range) -> stack helper. Tests: - Renamed `up replays one commit via cherry-pick` and `top replays all remaining commits and restores branch` to reflect the new behavior, and added SHA-equality assertions to lock in preservation. - Added `up cherry-picks remaining commits when amend exists below the cursor` and a symmetric test for `top`. - Updated the comment in `amend during nav session updates SHA in stack`. Its outcome is unchanged, but the mechanism is now checkout, not cherry-pick. Closes git-jaspr-2yz. commit-id: I4f05e5b7
1 parent 94d8ad5 commit 6b81fc3

2 files changed

Lines changed: 137 additions & 24 deletions

File tree

‎git-jaspr/src/main/kotlin/sims/michael/gitjaspr/GitJaspr.kt‎

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2643,8 +2643,8 @@ class GitJaspr(
26432643
}
26442644

26452645
/**
2646-
* Navigate up N commits by cherry-picking from the saved stack above the current HEAD. If all
2647-
* remaining commits are replayed, restores the source branch and ends the session.
2646+
* Navigate up N commits from the saved stack above the current HEAD. If all remaining commits
2647+
* are replayed, restores the source branch and ends the session.
26482648
*
26492649
* @return true if navigation was performed, false if there was no active session (caller can
26502650
* render a friendly message rather than treating it as an error)
@@ -2658,13 +2658,8 @@ class GitJaspr(
26582658
"Cannot move up $n commit(s) — only $aboveCount commit(s) above current position."
26592659
}
26602660

2661-
val updatedStack = state.stack.toMutableList()
2662-
for (i in 1..n) {
2663-
val entry = updatedStack[state.cursorIndex + i]
2664-
val newCommit =
2665-
gitClient.cherryPick(gitClient.log(entry.sha, 1).single(), commitIdentOverride)
2666-
updatedStack[state.cursorIndex + i] = entry.copy(sha = newCommit.hash)
2667-
}
2661+
val updatedStack =
2662+
replayEntries(state.stack, (state.cursorIndex + 1)..(state.cursorIndex + n))
26682663

26692664
val newCursor = state.cursorIndex + n
26702665
if (newCursor == updatedStack.lastIndex) { // We've replayed all commits, end the session
@@ -2686,18 +2681,41 @@ class GitJaspr(
26862681
val aboveCount = state.stack.size - state.cursorIndex - 1
26872682
require(aboveCount > 0) { "Already at the top of the stack." }
26882683

2689-
val updatedStack = state.stack.toMutableList()
2690-
for (i in (state.cursorIndex + 1)..updatedStack.lastIndex) {
2691-
val entry = updatedStack[i]
2692-
val newCommit =
2693-
gitClient.cherryPick(gitClient.log(entry.sha, 1).single(), commitIdentOverride)
2694-
updatedStack[i] = entry.copy(sha = newCommit.hash)
2695-
}
2684+
val updatedStack =
2685+
replayEntries(state.stack, (state.cursorIndex + 1)..state.stack.lastIndex)
26962686

26972687
endNavSession(state.copy(stack = updatedStack))
26982688
return true
26992689
}
27002690

2691+
/**
2692+
* Move HEAD up through the entries of [stack] in [range], returning the (possibly updated)
2693+
* stack. For each entry, if its git parent SHA matches the current HEAD, the entry is checked
2694+
* out as-is (preserving its SHA). Otherwise it is cherry-picked onto HEAD and the rewritten SHA
2695+
* replaces the entry in the returned list.
2696+
*
2697+
* Comparing against the entry's git parent (rather than against the previous entry's SHA in the
2698+
* returned list) matters once a cherry-pick has happened earlier in the replay: the previous
2699+
* entry's SHA has been overwritten with the rewritten one, but the next entry (not yet touched)
2700+
* still carries the original commit whose actual git parent is the unmodified previous SHA.
2701+
*/
2702+
private fun replayEntries(stack: List<StackEntry>, range: IntRange): List<StackEntry> {
2703+
val result = stack.toMutableList()
2704+
for (i in range) {
2705+
val entry = result[i]
2706+
val entryCommit = gitClient.log(entry.sha, 1).single()
2707+
val parentSha = gitClient.getParents(entryCommit).singleOrNull()?.hash
2708+
val headSha = gitClient.log(GitClient.HEAD, 1).single().hash
2709+
if (parentSha == headSha) {
2710+
gitClient.checkout(entry.sha)
2711+
} else {
2712+
val newCommit = gitClient.cherryPick(entryCommit, commitIdentOverride)
2713+
result[i] = entry.copy(sha = newCommit.hash)
2714+
}
2715+
}
2716+
return result
2717+
}
2718+
27012719
/**
27022720
* Build the initial [NavState] from the current branch. Called on the first nav down/bottom
27032721
* when no session exists yet.

‎git-jaspr/src/test/kotlin/sims/michael/gitjaspr/GitJasprTest.kt‎

Lines changed: 103 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import kotlin.test.assertContains
55
import kotlin.test.assertEquals
66
import kotlin.test.assertFailsWith
77
import kotlin.test.assertFalse
8+
import kotlin.test.assertNotEquals
89
import kotlin.test.assertNotNull
910
import kotlin.test.assertNull
1011
import kotlinx.coroutines.delay
@@ -326,7 +327,7 @@ interface GitJasprTest {
326327
}
327328

328329
@Test
329-
fun `up replays one commit via cherry-pick`() {
330+
fun `up checks out next existing commit when no amend has occurred`() {
330331
withTestSetup(useFakeRemote) {
331332
createCommitsFrom(
332333
testCase {
@@ -350,15 +351,15 @@ interface GitJasprTest {
350351

351352
gitJaspr.navigateUp(1)
352353

353-
// Should still be detached, with "two" replayed on top
354+
// HEAD should be the original "two" SHA -- a plain checkout, not a cherry-pick
354355
assertTrue(localGit.isHeadDetached())
355-
assertEquals("two", localGit.log(GitClient.HEAD, 1).single().shortMessage)
356+
assertEquals(stack[1].hash, localGit.log(GitClient.HEAD, 1).single().hash)
356357
assertNotNull(gitJaspr.readNavState())
357358
}
358359
}
359360

360361
@Test
361-
fun `top replays all remaining commits and restores branch`() {
362+
fun `top checks out remaining commits when no amend has occurred`() {
362363
withTestSetup(useFakeRemote) {
363364
createCommitsFrom(
364365
testCase {
@@ -374,6 +375,8 @@ interface GitJasprTest {
374375
}
375376
)
376377
localGit.fetch(remoteName)
378+
val originalStack =
379+
localGit.getLocalCommitStack(remoteName, GitClient.HEAD, DEFAULT_TARGET_REF)
377380

378381
// Navigate to bottom, then back to top
379382
gitJaspr.navigateToBottom(DEFAULT_TARGET_REF)
@@ -384,11 +387,11 @@ interface GitJasprTest {
384387
assertEquals("development", localGit.getCurrentBranchName())
385388
assertNull(gitJaspr.readNavState())
386389

387-
// Stack should still have 3 commits with the same messages
390+
// Stack should still have the same 3 commits at the SAME SHAs (plain checkouts,
391+
// not cherry-picks) since nothing was amended.
388392
val newStack =
389393
localGit.getLocalCommitStack(remoteName, GitClient.HEAD, DEFAULT_TARGET_REF)
390-
assertEquals(3, newStack.size)
391-
assertEquals(listOf("one", "two", "three"), newStack.map(Commit::shortMessage))
394+
assertEquals(originalStack.map(Commit::hash), newStack.map(Commit::hash))
392395
}
393396
}
394397

@@ -550,7 +553,8 @@ interface GitJasprTest {
550553
gitJaspr.navigateDown(DEFAULT_TARGET_REF, 1)
551554
assertEquals("A", localGit.log(GitClient.HEAD, 1).single().shortMessage)
552555

553-
// Nav up 1: should cherry-pick the amended B, not the original
556+
// Nav up 1: should check out the amended B' (its git parent A still matches HEAD,
557+
// so no cherry-pick is required)
554558
gitJaspr.navigateUp(1, DEFAULT_TARGET_REF)
555559
val replayedB = localGit.log(GitClient.HEAD, 1).single()
556560
assertEquals("B", replayedB.shortMessage)
@@ -560,6 +564,97 @@ interface GitJasprTest {
560564
}
561565
}
562566

567+
@Nav
568+
@Test
569+
fun `up cherry-picks remaining commits when amend exists below the cursor`() {
570+
withTestSetup(useFakeRemote) {
571+
createCommitsFrom(
572+
testCase {
573+
repository {
574+
commit { title = "A" }
575+
commit { title = "B" }
576+
commit {
577+
title = "C"
578+
localRefs += "development"
579+
}
580+
}
581+
checkout = "development"
582+
}
583+
)
584+
val originalStack =
585+
localGit.getLocalCommitStack(remoteName, GitClient.HEAD, DEFAULT_TARGET_REF)
586+
587+
// Bottom -> HEAD at A
588+
gitJaspr.navigateToBottom(DEFAULT_TARGET_REF)
589+
assertEquals(originalStack[0].hash, localGit.log(GitClient.HEAD, 1).single().hash)
590+
591+
// Amend A: changes its SHA, so subsequent entries' stored parents (the original A)
592+
// no longer match HEAD. Replay must cherry-pick B and C onto the amended chain.
593+
localRepo.resolve("amend.txt").writeText("amended\n")
594+
localGit.add("amend.txt")
595+
localGit.commit("A", footerLines = mapOf(COMMIT_ID_LABEL to "A"), amend = true)
596+
597+
// Up 2 reaches the top and ends the session, restoring "development" to the new tip.
598+
gitJaspr.navigateUp(2, DEFAULT_TARGET_REF)
599+
600+
val newStack =
601+
localGit.getLocalCommitStack(remoteName, GitClient.HEAD, DEFAULT_TARGET_REF)
602+
assertEquals(listOf("A", "B", "C"), newStack.map(Commit::shortMessage))
603+
for (i in originalStack.indices) {
604+
assertNotEquals(
605+
originalStack[i].hash,
606+
newStack[i].hash,
607+
"Entry $i should have a rewritten SHA",
608+
)
609+
}
610+
// Amend propagated through the cherry-picked B and C.
611+
assertTrue(localRepo.resolve("amend.txt").readText().contains("amended"))
612+
}
613+
}
614+
615+
@Nav
616+
@Test
617+
fun `top cherry-picks remaining commits when amend exists below the cursor`() {
618+
withTestSetup(useFakeRemote) {
619+
createCommitsFrom(
620+
testCase {
621+
repository {
622+
commit { title = "A" }
623+
commit { title = "B" }
624+
commit {
625+
title = "C"
626+
localRefs += "development"
627+
}
628+
}
629+
checkout = "development"
630+
}
631+
)
632+
val originalStack =
633+
localGit.getLocalCommitStack(remoteName, GitClient.HEAD, DEFAULT_TARGET_REF)
634+
635+
gitJaspr.navigateToBottom(DEFAULT_TARGET_REF)
636+
637+
// Amend A
638+
localRepo.resolve("amend.txt").writeText("amended\n")
639+
localGit.add("amend.txt")
640+
localGit.commit("A", footerLines = mapOf(COMMIT_ID_LABEL to "A"), amend = true)
641+
642+
gitJaspr.navigateToTop(DEFAULT_TARGET_REF)
643+
644+
val newStack =
645+
localGit.getLocalCommitStack(remoteName, GitClient.HEAD, DEFAULT_TARGET_REF)
646+
assertEquals(listOf("A", "B", "C"), newStack.map(Commit::shortMessage))
647+
for (i in originalStack.indices) {
648+
assertNotEquals(
649+
originalStack[i].hash,
650+
newStack[i].hash,
651+
"Entry $i should have a rewritten SHA",
652+
)
653+
}
654+
assertTrue(localRepo.resolve("amend.txt").readText().contains("amended"))
655+
}
656+
}
657+
563658
@Nav
564659
@Test
565660
fun `drop during nav session removes commit from stack`() {

0 commit comments

Comments
 (0)