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 {
return
}
url, pathInCoDir, err := extractGithubRepoName(spec)
cmd := exec.Command(gitProgram, "clone", url, checkoutDir)
repo, pathInCoDir, gitRef, err := parseGithubUrl(spec)
if err != nil {
return
}
cmd := exec.Command(
gitProgram,
"clone",
"https://github.com/"+repo+".git",
checkoutDir)
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
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
// https://github.com/someOrg/someRepo, extract path.
func extractGithubRepoName(n string) (string, string, error) {
// https://github.com/someOrg/someRepo?ref=someHash, extract
// the parts.
func parseGithubUrl(n string) (
repo string, path string, gitRef string, err error) {
for _, p := range []string{
// Order matters here.
"git::", "gh:", "https://", "http://",
@@ -90,15 +112,26 @@ func extractGithubRepoName(n string) (string, string, error) {
}
i := strings.Index(n, string(filepath.Separator))
if i < 1 {
return "", "", errors.New("no separator")
return "", "", "", errors.New("no separator")
}
j := strings.Index(n[i+1:], string(filepath.Separator))
if j < 0 {
// No path, so show entire repo.
return n, "", nil
if j >= 0 {
j += i + 1
repo = n[:j]
path, gitRef = peelQuery(n[j+1:])
} else {
path = ""
repo, gitRef = peelQuery(n)
}
j += i + 1
return n[:j], n[j+1:], nil
return
}
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) (

View File

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