3
3
package git
4
4
5
5
import (
6
+ "errors"
6
7
"fmt"
8
+ "path/filepath"
7
9
"strings"
8
10
9
11
"github.com/asdf-vm/asdf/internal/execute"
10
12
"github.com/go-git/go-git/v5"
11
- "github.com/go-git/go-git/v5/config"
12
13
"github.com/go-git/go-git/v5/plumbing"
13
14
)
14
15
@@ -55,9 +56,7 @@ func (r Repo) Clone(pluginURL, ref string) error {
55
56
err := cmd .Run ()
56
57
57
58
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 ()))
61
60
}
62
61
63
62
return nil
@@ -106,8 +105,6 @@ func (r Repo) Update(ref string) (string, string, string, error) {
106
105
return "" , "" , "" , err
107
106
}
108
107
109
- var checkoutOptions git.CheckoutOptions
110
-
111
108
if ref == "" {
112
109
// If no ref is provided checkout latest commit on current branch
113
110
head , err := repo .Head ()
@@ -128,28 +125,38 @@ func (r Repo) Update(ref string) (string, string, string, error) {
128
125
checkoutOptions = git.CheckoutOptions {Hash : plumbing .NewHash (ref ), Keep : true }
129
126
}
130
127
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 }
134
129
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 ()
136
137
137
138
if err != nil && err != git .NoErrAlreadyUpToDate {
138
- return "" , "" , "" , err
139
+ return "" , "" , "" , errors . New ( stdErrToErrMsg ( stdErr . String ()))
139
140
}
140
141
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 ()
145
147
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" ))
147
151
if err != nil {
148
- return "" , "" , "" , err
152
+ return ref , oldHash . String (), newHash . String (), errors . New ( stdErrToErrMsg ( stdErr2 . String ()))
149
153
}
154
+ return ref , oldHash .String (), newHash .String (), nil
155
+ }
150
156
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 ]
153
160
}
154
161
155
162
func gitOpen (directory string ) (* git.Repository , error ) {
0 commit comments