-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_up.go
66 lines (59 loc) · 2.14 KB
/
git_up.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
package gitgo
import (
"strings"
"github.com/pkg/errors"
"github.com/yyle88/erero"
)
// HasStagingChanges 查看是否有变动,使用场景是:没有变动就不要执行 commit 否则会报错或者产生空提交
func (G *Gcm) HasStagingChanges() (bool, error) {
if output, err := G.execConfig.Exec("git", "diff-index", "--cached", "--quiet", "HEAD"); err != nil {
if len(output) != 0 {
return false, errors.New(string(output))
}
return true, nil
}
return false, nil
}
// CheckStagedChanges 查看是否有变动,使用场景是:没有变动就不要执行 commit 否则会报错或者产生空提交
func (G *Gcm) CheckStagedChanges() *Gcm {
if output, err := G.execConfig.Exec("git", "diff", "--cached", "--quiet"); err == nil {
if len(output) != 0 {
return newWaGcm(G.execConfig, output, err, G.debugMode)
}
return newWaGcm(G.execConfig, []byte{}, errors.New("no-staged-changes"), G.debugMode)
}
return G
}
// LatestGitTag 获得项目的最新标签的名称
func (G *Gcm) LatestGitTag() (string, error) {
const commandBash = `
res=$(git describe --tags --abbrev=0 2>/dev/null)
if [ -z "$res" ]; then
echo ""
else
echo "$res"
fi
`
output, err := G.execConfig.ShallowClone().WithBash().Exec(strings.TrimSpace(commandBash))
if err != nil {
return "", erero.Wro(err)
}
return strings.TrimSpace(string(output)), nil
}
// GitCommitHash 获得某个分支或标签的哈希
func (G *Gcm) GitCommitHash(refName string) (string, error) {
output, err := G.execConfig.Exec("git", "rev-parse", refName)
if err != nil {
return "", erero.Wro(err)
}
return strings.TrimSpace(string(output)), nil
}
// SortedGitTags 获取项目的标签列表,按时间从前到后排列。使用场景是仅仅观察标签,让用户自己想出下一个标签的序号,因此这里只返回个字符串就行
func (G *Gcm) SortedGitTags() (string, error) {
const commandBash = "git for-each-ref --sort=creatordate --format '%(refname) %(creatordate)' refs/tags"
output, err := G.execConfig.ShallowClone().WithBash().Exec(strings.TrimSpace(commandBash))
if err != nil {
return "", erero.Wro(err)
}
return strings.TrimSpace(string(output)), nil
}