@@ -125,7 +125,7 @@ func (g *GitGetter) Get(dst string, u *url.URL) error {
125
125
return err
126
126
}
127
127
if err == nil {
128
- err = g .update (ctx , dst , sshKeyFile , ref , depth )
128
+ err = g .update (ctx , dst , sshKeyFile , u , ref , depth )
129
129
} else {
130
130
err = g .clone (ctx , dst , sshKeyFile , u , ref , depth )
131
131
}
@@ -228,21 +228,48 @@ func (g *GitGetter) clone(ctx context.Context, dst, sshKeyFile string, u *url.UR
228
228
return nil
229
229
}
230
230
231
- func (g * GitGetter ) update (ctx context.Context , dst , sshKeyFile , ref string , depth int ) error {
232
- // Determine if we're a branch. If we're NOT a branch, then we just
233
- // switch to master prior to checking out
234
- cmd := exec .CommandContext (ctx , "git" , "show-ref" , "-q" , "--verify" , "refs/heads/" + ref )
231
+ func (g * GitGetter ) update (ctx context.Context , dst , sshKeyFile string , u * url.URL , ref string , depth int ) error {
232
+ // Remove all variations of .git directories
233
+ err := removeCaseInsensitiveGitDirectory (dst )
234
+ if err != nil {
235
+ return err
236
+ }
237
+
238
+ // Initialize the git repository
239
+ cmd := exec .CommandContext (ctx , "git" , "init" )
235
240
cmd .Dir = dst
241
+ err = getRunCommand (cmd )
242
+ if err != nil {
243
+ return err
244
+ }
236
245
237
- if getRunCommand (cmd ) != nil {
238
- // Not a branch, switch to default branch. This will also catch
239
- // non-existent branches, in which case we want to switch to default
240
- // and then checkout the proper branch later.
241
- ref = findDefaultBranch (ctx , dst )
246
+ // Add the git remote
247
+ cmd = exec .CommandContext (ctx , "git" , "remote" , "add" , "origin" , "--" , u .String ())
248
+ cmd .Dir = dst
249
+ err = getRunCommand (cmd )
250
+ if err != nil {
251
+ return err
242
252
}
243
253
244
- // We have to be on a branch to pull
245
- if err := g .checkout (ctx , dst , ref ); err != nil {
254
+ // Fetch the remote ref
255
+ cmd = exec .CommandContext (ctx , "git" , "fetch" , "origin" , "--" , ref )
256
+ cmd .Dir = dst
257
+ err = getRunCommand (cmd )
258
+ if err != nil {
259
+ return err
260
+ }
261
+
262
+ // Reset the branch to the fetched ref
263
+ cmd = exec .CommandContext (ctx , "git" , "reset" , "--hard" , "FETCH_HEAD" )
264
+ cmd .Dir = dst
265
+ err = getRunCommand (cmd )
266
+ if err != nil {
267
+ return err
268
+ }
269
+
270
+ // Checkout ref branch
271
+ err = g .checkout (ctx , dst , ref )
272
+ if err != nil {
246
273
return err
247
274
}
248
275
@@ -377,3 +404,20 @@ func checkGitVersion(ctx context.Context, min string) error {
377
404
378
405
return nil
379
406
}
407
+
408
+ // removeCaseInsensitiveGitDirectory removes all .git directory variations
409
+ func removeCaseInsensitiveGitDirectory (dst string ) error {
410
+ files , err := os .ReadDir (dst )
411
+ if err != nil {
412
+ return fmt .Errorf ("Failed to read the destination directory %s during git update" , dst )
413
+ }
414
+ for _ , f := range files {
415
+ if strings .EqualFold (f .Name (), ".git" ) && f .IsDir () {
416
+ err := os .RemoveAll (filepath .Join (dst , f .Name ()))
417
+ if err != nil {
418
+ return fmt .Errorf ("Failed to remove the .git directory in the destination directory %s during git update" , dst )
419
+ }
420
+ }
421
+ }
422
+ return nil
423
+ }
0 commit comments