-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.go
162 lines (130 loc) · 2.88 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package llame
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"os/exec"
"sort"
"github.com/go-git/go-git/v5"
)
// The 50/72 rule is a guideline for writing clear and concise Git commit messages:
const (
GitCommitSubjectCharsMin = 50
GitCommiBodyCharsMax = 72
)
var NoStagedFilesErr = errors.New("no staged files")
// GitDiffStaged gets a diff for all staged files (if only called with context) or for the specified ones.
func GitDiffStaged(ctx context.Context, files ...string) ([]byte, error) {
cmd := exec.CommandContext(ctx, "git", append([]string{"diff", "--staged", "HEAD"}, files...)...)
var changes bytes.Buffer
cmd.Stdout = &changes
if err := cmd.Run(); err != nil {
return nil, err
}
diffs := changes.Bytes()
if len(diffs) == 0 {
return nil, NoStagedFilesErr
}
return diffs, nil
}
type GitFiles struct {
Tracked []string
Untracked []string
}
func FilesInCommit() (*GitFiles, error) {
repo, err := NewGitRepo()
if err != nil {
return nil, err
}
return stagedFiles(repo)
}
func NewGitRepo() (*git.Repository, error) {
wd, err := os.Getwd()
if err != nil {
return nil, err
}
Debugf("Opening git repo at %s", wd)
repo, err := git.PlainOpenWithOptions(wd, &git.PlainOpenOptions{
DetectDotGit: true,
})
if err != nil {
return nil, err
}
return repo, nil
}
func MustGitStatus() string {
repo, err := NewGitRepo()
if err != nil {
panic(err)
}
workTree, err := repo.Worktree()
if err != nil {
panic(err)
}
status, err := workTree.Status()
if err != nil {
panic(err)
}
return status.String()
}
func stagedFiles(repo *git.Repository) (*GitFiles, error) {
workTree, err := repo.Worktree()
if err != nil {
return nil, err
}
status, err := workTree.Status()
if err != nil {
return nil, err
}
untrackedFiles := make([]string, 0, len(status)/2)
trackedFiles := make([]string, 0, len(status))
for fileName, st := range status {
switch st.Staging {
case git.Untracked:
untrackedFiles = append(untrackedFiles, fileName)
continue
}
trackedFiles = append(trackedFiles, fileName)
}
return &GitFiles{
Tracked: trackedFiles,
Untracked: untrackedFiles,
}, nil
}
func GitCommit(commitMsg string) error {
repo, err := NewGitRepo()
if err != nil {
return err
}
workTree, err := repo.Worktree()
if err != nil {
return err
}
// TODO: perhaps add some commit options
_, err = workTree.Commit(commitMsg, &git.CommitOptions{})
return err
}
// TODO: rm
func logCommitFiles() {
gitFiles, err := FilesInCommit()
if err != nil {
Debugf("%s", err)
Fatalf("Failed to get files staged for commit")
}
sort.Strings(gitFiles.Tracked)
for i, file := range gitFiles.Tracked {
if i == 0 {
fmt.Printf("Files to be commited:\n")
}
fmt.Printf("\t%s\n", file)
}
sort.Strings(gitFiles.Untracked)
for i, file := range gitFiles.Untracked {
if i == 0 {
fmt.Printf("Untracked:\n")
}
fmt.Printf("\t%s\n", file)
}
}