-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-ls-remote.go
76 lines (65 loc) · 1.8 KB
/
git-ls-remote.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
package gomod
import (
"fmt"
"os/exec"
"regexp"
"strings"
"github.com/sirupsen/logrus"
)
var (
lsRemote _lsRemote
tagRegexp = regexp.MustCompile(`refs/tags/(.*)`)
headRegexp = regexp.MustCompile(`refs/heads/(master|main)`)
)
type _lsRemote struct {
url string
output string
}
func (lr *_lsRemote) setUrl(url string) *_lsRemote {
url = strings.ReplaceAll(url, "https://", "")
url = strings.ReplaceAll(url, "http://", "")
if v := strings.Split(url, "/"); len(v) > 3 {
url = strings.Join(v[:3], "/")
}
if !strings.HasPrefix(url, "https") {
url = "https://" + url
}
lr.url = url
return lr
}
func (lr *_lsRemote) command() error {
cmd := exec.Command("git", "ls-remote", "--heads", "--tags", "--sort=v:refname", lr.url)
output, err := cmd.CombinedOutput()
if err != nil {
logrus.WithField("cmd", cmd.String()).Errorf("run command failed: %v", err)
return err
}
lr.output = string(output)
return nil
}
func (lr *_lsRemote) tagOrCommitID() (string, error) {
if err := lr.command(); err != nil {
return "", err
}
lr.output = strings.Trim(lr.output, "\n ")
commits := strings.Split(lr.output, "\n")
if len(commits) == 0 {
return "", fmt.Errorf("get commit log empty")
}
commit := commits[len(commits)-1]
if strings.Contains(commit, "refs/heads/") {
matches := headRegexp.FindStringSubmatch(commit)
if len(matches) != 2 {
return "", fmt.Errorf("%s no match rule: %s", commit, headRegexp.String())
}
return strings.Trim(strings.ReplaceAll(commit, matches[0], ""), " \n\t\r"), nil
}
if strings.Contains(commit, "refs/tags/") {
matches := tagRegexp.FindStringSubmatch(commit)
if len(matches) != 2 {
return "", fmt.Errorf("%s no match rule: %s", commit, tagRegexp.String())
}
return matches[1], nil
}
return "", fmt.Errorf("%s no match heads and tags rule", commit)
}