@@ -92,6 +92,30 @@ func (r Repo) RemoteURL() (string, error) {
92
92
return remotes [0 ].Config ().URLs [0 ], nil
93
93
}
94
94
95
+ func (r Repo ) RemoteDefaultBranch () (string , error ) {
96
+ // @jiminychris - https://github.com/go-git/go-git/issues/510#issuecomment-2560116147
97
+ repo , err := gitOpen (r .Directory )
98
+ if err != nil {
99
+ return "" , err
100
+ }
101
+
102
+ // Get the remote you want to find the default for
103
+ remote , err := repo .Remote ("origin" )
104
+ if err != nil {
105
+ return "" , err
106
+ }
107
+
108
+ references , _ := remote .List (& git.ListOptions {})
109
+ // Search through the list of references in that remote for a symbolic reference named HEAD;
110
+ // Its target should be the default branch name.
111
+ for _ , reference := range references {
112
+ if reference .Name () == "HEAD" && reference .Type () == plumbing .SymbolicReference {
113
+ return reference .Target ().String (), nil
114
+ }
115
+ }
116
+ return "" , fmt .Errorf ("unable to find default branch for git directory %s" , r .Directory )
117
+ }
118
+
95
119
// Update updates the plugin's Git repository to the ref if provided, or the
96
120
// latest commit on the current branch
97
121
func (r Repo ) Update (ref string ) (string , string , string , error ) {
@@ -100,29 +124,17 @@ func (r Repo) Update(ref string) (string, string, string, error) {
100
124
return "" , "" , "" , err
101
125
}
102
126
103
- oldHash , err := repo .ResolveRevision ( plumbing . Revision ( "HEAD" ) )
127
+ oldHash , err := repo .Head ( )
104
128
if err != nil {
105
129
return "" , "" , "" , err
106
130
}
107
131
108
- if ref == "" {
109
- // If no ref is provided checkout latest commit on current branch
110
- head , err := repo . Head ()
132
+ // If no ref is provided we take the default branch of the remote
133
+ if strings . TrimSpace ( ref ) == "" {
134
+ ref , err = r . RemoteDefaultBranch ()
111
135
if err != nil {
112
136
return "" , "" , "" , err
113
137
}
114
-
115
- if ! head .Name ().IsBranch () {
116
- return "" , "" , "" , fmt .Errorf ("not on a branch, unable to update" )
117
- }
118
-
119
- // If on a branch checkout the latest version of it from the remote
120
- branch := head .Name ()
121
- ref = branch .String ()
122
- checkoutOptions = git.CheckoutOptions {Branch : branch , Keep : true }
123
- } else {
124
- // Checkout ref if provided
125
- checkoutOptions = git.CheckoutOptions {Hash : plumbing .NewHash (ref ), Keep : true }
126
138
}
127
139
128
140
commonOpts := []string {"--git-dir" , filepath .Join (r .Directory , ".git" ), "--work-tree" , r .Directory }
@@ -144,14 +156,15 @@ func (r Repo) Update(ref string) (string, string, string, error) {
144
156
var stdErr2 strings.Builder
145
157
cmd .Stderr = & stdErr2
146
158
err = cmd .Run ()
159
+ if err != nil {
160
+ return "" , "" , "" , errors .New (stdErrToErrMsg (stdErr2 .String ()))
161
+ }
147
162
148
- fmt .Printf ("stdErr2.String() %#+v\n " , stdErr2 .String ())
149
- repo , err = gitOpen (r .Directory )
150
- newHash , err := repo .ResolveRevision (plumbing .Revision ("HEAD" ))
163
+ newHash , err := repo .Head ()
151
164
if err != nil {
152
- return ref , oldHash .String (), newHash .String (), errors . New ( stdErrToErrMsg ( stdErr2 . String ()) )
165
+ return ref , oldHash .String (), newHash .Hash (). String (), fmt . Errorf ( "unable to resolve plugin new Git HEAD: %w" , err )
153
166
}
154
- return ref , oldHash .String (), newHash .String (), nil
167
+ return ref , oldHash .String (), newHash .Hash (). String (), nil
155
168
}
156
169
157
170
func stdErrToErrMsg (stdErr string ) string {
0 commit comments