Skip to content

Commit 2b9ffd9

Browse files
committed
WIP
1 parent b8764b3 commit 2b9ffd9

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

internal/git/git.go

+26-19
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
package git
44

55
import (
6+
"errors"
67
"fmt"
8+
"path/filepath"
79
"strings"
810

911
"github.com/asdf-vm/asdf/internal/execute"
1012
"github.com/go-git/go-git/v5"
11-
"github.com/go-git/go-git/v5/config"
1213
"github.com/go-git/go-git/v5/plumbing"
1314
)
1415

@@ -55,9 +56,7 @@ func (r Repo) Clone(pluginURL, ref string) error {
5556
err := cmd.Run()
5657

5758
if err != nil {
58-
lines := strings.Split(strings.TrimSuffix(stdErr.String(), "\n"), "\n")
59-
errMsg := lines[len(lines)-1]
60-
return fmt.Errorf("unable to clone plugin: %s", errMsg)
59+
return fmt.Errorf("unable to clone plugin: %s", stdErrToErrMsg(stdErr.String()))
6160
}
6261

6362
return nil
@@ -106,8 +105,6 @@ func (r Repo) Update(ref string) (string, string, string, error) {
106105
return "", "", "", err
107106
}
108107

109-
var checkoutOptions git.CheckoutOptions
110-
111108
if ref == "" {
112109
// If no ref is provided checkout latest commit on current branch
113110
head, err := repo.Head()
@@ -128,28 +125,38 @@ func (r Repo) Update(ref string) (string, string, string, error) {
128125
checkoutOptions = git.CheckoutOptions{Hash: plumbing.NewHash(ref), Keep: true}
129126
}
130127

131-
fetchOptions := git.FetchOptions{RemoteName: DefaultRemoteName, Force: true, RefSpecs: []config.RefSpec{
132-
config.RefSpec(ref + ":" + ref),
133-
}}
128+
commonOpts := []string{"--git-dir", filepath.Join(r.Directory, ".git"), "--work-tree", r.Directory}
134129

135-
err = repo.Fetch(&fetchOptions)
130+
refSpec := fmt.Sprintf("%s:%s", ref, ref)
131+
cmdStr := append(commonOpts, []string{"fetch", "--prune", "--update-head-ok", "origin", refSpec}...)
132+
133+
cmd := execute.New("git", cmdStr)
134+
var stdErr strings.Builder
135+
cmd.Stderr = &stdErr
136+
err = cmd.Run()
136137

137138
if err != nil && err != git.NoErrAlreadyUpToDate {
138-
return "", "", "", err
139+
return "", "", "", errors.New(stdErrToErrMsg(stdErr.String()))
139140
}
140141

141-
worktree, err := repo.Worktree()
142-
if err != nil {
143-
return "", "", "", err
144-
}
142+
cmdStr = append(commonOpts, []string{"-c", "advice.detachedHead=false", "checkout", "--force", ref}...)
143+
cmd = execute.New("git", cmdStr)
144+
var stdErr2 strings.Builder
145+
cmd.Stderr = &stdErr2
146+
err = cmd.Run()
145147

146-
err = worktree.Checkout(&checkoutOptions)
148+
fmt.Printf("stdErr2.String() %#+v\n", stdErr2.String())
149+
repo, err = gitOpen(r.Directory)
150+
newHash, err := repo.ResolveRevision(plumbing.Revision("HEAD"))
147151
if err != nil {
148-
return "", "", "", err
152+
return ref, oldHash.String(), newHash.String(), errors.New(stdErrToErrMsg(stdErr2.String()))
149153
}
154+
return ref, oldHash.String(), newHash.String(), nil
155+
}
150156

151-
newHash, err := repo.ResolveRevision(plumbing.Revision("HEAD"))
152-
return ref, oldHash.String(), newHash.String(), err
157+
func stdErrToErrMsg(stdErr string) string {
158+
lines := strings.Split(strings.TrimSuffix(stdErr, "\n"), "\n")
159+
return lines[len(lines)-1]
153160
}
154161

155162
func gitOpen(directory string) (*git.Repository, error) {

internal/git/git_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func TestRepoUpdate(t *testing.T) {
191191
ref := "non-existent"
192192
updatedToRef, _, _, err := repo.Update(ref)
193193
assert.Equal(t, updatedToRef, "")
194-
expectedErrMsg := "couldn't find remote ref \"non-existent\""
194+
expectedErrMsg := "fatal: couldn't find remote ref non-existent"
195195
assert.ErrorContains(t, err, expectedErrMsg)
196196
})
197197

0 commit comments

Comments
 (0)