-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.go
More file actions
58 lines (49 loc) · 1.06 KB
/
git.go
File metadata and controls
58 lines (49 loc) · 1.06 KB
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
package main
import (
"fmt"
"log"
"os/exec"
"strings"
)
func execCommand(args ...string) (string, error) {
cmd := exec.Command(args[0], args[1:]...)
out, err := cmd.CombinedOutput()
return string(out), err
}
func execGit(args ...string) (string, error) {
command := append([]string{"git"}, args...)
return execCommand(command...)
}
func execCommandLogFatal(command []string) string {
cmd := exec.Command(command[0], command[1:]...)
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(fmt.Sprint(err) + ": " + string(out))
}
output := strings.TrimSpace(string(out))
return output
}
func GetCurrentBranch() string {
return execCommandLogFatal([]string{
"git",
"symbolic-ref",
"--short",
"HEAD",
})
}
func GetConfigValue(name string) string {
val, err := execGit("config", name)
if err != nil {
log.Fatal("Unable to retrieve git config '", name, "': Please set it and try again.")
}
return strings.TrimSpace(val)
}
func SetConfigValue(name, value string) {
execCommandLogFatal([]string{
"git",
"config",
"--add",
name,
value,
})
}