Skip to content

Commit

Permalink
Don't fetch/push upstack branches on av commit create/amend
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-codaio committed May 1, 2024
1 parent 50d63e0 commit d0e8428
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/av/commit_amend.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ var commitAmendCmd = &cobra.Command{
// Even if it's not configured, there's no need to fetch/push
state.Config.NoFetch = true
state.Config.NoPush = true
err = actions.SyncStack(ctx, repo, client, tx, branchesToSync, state)
err = actions.SyncStack(ctx, repo, client, tx, branchesToSync, state, actions.WithLocalOnly())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/av/commit_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func commitCreate(repo *git.Repo, currentBranchName string, flags struct {

branchesToSync := meta.SubsequentBranches(tx, currentBranchName)

err = actions.SyncStack(ctx, repo, client, tx, branchesToSync, state)
err = actions.SyncStack(ctx, repo, client, tx, branchesToSync, state, actions.WithLocalOnly())
if err != nil {
return err
}
Expand Down
49 changes: 49 additions & 0 deletions e2e_tests/commit_amend_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package e2e_tests

import (
"testing"

"github.com/aviator-co/av/internal/git/gittest"
"github.com/aviator-co/av/internal/meta/jsonfiledb"
"github.com/stretchr/testify/require"
"golang.org/x/exp/maps"
)

func TestCommitAmendInStack(t *testing.T) {
repo := gittest.NewTempRepo(t)
Chdir(t, repo.Dir())
RequireCmd(t, "git", "fetch")
initialTimestamp := GetFetchHeadTimestamp(t, repo)

// Create a branch and commit a file.
filepath := gittest.CreateFile(t, repo, "one.txt", []byte("one"))
gittest.AddFile(t, repo, filepath)
RequireAv(t, "stack", "branch", "one")
RequireAv(t, "commit", "create", "-m", "one")

// Create another branch and commit a file.
filepath = gittest.CreateFile(t, repo, "two.txt", []byte("two"))
gittest.AddFile(t, repo, filepath)
RequireAv(t, "stack", "branch", "two")
RequireAv(t, "commit", "create", "-m", "two")

// Go back to the first branch and amend the commit with another file.
RequireCmd(t, "git", "checkout", "one")
filepath = gittest.CreateFile(t, repo, "one-b.txt", []byte("one-b"))
gittest.AddFile(t, repo, filepath)
RequireAv(t, "commit", "amend", "--no-edit")

// Verify that the branches are still there.
db, err := jsonfiledb.OpenRepo(repo)
require.NoError(t, err, "failed to open repo db")
branchNames := maps.Keys(db.ReadTx().AllBranches())
require.ElementsMatch(t, branchNames, []string{"one", "two"})

// Commit shouldn't have triggered a fetch.
updatedTimestamp := GetFetchHeadTimestamp(t, repo)
require.Equal(t, initialTimestamp, updatedTimestamp)

// It also shouldn't have triggered a push.
// TODO: once we support mocking the GitHub API and there is an associated PR,
// validate that a push didn't happen.
}
49 changes: 49 additions & 0 deletions e2e_tests/commit_create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package e2e_tests

import (
"testing"

"github.com/aviator-co/av/internal/git/gittest"
"github.com/aviator-co/av/internal/meta/jsonfiledb"
"github.com/stretchr/testify/require"
"golang.org/x/exp/maps"
)

func TestCommitCreateInStack(t *testing.T) {
repo := gittest.NewTempRepo(t)
Chdir(t, repo.Dir())
RequireCmd(t, "git", "fetch")
initialTimestamp := GetFetchHeadTimestamp(t, repo)

// Create a branch and commit a file.
filepath := gittest.CreateFile(t, repo, "one.txt", []byte("one"))
gittest.AddFile(t, repo, filepath)
RequireAv(t, "stack", "branch", "one")
RequireAv(t, "commit", "create", "-m", "one")

// Create another branch and commit a file.
filepath = gittest.CreateFile(t, repo, "two.txt", []byte("two"))
gittest.AddFile(t, repo, filepath)
RequireAv(t, "stack", "branch", "two")
RequireAv(t, "commit", "create", "-m", "two")

// Go back to the first branch and commit another file.
RequireCmd(t, "git", "checkout", "one")
filepath = gittest.CreateFile(t, repo, "one-b.txt", []byte("one-b"))
gittest.AddFile(t, repo, filepath)
RequireAv(t, "commit", "create", "-m", "one-b")

// Verify that the branches are still there.
db, err := jsonfiledb.OpenRepo(repo)
require.NoError(t, err, "failed to open repo db")
branchNames := maps.Keys(db.ReadTx().AllBranches())
require.ElementsMatch(t, branchNames, []string{"one", "two"})

// Commit shouldn't have triggered a fetch.
updatedTimestamp := GetFetchHeadTimestamp(t, repo)
require.Equal(t, initialTimestamp, updatedTimestamp)

// It also shouldn't have triggered a push.
// TODO: once we support mocking the GitHub API and there is an associated PR,
// validate that a push didn't happen.
}
8 changes: 8 additions & 0 deletions e2e_tests/helpers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package e2e_tests

import (
"os"
"testing"
"time"

"github.com/aviator-co/av/internal/git"
"github.com/aviator-co/av/internal/meta"
Expand All @@ -22,6 +24,12 @@ func RequireCurrentBranchName(t *testing.T, repo *git.Repo, name string) {
)
}

func GetFetchHeadTimestamp(t *testing.T, repo *git.Repo) time.Time {
fileInfo, err := os.Stat(repo.Dir() + "/.git/FETCH_HEAD")
require.NoError(t, err, "failed to stat .git/FETCH_HEAD")
return fileInfo.ModTime()
}

func GetStoredParentBranchState(t *testing.T, repo *git.Repo, name string) meta.BranchState {
// We shouldn't do this as part of an E2E test, but it's hard to ensure otherwise.
db, err := jsonfiledb.OpenRepo(repo)
Expand Down
11 changes: 9 additions & 2 deletions internal/actions/sync_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type (
SyncStackOpt func(*syncStackOpts)
syncStackOpts struct {
skipNextCommit bool
localOnly bool
}
)

Expand All @@ -68,6 +69,12 @@ func WithSkipNextCommit() SyncStackOpt {
}
}

func WithLocalOnly() SyncStackOpt {
return func(opts *syncStackOpts) {
opts.localOnly = true
}
}

// SyncStack performs stack sync on all branches in branchesToSync.
func SyncStack(ctx context.Context,
repo *git.Repo,
Expand All @@ -92,8 +99,8 @@ func SyncStack(ctx context.Context,
state.CurrentBranch = currentBranch
cont, err := SyncBranch(ctx, repo, client, tx, SyncBranchOpts{
Branch: currentBranch,
Fetch: !state.Config.NoFetch,
Push: !state.Config.NoPush,
Fetch: !state.Config.NoFetch && !opts.localOnly,
Push: !state.Config.NoPush && !opts.localOnly,
Continuation: state.Continuation,
ToTrunk: state.Config.Trunk,
Skip: skip,
Expand Down
12 changes: 3 additions & 9 deletions internal/git/gittest/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package gittest

import (
"fmt"
"os"
"path"
"testing"

"github.com/aviator-co/av/internal/git"
Expand Down Expand Up @@ -43,18 +41,14 @@ func CommitFile(
o(&opts)
}

filepath := path.Join(repo.Dir(), filename)
err := os.WriteFile(filepath, body, 0644)
require.NoError(t, err, "failed to write file: %s", filename)

_, err = repo.Git("add", filepath)
require.NoError(t, err, "failed to add file: %s", filename)
filepath := CreateFile(t, repo, filename, body)
AddFile(t, repo, filepath)

args := []string{"commit", "-m", opts.msg}
if opts.amend {
args = append(args, "--amend")
}
_, err = repo.Git(args...)
_, err := repo.Git(args...)
require.NoError(t, err, "failed to commit file: %s", filename)

head, err := repo.RevParse(&git.RevParse{Rev: "HEAD"})
Expand Down
31 changes: 31 additions & 0 deletions internal/git/gittest/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package gittest

import (
"os"
"path"
"testing"

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

func CreateFile(
t *testing.T,
repo *git.Repo,
filename string,
body []byte,
) string {
filepath := path.Join(repo.Dir(), filename)
err := os.WriteFile(filepath, body, 0644)
require.NoError(t, err, "failed to write file: %s", filename)
return filepath
}

func AddFile(
t *testing.T,
repo *git.Repo,
filepath string,
) {
_, err := repo.Git("add", filepath)
require.NoError(t, err, "failed to add file: %s", filepath)
}

0 comments on commit d0e8428

Please sign in to comment.