-
Notifications
You must be signed in to change notification settings - Fork 44
/
git.go
68 lines (57 loc) · 1.74 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
package utils
import (
"fmt"
"strings"
serrors "github.com/slsa-framework/slsa-verifier/v2/errors"
)
// NormalizeGitURI normalizes a git URI to include a git+https:// prefix.
func NormalizeGitURI(s string) string {
if !strings.HasPrefix(s, "git+") {
if !strings.Contains(s, "://") {
return "git+https://" + s
}
return "git+" + s
}
return s
}
// ParseGitURIAndRef retrieves the URI and ref from the given URI.
func ParseGitURIAndRef(uri string) (string, string, error) {
if uri == "" {
return "", "", fmt.Errorf("%w: empty uri", serrors.ErrorMalformedURI)
}
if !strings.HasPrefix(uri, "git+") {
return "", "", fmt.Errorf("%w: not a git URI: %q", serrors.ErrorMalformedURI, uri)
}
r := strings.SplitN(uri, "@", 2)
if len(r) < 2 {
return r[0], "", nil
}
return r[0], r[1], nil
}
// ParseGitRef parses the git ref and returns its type and name.
func ParseGitRef(ref string) (string, string) {
parts := strings.SplitN(ref, "/", 3)
if len(parts) < 3 || parts[0] != "refs" {
return "", ref
}
return parts[1], parts[2]
}
// ValidateGitRef validates that the given git ref is a valid ref of the given type and returns its name.
func ValidateGitRef(refType, ref string) (string, error) {
typ, name := ParseGitRef(ref)
if typ != refType {
return "", fmt.Errorf("%w: %q: unexpected ref type: %q", serrors.ErrorInvalidRef, ref, typ)
}
if name == "" {
return "", fmt.Errorf("%w: %q: empty ref name", serrors.ErrorInvalidRef, ref)
}
return name, nil
}
// TagFromGitRef returns the tagname from a tag ref.
func TagFromGitRef(ref string) (string, error) {
return ValidateGitRef("tags", ref)
}
// BranchFromGitRef returns the tagname from a tag ref.
func BranchFromGitRef(ref string) (string, error) {
return ValidateGitRef("heads", ref)
}