BlueGit

timfa bluegit

Files

bluegit / internal / server / commit_page.go

commit_page.go

feat: toon een commit zoals GitLab
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
dc9d9b26 History
commit_page.go 6.2 KiB Download Raw
1package server
2
3import (
4 "html"
5 "html/template"
6 "net/url"
7 "sort"
8 "strconv"
9 "strings"
10
11 "gitlab.com/Timfa/bluegit/internal/gitrepo"
12 "gitlab.com/Timfa/bluegit/internal/highlight"
13)
14
15// largeChange is het aantal toegevoegde en verwijderde regels waarna een diff dicht blijft.
16const largeChange = 200
17
18// CommitDiff is de diff van één commit.
19type CommitDiff struct {
20 Parents []ParentSHA
21 Files []CommitFile
22 Tree []CommitTreeRow
23 Added, Deleted, FilesN int
24}
25
26// ParentSHA is een parent van de commit.
27type ParentSHA struct {
28 SHA, Short string
29}
30
31// CommitFile is één gewijzigd bestand, in unified en side-by-side vorm.
32type CommitFile struct {
33 Path, OldPath, Status, Anchor, FileURL string
34 Added, Deleted int
35 Binary, Collapsed, HasBody bool
36 Lines []CommitLine
37 Pairs []CommitPair
38}
39
40// CommitLine is één unified diffregel. HTML is al geëscaped.
41type CommitLine struct {
42 Kind string
43 Old int
44 New int
45 HTML template.HTML
46}
47
48// CommitPair is één rij in de naast-elkaar-weergave.
49type CommitPair struct {
50 Hunk string
51 LKind, RKind string
52 LNo, RNo int
53 LHTML, RHTML template.HTML
54}
55
56// CommitTreeRow is een map of bestand in de boom.
57type CommitTreeRow struct {
58 Name, Path, Anchor, Status, Icon string
59 Dir bool
60 Depth, Added, Deleted int
61}
62
63func presentCommitDiff(repoPath, sha string, parents []string, files []gitrepo.PatchFile) *CommitDiff {
64 d := &CommitDiff{}
65 for _, p := range parents {
66 if !safeSHA(p) {
67 continue
68 }
69 d.Parents = append(d.Parents, ParentSHA{SHA: p, Short: shortSHA(p)})
70 }
71 parent := ""
72 if len(d.Parents) > 0 {
73 parent = d.Parents[0].SHA
74 }
75 seen := map[string]int{}
76 for _, f := range files {
77 if f.Path == "" {
78 continue
79 }
80 cf := CommitFile{
81 Path: f.Path, OldPath: f.OldPath, Status: f.Status,
82 Added: f.Added, Deleted: f.Deleted, Binary: f.Binary,
83 Anchor: diffAnchor(f.Path, seen),
84 Collapsed: !f.Binary && f.Added+f.Deleted > largeChange,
85 }
86 linkPath := f.Path
87 linkRev := sha
88 if f.Status == "deleted" {
89 linkPath = f.Path
90 linkRev = parent
91 }
92 if linkRev != "" {
93 cf.FileURL = blobLink(repoPath, linkRev, linkPath)
94 }
95 if f.Binary || len(f.Lines) > 0 {
96 cf.HasBody = true
97 }
98 if !f.Binary && len(f.Lines) > 0 {
99 name := f.Path
100 if f.Status == "deleted" {
101 name = f.Path
102 }
103 painter := highlight.NewPainter(name)
104 for _, ln := range f.Lines {
105 row := CommitLine{Kind: ln.Kind, Old: ln.Old, New: ln.New}
106 if ln.Kind == "hunk" {
107 row.HTML = template.HTML(html.EscapeString(ln.Text))
108 } else {
109 row.HTML = template.HTML(painter.Line(ln.Text))
110 }
111 cf.Lines = append(cf.Lines, row)
112 }
113 cf.Pairs = pairDiff(cf.Lines)
114 }
115 d.Added += cf.Added
116 d.Deleted += cf.Deleted
117 d.Files = append(d.Files, cf)
118 }
119 d.FilesN = len(d.Files)
120 d.Tree = commitTree(d.Files)
121 return d
122}
123
124func pairDiff(lines []CommitLine) []CommitPair {
125 var out []CommitPair
126 for i := 0; i < len(lines); {
127 ln := lines[i]
128 if ln.Kind == "hunk" {
129 out = append(out, CommitPair{Hunk: html.UnescapeString(string(ln.HTML))})
130 i++
131 continue
132 }
133 if ln.Kind == "ctx" {
134 out = append(out, CommitPair{
135 LKind: "ctx", RKind: "ctx", LNo: ln.Old, RNo: ln.New, LHTML: ln.HTML, RHTML: ln.HTML,
136 })
137 i++
138 continue
139 }
140 var dels, adds []CommitLine
141 for i < len(lines) && lines[i].Kind == "del" {
142 dels = append(dels, lines[i])
143 i++
144 }
145 for i < len(lines) && lines[i].Kind == "add" {
146 adds = append(adds, lines[i])
147 i++
148 }
149 n := len(dels)
150 if len(adds) > n {
151 n = len(adds)
152 }
153 for k := 0; k < n; k++ {
154 p := CommitPair{}
155 if k < len(dels) {
156 p.LKind = "del"
157 p.LNo = dels[k].Old
158 p.LHTML = dels[k].HTML
159 }
160 if k < len(adds) {
161 p.RKind = "add"
162 p.RNo = adds[k].New
163 p.RHTML = adds[k].HTML
164 }
165 out = append(out, p)
166 }
167 }
168 return out
169}
170
171func diffAnchor(path string, seen map[string]int) string {
172 var b strings.Builder
173 b.WriteString("diff-")
174 for _, r := range path {
175 switch {
176 case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9', r == '-', r == '_':
177 b.WriteRune(r)
178 default:
179 b.WriteByte('-')
180 }
181 }
182 base := b.String()
183 n := seen[base]
184 seen[base]++
185 if n == 0 {
186 return base
187 }
188 return base + "-" + strconv.Itoa(n)
189}
190
191func blobLink(repoPath, rev, path string) string {
192 parts := strings.Split(path, "/")
193 for i, p := range parts {
194 parts[i] = url.PathEscape(p)
195 }
196 return "/" + repoPath + "/-/blob/" + strings.Join(parts, "/") + "?rev=" + url.QueryEscape(rev)
197}
198
199func commitTree(files []CommitFile) []CommitTreeRow {
200 root := &treeNode{kids: map[string]*treeNode{}}
201 for i := range files {
202 f := &files[i]
203 parts := strings.Split(f.Path, "/")
204 cur := root
205 acc := ""
206 for j, part := range parts {
207 if acc == "" {
208 acc = part
209 } else {
210 acc += "/" + part
211 }
212 n := cur.kids[part]
213 if n == nil {
214 n = &treeNode{name: part, path: acc, kids: map[string]*treeNode{}}
215 cur.kids[part] = n
216 cur.order = append(cur.order, part)
217 }
218 if j == len(parts)-1 {
219 n.file = f
220 } else {
221 n.dir = true
222 }
223 cur = n
224 }
225 }
226 var rows []CommitTreeRow
227 var walk func(n *treeNode, depth int)
228 walk = func(n *treeNode, depth int) {
229 names := append([]string(nil), n.order...)
230 sort.Slice(names, func(i, j int) bool {
231 a, b := n.kids[names[i]], n.kids[names[j]]
232 aDir := a.dir && a.file == nil
233 bDir := b.dir && b.file == nil
234 if aDir != bDir {
235 return aDir
236 }
237 return strings.ToLower(a.name) < strings.ToLower(b.name)
238 })
239 for _, name := range names {
240 child := n.kids[name]
241 shown := depth
242 if shown > 8 {
243 shown = 8
244 }
245 if child.dir && child.file == nil {
246 rows = append(rows, CommitTreeRow{
247 Name: child.name, Path: child.path, Dir: true, Depth: shown, Icon: "dir",
248 })
249 walk(child, depth+1)
250 continue
251 }
252 f := child.file
253 rows = append(rows, CommitTreeRow{
254 Name: child.name, Path: f.Path, Anchor: f.Anchor, Status: f.Status,
255 Depth: shown, Added: f.Added, Deleted: f.Deleted, Icon: fileIcon(child.name, "blob"),
256 })
257 }
258 }
259 walk(root, 0)
260 return rows
261}
262
263type treeNode struct {
264 name, path string
265 dir bool
266 file *CommitFile
267 kids map[string]*treeNode
268 order []string
269}