Skip to content

Commit

Permalink
[MER-2351] Add --all option to sync all branches (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
draftcode authored May 26, 2023
1 parent 7274802 commit e7e8cfe
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cmd/av/stack_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,19 @@ base branch.
// we try to do something in the repo before we finish that)
branchesToSync = []string{state.CurrentBranch}
state.Branches = branchesToSync
} else if syncFlags.All {
for _, br := range tx.AllBranches() {
if !br.IsStackRoot() {
continue
}
branchesToSync = append(branchesToSync, br.Name)
nextBranches, err := meta.SubsequentBranches(tx, branchesToSync[len(branchesToSync)-1])
if err != nil {
return err
}
branchesToSync = append(branchesToSync, nextBranches...)
}
state.Branches = branchesToSync
} else {
currentBranch, err := repo.CurrentBranchName()
if err != nil {
Expand Down Expand Up @@ -251,6 +264,10 @@ base branch.
}

func init() {
stackSyncCmd.Flags().BoolVar(
&syncFlags.All, "all", false,
"synchronize all branches",
)
stackSyncCmd.Flags().BoolVar(
&syncFlags.Current, "current", false,
"only sync changes to the current branch\n(don't recurse into descendant branches)",
Expand Down Expand Up @@ -288,6 +305,7 @@ func init() {
&syncFlags.Parent, "parent", "",
"parent branch to rebase onto",
)
stackSyncCmd.MarkFlagsMutuallyExclusive("current", "all")
}

func countBools(bs ...bool) int {
Expand Down
44 changes: 44 additions & 0 deletions e2e_tests/stack_sync_all_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package e2e_tests

import (
"testing"

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

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

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

require.Equal(t, 0, Cmd(t, "git", "switch", "main").ExitCode)
RequireAv(t, "stack", "branch", "stack-2")
gittest.CommitFile(t, repo, "my-file", []byte("2a\n"), gittest.WithMessage("Commit 2a"))

require.Equal(t, 0, Cmd(t, "git", "switch", "main").ExitCode)
gittest.CommitFile(t, repo, "other-file", []byte("X2\n"), gittest.WithMessage("Commit X2"))
require.Equal(t, 0, Cmd(t, "git", "push", "origin", "main").ExitCode)

// main: X -> X2
// stack-1: \ -> 1a
// stack-2: \ -> 2a

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

// main: X -> X2
// stack-1: \ -> 1a
// stack-2: \ -> 2a

require.Equal(t, 0,
Cmd(t, "git", "merge-base", "--is-ancestor", "main", "stack-1").ExitCode,
"HEAD of main should be an ancestor of HEAD of stack-1",
)
require.Equal(t, 0,
Cmd(t, "git", "merge-base", "--is-ancestor", "main", "stack-2").ExitCode,
"HEAD of main should be an ancestor of HEAD of stack-2",
)
}
2 changes: 2 additions & 0 deletions internal/actions/sync_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type StackSyncFlags struct {
Abort bool
// If set, skip a commit and continue a previous sync.
Skip bool
// If set, synchronize all branches.
All bool
}

// stackSyncState is the state of an in-progress sync operation.
Expand Down

0 comments on commit e7e8cfe

Please sign in to comment.