Deal with branch spec in simpleGitCloner.

This commit is contained in:
jregan
2018-11-22 10:53:14 -08:00
parent 5990af8ced
commit 02d2d38c21
2 changed files with 84 additions and 39 deletions

View File

@@ -63,20 +63,42 @@ func simpleGitCloner(spec string) (
if err != nil { if err != nil {
return return
} }
url, pathInCoDir, err := extractGithubRepoName(spec) repo, pathInCoDir, gitRef, err := parseGithubUrl(spec)
cmd := exec.Command(gitProgram, "clone", url, checkoutDir) if err != nil {
return
}
cmd := exec.Command(
gitProgram,
"clone",
"https://github.com/"+repo+".git",
checkoutDir)
var out bytes.Buffer var out bytes.Buffer
cmd.Stdout = &out cmd.Stdout = &out
err = cmd.Run() err = cmd.Run()
if err != nil { if err != nil {
return "", "", errors.Wrapf(err, "trouble cloning %s", spec) return "", "",
errors.Wrapf(err, "trouble cloning %s", spec)
} }
return if gitRef == "" {
return
}
cmd = exec.Command(gitProgram, "checkout", gitRef)
cmd.Dir = checkoutDir
err = cmd.Run()
if err != nil {
return "", "",
errors.Wrapf(err, "trouble checking out href %s", gitRef)
}
return checkoutDir, pathInCoDir, nil
} }
const refQuery = "?ref="
// From strings like git@github.com:someOrg/someRepo.git or // From strings like git@github.com:someOrg/someRepo.git or
// https://github.com/someOrg/someRepo, extract path. // https://github.com/someOrg/someRepo?ref=someHash, extract
func extractGithubRepoName(n string) (string, string, error) { // the parts.
func parseGithubUrl(n string) (
repo string, path string, gitRef string, err error) {
for _, p := range []string{ for _, p := range []string{
// Order matters here. // Order matters here.
"git::", "gh:", "https://", "http://", "git::", "gh:", "https://", "http://",
@@ -90,15 +112,26 @@ func extractGithubRepoName(n string) (string, string, error) {
} }
i := strings.Index(n, string(filepath.Separator)) i := strings.Index(n, string(filepath.Separator))
if i < 1 { if i < 1 {
return "", "", errors.New("no separator") return "", "", "", errors.New("no separator")
} }
j := strings.Index(n[i+1:], string(filepath.Separator)) j := strings.Index(n[i+1:], string(filepath.Separator))
if j < 0 { if j >= 0 {
// No path, so show entire repo. j += i + 1
return n, "", nil repo = n[:j]
path, gitRef = peelQuery(n[j+1:])
} else {
path = ""
repo, gitRef = peelQuery(n)
} }
j += i + 1 return
return n[:j], n[j+1:], nil }
func peelQuery(arg string) (string, string) {
j := strings.Index(arg, refQuery)
if j >= 0 {
return arg[:j], arg[j+len(refQuery):]
}
return arg, ""
} }
func hashicorpGitCloner(repoUrl string) ( func hashicorpGitCloner(repoUrl string) (

View File

@@ -181,7 +181,9 @@ whatever
var repoNames = []string{"someOrg/someRepo", "kubernetes/website"} var repoNames = []string{"someOrg/someRepo", "kubernetes/website"}
var paths = []string{"", "README.md", "foo/index.md"} var paths = []string{"README.md", "foo/krusty.txt", ""}
var hrefArgs = []string{"someBranch", ""}
var extractFmts = []string{ var extractFmts = []string{
"gh:%s", "gh:%s",
@@ -190,38 +192,48 @@ var extractFmts = []string{
"https://github.com/%s", "https://github.com/%s",
"hTTps://github.com/%s", "hTTps://github.com/%s",
"git::https://gitlab.com/%s", "git::https://gitlab.com/%s",
"git@gitHUB.com:%s.git",
"github.com:%s", "github.com:%s",
} }
func TestExtractGithubRepoName(t *testing.T) { func TestParseGithubUrl(t *testing.T) {
for _, repoName := range repoNames { for _, repoName := range repoNames {
for _, pathName := range paths { for _, pathName := range paths {
for _, extractFmt := range extractFmts { for _, extractFmt := range extractFmts {
spec := repoName for _, hrefArg := range hrefArgs {
if len(pathName) > 0 { spec := repoName
spec = filepath.Join(spec, pathName) if len(pathName) > 0 {
} spec = filepath.Join(spec, pathName)
input := fmt.Sprintf(extractFmt, spec) }
if !isRepoUrl(input) { input := fmt.Sprintf(extractFmt, spec)
t.Errorf("Should smell like github arg: %s\n", input) if hrefArg != "" {
continue input = input + refQuery + hrefArg
} }
repo, path, err := extractGithubRepoName(input) if !isRepoUrl(input) {
if err != nil { t.Errorf("Should smell like github arg: %s\n", input)
t.Errorf("problem %v", err) continue
} }
if repo != repoName { repo, path, gitRef, err := parseGithubUrl(input)
t.Errorf("\n"+ if err != nil {
" from %s\n"+ t.Errorf("problem %v", err)
" gotRepo %s\n"+ }
"desiredRepo %s\n", input, repo, repoName) if repo != repoName {
} t.Errorf("\n"+
if path != pathName { " from %s\n"+
t.Errorf("\n"+ " actual Repo %s\n"+
" from %s\n"+ "expected Repo %s\n", input, repo, repoName)
" gotPath %s\n"+ }
"desiredPath %s\n", input, path, pathName) if path != pathName {
t.Errorf("\n"+
" from %s\n"+
" actual Path %s\n"+
"expected Path %s\n", input, path, pathName)
}
if gitRef != hrefArg {
t.Errorf("\n"+
" from %s\n"+
" actual Href %s\n"+
"expected Href %s\n", input, gitRef, hrefArg)
}
} }
} }
} }