forked from leg100/otf
-
Notifications
You must be signed in to change notification settings - Fork 5
/
git.go
41 lines (37 loc) · 1.01 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
package internal
import (
"fmt"
"strings"
)
// ParseBranchRef parses a git ref expecting it to be a reference to a branch. If
// it is not then false is returned, otherwise the branch name along with
// true is returned.
func ParseBranchRef(ref string) (string, bool) {
parts := strings.Split(ref, "/")
if len(parts) != 3 {
return "", false
}
if parts[0] == "refs" && parts[1] == "heads" {
return parts[2], true
}
return "", false
}
// ParseRef parses a git ref of the format refs/[tags|heads]/[name],
func ParseRef(ref string) (string, bool) {
parts := strings.Split(ref, "/")
if len(parts) != 3 || parts[0] != "refs" {
return "", false
}
if parts[0] == "refs" && parts[1] == "heads" {
return parts[2], true
}
return "", false
}
// ParseTagRef parses the tag from a git reference with the format refs/tags/<tag>
func ParseTagRef(ref string) (string, error) {
tag, found := strings.CutPrefix(ref, "refs/tags/")
if !found {
return "", fmt.Errorf("invalid tag reference: %s", ref)
}
return tag, nil
}