-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_utils.go
More file actions
70 lines (55 loc) · 1.22 KB
/
Copy pathdiff_utils.go
File metadata and controls
70 lines (55 loc) · 1.22 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
package main
import (
"log"
"os/exec"
"regexp"
"strings"
)
func diffOutput() []byte {
cmd := exec.Command("git", "diff")
stdout, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
return stdout
}
type File struct {
Name string
Data []string
}
func ProcessDiff() []File {
stdout := string(diffOutput())
filelREG := regexp.MustCompile(`(?m)^diff --git `)
chunks := filelREG.Split(stdout, -1)
files := []File{}
for _, chunk := range chunks {
if chunk == "" {
continue
}
hunkREG := regexp.MustCompile(`(?m)^@@ .*? @@`)
loc := hunkREG.FindStringIndex(chunk)
var filename string
var lines []string
if loc != nil {
filename = chunk[:loc[0]]
header := chunk[loc[0]:loc[1]]
rest := chunk[loc[1]:]
if !strings.HasPrefix(rest, "\n") && !strings.HasPrefix(rest, "\r\n") {
// Git usually adds a single space before the context code.
// Trim that space and inject a newline so the context becomes its own line.
rest = "\n" + strings.TrimPrefix(rest, " ")
}
// Recombine and split
lines = strings.Split(header+rest, "\n")
} else {
filename = "none"
lines = []string{}
}
file := File{
Name: filename,
Data: lines,
}
files = append(files, file)
}
return files
}