-
Notifications
You must be signed in to change notification settings - Fork 220
/
repository.go
374 lines (313 loc) · 8.91 KB
/
repository.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package git
import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/spf13/afero"
"github.com/evilmartians/lefthook/internal/log"
"github.com/evilmartians/lefthook/internal/version"
)
const (
minGitVersion = "2.31.0"
stashMessage = "lefthook auto backup"
unstagedPatchName = "lefthook-unstaged.patch"
infoDirMode = 0o775
minStatusLen = 3
)
var (
reHeadBranch = regexp.MustCompile(`HEAD -> (?P<name>.*)$`)
reVersion = regexp.MustCompile(`\d+\.\d+\.\d+`)
cmdPushFilesBase = []string{"git", "diff", "--name-only", "HEAD", "@{push}"}
cmdPushFilesHead = []string{"git", "diff", "--name-only", "HEAD"}
cmdStagedFiles = []string{"git", "diff", "--name-only", "--cached", "--diff-filter=ACMR"}
cmdStatusShort = []string{"git", "status", "--short", "--porcelain"}
cmdListStash = []string{"git", "stash", "list"}
cmdRootPath = []string{"git", "rev-parse", "--path-format=absolute", "--show-toplevel"}
cmdHooksPath = []string{"git", "rev-parse", "--path-format=absolute", "--git-path", "hooks"}
cmdInfoPath = []string{"git", "rev-parse", "--path-format=absolute", "--git-path", "info"}
cmdGitPath = []string{"git", "rev-parse", "--path-format=absolute", "--git-dir"}
cmdAllFiles = []string{"git", "ls-files", "--cached"}
cmdCreateStash = []string{"git", "stash", "create"}
cmdStageFiles = []string{"git", "add"}
cmdRemotes = []string{"git", "branch", "--remotes"}
cmdHideUnstaged = []string{"git", "checkout", "--force", "--"}
cmdEmptyTreeSHA = []string{"git", "hash-object", "-t", "tree", "/dev/null"}
cmdGitVersion = []string{"git", "version"}
)
// Repository represents a git repository.
type Repository struct {
Fs afero.Fs
Git *CommandExecutor
HooksPath string
RootPath string
GitPath string
InfoPath string
unstagedPatchPath string
headBranch string
emptyTreeSHA string
}
// NewRepository returns a Repository or an error, if git repository it not initialized.
func NewRepository(fs afero.Fs, git *CommandExecutor) (*Repository, error) {
gitVersionOut, err := git.Cmd(cmdGitVersion)
if err == nil {
gitVersion := reVersion.FindString(gitVersionOut)
if err = version.Check(minGitVersion, gitVersion); err != nil {
log.Debugf("[lefthook] version check warning: %s %s", gitVersion, err)
if errors.Is(err, version.ErrUncoveredVersion) {
log.Warn("Git version is too old. Minimum supported version is " + minGitVersion)
}
}
}
rootPath, err := git.Cmd(cmdRootPath)
if err != nil {
return nil, err
}
hooksPath, err := git.Cmd(cmdHooksPath)
if err != nil {
return nil, err
}
infoPath, err := git.Cmd(cmdInfoPath)
if err != nil {
return nil, err
}
infoPath = filepath.Clean(infoPath)
if exists, _ := afero.DirExists(fs, infoPath); !exists {
err = fs.Mkdir(infoPath, infoDirMode)
if err != nil {
return nil, err
}
}
gitPath, err := git.Cmd(cmdGitPath)
if err != nil {
return nil, err
}
emptyTreeSHA, err := git.Cmd(cmdEmptyTreeSHA)
if err != nil {
log.Debug("Couldn't get empty tree SHA value, not critical")
}
git.root = rootPath
return &Repository{
Fs: fs,
Git: git,
HooksPath: hooksPath,
RootPath: rootPath,
GitPath: gitPath,
InfoPath: infoPath,
unstagedPatchPath: filepath.Join(infoPath, unstagedPatchName),
emptyTreeSHA: emptyTreeSHA,
}, nil
}
// StagedFiles returns a list of staged files
// or an error if git command fails.
func (r *Repository) StagedFiles() ([]string, error) {
return r.FilesByCommand(cmdStagedFiles, "")
}
// StagedFiles returns a list of all files in repository
// or an error if git command fails.
func (r *Repository) AllFiles() ([]string, error) {
return r.FilesByCommand(cmdAllFiles, "")
}
// PushFiles returns a list of files that are ready to be pushed
// or an error if git command fails.
func (r *Repository) PushFiles() ([]string, error) {
res, err := r.FilesByCommand(cmdPushFilesBase, "")
if err == nil {
return res, nil
}
if len(r.headBranch) == 0 {
branches, err := r.Git.CmdLines(cmdRemotes)
if err != nil {
return nil, err
}
for _, branch := range branches {
if !reHeadBranch.MatchString(branch) {
continue
}
matches := reHeadBranch.FindStringSubmatch(branch)
r.headBranch = matches[reHeadBranch.SubexpIndex("name")]
break
}
}
// Nothing has been pushed yet or upstream is not set
if len(r.headBranch) == 0 {
r.headBranch = r.emptyTreeSHA
}
return r.FilesByCommand(append(cmdPushFilesHead, r.headBranch), "")
}
// PartiallyStagedFiles returns the list of files that have both staged and
// unstaged changes.
// See https://git-scm.com/docs/git-status#_short_format.
func (r *Repository) PartiallyStagedFiles() ([]string, error) {
lines, err := r.Git.CmdLines(cmdStatusShort)
if err != nil {
return []string{}, err
}
partiallyStaged := make([]string, 0)
for _, line := range lines {
if len(line) < minStatusLen {
continue
}
index := line[0]
workingTree := line[1]
filename := line[3:]
idx := strings.Index(filename, "->")
if idx != -1 {
filename = filename[idx+3:]
}
if index != ' ' && index != '?' && workingTree != ' ' && workingTree != '?' && len(filename) > 0 {
partiallyStaged = append(partiallyStaged, filename)
}
}
return partiallyStaged, nil
}
func (r *Repository) SaveUnstaged(files []string) error {
_, err := r.Git.BatchedCmd(
[]string{
"git",
"diff",
"--binary", // support binary files
"--unified=0", // do not add lines around diff for consistent behavior
"--no-color", // disable colors for consistent behavior
"--no-ext-diff", // disable external diff tools for consistent behavior
"--src-prefix=a/", // force prefix for consistent behavior
"--dst-prefix=b/", // force prefix for consistent behavior
"--patch", // output a patch that can be applied
"--submodule=short", // always use the default short format for submodules
"--output",
r.unstagedPatchPath,
"--",
}, files)
return err
}
func (r *Repository) HideUnstaged(files []string) error {
_, err := r.Git.BatchedCmd(cmdHideUnstaged, files)
return err
}
func (r *Repository) RestoreUnstaged() error {
if ok, _ := afero.Exists(r.Fs, r.unstagedPatchPath); !ok {
return nil
}
stat, err := r.Fs.Stat(r.unstagedPatchPath)
if err != nil {
return err
}
if stat.Size() == 0 {
err = r.Fs.Remove(r.unstagedPatchPath)
if err != nil {
return fmt.Errorf("couldn't remove the patch %s: %w", r.unstagedPatchPath, err)
}
return nil
}
_, err = r.Git.Cmd([]string{
"git",
"apply",
"-v",
"--whitespace=nowarn",
"--recount",
"--unidiff-zero",
r.unstagedPatchPath,
})
if err != nil {
return fmt.Errorf("couldn't apply the patch %s: %w", r.unstagedPatchPath, err)
}
err = r.Fs.Remove(r.unstagedPatchPath)
if err != nil {
return fmt.Errorf("couldn't remove the patch %s: %w", r.unstagedPatchPath, err)
}
return nil
}
func (r *Repository) StashUnstaged() error {
stashHash, err := r.Git.Cmd(cmdCreateStash)
if err != nil {
return err
}
_, err = r.Git.Cmd([]string{
"git",
"stash",
"store",
"--quiet",
"--message",
stashMessage,
stashHash,
})
if err != nil {
return err
}
return nil
}
func (r *Repository) DropUnstagedStash() error {
lines, err := r.Git.CmdLines(cmdListStash)
if err != nil {
return err
}
stashRegexp := regexp.MustCompile(`^(?P<stash>[^ ]+):\s*` + stashMessage)
for i := range lines {
line := lines[len(lines)-i-1]
matches := stashRegexp.FindStringSubmatch(line)
if len(matches) == 0 {
continue
}
stashID := stashRegexp.SubexpIndex("stash")
if len(matches[stashID]) > 0 {
_, err := r.Git.Cmd([]string{
"git",
"stash",
"drop",
"--quiet",
matches[stashID],
})
if err != nil {
return err
}
}
}
return nil
}
func (r *Repository) AddFiles(files []string) error {
if len(files) == 0 {
return nil
}
_, err := r.Git.BatchedCmd(cmdStageFiles, files)
return err
}
// FilesByCommand accepts git command and returns its result as a list of filepaths.
func (r *Repository) FilesByCommand(command []string, folder string) ([]string, error) {
lines, err := r.Git.CmdLinesWithinFolder(command, folder)
if err != nil {
return nil, err
}
return r.extractFiles(lines)
}
func (r *Repository) extractFiles(lines []string) ([]string, error) {
var files []string
for _, line := range lines {
file := strings.TrimSpace(line)
if len(file) == 0 {
continue
}
isFile, err := r.isFile(file)
if err != nil {
return nil, err
}
if isFile {
files = append(files, file)
}
}
return files, nil
}
func (r *Repository) isFile(path string) (bool, error) {
if !strings.HasPrefix(path, r.RootPath) {
path = filepath.Join(r.RootPath, path)
}
stat, err := r.Fs.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return !stat.IsDir(), nil
}