Skip to content

Commit

Permalink
Compute checked out branch at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
saquibmian committed Nov 10, 2023
1 parent 6f8e6d4 commit 4647a62
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 25 deletions.
20 changes: 13 additions & 7 deletions private/buf/bufsync/commits_to_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func TestCommitsToSyncWithNoPreviousSyncPoints(t *testing.T) {
require.NoError(t, err)
const defaultBranchName = "main"
repo, repoDir := scaffoldGitRepository(t, defaultBranchName)
prepareGitRepoSyncWithNoPreviousSyncPoints(t, repoDir, moduleIdentityInHEAD, defaultBranchName)
runner := command.NewRunner()
prepareGitRepoSyncWithNoPreviousSyncPoints(t, runner, repoDir, moduleIdentityInHEAD, defaultBranchName)
type testCase struct {
name string
branch string
Expand Down Expand Up @@ -63,20 +64,20 @@ func TestCommitsToSyncWithNoPreviousSyncPoints(t *testing.T) {
expectedCommits: 1,
},
}
handler := newMockSyncHandler() // use same handler for all test cases
for _, withOverride := range []bool{false, true} {
for _, tc := range testCases {
func(tc testCase) {
t.Run(fmt.Sprintf("%s/override_%t", tc.name, withOverride), func(t *testing.T) {
// check out the branch to sync
runInDir(t, runner, repoDir, "git", "checkout", tc.branch)
const moduleDir = "."
opts := []bufsync.SyncerOption{
bufsync.SyncerWithAllBranches(),
}
var opts []bufsync.SyncerOption
if withOverride {
opts = append(opts, bufsync.SyncerWithModule(moduleDir, moduleIdentityOverride))
} else {
opts = append(opts, bufsync.SyncerWithModule(moduleDir, nil))
}
handler := newMockSyncHandler()
syncer, err := bufsync.NewSyncer(
zaptest.NewLogger(t),
bufsync.NewRealClock(),
Expand All @@ -101,8 +102,13 @@ func TestCommitsToSyncWithNoPreviousSyncPoints(t *testing.T) {
// | o-o----------o-----------------o (master)
// | └o-o (foo) └o--------o (bar)
// | └o (baz)
func prepareGitRepoSyncWithNoPreviousSyncPoints(t *testing.T, repoDir string, moduleIdentity bufmoduleref.ModuleIdentity, defaultBranchName string) {
runner := command.NewRunner()
func prepareGitRepoSyncWithNoPreviousSyncPoints(
t *testing.T,
runner command.Runner,
repoDir string,
moduleIdentity bufmoduleref.ModuleIdentity,
defaultBranchName string,
) {
var allBranches = []string{defaultBranchName, "foo", "bar", "baz"}

var commitsCounter int
Expand Down
5 changes: 4 additions & 1 deletion private/buf/bufsync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ func (s *syncer) prepareSync(ctx context.Context) error {
branchesToSync = stringutil.MapToSlice(allBranches)
} else {
// sync current branch, make sure it's present
currentBranch := s.repo.CurrentBranch()
currentBranch, err := s.repo.CurrentBranch(ctx)
if err != nil {
return fmt.Errorf("determine checked out branch")
}
if _, currentBranchPresent := allBranches[currentBranch]; !currentBranchPresent {
return fmt.Errorf("current branch %s is not present %s", currentBranch, remoteErrMsg)
}
Expand Down
2 changes: 1 addition & 1 deletion private/pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ type Repository interface {
// remote named `origin`). It can be customized via the `OpenRepositoryWithDefaultBranch` option.
DefaultBranch() string
// CurrentBranch is the current checked out branch.
CurrentBranch() string
CurrentBranch(ctx context.Context) (string, error)
// ForEachBranch ranges over branches in the repository in an undefined order.
ForEachBranch(f func(branch string, headHash Hash) error, options ...ForEachBranchOption) error
// ForEachCommit ranges over commits in reverse topological order, going backwards in time always
Expand Down
24 changes: 10 additions & 14 deletions private/pkg/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ type openRepositoryOpts struct {
}

type repository struct {
gitDirPath string
defaultBranch string
checkedOutBranch string
objectReader *objectReader
gitDirPath string
defaultBranch string
objectReader *objectReader
runner command.Runner

// packedOnce controls the fields below related to reading the `packed-refs` file
packedOnce sync.Once
Expand Down Expand Up @@ -77,15 +77,11 @@ func openGitRepository(
return nil, fmt.Errorf("automatically determine default branch: %w", err)
}
}
checkedOutBranch, err := detectCheckedOutBranch(ctx, gitDirPath, runner)
if err != nil {
return nil, fmt.Errorf("automatically determine checked out branch: %w", err)
}
return &repository{
gitDirPath: gitDirPath,
defaultBranch: opts.defaultBranch,
checkedOutBranch: checkedOutBranch,
objectReader: reader,
gitDirPath: gitDirPath,
defaultBranch: opts.defaultBranch,
objectReader: reader,
runner: runner,
}, nil
}

Expand Down Expand Up @@ -157,8 +153,8 @@ func (r *repository) DefaultBranch() string {
return r.defaultBranch
}

func (r *repository) CurrentBranch() string {
return r.checkedOutBranch
func (r *repository) CurrentBranch(ctx context.Context) (string, error) {
return detectCheckedOutBranch(ctx, r.gitDirPath, r.runner)
}

func (r *repository) ForEachCommit(f func(Commit) error, options ...ForEachCommitOption) error {
Expand Down
7 changes: 5 additions & 2 deletions private/pkg/git/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package git_test

import (
"context"
"testing"

"github.com/bufbuild/buf/private/pkg/git"
Expand Down Expand Up @@ -235,9 +236,11 @@ func TestForEachBranch(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
repo := gittest.ScaffoldGitRepository(t)
assert.Equal(t, gittest.DefaultBranch, repo.CurrentBranch())
currentBranch, err := repo.CurrentBranch(context.Background())
require.NoError(t, err)
assert.Equal(t, gittest.DefaultBranch, currentBranch)
branches := make(map[string]struct{})
err := repo.ForEachBranch(func(branch string, headHash git.Hash) error {
err = repo.ForEachBranch(func(branch string, headHash git.Hash) error {
require.NotEmpty(t, branch)
if _, alreadySeen := branches[branch]; alreadySeen {
assert.Fail(t, "duplicate branch", branch)
Expand Down

0 comments on commit 4647a62

Please sign in to comment.