-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.go
More file actions
325 lines (280 loc) · 8.88 KB
/
git.go
File metadata and controls
325 lines (280 loc) · 8.88 KB
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
package main
import (
"bufio"
"bytes"
"fmt"
"os/exec"
"strings"
)
type Repository struct {
path string
}
// Fetch fetches the specified branch from origin.
func (r *Repository) Fetch(branch string) error {
cmd := exec.Command("git", "fetch", "origin", branch)
cmd.Dir = r.path
if err := cmd.Run(); err != nil {
if ee, ok := err.(*exec.ExitError); ok {
return fmt.Errorf("fetch: %s", strings.TrimSpace(string(ee.Stderr)))
}
return fmt.Errorf("fetch: %w", err)
}
return nil
}
// CommitsBetween returns the commits between base and head, oldest first.
// This is equivalent to `git rev-list --reverse base..head`.
// Returns an error if base is not an ancestor of head.
func (r *Repository) CommitsBetween(base, head string) ([]string, error) {
// First verify that base is an ancestor of head
cmd := exec.Command("git", "merge-base", "--is-ancestor", base, head)
cmd.Dir = r.path
if err := cmd.Run(); err != nil {
if _, ok := err.(*exec.ExitError); ok {
return nil, fmt.Errorf("%s is not an ancestor of %s (histories have diverged)", base, head)
}
return nil, fmt.Errorf("check ancestry: %w", err)
}
cmd = exec.Command("git", "rev-list", "--reverse", base+".."+head)
cmd.Dir = r.path
out, err := cmd.Output()
if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
return nil, fmt.Errorf("list commits: %s", strings.TrimSpace(string(ee.Stderr)))
}
return nil, fmt.Errorf("list commits: %w", err)
}
var commits []string
scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
commits = append(commits, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, err
}
return commits, nil
}
// CommitsSince returns the commits between base and HEAD, oldest first.
// This is equivalent to `git rev-list --reverse base..HEAD`.
// Returns an error if base is not an ancestor of HEAD.
func (r *Repository) CommitsSince(base string) ([]string, error) {
// First verify that base is an ancestor of HEAD
cmd := exec.Command("git", "merge-base", "--is-ancestor", base, "HEAD")
cmd.Dir = r.path
if err := cmd.Run(); err != nil {
if _, ok := err.(*exec.ExitError); ok {
return nil, fmt.Errorf("remote HEAD %s is not an ancestor of local HEAD (histories have diverged)", base)
}
return nil, fmt.Errorf("check ancestry: %w", err)
}
cmd = exec.Command("git", "rev-list", "--reverse", base+"..HEAD")
cmd.Dir = r.path
out, err := cmd.Output()
if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
return nil, fmt.Errorf("list commits: %s", strings.TrimSpace(string(ee.Stderr)))
}
return nil, fmt.Errorf("list commits: %w", err)
}
var commits []string
scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
commits = append(commits, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, err
}
return commits, nil
}
// Returns a Change for each supplied commit hash
func (r *Repository) Changes(commits ...string) ([]Change, error) {
changes := make([]Change, len(commits))
for i, h := range commits {
change, err := r.changed(h)
if err != nil {
return nil, fmt.Errorf("get change %s: %w", h, err)
}
changes[i] = change
}
return changes, nil
}
// Returns a Change for the specific commit hash
func (r *Repository) changed(commit string) (Change, error) {
parents, author, message, err := r.catfile(commit)
if err != nil {
return Change{}, err
}
if len(parents) > 1 {
return Change{}, fmt.Errorf("range includes a merge commit (%s), not continuing", commit)
}
change := Change{
hash: commit,
message: message,
author: author,
entries: map[string]FileEntry{},
}
change.entries, err = r.changedFiles(commit)
if err != nil {
return Change{}, err
}
return change, nil
}
func (r *Repository) catfile(commit string) ([]string, string, string, error) {
cmd := exec.Command("git", "cat-file", "commit", commit)
cmd.Dir = r.path
out, err := cmd.Output()
if err != nil {
return nil, "", "", err
}
parents := []string{}
author, message := "", ""
scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
ln := scanner.Text()
// End of headers, start of message
if ln == "" {
break
}
key, value, _ := strings.Cut(ln, " ")
switch key {
case "parent":
parents = append(parents, value)
case "author":
// author line is First Last <email@domain.com> timestamp timezone
// so we can just grab up to the last >
marker := strings.LastIndex(value, ">")
if marker == -1 {
// no author, or malformed, so make one up
logger.Warningf("Author is malformed (%s), using placeholder", value)
author = "Commit Headless <commit-headless-bot@datadoghq.com>"
} else {
author = value[:marker+1]
}
}
}
mb := &strings.Builder{}
for scanner.Scan() {
mb.WriteString(scanner.Text())
mb.WriteString("\n")
}
message = strings.TrimSpace(mb.String())
if err := scanner.Err(); err != nil {
return nil, "", "", err
}
return parents, author, message, nil
}
// Returns the files changed in the given commit, along with their contents and modes.
// Deleted files will have nil content.
func (r *Repository) changedFiles(commit string) (map[string]FileEntry, error) {
cmd := exec.Command("git", "diff-tree", "--no-commit-id", "--name-status", "-r", commit)
cmd.Dir = r.path
out, err := cmd.Output()
if err != nil {
return nil, err
}
changes := map[string]FileEntry{}
scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
ln := scanner.Text()
status, value, _ := strings.Cut(ln, "\t")
switch {
case status == "A" || status == "M":
content, mode, err := r.fileContentAndMode(commit, value)
if err != nil {
return nil, fmt.Errorf("get content %s:%s: %w", commit, value, err)
}
changes[value] = FileEntry{Content: content, Mode: mode}
case strings.HasPrefix(status, "R"): // Renames may have a similarity score after the R
from, to, _ := strings.Cut(value, "\t")
changes[from] = FileEntry{Content: nil, Mode: ""}
content, mode, err := r.fileContentAndMode(commit, to)
if err != nil {
return nil, fmt.Errorf("get content %s:%s: %w", commit, to, err)
}
changes[to] = FileEntry{Content: content, Mode: mode}
case status == "D":
changes[value] = FileEntry{Content: nil, Mode: ""}
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return changes, nil
}
func (r *Repository) fileContentAndMode(commit, path string) ([]byte, string, error) {
// Get the file mode from ls-tree
cmd := exec.Command("git", "ls-tree", commit, "--", path)
cmd.Dir = r.path
out, err := cmd.Output()
if err != nil {
return nil, "", fmt.Errorf("ls-tree: %w", err)
}
// Output format: mode SP type SP hash TAB path
mode := strings.SplitN(string(out), " ", 2)[0]
// Get the file content
cmd = exec.Command("git", "cat-file", "blob", fmt.Sprintf("%s:%s", commit, path))
cmd.Dir = r.path
content, err := cmd.Output()
if err != nil {
return nil, "", fmt.Errorf("cat-file: %w", err)
}
return content, mode, nil
}
// StagedChanges returns the files staged for commit along with their contents and modes.
// Deleted files have nil content. Returns an empty map if there are no staged changes.
func (r *Repository) StagedChanges() (map[string]FileEntry, error) {
cmd := exec.Command("git", "diff", "--cached", "--name-status")
cmd.Dir = r.path
out, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("get staged changes: %w", err)
}
changes := map[string]FileEntry{}
scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
ln := scanner.Text()
if ln == "" {
continue
}
status, path, _ := strings.Cut(ln, "\t")
switch {
case status == "A" || status == "M":
content, mode, err := r.stagedContentAndMode(path)
if err != nil {
return nil, fmt.Errorf("get staged content %s: %w", path, err)
}
changes[path] = FileEntry{Content: content, Mode: mode}
case strings.HasPrefix(status, "R"): // Renames have the form "Rxxx\told\tnew"
from, to, _ := strings.Cut(path, "\t")
changes[from] = FileEntry{Content: nil, Mode: ""}
content, mode, err := r.stagedContentAndMode(to)
if err != nil {
return nil, fmt.Errorf("get staged content %s: %w", to, err)
}
changes[to] = FileEntry{Content: content, Mode: mode}
case status == "D":
changes[path] = FileEntry{Content: nil, Mode: ""}
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return changes, nil
}
func (r *Repository) stagedContentAndMode(path string) ([]byte, string, error) {
// Get mode from ls-files -s (format: mode SP hash SP stage TAB path)
cmd := exec.Command("git", "ls-files", "-s", "--", path)
cmd.Dir = r.path
out, err := cmd.Output()
if err != nil {
return nil, "", fmt.Errorf("ls-files: %w", err)
}
mode := strings.SplitN(string(out), " ", 2)[0]
// Get content from the index
cmd = exec.Command("git", "cat-file", "blob", ":"+path)
cmd.Dir = r.path
content, err := cmd.Output()
if err != nil {
return nil, "", fmt.Errorf("cat-file: %w", err)
}
return content, mode, nil
}