-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.go
46 lines (36 loc) · 1.34 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
package cicd
import (
"strings"
)
func GitLastCommit(url string, branch string) (string, int) {
out, _, ecode := ExecuteCommand("git", []string{"ls-remote", "--heads", url, branch}, nil)
return processGitLastCommit(branch, out, ecode)
}
func GitLastCommitSSH(url string, branch string, idrsa string) (string, int) {
out, _, ecode := ExecuteCommand("git", []string{"ls-remote", "--heads", url, branch}, []string{"GIT_SSH_VARIANT=ssh", "GIT_SSH_COMMAND=ssh -i " + idrsa})
return processGitLastCommit(branch, out, ecode)
}
func processGitLastCommit(branch string, out string, ecode int) (string, int) {
if ecode != 0 {
return "", ecode
}
lines := strings.Split(out, "\n")
for _, l := range lines {
fields := strings.Fields(l)
if len(fields) == 2 && strings.HasPrefix(fields[1], "refs/heads/") {
br := strings.ReplaceAll(fields[1], "refs/heads/", "")
if br == branch {
return fields[0], ecode
}
}
}
return "", ecode
}
func GitClone(url string, branch string, folder string) int {
_, _, ecode := ExecuteCommand("git", []string{"clone", "-b", branch, url, folder}, nil)
return ecode
}
func GitCloneSSH(url string, branch string, folder string, idrsa string) int {
_, _, ecode := ExecuteCommand("git", []string{"clone", "-b", branch, url, folder}, []string{"GIT_SSH_VARIANT=ssh", "GIT_SSH_COMMAND=ssh -i " + idrsa})
return ecode
}