Skip to content

Commit 02d2d38

Browse files
committed
Deal with branch spec in simpleGitCloner.
1 parent 5990af8 commit 02d2d38

File tree

2 files changed

+84
-39
lines changed

2 files changed

+84
-39
lines changed

pkg/loader/gitcloner.go

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,42 @@ func simpleGitCloner(spec string) (
6363
if err != nil {
6464
return
6565
}
66-
url, pathInCoDir, err := extractGithubRepoName(spec)
67-
cmd := exec.Command(gitProgram, "clone", url, checkoutDir)
66+
repo, pathInCoDir, gitRef, err := parseGithubUrl(spec)
67+
if err != nil {
68+
return
69+
}
70+
cmd := exec.Command(
71+
gitProgram,
72+
"clone",
73+
"https://github.com/"+repo+".git",
74+
checkoutDir)
6875
var out bytes.Buffer
6976
cmd.Stdout = &out
7077
err = cmd.Run()
7178
if err != nil {
72-
return "", "", errors.Wrapf(err, "trouble cloning %s", spec)
79+
return "", "",
80+
errors.Wrapf(err, "trouble cloning %s", spec)
7381
}
74-
return
82+
if gitRef == "" {
83+
return
84+
}
85+
cmd = exec.Command(gitProgram, "checkout", gitRef)
86+
cmd.Dir = checkoutDir
87+
err = cmd.Run()
88+
if err != nil {
89+
return "", "",
90+
errors.Wrapf(err, "trouble checking out href %s", gitRef)
91+
}
92+
return checkoutDir, pathInCoDir, nil
7593
}
7694

95+
const refQuery = "?ref="
96+
7797
// From strings like [email protected]:someOrg/someRepo.git or
78-
// https://github.com/someOrg/someRepo, extract path.
79-
func extractGithubRepoName(n string) (string, string, error) {
98+
// https://github.com/someOrg/someRepo?ref=someHash, extract
99+
// the parts.
100+
func parseGithubUrl(n string) (
101+
repo string, path string, gitRef string, err error) {
80102
for _, p := range []string{
81103
// Order matters here.
82104
"git::", "gh:", "https://", "http://",
@@ -90,15 +112,26 @@ func extractGithubRepoName(n string) (string, string, error) {
90112
}
91113
i := strings.Index(n, string(filepath.Separator))
92114
if i < 1 {
93-
return "", "", errors.New("no separator")
115+
return "", "", "", errors.New("no separator")
94116
}
95117
j := strings.Index(n[i+1:], string(filepath.Separator))
96-
if j < 0 {
97-
// No path, so show entire repo.
98-
return n, "", nil
118+
if j >= 0 {
119+
j += i + 1
120+
repo = n[:j]
121+
path, gitRef = peelQuery(n[j+1:])
122+
} else {
123+
path = ""
124+
repo, gitRef = peelQuery(n)
125+
}
126+
return
127+
}
128+
129+
func peelQuery(arg string) (string, string) {
130+
j := strings.Index(arg, refQuery)
131+
if j >= 0 {
132+
return arg[:j], arg[j+len(refQuery):]
99133
}
100-
j += i + 1
101-
return n[:j], n[j+1:], nil
134+
return arg, ""
102135
}
103136

104137
func hashicorpGitCloner(repoUrl string) (

pkg/loader/gitcloner_test.go

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ whatever
181181

182182
var repoNames = []string{"someOrg/someRepo", "kubernetes/website"}
183183

184-
var paths = []string{"", "README.md", "foo/index.md"}
184+
var paths = []string{"README.md", "foo/krusty.txt", ""}
185+
186+
var hrefArgs = []string{"someBranch", ""}
185187

186188
var extractFmts = []string{
187189
"gh:%s",
@@ -190,38 +192,48 @@ var extractFmts = []string{
190192
"https://github.com/%s",
191193
"hTTps://github.com/%s",
192194
"git::https://gitlab.com/%s",
193-
194195
"github.com:%s",
195196
}
196197

197-
func TestExtractGithubRepoName(t *testing.T) {
198+
func TestParseGithubUrl(t *testing.T) {
198199
for _, repoName := range repoNames {
199200
for _, pathName := range paths {
200201
for _, extractFmt := range extractFmts {
201-
spec := repoName
202-
if len(pathName) > 0 {
203-
spec = filepath.Join(spec, pathName)
204-
}
205-
input := fmt.Sprintf(extractFmt, spec)
206-
if !isRepoUrl(input) {
207-
t.Errorf("Should smell like github arg: %s\n", input)
208-
continue
209-
}
210-
repo, path, err := extractGithubRepoName(input)
211-
if err != nil {
212-
t.Errorf("problem %v", err)
213-
}
214-
if repo != repoName {
215-
t.Errorf("\n"+
216-
" from %s\n"+
217-
" gotRepo %s\n"+
218-
"desiredRepo %s\n", input, repo, repoName)
219-
}
220-
if path != pathName {
221-
t.Errorf("\n"+
222-
" from %s\n"+
223-
" gotPath %s\n"+
224-
"desiredPath %s\n", input, path, pathName)
202+
for _, hrefArg := range hrefArgs {
203+
spec := repoName
204+
if len(pathName) > 0 {
205+
spec = filepath.Join(spec, pathName)
206+
}
207+
input := fmt.Sprintf(extractFmt, spec)
208+
if hrefArg != "" {
209+
input = input + refQuery + hrefArg
210+
}
211+
if !isRepoUrl(input) {
212+
t.Errorf("Should smell like github arg: %s\n", input)
213+
continue
214+
}
215+
repo, path, gitRef, err := parseGithubUrl(input)
216+
if err != nil {
217+
t.Errorf("problem %v", err)
218+
}
219+
if repo != repoName {
220+
t.Errorf("\n"+
221+
" from %s\n"+
222+
" actual Repo %s\n"+
223+
"expected Repo %s\n", input, repo, repoName)
224+
}
225+
if path != pathName {
226+
t.Errorf("\n"+
227+
" from %s\n"+
228+
" actual Path %s\n"+
229+
"expected Path %s\n", input, path, pathName)
230+
}
231+
if gitRef != hrefArg {
232+
t.Errorf("\n"+
233+
" from %s\n"+
234+
" actual Href %s\n"+
235+
"expected Href %s\n", input, gitRef, hrefArg)
236+
}
225237
}
226238
}
227239
}

0 commit comments

Comments
 (0)