-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
85 lines (75 loc) · 1.79 KB
/
status.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
package git_cmd
import (
"bufio"
"github.com/xunull/goc/commandx"
"io"
"strings"
)
func (g *GitApi) WorktreeIsClean() (bool, error) {
cmd := []string{"git", "status"}
cmdRes := commandx.RunCommand(cmd, commandx.WithDir(g.Dir))
if cmdRes.Success {
if strings.Contains(cmdRes.Stdout.String(), "working tree clean") {
return true, nil
} else if strings.Contains(cmdRes.Stdout.String(), "No commits yet") {
return true, nil
} else {
return false, nil
}
} else {
return false, cmdRes.Err
}
}
func (g *GitApi) CheckUpToDate(all bool) (bool, *commandx.CommandResult) {
var cmdOne []string
if all {
cmdOne = []string{"git", "fetch", "--all"}
} else {
cmdOne = []string{"git", "fetch"}
}
cmdTwo := []string{"git", "status"}
success, res := commandx.RunCommandForLast([][]string{cmdOne, cmdTwo}, commandx.WithDir(g.Dir))
if success {
if strings.Contains(res.Stdout.String(), "up to date") {
return true, nil
} else {
return false, nil
}
} else {
return false, res
}
}
func (g *GitApi) StatusPorcelain() ([]string, error) {
cmd := []string{"git", "status", "--porcelain=1"}
cmdRes := commandx.RunCommand(cmd, commandx.WithDir(g.Dir))
if cmdRes.Success {
lines := make([]string, 0)
buff := bufio.NewReader(&cmdRes.Stdout)
for {
line, err := buff.ReadString('\n')
if io.EOF == err {
break
}
if err != nil {
return nil, err
}
lines = append(lines, line)
}
return lines, nil
} else {
return nil, cmdRes.Err
}
}
func (g *GitApi) CheckForgetPush() (bool, error) {
cmd := []string{"git", "status"}
cmdRes := commandx.RunCommand(cmd, commandx.WithDir(g.Dir))
if cmdRes.Success {
if strings.Contains(cmdRes.Stdout.String(), "is ahead of") {
return true, nil
} else {
return false, nil
}
} else {
return false, cmdRes.Err
}
}