Skip to content

Commit

Permalink
Process entire stack subtree when syncing stack (#269)
Browse files Browse the repository at this point in the history
When running `av stack sync`, sync the entire "stack" (starting from root) instead of just the subtree from the current branch.

Updated test to document new behavior.
  • Loading branch information
oleg-codaio authored May 2, 2024
1 parent 17c7522 commit 270194a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 16 deletions.
4 changes: 1 addition & 3 deletions cmd/av/stack_foreach.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,10 @@ Examples:
branches = meta.SubsequentBranches(tx, currentBranch)
branches = append([]string{currentBranch}, branches...)
} else {
branches, err = meta.PreviousBranches(tx, currentBranch)
branches, err = meta.StackBranches(tx, currentBranch)
if err != nil {
return err
}
branches = append(branches, currentBranch)
branches = append(branches, meta.SubsequentBranches(tx, currentBranch)...)
}

_, _ = fmt.Fprint(os.Stderr,
Expand Down
19 changes: 13 additions & 6 deletions cmd/av/stack_submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,21 @@ If the --current flag is given, this command will create pull requests up to the
if err != nil {
return err
}
previousBranches, err := meta.PreviousBranches(tx, currentBranch)
if err != nil {
return err
}

var branchesToSubmit []string
branchesToSubmit = append(branchesToSubmit, previousBranches...)
branchesToSubmit = append(branchesToSubmit, currentBranch)
if stackSubmitFlags.Current {
previousBranches, err := meta.PreviousBranches(tx, currentBranch)
if err != nil {
return err
}
branchesToSubmit = append(branchesToSubmit, previousBranches...)
branchesToSubmit = append(branchesToSubmit, currentBranch)
} else {
branchesToSubmit, err = meta.StackBranches(tx, currentBranch)
if err != nil {
return err
}
}

if !stackSubmitFlags.Current {
subsequentBranches := meta.SubsequentBranches(tx, currentBranch)
Expand Down
5 changes: 1 addition & 4 deletions cmd/av/stack_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,10 @@ base branch.
if err != nil {
return err
}
branchesToSync, err = meta.PreviousBranches(tx, currentBranch)
branchesToSync, err = meta.StackBranches(tx, currentBranch)
if err != nil {
return err
}
branchesToSync = append(branchesToSync, currentBranch)
nextBranches := meta.SubsequentBranches(tx, branchesToSync[len(branchesToSync)-1])
branchesToSync = append(branchesToSync, nextBranches...)
state.Branches = branchesToSync
}
// Either way (--continue or not), we sync all subsequent branches
Expand Down
18 changes: 15 additions & 3 deletions e2e_tests/stack_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func TestStackSync(t *testing.T) {
// Our stack looks like:
// stack-1: main -> 1a
// stack-2: \ -> 2a
// stack-3: \ -> 3a
// stack-3: | \ -> 3a
// stack-4: \ -> 4a
// Note: we create the first branch with a "vanilla" git checkout just to
// make sure that's working as intended.
require.Equal(t, 0, Cmd(t, "git", "checkout", "-b", "stack-1").ExitCode)
Expand All @@ -41,6 +42,10 @@ func TestStackSync(t *testing.T) {
[]byte("1a\n2a\n3a\n"),
gittest.WithMessage("Commit 3a"),
)
require.Equal(t, 0, Cmd(t, "git", "checkout", "stack-1").ExitCode)
require.Equal(t, 0, Av(t, "stack", "branch", "stack-4").ExitCode)
gittest.CommitFile(t, repo, "another-file", []byte("1a\n4a\n"), gittest.WithMessage("Commit 4a"))
require.Equal(t, 0, Cmd(t, "git", "checkout", "stack-3").ExitCode)

// Everything up to date now, so this should be a no-op.
require.Equal(t, 0, Av(t, "stack", "sync", "--no-fetch", "--no-push").ExitCode)
Expand All @@ -49,19 +54,22 @@ func TestStackSync(t *testing.T) {
// Our stack looks like:
// stack-1: main -> 1a -> 1b
// stack-2: \ -> 2a
// stack-3: \ -> 3a
// stack-3: | \ -> 3a
// stack-4: \ -> 4a

// (note that stack-2 has diverged with stack-1)
// Ultimately, after the sync (and resolving conflicts), our stack should look like:
// stack-1: main -> 1a -> 1b
// stack-2: \ -> 2a'
// stack-3: \ -> 3a'
// stack-3: | \ -> 3a'
// stack-4: \ -> 4a'
// It's very important here to make sure to handle the sync of stack-3 correctly.
// After syncing stack-2 onto stack-1 (before syncinc stack-3), our commit
// graph looks like:
// stack-1: main -> 1a -> 1b
// stack-2: \ -> 2a'
// stack-3: \ -> 2a -> 3a
// stack-4: \ -> 4a

// (remember that we haven't yet modified stack-3, so 3a still has parent 2a,
// but 2a is actually not even associated with stack-2 anymore since we had
Expand Down Expand Up @@ -159,6 +167,10 @@ func TestStackSync(t *testing.T) {
Name: "stack-2",
Head: stack2Commit,
}, GetStoredParentBranchState(t, repo, "stack-3"))
require.Equal(t, meta.BranchState{
Name: "stack-1",
Head: stack1Commit,
}, GetStoredParentBranchState(t, repo, "stack-4"))
}

func TestStackSyncAbort(t *testing.T) {
Expand Down
12 changes: 12 additions & 0 deletions internal/meta/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ func SubsequentBranches(tx ReadTx, name string) []string {
return res
}

// StackBranches returns branches in the stack associated with the given branch.
func StackBranches(tx ReadTx, name string) ([]string, error) {
root, found := Root(tx, name)
if !found {
return nil, errors.Errorf("branch %q is not in a stack", name)
}

var res = []string{root}
res = append(res, SubsequentBranches(tx, root)...)
return res, nil
}

// Root determines the stack root of a branch.
func Root(tx ReadTx, name string) (string, bool) {
for name != "" {
Expand Down

0 comments on commit 270194a

Please sign in to comment.