-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stack reorder): Implement internal Reorder API (#141)
- Loading branch information
Showing
9 changed files
with
286 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package reorder | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestState(t *testing.T) { | ||
state := &State{ | ||
Branch: "main", | ||
Head: "owouwu", | ||
Commands: []Cmd{ | ||
StackBranchCmd{Name: "one", Trunk: "main"}, | ||
PickCmd{"abcd"}, | ||
StackBranchCmd{Name: "two", Parent: "one"}, | ||
PickCmd{"efgh"}, | ||
}, | ||
} | ||
|
||
serialized, err := json.Marshal(state) | ||
require.NoError(t, err, "failed to serialize state") | ||
|
||
var deserialized State | ||
err = json.Unmarshal(serialized, &deserialized) | ||
require.NoError(t, err, "failed to deserialize state") | ||
|
||
require.Equal(t, *state, deserialized, "deserialized command sequence does not match original") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,34 @@ | ||
package reorder | ||
|
||
type Opts struct{} | ||
import ( | ||
"emperror.dev/errors" | ||
"fmt" | ||
"github.com/aviator-co/av/internal/utils/colors" | ||
"os" | ||
) | ||
|
||
func Reorder(opts Opts) error { | ||
panic("not implemented") | ||
// Reorder executes a reorder. | ||
// If the reorder couldn't be completed (due to a conflict), a continuation is returned. | ||
// If the reorder was completed successfully, a nil continuation and nil error is returned. | ||
func Reorder(ctx Context) (*Continuation, error) { | ||
if ctx.Output == nil { | ||
ctx.Output = os.Stderr | ||
} | ||
|
||
for _, cmd := range ctx.State.Commands { | ||
err := cmd.Execute(&ctx) | ||
if errors.Is(err, ErrInterruptReorder) { | ||
return &Continuation{State: ctx.State}, nil | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
ctx.State.Commands = ctx.State.Commands[1:] | ||
} | ||
|
||
_, _ = fmt.Fprint(ctx.Output, colors.Success("Reorder complete!\n")) | ||
return nil, nil | ||
} | ||
|
||
type Continuation struct { | ||
State *State | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package reorder_test | ||
|
||
import ( | ||
"fmt" | ||
"github.com/aviator-co/av/internal/git" | ||
"github.com/aviator-co/av/internal/git/gittest" | ||
"github.com/aviator-co/av/internal/meta/jsonfiledb" | ||
"github.com/aviator-co/av/internal/reorder" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestReorder(t *testing.T) { | ||
repo := gittest.NewTempRepo(t) | ||
db, err := jsonfiledb.OpenRepo(repo) | ||
require.NoError(t, err) | ||
|
||
initial, err := repo.RevParse(&git.RevParse{Rev: "HEAD"}) | ||
require.NoError(t, err) | ||
|
||
_, err = repo.CheckoutBranch(&git.CheckoutBranch{Name: "one", NewBranch: true}) | ||
require.NoError(t, err) | ||
c1a := gittest.CommitFile(t, repo, "file", []byte("hello\n")) | ||
c1b := gittest.CommitFile(t, repo, "file", []byte("hello\nworld\n")) | ||
c2a := gittest.CommitFile(t, repo, "fichier", []byte("bonjour\n")) | ||
c2b := gittest.CommitFile(t, repo, "fichier", []byte("bonjour\nle monde\n")) | ||
|
||
continuation, err := reorder.Reorder(reorder.Context{ | ||
Repo: repo, | ||
DB: db, | ||
State: &reorder.State{ | ||
Branch: "", | ||
Head: "", | ||
Commands: []reorder.Cmd{ | ||
reorder.StackBranchCmd{Name: "one", Trunk: fmt.Sprintf("main@%s", initial)}, | ||
reorder.PickCmd{Commit: c1a}, | ||
reorder.PickCmd{Commit: c1b}, | ||
reorder.StackBranchCmd{Name: "two", Parent: "one"}, | ||
reorder.PickCmd{Commit: c2a}, | ||
reorder.PickCmd{Commit: c2b}, | ||
}, | ||
}, | ||
}) | ||
require.NoError(t, err, "expected reorder to complete cleanly") | ||
require.Nil(t, continuation, "expected reorder to complete cleanly") | ||
|
||
mainHead, err := repo.RevParse(&git.RevParse{Rev: "main"}) | ||
require.NoError(t, err) | ||
assert.Equal(t, initial, mainHead, "expected main to be at initial commit") | ||
|
||
oneHead, err := repo.RevParse(&git.RevParse{Rev: "one"}) | ||
require.NoError(t, err) | ||
assert.Equal(t, c1b, oneHead, "expected one to be at c1b") | ||
|
||
twoHead, err := repo.RevParse(&git.RevParse{Rev: "two"}) | ||
require.NoError(t, err) | ||
assert.Equal(t, c2b, twoHead, "expected two to be at c2b") | ||
} | ||
|
||
func TestReorderConflict(t *testing.T) { | ||
repo := gittest.NewTempRepo(t) | ||
db, err := jsonfiledb.OpenRepo(repo) | ||
require.NoError(t, err) | ||
|
||
initial, err := repo.RevParse(&git.RevParse{Rev: "HEAD"}) | ||
require.NoError(t, err) | ||
|
||
_, err = repo.CheckoutBranch(&git.CheckoutBranch{Name: "one", NewBranch: true}) | ||
require.NoError(t, err) | ||
c1a := gittest.CommitFile(t, repo, "file", []byte("hello\n")) | ||
c1b := gittest.CommitFile(t, repo, "file", []byte("hello\nworld\n")) | ||
|
||
_, err = repo.Git("reset", "--hard", initial) | ||
require.NoError(t, err) | ||
c2a := gittest.CommitFile(t, repo, "file", []byte("bonjour\n")) | ||
c2b := gittest.CommitFile(t, repo, "file", []byte("bonjour\nle monde\n")) | ||
|
||
continuation, err := reorder.Reorder(reorder.Context{ | ||
Repo: repo, | ||
DB: db, | ||
State: &reorder.State{ | ||
Branch: "", | ||
Head: "", | ||
Commands: []reorder.Cmd{ | ||
reorder.StackBranchCmd{Name: "one", Trunk: fmt.Sprintf("main@%s", initial)}, | ||
reorder.PickCmd{Commit: c1a}, | ||
reorder.PickCmd{Commit: c1b}, | ||
reorder.PickCmd{Commit: c2a}, | ||
reorder.PickCmd{Commit: c2b}, | ||
}, | ||
}, | ||
}) | ||
require.NoError(t, err, "expected reorder to complete without error even with conflicts") | ||
require.NotNil(t, continuation, "expected continuation to be returned after conflicts") | ||
require.Equal(t, continuation.State.Commands[0], reorder.PickCmd{Commit: c2a}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters