Skip to content

Commit 131957a

Browse files
committed
Recover prefetched server rewrites
Recover the previous remote tip from branch reflogs when an earlier fetch already advanced the tracking ref. Adopt equivalent server rebases while stopping sync before it overwrites non-equivalent remote commits, without blocking local-only rewrites.
1 parent f9c9bbb commit 131957a

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎cmd/rebase_test.go‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,7 @@ func TestRebase_BranchDiverged_NoFF(t *testing.T) {
14191419
}
14201420
return true, nil
14211421
}
1422+
mock.MergeBaseForkPointFn = func(string, string) (string, error) { return "b1-remote-sha", nil }
14221423
mock.UpdateBranchRefFn = func(string, string) error {
14231424
updateBranchRefCalls++
14241425
return nil
@@ -2262,6 +2263,7 @@ func TestIntegration_AdoptedBranchRebasesFromCommonAncestor(t *testing.T) {
22622263
type serverRebasedRepo struct {
22632264
dir string
22642265
gitDir string
2266+
serverDir string
22652267
oldImported string
22662268
newImported string
22672269
parentSHA string
@@ -2321,6 +2323,7 @@ func setupServerRebasedRepo(t *testing.T) serverRebasedRepo {
23212323
return serverRebasedRepo{
23222324
dir: localDir,
23232325
gitDir: gitDir,
2326+
serverDir: serverDir,
23242327
oldImported: oldImported,
23252328
newImported: newImported,
23262329
parentSHA: parentSHA,
@@ -2341,8 +2344,9 @@ func assertServerRebasedRepoAdopted(t *testing.T, repo serverRebasedRepo) {
23412344
assert.Equal(t, repo.newImported, sf.Stacks[0].Branches[1].Head)
23422345
}
23432346

2344-
func TestIntegration_RebaseAdoptsServerRebasedBranches(t *testing.T) {
2347+
func TestIntegration_RebaseAdoptsServerRebasedBranchesAfterPriorFetch(t *testing.T) {
23452348
repo := setupServerRebasedRepo(t)
2349+
issue250Git(t, repo.dir, "fetch", "origin")
23462350
withIssue250Repo(t, repo.dir)
23472351
cfg := issue250TestConfig(t)
23482352

@@ -2358,3 +2362,21 @@ func TestIntegration_SyncAdoptsServerRebasedBranches(t *testing.T) {
23582362
require.NoError(t, runSync(cfg, &syncOptions{remote: "origin"}))
23592363
assertServerRebasedRepoAdopted(t, repo)
23602364
}
2365+
2366+
func TestIntegration_SyncRejectsPrefetchedNonEquivalentRemoteRewrite(t *testing.T) {
2367+
repo := setupServerRebasedRepo(t)
2368+
issue250Git(t, repo.serverDir, "checkout", "imported")
2369+
issue250WriteFile(t, repo.serverDir, "server-only.txt", "server only\n")
2370+
issue250Git(t, repo.serverDir, "add", ".")
2371+
issue250Git(t, repo.serverDir, "commit", "-m", "server-only change")
2372+
issue250Git(t, repo.serverDir, "push", "origin", "imported")
2373+
issue250Git(t, repo.dir, "fetch", "origin")
2374+
2375+
withIssue250Repo(t, repo.dir)
2376+
cfg := issue250TestConfig(t)
2377+
require.ErrorIs(t, runSync(cfg, &syncOptions{remote: "origin"}), ErrSilent)
2378+
2379+
assert.Equal(t, repo.oldImported, issue250Git(t, repo.dir, "rev-parse", "imported"))
2380+
issue250Git(t, repo.serverDir, "fetch", "origin")
2381+
assert.Equal(t, "server only", issue250Git(t, repo.serverDir, "show", "origin/imported:server-only.txt"))
2382+
}

‎cmd/utils.go‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,9 +842,26 @@ func snapshotBranchTips(s *stack.Stack, remote string) map[string]branchTipSnaps
842842
continue
843843
}
844844
snapshot := branchTipSnapshot{localSHA: localSHA}
845-
if remoteSHA, err := git.RevParse(remote + "/" + br.Branch); err == nil {
845+
remoteRef := remote + "/" + br.Branch
846+
if remoteSHA, err := git.RevParse(remoteRef); err == nil {
846847
snapshot.remoteSHA = remoteSHA
847848
snapshot.hasRemote = true
849+
if remoteSHA != localSHA {
850+
localForkPoint, _ := git.MergeBaseForkPoint(br.Branch, remoteRef)
851+
if localForkPoint != remoteSHA {
852+
snapshot.hasRemote = false
853+
for _, candidate := range []string{localSHA, br.Head} {
854+
if candidate == "" {
855+
continue
856+
}
857+
if forkPoint, _ := git.MergeBaseForkPoint(remoteRef, candidate); forkPoint == candidate {
858+
snapshot.remoteSHA = candidate
859+
snapshot.hasRemote = true
860+
break
861+
}
862+
}
863+
}
864+
}
848865
}
849866
snapshots[br.Branch] = snapshot
850867
}

0 commit comments

Comments
 (0)