@@ -125,7 +125,7 @@ func (g *GitGetter) Get(dst string, u *url.URL) error {
125125 return err
126126 }
127127 if err == nil {
128- err = g .update (ctx , dst , sshKeyFile , ref , depth )
128+ err = g .update (ctx , dst , sshKeyFile , u , ref , depth )
129129 } else {
130130 err = g .clone (ctx , dst , sshKeyFile , u , ref , depth )
131131 }
@@ -228,28 +228,64 @@ func (g *GitGetter) clone(ctx context.Context, dst, sshKeyFile string, u *url.UR
228228 return nil
229229}
230230
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 )
235264 cmd .Dir = dst
265+ err = getRunCommand (cmd )
266+ if err != nil {
267+ return err
268+ }
236269
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
242276 }
243277
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 {
246281 return err
247282 }
248283
284+ // Pull the latest changes from the ref branch
249285 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 )
251287 } else {
252- cmd = exec .CommandContext (ctx , "git" , "pull" , "--ff-only" )
288+ cmd = exec .CommandContext (ctx , "git" , "pull" , "origin" , " --ff-only", "--" , ref )
253289 }
254290
255291 cmd .Dir = dst
@@ -377,3 +413,20 @@ func checkGitVersion(ctx context.Context, min string) error {
377413
378414 return nil
379415}
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