forked from x-motemen/ghq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
94 lines (73 loc) · 2.02 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"syscall"
"github.com/motemen/ghq/utils"
)
// GitConfigSingle fetches single git-config variable.
// returns an empty string and no error if no variable is found with the given key.
func GitConfigSingle(key string) (string, error) {
return GitConfig("--get", key)
}
// GitConfigAll fetches git-config variable of multiple values.
func GitConfigAll(key string) ([]string, error) {
value, err := GitConfig("--get-all", key)
if err != nil {
return nil, err
}
// No results found, return an empty slice
if value == "" {
return nil, nil
}
return strings.Split(value, "\000"), nil
}
// GitConfig invokes 'git config' and handles some errors properly.
func GitConfig(args ...string) (string, error) {
gitArgs := append([]string{"config", "--path", "--null"}, args...)
cmd := exec.Command("git", gitArgs...)
cmd.Stderr = os.Stderr
buf, err := cmd.Output()
if exitError, ok := err.(*exec.ExitError); ok {
if waitStatus, ok := exitError.Sys().(syscall.WaitStatus); ok {
if waitStatus.ExitStatus() == 1 {
// The key was not found, do not treat as an error
return "", nil
}
}
return "", err
}
return strings.TrimRight(string(buf), "\000"), nil
}
var versionRx = regexp.MustCompile(`(\d+)\.(\d+)\.(\d+)`)
var featureConfigURLMatchVersion = []uint{1, 8, 5}
func GitHasFeatureConfigURLMatch() bool {
cmd := exec.Command("git", "--version")
buf, err := cmd.Output()
if err != nil {
return false
}
return gitVersionOutputSatisfies(string(buf), featureConfigURLMatchVersion)
}
func gitVersionOutputSatisfies(gitVersionOutput string, baseVersionParts []uint) bool {
versionStrings := versionRx.FindStringSubmatch(gitVersionOutput)
if versionStrings == nil {
return false
}
for i, v := range baseVersionParts {
thisV64, err := strconv.ParseUint(versionStrings[i+1], 10, 0)
utils.PanicIf(err)
thisV := uint(thisV64)
if thisV > v {
return true
} else if v == thisV {
continue
} else {
return false
}
}
return true
}