diff --git a/go.mod b/go.mod index dacf9f69..95b4a63c 100644 --- a/go.mod +++ b/go.mod @@ -34,7 +34,7 @@ require ( github.com/googleapis/gax-go/v2 v2.12.4 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect - github.com/hashicorp/go-getter v1.7.4 // indirect + github.com/hashicorp/go-getter v1.7.5 // indirect github.com/hashicorp/go-hclog v1.6.3 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/go-plugin v1.6.1 // indirect diff --git a/go.sum b/go.sum index 2121208d..f559b9a0 100644 --- a/go.sum +++ b/go.sum @@ -406,8 +406,8 @@ github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtng github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= github.com/hashicorp/go-getter v1.5.3/go.mod h1:BrrV/1clo8cCYu6mxvboYg+KutTiFnXjMEgDD8+i7ZI= -github.com/hashicorp/go-getter v1.7.4 h1:3yQjWuxICvSpYwqSayAdKRFcvBl1y/vogCxczWSmix0= -github.com/hashicorp/go-getter v1.7.4/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744= +github.com/hashicorp/go-getter v1.7.5 h1:dT58k9hQ/vbxNMwoI5+xFYAJuv6152UNvdHokfI5wE4= +github.com/hashicorp/go-getter v1.7.5/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744= github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd/go.mod h1:9bjs9uLqI8l75knNv3lV1kA55veR+WUPSiKIWcQHudI= github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ= github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k= diff --git a/vendor/github.com/hashicorp/go-getter/get_git.go b/vendor/github.com/hashicorp/go-getter/get_git.go index 28931753..f38e0d29 100644 --- a/vendor/github.com/hashicorp/go-getter/get_git.go +++ b/vendor/github.com/hashicorp/go-getter/get_git.go @@ -125,7 +125,7 @@ func (g *GitGetter) Get(dst string, u *url.URL) error { return err } if err == nil { - err = g.update(ctx, dst, sshKeyFile, ref, depth) + err = g.update(ctx, dst, sshKeyFile, u, ref, depth) } else { err = g.clone(ctx, dst, sshKeyFile, u, ref, depth) } @@ -228,28 +228,64 @@ func (g *GitGetter) clone(ctx context.Context, dst, sshKeyFile string, u *url.UR return nil } -func (g *GitGetter) update(ctx context.Context, dst, sshKeyFile, ref string, depth int) error { - // Determine if we're a branch. If we're NOT a branch, then we just - // switch to master prior to checking out - cmd := exec.CommandContext(ctx, "git", "show-ref", "-q", "--verify", "refs/heads/"+ref) +func (g *GitGetter) update(ctx context.Context, dst, sshKeyFile string, u *url.URL, ref string, depth int) error { + // Remove all variations of .git directories + err := removeCaseInsensitiveGitDirectory(dst) + if err != nil { + return err + } + + // Initialize the git repository + cmd := exec.CommandContext(ctx, "git", "init") + cmd.Dir = dst + err = getRunCommand(cmd) + if err != nil { + return err + } + + // Add the git remote + cmd = exec.CommandContext(ctx, "git", "remote", "add", "origin", "--", u.String()) + cmd.Dir = dst + err = getRunCommand(cmd) + if err != nil { + return err + } + + // Fetch the remote ref + cmd = exec.CommandContext(ctx, "git", "fetch", "--tags") + cmd.Dir = dst + err = getRunCommand(cmd) + if err != nil { + return err + } + + // Fetch the remote ref + cmd = exec.CommandContext(ctx, "git", "fetch", "origin", "--", ref) cmd.Dir = dst + err = getRunCommand(cmd) + if err != nil { + return err + } - if getRunCommand(cmd) != nil { - // Not a branch, switch to default branch. This will also catch - // non-existent branches, in which case we want to switch to default - // and then checkout the proper branch later. - ref = findDefaultBranch(ctx, dst) + // Reset the branch to the fetched ref + cmd = exec.CommandContext(ctx, "git", "reset", "--hard", "FETCH_HEAD") + cmd.Dir = dst + err = getRunCommand(cmd) + if err != nil { + return err } - // We have to be on a branch to pull - if err := g.checkout(ctx, dst, ref); err != nil { + // Checkout ref branch + err = g.checkout(ctx, dst, ref) + if err != nil { return err } + // Pull the latest changes from the ref branch if depth > 0 { - cmd = exec.CommandContext(ctx, "git", "pull", "--depth", strconv.Itoa(depth), "--ff-only") + cmd = exec.CommandContext(ctx, "git", "pull", "origin", "--depth", strconv.Itoa(depth), "--ff-only", "--", ref) } else { - cmd = exec.CommandContext(ctx, "git", "pull", "--ff-only") + cmd = exec.CommandContext(ctx, "git", "pull", "origin", "--ff-only", "--", ref) } cmd.Dir = dst @@ -377,3 +413,20 @@ func checkGitVersion(ctx context.Context, min string) error { return nil } + +// removeCaseInsensitiveGitDirectory removes all .git directory variations +func removeCaseInsensitiveGitDirectory(dst string) error { + files, err := os.ReadDir(dst) + if err != nil { + return fmt.Errorf("Failed to read the destination directory %s during git update", dst) + } + for _, f := range files { + if strings.EqualFold(f.Name(), ".git") && f.IsDir() { + err := os.RemoveAll(filepath.Join(dst, f.Name())) + if err != nil { + return fmt.Errorf("Failed to remove the .git directory in the destination directory %s during git update", dst) + } + } + } + return nil +} diff --git a/vendor/modules.txt b/vendor/modules.txt index d4a7e06b..936cbc01 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -186,7 +186,7 @@ github.com/hashicorp/errwrap # github.com/hashicorp/go-cleanhttp v0.5.2 ## explicit; go 1.13 github.com/hashicorp/go-cleanhttp -# github.com/hashicorp/go-getter v1.7.4 +# github.com/hashicorp/go-getter v1.7.5 ## explicit; go 1.13 github.com/hashicorp/go-getter github.com/hashicorp/go-getter/helper/url