Skip to content

Commit

Permalink
feat: Use remote tracking branch if it's trunk
Browse files Browse the repository at this point in the history
  • Loading branch information
draftcode committed May 9, 2023
1 parent 8babc9e commit b1ba753
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
16 changes: 16 additions & 0 deletions e2e_tests/stack_reparent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ func TestStackSyncReparent(t *testing.T) {
Cmd(t, "git", "log")
}

func TestStackSyncReparentTrunk(t *testing.T) {
repo := gittest.NewTempRepo(t)
Chdir(t, repo.Dir())

RequireAv(t, "stack", "branch", "foo")
gittest.CommitFile(t, repo, "foo.txt", []byte("foo"))

RequireAv(t, "stack", "branch", "bar")
gittest.CommitFile(t, repo, "bar.txt", []byte("bar"))

// Delete the local main. av should use the remote tracking branch.
Cmd(t, "git", "branch", "-D", "main")

RequireAv(t, "stack", "sync", "--parent", "main", "--no-fetch", "--no-push")
}

func requireFileContent(t *testing.T, file string, expected string, args ...any) {
actual, err := os.ReadFile(file)
if err != nil {
Expand Down
17 changes: 12 additions & 5 deletions internal/actions/reparent.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,26 @@ func Reparent(
}

// Check that the parent branch actually exists
parentSha, err := repo.RevParse(&git.RevParse{Rev: opts.NewParent})
parentBranch := opts.NewParent
if opts.NewParentTrunk {
parentBranch = "remotes/origin/" + opts.NewParent
}
parentSha, err := repo.RevParse(&git.RevParse{Rev: parentBranch})
if err != nil {
_, _ = fmt.Fprint(os.Stderr,
colors.Failure(" - ERROR:"),
"cannot re-parent branch ", colors.UserInput(opts.Branch),
": new parent branch ", colors.UserInput(opts.NewParent),
": new parent branch ", colors.UserInput(parentBranch),
" does not exist\n",
)
return nil, errors.Errorf("parent branch %q does not exist", opts.NewParent)
return nil, errors.Errorf("parent branch %q does not exist", parentBranch)
}

branchMeta, _ := tx.Branch(opts.Branch)
upstream := branchMeta.Parent.Name
if branchMeta.Parent.Trunk {
upstream = "remotes/origin/" + branchMeta.Parent.Name
}

// We might need to rebase the branch on top of the new parent. This
// requires a special rebase command because the "normal" rebase command
Expand All @@ -87,12 +94,12 @@ func Reparent(
// the commits that are reachable from B3 but not B2 on top of B1.
logrus.WithFields(logrus.Fields{
"branch": opts.Branch,
"onto_branch": opts.NewParent,
"onto_branch": parentBranch,
"onto_head": parentSha,
"upstream": upstream,
}).Debug("rebasing branch")
output, err := repo.Rebase(git.RebaseOpts{
Onto: opts.NewParent,
Onto: parentSha,
Upstream: upstream,
Branch: opts.Branch,
})
Expand Down

0 comments on commit b1ba753

Please sign in to comment.