Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions cmd/rebase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2843,6 +2843,57 @@ func TestIntegration_AmendedParentWithoutForkPointFailsSafely(t *testing.T) {
assert.Equal(t, childBefore, issue250Git(t, repo.dir, "rev-parse", "child"))
}

func TestRebase_ImportedStackAfterParentAdvance(t *testing.T) {
repo := setupIssue512Repo(t)
issue250Git(t, repo.dir, "checkout", "parent")
issue250WriteFile(t, repo.dir, "parent-extra.txt", "new parent content\n")
issue250Git(t, repo.dir, "add", "parent-extra.txt")
issue250Git(t, repo.dir, "commit", "-m", "parent extra")
issue250Git(t, repo.dir, "push", "origin", "parent")
newParent := issue250Git(t, repo.dir, "rev-parse", "parent")

prs := []*github.PullRequest{
{Number: 10, HeadRefName: "parent", BaseRefName: "main", BaseRefOID: repo.main, State: "OPEN"},
{Number: 11, HeadRefName: "child", BaseRefName: "parent", BaseRefOID: repo.parent, State: "OPEN"},
}
remote := github.RemoteStack{
ID: 1, Number: 1, Base: github.RemoteStackBase{Ref: "main"}, PullRequests: []int{10, 11},
}
cfg := issue250TestConfig(t)
cfg.GitHubClientOverride = &github.MockClient{
ListStacksFn: func() ([]github.RemoteStack, error) {
return []github.RemoteStack{remote}, nil
},
GetStackFn: func(int) (*github.RemoteStack, error) { return &remote, nil },
FindPRByNumberFn: func(number int) (*github.PullRequest, error) {
return prs[number-10], nil
},
}
withIssue250Repo(t, repo.writer)
gitDir := filepath.Join(repo.writer, ".git")
sf, err := stack.Load(gitDir)
require.NoError(t, err)
s, err := importRemoteStack(cfg, sf, gitDir, "origin", "main", prs, "1", 1)
require.NoError(t, err)
require.Equal(t, newParent, s.Branches[1].Base)
require.NoError(t, stack.Save(gitDir, sf))
issue250Git(t, repo.writer, "checkout", "child")
forkPoint, err := git.MergeBaseForkPoint("parent", "child")
require.NoError(t, err)
require.Empty(t, forkPoint)

require.NoError(t, runRebase(cfg, &rebaseOptions{remote: "origin"}))

assert.Equal(t, newParent, issue250Git(t, repo.writer, "merge-base", "parent", "child"))
assert.Equal(t, "child only", issue250Git(t, repo.writer, "log", "--format=%s", "parent..child"))
assert.Equal(t, "new parent content", issue250Git(t, repo.writer, "show", "child:parent-extra.txt"))
assert.Equal(t, "child", issue250Git(t, repo.writer, "show", "child:child.txt"))
sf, err = stack.Load(gitDir)
require.NoError(t, err)
assert.Equal(t, newParent, sf.Stacks[0].Branches[1].Base)
assert.NoFileExists(t, filepath.Join(gitDir, rebaseStateFile))
}

