From ae1789827b1a04be04b58d81fbbb8ab3ea3c08cc Mon Sep 17 00:00:00 2001 From: Kyle Hornberger Date: Fri, 6 May 2016 15:37:22 -0700 Subject: [PATCH] fix repo/path/tag detection to work with https repos --- stevedore.go | 36 ++++++++++++++++++++++++------------ stevedore_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/stevedore.go b/stevedore.go index 8ff6d96..82f9634 100644 --- a/stevedore.go +++ b/stevedore.go @@ -27,7 +27,11 @@ func FindImagesInCwd(filter cmd.FilterFunc) ([]Image, error) { } func findImages(filter cmd.FilterFunc, wd string) (images []Image, err error) { - repo, path, tag := detectRepoPathAndTag(wd) + repo, err := runCmdAndGetOutput("git", "config", "--get", "remote.origin.url") + if err != nil { + log.Fatal("error detecting git repo", err) + } + repo, path, tag := detectRepoPathAndTag(repo, wd) dockerfiles := findDockerfiles() for dockerfile, repos := range mapDockerfileToRepos(repo, path, tag, dockerfiles...) { if !filter(dockerfile) { @@ -57,21 +61,30 @@ func (i Image) Push() (err error) { return runCmdAndPipeOutput(cmd.Output, "docker", "push", i.Url) } -func detectRepoPathAndTag(wd string) (repo, path, tag string) { - repo, err := runCmdAndGetOutput("git", "config", "--get", "remote.origin.url") - if err != nil { - log.Fatal("error detecting git repo", err) - } - - if index := strings.LastIndex(repo, ":"); index != -1 { - repo = repo[index+1:] +func extractRepo(repo string) string { + if strings.HasPrefix(repo, "git") { + if index := strings.LastIndex(repo, ":"); index != -1 { + repo = repo[index+1:] + } + } else if strings.HasPrefix(repo, "http") { + if last := strings.LastIndex(repo, "/"); last != -1 { + tmp := repo[:last] + if first := strings.LastIndex(tmp, "/"); first != -1 { + repo = repo[first+1:] + } + } } if strings.HasSuffix(repo, ".git") { repo = repo[:len(repo)-4] } - path, err = runCmdAndGetOutput("git", "rev-parse", "--show-toplevel") + return repo +} + +func detectRepoPathAndTag(gitRemote, wd string) (repo, path, tag string) { + repo = extractRepo(gitRemote) + path, err := runCmdAndGetOutput("git", "rev-parse", "--show-toplevel") switch { case wd == path: path = "" @@ -81,7 +94,7 @@ func detectRepoPathAndTag(wd string) (repo, path, tag string) { default: log.Fatal("Current directory is not child of top level", wd, path) } - + if cmd.Tag == "" { tag, err = runCmdAndGetOutput("git", "rev-parse", "HEAD") @@ -92,7 +105,6 @@ func detectRepoPathAndTag(wd string) (repo, path, tag string) { tag = cmd.Tag } - if len(tag) > 7 { tag = tag[:7] } diff --git a/stevedore_test.go b/stevedore_test.go index 6989dc1..08ed7fe 100644 --- a/stevedore_test.go +++ b/stevedore_test.go @@ -37,3 +37,32 @@ func TestGenerateRepoNames(t *testing.T) { } } } + +type detectPathTagCase struct { + repo string + expected string +} + +var ( + testDetectPathTagCases = []detectPathTagCase{ + detectPathTagCase{ + repo: "git@github.com:foo/bar.git", + expected: "foo/bar", + }, + detectPathTagCase{ + repo: "https://github.com/foo/bar.git", + expected: "foo/bar", + }, + } +) + +func TestDetectRepoPathAndTag(t *testing.T) { + for _, testCase := range testDetectPathTagCases { + actual := extractRepo(testCase.repo) + + if actual != testCase.expected { + t.Errorf("Expected (%q), got (%q)", testCase.expected, actual) + t.FailNow() + } + } +}