/
branch.go
57 lines (41 loc) · 1.27 KB
/
branch.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
package git
import (
"os/exec"
"regexp"
)
type execFuncDef func(s1 string, s2 ...string) *exec.Cmd
var execFunc = execFuncDef(exec.Command)
//GetCurrentBranchName executes 'git branch' to get the current branch
func GetCurrentBranchName() (string, error) {
outputBytes, err := execFunc("git", "branch").CombinedOutput()
if err != nil {
return "", err
}
branchName := getBranchNameFromGitOutput(string(outputBytes))
if branchName == "" {
outputBytes, err := execFunc("git", "log").CombinedOutput()
if err != nil {
return "", err
}
branchName = getBranchNameFromGitLogOutput(string(outputBytes))
}
return branchName, nil
}
func getBranchNameFromGitOutput(gitOutput string) string {
return extractFromString(gitOutput, `(?m)^\* (.*)$`)
}
func getBranchNameFromGitLogOutput(gitOutput string) string {
// https://github.com/git/git/blob/ed843436dd4924c10669820cc73daf50f0b4dabd/revision.c#L2303
pattern := `(?m)^fatal: your current branch '(.*)' does not have any commits yet$`
return extractFromString(gitOutput, pattern)
}
func extractFromString(s, regexPattern string) string {
var re = regexp.MustCompile(regexPattern)
matches := re.FindAllStringSubmatch(s, 1)
if len(matches) > 0 {
if len(matches[0]) > 1 {
return matches[0][1]
}
}
return ""
}