@@ -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,28 +228,64 @@ 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" )
240
+ cmd .Dir = dst
241
+ err = getRunCommand (cmd )
242
+ if err != nil {
243
+ return err
244
+ }
245
+
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
252
+ }
253
+
254
+ // Fetch the remote ref
255
+ cmd = exec .CommandContext (ctx , "git" , "fetch" , "--tags" )
256
+ cmd .Dir = dst
257
+ err = getRunCommand (cmd )
258
+ if err != nil {
259
+ return err
260
+ }
261
+
262
+ // Fetch the remote ref
263
+ cmd = exec .CommandContext (ctx , "git" , "fetch" , "origin" , "--" , ref )
235
264
cmd .Dir = dst
265
+ err = getRunCommand (cmd )
266
+ if err != nil {
267
+ return err
268
+ }
236
269
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 )
270
+ // Reset the branch to the fetched ref
271
+ cmd = exec .CommandContext (ctx , "git" , "reset" , "--hard" , "FETCH_HEAD" )
272
+ cmd .Dir = dst
273
+ err = getRunCommand (cmd )
274
+ if err != nil {
275
+ return err
242
276
}
243
277
244
- // We have to be on a branch to pull
245
- if err := g .checkout (ctx , dst , ref ); err != nil {
278
+ // Checkout ref branch
279
+ err = g .checkout (ctx , dst , ref )
280
+ if err != nil {
246
281
return err
247
282
}
248
283
284
+ // Pull the latest changes from the ref branch
249
285
if depth > 0 {
250
- cmd = exec .CommandContext (ctx , "git" , "pull" , "--depth" , strconv .Itoa (depth ), "--ff-only" )
286
+ cmd = exec .CommandContext (ctx , "git" , "pull" , "origin" , " --depth" , strconv .Itoa (depth ), "--ff-only" , "--" , ref )
251
287
} else {
252
- cmd = exec .CommandContext (ctx , "git" , "pull" , "--ff-only" )
288
+ cmd = exec .CommandContext (ctx , "git" , "pull" , "origin" , " --ff-only", "--" , ref )
253
289
}
254
290
255
291
cmd .Dir = dst
@@ -377,3 +413,20 @@ func checkGitVersion(ctx context.Context, min string) error {
377
413
378
414
return nil
379
415
}
416
+
417
+ // removeCaseInsensitiveGitDirectory removes all .git directory variations
418
+ func removeCaseInsensitiveGitDirectory (dst string ) error {
419
+ files , err := os .ReadDir (dst )
420
+ if err != nil {
421
+ return fmt .Errorf ("Failed to read the destination directory %s during git update" , dst )
422
+ }
423
+ for _ , f := range files {
424
+ if strings .EqualFold (f .Name (), ".git" ) && f .IsDir () {
425
+ err := os .RemoveAll (filepath .Join (dst , f .Name ()))
426
+ if err != nil {
427
+ return fmt .Errorf ("Failed to remove the .git directory in the destination directory %s during git update" , dst )
428
+ }
429
+ }
430
+ }
431
+ return nil
432
+ }
0 commit comments