Fixes cloning of non-master branches
Problem:
Cloning of non-master branches fails with missing reference message.
Solution:
All of the go-git methods (cloning,pulling etc) must receive
ReferenceName option referring to the required branch.
@@ -97,9 +97,10 @@ func (ch *Checkout) Clone(ctx context.Context, cloneSubdir string) error {
}
ch.Dir = repoDir
- repo, err := gogit.PlainClone(repoDir, false, &gogit.CloneOptions{
+ repo, err := gogit.PlainCloneContext(ctx, repoDir, false, &gogit.CloneOptions{
URL: ch.Config.URL,
Auth: ch.auth,
+ ReferenceName: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", ch.Config.Branch)),
})
if err != nil {
return err
@@ -110,14 +111,6 @@ func (ch *Checkout) Clone(ctx context.Context, cloneSubdir string) error {
return err
}
- br := ch.Config.Branch
- err = wt.Checkout(&gogit.CheckoutOptions{
- Branch: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", br)),
- })
- if err != nil {
- return err
- }
-
ch.Repo = repo
ch.worktree = wt
@@ -183,6 +176,7 @@ func (ch *Checkout) Pull(ctx context.Context) error {
err := w.Pull(&gogit.PullOptions{
RemoteName: "origin",
Auth: ch.auth,
+ ReferenceName: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", ch.Config.Branch)),
})
if err != nil && err != gogit.NoErrAlreadyUpToDate {
return err
