-
Notifications
You must be signed in to change notification settings - Fork 2
/
commit.go
70 lines (62 loc) · 1.62 KB
/
commit.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 git
import (
"fmt"
"os"
"os/exec"
"strings"
)
// ReviewSummary fixes the commit-summary by some simple rules.
func ReviewSummary(summary string) string {
cleanedOfWhitespace := strings.TrimSpace(summary)
return cleanedOfWhitespace
}
// Commit executes git-commit with the build message.
func Commit(commitMsg string) {
out, err := exec.Command("git", "commit", "-m", commitMsg).Output()
if err != nil {
fmt.Printf("%s", err)
}
output := string(out[:])
fmt.Print(output)
}
// Add executes the git-add command with a mode.
// A mode could be: -p or simply a dot.
func Add(mode, extraArg string) {
var cmd *exec.Cmd
if extraArg == "" {
cmd = exec.Command("git", "add", mode)
} else {
cmd = exec.Command("git", "add", mode, extraArg)
}
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
_ = cmd.Run()
}
// AreThereChanges checks if there are local changes in the current dir.
func AreThereChanges() bool {
out, err := exec.Command("git", "status", "--porcelain").Output()
if err != nil {
fmt.Printf("%s", err)
}
output := string(out[:])
return output != ""
}
// AnythingStaged checks if there are staged changes in the current dir.
func AnythingStaged() bool {
out, err := exec.Command("git", "diff", "--name-only", "--cached").Output()
if err != nil {
fmt.Printf("%s", err)
}
output := strings.TrimSpace(string(out[:]))
return output != ""
}
// Make an commit with no content
func EmptyCommit(commitMsg string) {
out, err := exec.Command("git", "commit", "--allow-empty", "-m", commitMsg).Output()
if err != nil {
fmt.Printf("%s", err)
}
output := string(out[:])
fmt.Print(output)
}