-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
39 lines (34 loc) · 1.31 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package gro
import (
"errors"
"regexp"
)
type RepoInfo struct {
Hostname, User, Repo string
}
const invalidURLMessage string = "invalid git remote url.\nPlease run 'git remote -v' to check it."
func GetRepoURL(remoteURLString string) (string, error) {
whenSSHRegexp := regexp.MustCompile(`^origin\s+git@`)
whenHTTPSRegexp := regexp.MustCompile(`^origin\s+https:`)
var repoInfoVal RepoInfo
var repoInfoErr error
if whenSSHRegexp.MatchString(remoteURLString) {
repoInfoVal, repoInfoErr = buildRepoInfo(remoteURLString, `^origin\s+git@(.*):(.*)\/(.*).git`)
} else if whenHTTPSRegexp.MatchString(remoteURLString) {
repoInfoVal, repoInfoErr = buildRepoInfo(remoteURLString, `^origin\s+https:\/\/(.*)\/(.*)\/(.*).git`)
} else {
return "", errors.New(invalidURLMessage)
}
if repoInfoErr != nil {
return "", errors.New(invalidURLMessage)
}
return "https://" + repoInfoVal.Hostname + "/" + repoInfoVal.User + "/" + repoInfoVal.Repo, nil
}
func buildRepoInfo(remoteURLString string, regexpString string) (RepoInfo, error) {
repoInfoRegexp := regexp.MustCompile(regexpString)
matchingResult := repoInfoRegexp.FindStringSubmatch(remoteURLString)
if len(matchingResult) == 0 {
return RepoInfo{"", "", ""}, errors.New(invalidURLMessage)
}
return RepoInfo{matchingResult[1], matchingResult[2], matchingResult[3]}, nil
}