-
Notifications
You must be signed in to change notification settings - Fork 1
/
git_service.go
113 lines (96 loc) · 3.09 KB
/
git_service.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"net/url"
"regexp"
"strings"
)
var (
googlesourceRegexp = regexp.MustCompile("^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\\.googlesource\\.com$")
)
type buildURLFunc func(url url.URL, filePath, branch string, line1, line2 int) string
// buildGithubURL build URL for Github
// Format: https://github.com/<user>/<repos>/tree/<branch>/path/to/file.txt#L10-L20
func buildGithubURL(baseURL url.URL, filePath, branch string, line1, line2 int) string {
filePath = strings.TrimLeft(filePath, "/")
lineStr := ""
if line1 != 0 {
lineStr = fmt.Sprintf("L%d", line1)
if line2 != 0 {
lineStr = lineStr + fmt.Sprintf("-L%d", line2)
}
}
baseURL.Path = fmt.Sprintf("%s/tree/%s/%s", baseURL.Path, branch, filePath)
baseURL.Fragment = lineStr
return baseURL.String()
}
// buildBitbucketURL build URL for bitbucket
// Format: https://bitbucket.org/<user>/<repos>/src/<branch>/file.txt#lines-10:20
func buildBitbucketURL(baseURL url.URL, filePath, branch string, line1, line2 int) string {
filePath = strings.TrimLeft(filePath, "/")
lineStr := ""
if line1 != 0 {
lineStr = fmt.Sprintf("lines-%d", line1)
if line2 != 0 {
lineStr = lineStr + fmt.Sprintf(":%d", line2)
}
}
baseURL.Path = fmt.Sprintf("%s/src/%s/%s", baseURL.Path, branch, filePath)
baseURL.Fragment = lineStr
return baseURL.String()
}
// buildGitlabURL build URL for gitlab
// Format: https://gitlab.com/<user>/<repos>/-/blob/<branch>/file.txt#L10-20
func buildGitlabURL(baseURL url.URL, filePath, branch string, line1, line2 int) string {
filePath = strings.TrimLeft(filePath, "/")
lineStr := ""
if line1 != 0 {
lineStr = fmt.Sprintf("L%d", line1)
if line2 != 0 {
lineStr = lineStr + fmt.Sprintf("-%d", line2)
}
}
baseURL.Path = fmt.Sprintf("%s/-/blob/%s/%s", baseURL.Path, branch, filePath)
baseURL.Fragment = lineStr
return baseURL.String()
}
// buildGooglesourceURL build URL for *.googlesource.com
// Format: https://code.googlesource.com/<repos>/+/<branch>/file.txt#2
func buildGooglesourceURL(baseURL url.URL, filePath, branch string, line1, line2 int) string {
filePath = strings.TrimLeft(filePath, "/")
lineStr := ""
if line1 != 0 {
lineStr = fmt.Sprintf("%d", line1)
}
baseURL.Path = fmt.Sprintf("%s/+/%s/%s", baseURL.Path, branch, filePath)
baseURL.Fragment = lineStr
return baseURL.String()
}
func buildURL(baseURL url.URL, path, branch string, line1, line2 int, urlType string) (string, error) {
buildFunc, err := getGitURLBuilder(baseURL, urlType)
if err != nil {
return "", err
}
remoteURL := buildFunc(baseURL, path, branch, line1, line2)
return remoteURL, nil
}
func getGitURLBuilder(baseURL url.URL, urlType string) (buildURLFunc, error) {
host := baseURL.Hostname()
if urlType != "" {
host = urlType
}
switch host {
case "github.com":
return buildGithubURL, nil
case "gitlab.com":
return buildGitlabURL, nil
case "bitbucket.org":
return buildBitbucketURL, nil
}
// *.googlesource.com (eg: code.googlesource.com)
switch {
case googlesourceRegexp.MatchString(host):
return buildGooglesourceURL, nil
}
return nil, fmt.Errorf("unknown git service: '%s'", host)
}