-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.go
70 lines (62 loc) · 1.9 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
package main
import (
"fmt"
"os/exec"
)
type GPGFormat string
const (
OPENPGP GPGFormat = "openpgp"
SSH GPGFormat = "ssh"
X509 GPGFormat = "x509"
)
var gpgFormat = []GPGFormat{OPENPGP, SSH, X509}
func isGitDirectory() bool {
cmd := exec.Command("git", "rev-parse")
err := cmd.Run()
if err != nil && cmd.ProcessState.ExitCode() != 128 {
panic(err)
}
return cmd.ProcessState.ExitCode() == 0
}
func unsetConfig(pattern string) error {
var cmd *exec.Cmd
if isGitDirectory() {
cmd = exec.Command("git", "config", "--unset", "include.path", pattern)
} else {
cmd = exec.Command("git", "config", "--global", "--unset", "include.path", pattern)
}
gitOutput, err := cmd.CombinedOutput()
if err != nil && cmd.ProcessState.ExitCode() != 5 { // try to unset an option that does not exist will give exit 5
fmt.Printf("git: %s", string(gitOutput))
return err
}
return nil
}
func applyConfig(configPath string, isGlobal bool) error {
var cmd *exec.Cmd
if isGlobal {
cmd = exec.Command("git", "config", "--global", "--replace-all", "include.path", configPath, fmt.Sprintf("%s.*gitconfig$", saveDirName))
} else {
cmd = exec.Command("git", "config", "--replace-all", "include.path", configPath, fmt.Sprintf("%s.*gitconfig$", saveDirName))
}
gitOutput, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("git: %s", string(gitOutput))
return err
}
return nil
}
func getCurrentConfig() (string, error) {
var cmd *exec.Cmd
if !isGlobal && isGitDirectory() {
cmd = exec.Command("git", "config", "--worktree", "--get", "include.path", fmt.Sprintf("%s.*gitconfig$", saveDirName))
} else {
cmd = exec.Command("git", "config", "--global", "--get", "include.path", fmt.Sprintf("%s.*gitconfig$", saveDirName))
}
gitOutput, err := cmd.CombinedOutput()
if err != nil && cmd.ProcessState.ExitCode() != 1 {
fmt.Printf("git: %s", string(gitOutput))
return "", err
}
return string(gitOutput), nil
}