Skip to content

Commit

Permalink
fix: multi-gitter does now only fetch the base and head branch
Browse files Browse the repository at this point in the history
  • Loading branch information
lindell committed Jan 16, 2021
1 parent e30c408 commit b272644
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
25 changes: 22 additions & 3 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/url"
"time"

"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/lindell/multi-gitter/internal/domain"
"github.com/pkg/errors"
Expand All @@ -24,8 +25,10 @@ type Git struct {
repo *git.Repository // The repository after the clone has been made
}

const fetchDepth = 10

// Clone a repository
func (g *Git) Clone(branchName string) error {
func (g *Git) Clone(baseName, headName string) error {
u, err := url.Parse(g.Repo)
if err != nil {
return err
Expand All @@ -34,12 +37,28 @@ func (g *Git) Clone(branchName string) error {
r, err := git.PlainClone(g.Directory, false, &git.CloneOptions{
URL: u.String(),
RemoteName: "origin",
Depth: 10,
ReferenceName: plumbing.NewBranchReferenceName(branchName),
Depth: fetchDepth,
ReferenceName: plumbing.NewBranchReferenceName(baseName),
SingleBranch: true,
})
if err != nil {
return errors.Wrap(err, "could not clone from the remote")
}

if headName != "" {
err = r.Fetch(&git.FetchOptions{
RefSpecs: []config.RefSpec{
config.RefSpec(fmt.Sprintf("refs/heads/%s:refs/remotes/origin/%s", headName, headName)),
},
Depth: fetchDepth,
})
if err != nil {
if _, ok := err.(git.NoMatchingRefSpecError); !ok {
return err
}
}
}

g.repo = r

return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/multigitter/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (r Printer) runSingleRepo(ctx context.Context, repo domain.Repository) erro
Repo: repo.URL(r.Token),
}

err = sourceController.Clone(repo.DefaultBranch())
err = sourceController.Clone(repo.DefaultBranch(), "")
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/multigitter/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (r Runner) runSingleRepo(ctx context.Context, repo domain.Repository) error
baseBranch = repo.DefaultBranch()
}

err = sourceController.Clone(baseBranch)
err = sourceController.Clone(baseBranch, r.FeatureBranch)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion tests/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestTable(t *testing.T) {
},
verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) {
require.Len(t, vcMock.PullRequests, 0)
assert.Contains(t, runData.logOut, `msg="could not clone from the remote: reference not found"`)
assert.Contains(t, runData.logOut, `msg="could not clone from the remote: couldn't find remote ref \"refs/heads/custom-base-branch\""`)
},
},

Expand Down

0 comments on commit b272644

Please sign in to comment.