-
Notifications
You must be signed in to change notification settings - Fork 1
/
git.go
86 lines (77 loc) · 1.92 KB
/
git.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
package main
import (
"fmt"
"os/exec"
"regexp"
"strings"
)
func GitRemotes() ([]string, error) {
bin, err := exec.LookPath("git")
if err != nil {
return nil, fmt.Errorf("git command not found ? , %s", err)
}
c := exec.Command(bin, "remote", "-v")
b, err := c.Output()
if err != nil {
return nil, fmt.Errorf("can not read git remote url , %s", err)
}
if string(b) == "" {
return nil, fmt.Errorf(`remote not found, please run "git remote add" first`)
}
lines := strings.Split(string(b), "\n")
var result []string
re := regexp.MustCompile(`[\t\s]+`)
for _, line := range lines {
if line == "" {
continue
}
arr := re.Split(line, -1)
if len(arr) == 3 {
result = append(result, arr[1])
}
}
return git2https(uniq(result)), nil
}
// remove duplicate string
func uniq(list []string) []string {
var m = make(map[string]bool)
for i := range list {
m[list[i]] = true
}
var result []string
for k := range m {
result = append(result, k)
}
return result
}
// git ssh protocol address convert to https protocol address
// $remote = $remote =~ s/\.git$//r;
// $remote = $remote =~ s/^git@/https:\/\//r;
// $remote = $remote =~ s/(:)([^\/])/\/$2/r;
func git2https(origins []string) []string {
var result []string
end := regexp.MustCompile(`.git$`)
protocol := regexp.MustCompile(`^git@`)
s := regexp.MustCompile(`(:)([^\/])`)
for _, origin := range origins {
if protocol.Match([]byte(origin)) {
r := end.ReplaceAll([]byte(origin), []byte(""))
r = protocol.ReplaceAll(r, []byte("https://"))
r = s.ReplaceAll(r, []byte("/$2"))
result = append(result, string(r))
} else {
result = append(result, origin)
}
}
return result
}
func ISGithub(origin string) bool {
return strings.Contains(origin, "https://github.com")
}
func WithPipeline(origin string) string {
origin = strings.TrimSuffix(origin, ".git")
if ISGithub((origin)) {
return origin + `/actions`
}
return origin + `/-/pipelines`
}