func TestIntegration_AdoptedBranchRebasesFromCommonAncestor(t *testing.T) {
remoteDir := filepath.Join(t.TempDir(), "remote.git")
cloneDir := filepath.Join(t.TempDir(), "clone")
Expand Down
20 changes: 16 additions & 4 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ func syncStackPRs(cfg *config.Config, s *stack.Stack) map[string]*github.PRDetai
// Each branch is processed concurrently; results are collected and applied sequentially.
type branchResult struct {
index int
pullRequest *stack.PullRequestRef
pullRequest *github.PullRequest
queued bool
details *github.PRDetails
skip bool // true means keep existing data, don't update
Expand Down Expand Up @@ -603,7 +603,7 @@ func syncStackPRs(cfg *config.Config, s *stack.Stack) map[string]*github.PRDetai
}
if pr != nil && pr.State != "CLOSED" {
// PR is open or merged — keep it
res.pullRequest = pullRequestRef(pr, branch.PullRequest)
res.pullRequest = pr
res.queued = pr.IsQueued()
res.details = prDetailsFromPR(pr)
results[idx] = res
Expand All @@ -622,7 +622,7 @@ func syncStackPRs(cfg *config.Config, s *stack.Stack) map[string]*github.PRDetai
results[idx] = res
return
}
res.pullRequest = pullRequestRef(pr, nil)
res.pullRequest = pr
res.queued = pr.IsQueued()
// FindPRForBranch only returns OPEN PRs
res.details = &github.PRDetails{
Expand Down Expand Up @@ -651,8 +651,9 @@ func syncStackPRs(cfg *config.Config, s *stack.Stack) map[string]*github.PRDetai
}
b := &s.Branches[res.index]
if res.pullRequest != nil {
b.PullRequest = res.pullRequest
b.PullRequest = pullRequestRef(res.pullRequest, b.PullRequest)
b.Queued = res.queued
updateBaseFromPR(s, b, res.pullRequest)
} else if !b.IsMerged() {
// Clear if we didn't find anything (and original was cleared during discovery)
if b.PullRequest != nil && res.pullRequest == nil {
Expand Down Expand Up @@ -694,6 +695,16 @@ func pullRequestRef(pr *github.PullRequest, previous *stack.PullRequestRef) *sta
return ref
}

func updateBaseFromPR(s *stack.Stack, b *stack.BranchRef, pr *github.PullRequest) {
if b.IsMerged() || pr.BaseRefOID == "" || pr.BaseRefName != s.ActiveBaseBranch(b.Branch) {
return
}
// A PR retains its old parent tip even when a fresh clone has no reflog for it.
if canUpdateBase(pr.BaseRefOID, b.Branch, b.Base) {
b.Base = pr.BaseRefOID
Comment on lines +703 to +704
Comment on lines +703 to +704
}
}

// prDetailsFromPR builds PRDetails from a PullRequest returned by FindPRByNumber.
func prDetailsFromPR(pr *github.PullRequest) *github.PRDetails {
if pr == nil {
Expand Down Expand Up @@ -854,6 +865,7 @@ func syncStackPRsFromRemote(client github.ClientOps, s *stack.Stack) (map[string
}
b.PullRequest = pullRequestRef(pr, b.PullRequest)
b.Queued = pr.IsQueued()
updateBaseFromPR(s, b, pr)
details[b.Branch] = prDetailsFromPR(pr)
}

Expand Down
38 changes: 34 additions & 4 deletions cmd/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1261,10 +1261,12 @@ func TestSyncStackPRs_NoTrackedPR_AdoptsOpenPR(t *testing.T) {
cfg.GitHubClientOverride = &github.MockClient{
FindPRForBranchFn: func(branch string) (*github.PullRequest, error) {
return &github.PullRequest{
Number: 99,
ID: "PR_99",
URL: "https://github.com/o/r/pull/99",
State: "OPEN",
Number: 99,
ID: "PR_99",
URL: "https://github.com/o/r/pull/99",
State: "OPEN",
BaseRefName: "main",
BaseRefOID: "base-sha",
}, nil
},
}
Expand All @@ -1275,6 +1277,34 @@ func TestSyncStackPRs_NoTrackedPR_AdoptsOpenPR(t *testing.T) {
require.NotNil(t, s.Branches[0].PullRequest)
assert.Equal(t, 99, s.Branches[0].PullRequest.Number)
assert.False(t, s.Branches[0].PullRequest.Merged)
assert.Equal(t, "base-sha", s.Branches[0].Base)
}

func TestSyncStackPRs_PreservesDifferentLocalBaseBranch(t *testing.T) {
s := &stack.Stack{
Trunk: stack.BranchRef{Branch: "main"},
Branches: []stack.BranchRef{{
Branch: "feature", Base: "local-base", PullRequest: &stack.PullRequestRef{Number: 99},
}},
}
restore := git.SetOps(&git.MockOps{
IsAncestorFn: func(a, b string) (bool, error) {
return a == "remote-base" && b == "feature", nil
},
})
defer restore()
cfg := issue250TestConfig(t)
cfg.GitHubClientOverride = &github.MockClient{
FindPRByNumberFn: func(int) (*github.PullRequest, error) {
return &github.PullRequest{
Number: 99, State: "OPEN", BaseRefName: "previous-parent", BaseRefOID: "remote-base",
}, nil
},
}

syncStackPRs(cfg, s)

assert.Equal(t, "local-base", s.Branches[0].Base)
}

func TestSyncStackPRs_TrackedPR_DetectsMerge(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions internal/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type PullRequest struct {
HeadRefName string `graphql:"headRefName"`
HeadRefOID string `graphql:"headRefOid"`
BaseRefName string `graphql:"baseRefName"`
BaseRefOID string `graphql:"baseRefOid"`
IsDraft bool `graphql:"isDraft"`
Merged bool `graphql:"merged"`
MergeQueueEntry *MergeQueueEntry `graphql:"mergeQueueEntry"`
Expand Down Expand Up @@ -116,6 +117,7 @@ func (c *Client) FindPRForBranch(branch string) (*PullRequest, error) {
Title string `graphql:"title"`
Body string `graphql:"body"`
BaseRefName string `graphql:"baseRefName"`
BaseRefOID string `graphql:"baseRefOid"`
IsDraft bool `graphql:"isDraft"`
MergeQueueEntry *MergeQueueEntry `graphql:"mergeQueueEntry"`
AutoMergeRequest *AutoMergeRequest `graphql:"autoMergeRequest"`
Expand Down Expand Up @@ -147,6 +149,7 @@ func (c *Client) FindPRForBranch(branch string) (*PullRequest, error) {
Title: n.Title,
Body: n.Body,
BaseRefName: n.BaseRefName,
BaseRefOID: n.BaseRefOID,
IsDraft: n.IsDraft,
MergeQueueEntry: n.MergeQueueEntry,
AutoMergeRequest: n.AutoMergeRequest,
Expand Down Expand Up @@ -376,6 +379,7 @@ func (c *Client) FindPRByNumber(number int) (*PullRequest, error) {
HeadRefName string `graphql:"headRefName"`
HeadRefOID string `graphql:"headRefOid"`
BaseRefName string `graphql:"baseRefName"`
BaseRefOID string `graphql:"baseRefOid"`
IsDraft bool `graphql:"isDraft"`
Merged bool `graphql:"merged"`
MergeQueueEntry *MergeQueueEntry `graphql:"mergeQueueEntry"`
Expand Down Expand Up @@ -408,6 +412,7 @@ func (c *Client) FindPRByNumber(number int) (*PullRequest, error) {
HeadRefName: n.HeadRefName,
HeadRefOID: n.HeadRefOID,
BaseRefName: n.BaseRefName,
BaseRefOID: n.BaseRefOID,
IsDraft: n.IsDraft,
Merged: n.Merged,
MergeQueueEntry: n.MergeQueueEntry,
Expand Down
3 changes: 3 additions & 0 deletions internal/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func TestFindPRByNumber_MergedHead(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
pr := map[string]any{
"number": 42, "state": "MERGED", "merged": true, "headRefName": "feature",
"baseRefOid": "previous-base",
}
if !tt.omit {
pr["headRefOid"] = tt.head
Expand All @@ -132,6 +133,7 @@ func TestFindPRByNumber_MergedHead(t *testing.T) {
}
require.NoError(t, json.NewDecoder(r.Body).Decode(&request))
assert.Contains(t, request.Query, "headRefOid")
assert.Contains(t, request.Query, "baseRefOid")
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(body)),
Expand All @@ -150,6 +152,7 @@ func TestFindPRByNumber_MergedHead(t *testing.T) {
require.NotNil(t, got)
assert.True(t, got.Merged)
assert.Equal(t, tt.head, got.HeadRefOID)
assert.Equal(t, "previous-base", got.BaseRefOID)
})
}
}
Expand Down
Loading