BlueGit

timfa bluegit

Files

bluegit / internal / gitrepo / diff.go

diff.go

feat: suggesties op merge requests
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Failed 3902d232 History
diff.go 3.7 KiB Download Raw
1package gitrepo
2
3import (
4 "context"
5 "errors"
6 "os/exec"
7 "strconv"
8 "strings"
9)
10
11// DiffLine is één regel in een unified diff.
12// New is het regelnummer in het nieuwe bestand. 0 betekent dat de regel
13// alleen in het oude bestand staat.
14type DiffLine struct {
15 Kind string
16 Old int
17 New int
18 Text string
19}
20
21// DiffFile is één bestand in een diff.
22type DiffFile struct {
23 Path string
24 Lines []DiffLine
25}
26
27// DiffFiles vergelijkt source met de merge-base van target.
28// Een diff zonder wijzigingen is een lege lijst.
29func DiffFiles(ctx context.Context, gitBin, dir, target, source string) ([]DiffFile, error) {
30 out, err := runAllow(ctx, gitBin, nil, 1, "-C", dir, "diff", "--no-color", "--unified=3", target+"..."+source)
31 if err != nil {
32 return nil, err
33 }
34 return ParseDiff(out), nil
35}
36
37// BlobText leest een bestand op een revisie. Een ontbrekend bestand is een fout.
38func BlobText(ctx context.Context, gitBin, dir, rev, path string) (string, error) {
39 return run(ctx, gitBin, nil, "-C", dir, "show", rev+":"+path)
40}
41
42// ParseDiff leest de tekst van git diff.
43func ParseDiff(raw string) []DiffFile {
44 var files []DiffFile
45 var cur *DiffFile
46 var oldLine, newLine int
47 inHunk := false
48 for _, line := range strings.Split(raw, "\n") {
49 switch {
50 case strings.HasPrefix(line, "diff --git "):
51 files = append(files, DiffFile{})
52 cur = &files[len(files)-1]
53 oldLine, newLine = 0, 0
54 inHunk = false
55 case cur == nil:
56 continue
57 case !inHunk && strings.HasPrefix(line, "rename to "):
58 cur.Path = strings.TrimSpace(strings.TrimPrefix(line, "rename to "))
59 case !inHunk && strings.HasPrefix(line, "+++ "):
60 p := strings.TrimSpace(strings.TrimPrefix(line, "+++ "))
61 if p != "/dev/null" {
62 cur.Path = stripAB(p)
63 }
64 case strings.HasPrefix(line, "@@"):
65 oldLine, newLine = hunkStart(line)
66 oldLine--
67 newLine--
68 inHunk = true
69 cur.Lines = append(cur.Lines, DiffLine{Kind: "hunk", Text: line})
70 case !inHunk:
71 continue
72 case strings.HasPrefix(line, `\`):
73 continue
74 case strings.HasPrefix(line, "+"):
75 newLine++
76 cur.Lines = append(cur.Lines, DiffLine{Kind: "add", New: newLine, Text: line[1:]})
77 case strings.HasPrefix(line, "-"):
78 oldLine++
79 cur.Lines = append(cur.Lines, DiffLine{Kind: "del", Old: oldLine, Text: line[1:]})
80 case strings.HasPrefix(line, " "):
81 oldLine++
82 newLine++
83 cur.Lines = append(cur.Lines, DiffLine{Kind: "ctx", Old: oldLine, New: newLine, Text: line[1:]})
84 }
85 }
86 var out []DiffFile
87 for _, f := range files {
88 if f.Path != "" {
89 out = append(out, f)
90 }
91 }
92 return out
93}
94
95func hunkStart(line string) (old, new int) {
96 parts := strings.Fields(line)
97 if len(parts) < 3 {
98 return 0, 0
99 }
100 return sideStart(parts[1]), sideStart(parts[2])
101}
102
103func sideStart(s string) int {
104 s = strings.TrimLeft(s, "+-")
105 if i := strings.IndexByte(s, ','); i >= 0 {
106 s = s[:i]
107 }
108 n, _ := strconv.Atoi(s)
109 return n
110}
111
112func stripAB(p string) string {
113 if strings.HasPrefix(p, `"`) {
114 if u, err := strconv.Unquote(p); err == nil {
115 p = u
116 }
117 }
118 if strings.HasPrefix(p, "a/") || strings.HasPrefix(p, "b/") {
119 return p[2:]
120 }
121 return p
122}
123
124// runAllow is run, maar exitcode allow is geen fout. git diff doet dat bij wijzigingen.
125func runAllow(ctx context.Context, bin string, env []string, allow int, args ...string) (string, error) {
126 if bin == "" {
127 bin = "git"
128 }
129 cmd := exec.CommandContext(ctx, bin, args...)
130 if env != nil {
131 cmd.Env = env
132 }
133 var stdout, stderr strings.Builder
134 cmd.Stdout = &stdout
135 cmd.Stderr = &stderr
136 err := cmd.Run()
137 if err == nil {
138 return stdout.String(), nil
139 }
140 var ee *exec.ExitError
141 if errors.As(err, &ee) && ee.ExitCode() == allow {
142 return stdout.String(), nil
143 }
144 msg := strings.TrimSpace(stderr.String())
145 if msg == "" {
146 msg = err.Error()
147 }
148 return "", errors.New(msg)
149}