Adopt equivalent server-rebased branches - #344
Open
skarim wants to merge 3 commits into
Open
Conversation
Snapshot tracking refs before fetch so rebase and sync can distinguish remote rewrites from local work. Adopt only correctly stacked, same-order range-diff-equivalent commits, and stop safely when both sides changed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 30bcbf2a-5ef1-4bdb-a0fc-618294ae8ded
Contributor
There was a problem hiding this comment.
Pull request overview
Adds safe adoption of equivalent server-rebased stack branches.
Changes:
- Snapshots branch tips and validates rewritten histories with
git range-diff. - Integrates adoption into
rebaseandsync, including rollback handling. - Adds tests and user documentation.
Show a summary per file
| File | Description |
|---|---|
cmd/utils.go |
Implements snapshotting and remote-tip adoption. |
cmd/utils_test.go |
Tests adoption and rollback scenarios. |
cmd/rebase.go |
Adopts remote rewrites during rebase. |
cmd/rebase_test.go |
Adds end-to-end rebase and sync coverage. |
cmd/sync.go |
Integrates adoption into synchronization. |
internal/git/git.go |
Exposes range equivalence checking. |
internal/git/gitops.go |
Implements git range-diff validation. |
internal/git/gitops_test.go |
Tests equivalence, ordering, and merges. |
internal/git/mock_ops.go |
Adds range-diff mock support. |
README.md |
Documents adoption behavior. |
docs/src/content/docs/reference/cli.md |
Updates CLI reference. |
docs/src/content/docs/guides/ui.md |
Documents server-rebase workflow. |
skills/gh-stack/SKILL.md |
Updates agent command guidance. |
Review details
Comments suppressed due to low confidence (2)
cmd/utils.go:998
- The earlier SHA comparison is not atomic with these destructive updates. Another Git process can advance a branch after it is checked at line 878, and
ResetHard/branch -fwill then overwrite that new local work even though the adoption path promises to stop when the local side changed. Update refs with an expected-old SHA (ideally as one ref transaction), and guard the checked-out branch before resetting its worktree.
if update.branch == currentBranch {
updateErr = git.ResetHard(update.remoteRef)
} else {
updateErr = git.UpdateBranchRef(update.branch, update.remoteSHA)
}
cmd/utils.go:1005
- Rollback failures are discarded here. If restoring an already-adopted branch fails, the command reports only the later update failure and leaves the stack partially rewritten without telling the user which ref needs recovery. Collect and include every rollback error in the returned error (or make the updates transactional).
if previous.branch == currentBranch {
_ = git.ResetHard(previous.localSHA)
} else {
_ = git.UpdateBranchRef(previous.branch, previous.localSHA)
- Files reviewed: 13/13 changed files
- Comments generated: 1
- Review effort level: Medium
Only require a clean worktree when adopting the checked-out branch with reset --hard. Non-current branch updates remain safe with unrelated working-tree changes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 30bcbf2a-5ef1-4bdb-a0fc-618294ae8ded
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
After a stack was rebased on GitHub,
gh stack rebaseandgh stack syncfetched the rewritten branch tips but would not adopt them because the local branches had diverged from their remote-tracking refs. The commands then tried to rebase or push the stale local branches, often failing even though the server-side rebase had completed successfully.This change snapshots branch tips before fetching and adopts a server-side rewrite only when the local branch was unchanged, the remote stack is still properly ordered, and
git range-diffconfirms the same commits in the same order. If both sides changed, commits differ, or the history contains merges, the command stops instead of overwriting either side.Both
rebaseandsyncnow update local branches and stack metadata directly when a safe equivalent server rewrite is detected.Testing
go vet ./...go test -race -count=1 ./...rebaseandsyncStack created with GitHub Stacks CLI • Give Feedback 💬