Skip to content

Commit

Permalink
Regex tweaks
Browse files Browse the repository at this point in the history
- No need to compile regexes in the function, moved them to vars
- Fixed github http regex: it was not matching https://github.com/foo/bar (no git extension)
  • Loading branch information
sosedoff committed Feb 14, 2019
1 parent 2e52ef6 commit fb77d5a
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions common/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ import (
yaml "gopkg.in/yaml.v2"
)

var cloneLock sync.Mutex
var (
codeCommitHTTPRegex = regexp.MustCompile(`^https?://git-codecommit\.(.+)\.amazonaws.com/v1/repos/(.+)$`)
codeCommitSSHRegex = regexp.MustCompile(`ssh://git-codecommit\.(.+)\.amazonaws.com/v1/repos/(.+)$`)
githubHTTPRegex = regexp.MustCompile(`^https?://.*github.com.*/(.+)/(.+?)(?:.git)?$`)
githubSSHRegex = regexp.MustCompile(`github.com[:/](.+)/(.+).git$`)

cloneLock sync.Mutex
)

// FindGitRevision get the current git revision
func FindGitRevision(file string) (shortSha string, sha string, err error) {
Expand Down Expand Up @@ -134,18 +141,13 @@ func findGitRemoteURL(file string) (string, error) {
}

func findGitSlug(url string) (string, string, error) {
codeCommitHTTPRegex := regexp.MustCompile(`^http(s?)://git-codecommit\.(.+)\.amazonaws.com/v1/repos/(.+)$`)
codeCommitSSHRegex := regexp.MustCompile(`ssh://git-codecommit\.(.+)\.amazonaws.com/v1/repos/(.+)$`)
httpRegex := regexp.MustCompile("^http(s?)://.*github.com.*/(.+)/(.+).git$")
sshRegex := regexp.MustCompile("github.com[:/](.+)/(.+).git$")

if matches := codeCommitHTTPRegex.FindStringSubmatch(url); matches != nil {
return "CodeCommit", matches[3], nil
return "CodeCommit", matches[2], nil
} else if matches := codeCommitSSHRegex.FindStringSubmatch(url); matches != nil {
return "CodeCommit", matches[2], nil
} else if matches := httpRegex.FindStringSubmatch(url); matches != nil {
return "GitHub", fmt.Sprintf("%s/%s", matches[2], matches[3]), nil
} else if matches := sshRegex.FindStringSubmatch(url); matches != nil {
} else if matches := githubHTTPRegex.FindStringSubmatch(url); matches != nil {
return "GitHub", fmt.Sprintf("%s/%s", matches[1], matches[2]), nil
} else if matches := githubSSHRegex.FindStringSubmatch(url); matches != nil {
return "GitHub", fmt.Sprintf("%s/%s", matches[1], matches[2]), nil
}
return "", url, nil
Expand Down

0 comments on commit fb77d5a

Please sign in to comment.