Skip to content

Commit

Permalink
fix: Make branch adoption work
Browse files Browse the repository at this point in the history
Close #120.
  • Loading branch information
draftcode committed May 25, 2023
1 parent 255c55f commit 5cb01ac
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
29 changes: 29 additions & 0 deletions e2e_tests/stack_sync_adopt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package e2e_tests

import (
"testing"

"github.com/aviator-co/av/internal/git/gittest"
"github.com/aviator-co/av/internal/meta"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

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

require.Equal(t, 0, Cmd(t, "git", "checkout", "-b", "stack-1").ExitCode)
gittest.CommitFile(t, repo, "my-file", []byte("1a\n"), gittest.WithMessage("Commit 1a"))

require.Equal(t, 0, Av(t, "stack", "sync", "--no-fetch", "--no-push", "--parent", "main").ExitCode)

assert.Equal(t,
meta.BranchState{
Name: "main",
Trunk: true,
},
GetStoredParentBranchState(t, repo, "stack-1"),
"stack-1 should be re-rooted onto main",
)
}
19 changes: 18 additions & 1 deletion internal/actions/reparent.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ func Reparent(
tx meta.WriteTx,
opts ReparentOpts,
) (*ReparentResult, error) {
branchMeta, exist := tx.Branch(opts.Branch)
if !exist {
_, _ = fmt.Fprint(os.Stderr,
" - Adopting a branch ", colors.UserInput(opts.Branch), " to Av", colors.UserInput(opts.NewParent),
"\n",
)
branchMeta.Parent.Name = opts.NewParent
branchMeta.Parent.Trunk = opts.NewParentTrunk
if !branchMeta.Parent.Trunk {
head, err := repo.RevParse(&git.RevParse{Rev: opts.NewParent})
if err != nil {
return nil, errors.WrapIff(err, "failed to get HEAD of %q", opts.NewParent)
}
branchMeta.Parent.Head = head
}
tx.SetBranch(branchMeta)
}

_, _ = fmt.Fprint(os.Stderr,
" - Re-parenting branch ", colors.UserInput(opts.Branch),
" onto ", colors.UserInput(opts.NewParent),
Expand Down Expand Up @@ -73,7 +91,6 @@ func Reparent(
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
Expand Down

0 comments on commit 5cb01ac

Please sign in to comment.