Skip to content

Commit 9b7ddd6

Browse files
committed
Merge in cloner_tmp, preserving blame history.
2 parents bcb939c + e2102de commit 9b7ddd6

File tree

2 files changed

+503
-0
lines changed

2 files changed

+503
-0
lines changed

pkg/git/cloner_tmp.go

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package git
18+
19+
import (
20+
"bytes"
21+
"io/ioutil"
22+
"os/exec"
23+
"path/filepath"
24+
"regexp"
25+
"strings"
26+
27+
"github.com/pkg/errors"
28+
)
29+
30+
// Cloner is a function that can clone a git repo.
31+
type Cloner func(url string) (
32+
// Directory where the repo is cloned to.
33+
checkoutDir string,
34+
// Relative path in the checkoutDir to location
35+
// of kustomization file.
36+
pathInCoDir string,
37+
// Any error encountered when cloning.
38+
err error)
39+
40+
// IsRepoUrl checks if a string is likely a github repo Url.
41+
func IsRepoUrl(arg string) bool {
42+
arg = strings.ToLower(arg)
43+
return !filepath.IsAbs(arg) &&
44+
(strings.HasPrefix(arg, "git::") ||
45+
strings.HasPrefix(arg, "gh:") ||
46+
strings.HasPrefix(arg, "ssh:") ||
47+
strings.HasPrefix(arg, "github.com") ||
48+
strings.HasPrefix(arg, "git@") ||
49+
strings.Index(arg, "github.com/") > -1 ||
50+
isAzureHost(arg) || isAWSHost(arg))
51+
}
52+
53+
func makeTmpDir() (string, error) {
54+
return ioutil.TempDir("", "kustomize-")
55+
}
56+
57+
func ClonerUsingGitExec(spec string) (
58+
checkoutDir string, pathInCoDir string, err error) {
59+
gitProgram, err := exec.LookPath("git")
60+
if err != nil {
61+
return "", "", errors.Wrap(err, "no 'git' program on path")
62+
}
63+
checkoutDir, err = makeTmpDir()
64+
if err != nil {
65+
return
66+
}
67+
repo, pathInCoDir, gitRef, err := parseUrl(spec)
68+
if err != nil {
69+
return
70+
}
71+
cmd := exec.Command(
72+
gitProgram,
73+
"clone",
74+
repo,
75+
checkoutDir)
76+
var out bytes.Buffer
77+
cmd.Stdout = &out
78+
err = cmd.Run()
79+
if err != nil {
80+
return "", "",
81+
errors.Wrapf(err, "trouble cloning %s", spec)
82+
}
83+
if gitRef == "" {
84+
return
85+
}
86+
cmd = exec.Command(gitProgram, "checkout", gitRef)
87+
cmd.Dir = checkoutDir
88+
err = cmd.Run()
89+
if err != nil {
90+
return "", "",
91+
errors.Wrapf(err, "trouble checking out href %s", gitRef)
92+
}
93+
return checkoutDir, pathInCoDir, nil
94+
}
95+
96+
func parseUrl(n string) (
97+
repo string, path string, gitRef string, err error) {
98+
host, repo, path, gitRef, err := parseGithubUrl(n)
99+
if err != nil {
100+
return
101+
}
102+
if isAzureHost(host) || isAWSHost(host) {
103+
repo = host + repo
104+
return
105+
}
106+
repo = host + repo + ".git"
107+
return
108+
}
109+
110+
const (
111+
refQuery = "?ref="
112+
gitSuffix = ".git"
113+
)
114+
115+
// From strings like [email protected]:someOrg/someRepo.git or
116+
// https://github.com/someOrg/someRepo?ref=someHash, extract
117+
// the parts.
118+
func parseGithubUrl(n string) (
119+
host string, repo string, path string, gitRef string, err error) {
120+
host, n = parseHostSpec(n)
121+
host = normalizeGitHostSpec(host)
122+
123+
if strings.HasSuffix(n, gitSuffix) {
124+
repo = n[0 : len(n)-len(gitSuffix)]
125+
return
126+
}
127+
if strings.Contains(n, gitSuffix) {
128+
index := strings.Index(n, gitSuffix)
129+
repo = n[0:index]
130+
n = n[index+len(gitSuffix):]
131+
path, gitRef = peelQuery(n)
132+
return
133+
}
134+
i := strings.Index(n, "/")
135+
if i < 1 {
136+
return "", "", "", "", errors.New("no separator")
137+
}
138+
j := strings.Index(n[i+1:], "/")
139+
if j >= 0 {
140+
j += i + 1
141+
repo = n[:j]
142+
path, gitRef = peelQuery(n[j+1:])
143+
} else {
144+
path = ""
145+
repo, gitRef = peelQuery(n)
146+
}
147+
return
148+
}
149+
150+
func peelQuery(arg string) (string, string) {
151+
j := strings.Index(arg, refQuery)
152+
if j >= 0 {
153+
return arg[:j], arg[j+len(refQuery):]
154+
}
155+
return arg, ""
156+
}
157+
158+
func parseHostSpec(n string) (string, string) {
159+
var host string
160+
for _, p := range []string{
161+
// Order matters here.
162+
"git::", "gh:", "ssh://", "https://", "http://",
163+
"git@", "github.com:", "github.com/"} {
164+
if strings.ToLower(n[:len(p)]) == p {
165+
n = n[len(p):]
166+
host = host + p
167+
}
168+
}
169+
170+
// If host is a http(s) or ssh URL, grab the domain part.
171+
for _, p := range []string{
172+
"ssh://", "https://", "http://"} {
173+
if strings.HasSuffix(strings.ToLower(host), p) {
174+
index := regexp.MustCompile("^(.*?)/").FindStringIndex(n)
175+
if len(index) > 0 {
176+
host = host + n[0:index[len(index)-1]]
177+
n = n[index[len(index)-1]:]
178+
}
179+
}
180+
}
181+
return host, n
182+
}
183+
184+
func normalizeGitHostSpec(host string) string {
185+
s := strings.ToLower(host)
186+
if strings.Contains(s, "github.com") {
187+
if strings.Contains(s, "git@") || strings.Contains(s, "ssh:") {
188+
189+
} else {
190+
host = "https://github.com/"
191+
}
192+
}
193+
if strings.HasPrefix(s, "git::") {
194+
host = strings.TrimLeft(s, "git::")
195+
}
196+
return host
197+
}
198+
199+
// The format of Azure repo URL is documented
200+
// https://docs.microsoft.com/en-us/azure/devops/repos/git/clone?view=vsts&tabs=visual-studio#clone_url
201+
func isAzureHost(host string) bool {
202+
return strings.Contains(host, "dev.azure.com") ||
203+
strings.Contains(host, "visualstudio.com")
204+
}
205+
206+
// The format of AWS repo URL is documented
207+
// https://docs.aws.amazon.com/codecommit/latest/userguide/regions.html
208+
func isAWSHost(host string) bool {
209+
return strings.Contains(host, "amazonaws.com")
210+
}

0 commit comments

Comments
 (0)