BlueGit

timfa bluegit

Files

bluegit / internal / gitrepo / patch.go

patch.go

feat: toon een commit zoals GitLab
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
dc9d9b26 History
patch.go 5.0 KiB Download Raw
1package gitrepo
2
3import (
4 "context"
5 "strconv"
6 "strings"
7)
8
9// PatchFile is één bestand in de diff van een commit tegen zijn eerste parent.
10// Een root-commit wordt vergeleken met de lege boom.
11type PatchFile struct {
12 Status string
13 Path string
14 OldPath string
15 Added int
16 Deleted int
17 Binary bool
18 Lines []DiffLine
19}
20
21// CommitPatch leest de parents en de diff van sha.
22// Een mergecommit wordt tegen de eerste parent gezet.
23func CommitPatch(ctx context.Context, gitBin, dir, sha string) (parents []string, files []PatchFile, err error) {
24 out, err := run(ctx, gitBin, nil, "-C", dir, "rev-list", "--parents", "-n", "1", sha)
25 if err != nil {
26 return nil, nil, err
27 }
28 fields := strings.Fields(out)
29 if len(fields) == 0 {
30 return nil, nil, err
31 }
32 if len(fields) > 1 {
33 parents = append([]string(nil), fields[1:]...)
34 }
35 base := ""
36 if len(parents) > 0 {
37 base = parents[0]
38 } else {
39 empty, err := run(ctx, gitBin, nil, "hash-object", "-t", "tree", "--stdin")
40 if err != nil {
41 return nil, nil, err
42 }
43 base = strings.TrimSpace(empty)
44 }
45 raw, err := runAllow(ctx, gitBin, nil, 1, "-c", "core.quotepath=false", "-C", dir, "diff", "--no-color", "--find-renames", "--unified=3", base, sha)
46 if err != nil {
47 return nil, nil, err
48 }
49 return parents, ParsePatch(raw), nil
50}
51
52// ParsePatch leest de tekst van git diff, inclusief hernoemen en binaire bestanden.
53func ParsePatch(raw string) []PatchFile {
54 var files []PatchFile
55 var cur *PatchFile
56 var oldGuess, newGuess string
57 var oldLine, newLine int
58 inHunk := false
59 skipBinary := false
60 for _, line := range strings.Split(raw, "\n") {
61 switch {
62 case strings.HasPrefix(line, "diff --git "):
63 files = append(files, PatchFile{Status: "modified"})
64 cur = &files[len(files)-1]
65 oldGuess, newGuess = splitDiffPaths(line)
66 cur.Path = newGuess
67 oldLine, newLine = 0, 0
68 inHunk = false
69 skipBinary = false
70 case cur == nil || skipBinary:
71 continue
72 case strings.HasPrefix(line, "new file mode "):
73 cur.Status = "added"
74 case strings.HasPrefix(line, "deleted file mode "):
75 cur.Status = "deleted"
76 if oldGuess != "" {
77 cur.Path = oldGuess
78 }
79 case strings.HasPrefix(line, "rename from ") || strings.HasPrefix(line, "copy from "):
80 cur.Status = "renamed"
81 cur.OldPath = metaPath(line)
82 case strings.HasPrefix(line, "rename to ") || strings.HasPrefix(line, "copy to "):
83 cur.Status = "renamed"
84 if p := metaPath(line); p != "" {
85 cur.Path = p
86 }
87 case strings.HasPrefix(line, "Binary files ") || strings.HasPrefix(line, "GIT binary patch"):
88 cur.Binary = true
89 cur.Lines = nil
90 cur.Added, cur.Deleted = 0, 0
91 skipBinary = true
92 inHunk = false
93 case strings.HasPrefix(line, "@@"):
94 oldLine, newLine = hunkStart(line)
95 oldLine--
96 newLine--
97 inHunk = true
98 cur.Lines = append(cur.Lines, DiffLine{Kind: "hunk", Text: line})
99 case !inHunk:
100 continue
101 case strings.HasPrefix(line, `\`):
102 continue
103 case strings.HasPrefix(line, "+"):
104 newLine++
105 cur.Added++
106 cur.Lines = append(cur.Lines, DiffLine{Kind: "add", New: newLine, Text: line[1:]})
107 case strings.HasPrefix(line, "-"):
108 oldLine++
109 cur.Deleted++
110 cur.Lines = append(cur.Lines, DiffLine{Kind: "del", Old: oldLine, Text: line[1:]})
111 case strings.HasPrefix(line, " "):
112 oldLine++
113 newLine++
114 cur.Lines = append(cur.Lines, DiffLine{Kind: "ctx", Old: oldLine, New: newLine, Text: line[1:]})
115 }
116 }
117 var out []PatchFile
118 for _, f := range files {
119 if f.Path == "" {
120 continue
121 }
122 if f.Status != "renamed" {
123 f.OldPath = ""
124 }
125 out = append(out, f)
126 }
127 return out
128}
129
130func splitDiffPaths(line string) (oldPath, newPath string) {
131 rest := strings.TrimSpace(strings.TrimPrefix(line, "diff --git "))
132 a, b, ok := splitQuotedPair(rest)
133 if !ok {
134 return "", ""
135 }
136 return stripAB(a), stripAB(b)
137}
138
139func splitQuotedPair(s string) (string, string, bool) {
140 s = strings.TrimSpace(s)
141 if s == "" {
142 return "", "", false
143 }
144 if strings.HasPrefix(s, `"`) {
145 a, rest, ok := unquotePrefix(s)
146 if !ok {
147 return "", "", false
148 }
149 rest = strings.TrimSpace(rest)
150 if strings.HasPrefix(rest, `"`) {
151 b, _, ok := unquotePrefix(rest)
152 return a, b, ok
153 }
154 return a, rest, rest != ""
155 }
156 i := strings.Index(s, " b/")
157 if i < 0 {
158 return "", "", false
159 }
160 return s[:i], s[i+1:], true
161}
162
163func unquotePrefix(s string) (string, string, bool) {
164 if len(s) < 2 || s[0] != '"' {
165 return "", "", false
166 }
167 for i := 1; i < len(s); i++ {
168 if s[i] == '\\' {
169 i++
170 continue
171 }
172 if s[i] == '"' {
173 u, err := strconv.Unquote(s[:i+1])
174 if err != nil {
175 return "", "", false
176 }
177 return u, s[i+1:], true
178 }
179 }
180 return "", "", false
181}
182
183func metaPath(line string) string {
184 if i := strings.IndexByte(line, ' '); i >= 0 {
185 line = line[i+1:]
186 }
187 // "rename from pad" heeft twee spaties in het commando.
188 if i := strings.IndexByte(line, ' '); i >= 0 && !strings.HasPrefix(line, `"`) && (strings.HasPrefix(line, "from ") || strings.HasPrefix(line, "to ")) {
189 line = line[i+1:]
190 }
191 line = strings.TrimSpace(line)
192 if strings.HasPrefix(line, `"`) {
193 if u, err := strconv.Unquote(line); err == nil {
194 return u
195 }
196 }
197 return line
198}