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