diff --git a/e2e_tests/stack_sync_adopt_test.go b/e2e_tests/stack_sync_adopt_test.go new file mode 100644 index 00000000..e7338bf7 --- /dev/null +++ b/e2e_tests/stack_sync_adopt_test.go @@ -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", + ) +} diff --git a/internal/actions/reparent.go b/internal/actions/reparent.go index 73abb0a0..df6ec45a 100644 --- a/internal/actions/reparent.go +++ b/internal/actions/reparent.go @@ -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), @@ -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