-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
51 lines (47 loc) · 1.06 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
package main
import (
"fmt"
"os"
"os/exec"
"strings"
)
func filesInStaging() ([]string, error) {
cmd := exec.Command("git", "diff", "--no-ext-diff", "--cached", "--name-only")
output, err := cmd.CombinedOutput()
if err != nil {
return []string{}, fmt.Errorf(string(output))
}
lines := strings.TrimSpace(string(output))
if lines == "" {
return []string{}, fmt.Errorf("no files added to staging area")
}
return strings.Split(lines, "\n"), nil
}
func findGitDir() error {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf(string(output))
}
return nil
}
func commit(msg string, body bool, signOff bool) error {
gitArgs := os.Args[1:]
if len(os.Args) > 1 && os.Args[1] == "-m" {
gitArgs = os.Args[3:]
}
args := append([]string{
"commit", "-m", msg,
}, gitArgs...)
if body {
args = append(args, "-e")
}
if signOff {
args = append(args, "-s")
}
cmd := exec.Command("git", args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}