-
Notifications
You must be signed in to change notification settings - Fork 2
/
commit.go
596 lines (537 loc) · 16.5 KB
/
commit.go
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
package main
import (
"bytes"
"context"
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
"os/exec"
"path"
"sort"
"strings"
"syscall"
"time"
statepkg "dmitri.shuralyov.com/state"
"github.com/shurcooL/highlight_diff"
homecomponent "github.com/shurcooL/home/component"
"github.com/shurcooL/home/internal/code"
"github.com/shurcooL/home/internal/exp/service/change"
issues "github.com/shurcooL/home/internal/exp/service/issue"
"github.com/shurcooL/home/internal/exp/service/notification"
"github.com/shurcooL/home/internal/route"
"github.com/shurcooL/htmlg"
"github.com/shurcooL/httperror"
issuescomponent "github.com/shurcooL/issuesapp/component"
"github.com/shurcooL/octicon"
"github.com/shurcooL/users"
"github.com/sourcegraph/annotate"
"github.com/sourcegraph/go-diff/diff"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
// commitHandler is a handler for displaying a commit of a git repository.
type commitHandler struct {
Repo repoInfo
issues issueCounter
change changeCounter
notification notification.Service
users users.Service
gitUsers map[string]users.User // Key is lower git author email.
}
var commitHTML = template.Must(template.New("").Parse(`<html>
<head>
{{.AnalyticsHTML}} <title>{{.FullName}} - Commit {{.Hash}}</title>
<link href="/icon.svg" rel="icon" type="image/svg+xml">
<meta name="viewport" content="width=device-width">
<link href="/assets/fonts/fonts.css" rel="stylesheet" type="text/css">
<link href="/assets/commit/style.css" rel="stylesheet" type="text/css">
</head>
<body>
{{define "CommitMessage"}}
<div class="list-entry list-entry-border commit-message">
<div class="list-entry-header">
<div style="display: flex;">
<pre style="flex-grow: 1;"><strong>{{.Subject}}</strong>{{with .Body}}
{{.}}{{end}}</pre>
{{.ViewCode}}
</div>
</div>
<div class="list-entry-body">
<span style="display: inline-block; vertical-align: bottom; margin-right: 5px;">{{.Avatar}}</span>{{/*
*/}}<span style="display: inline-block;">{{.User}} committed {{.Time}}</span>
<span style="float: right;">
<span>commit <code>{{.CommitHash}}</code></span>
</span>
</div>
</div>
{{end}}
{{define "FileDiff"}}
<div class="list-entry list-entry-border">
<div class="list-entry-header">{{.Title}}</div>
<div class="list-entry-body">
<pre class="highlight">{{.Diff}}</pre>
</div>
</div>
{{end}}
`))
func (h *commitHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) error {
if req.Method != "GET" {
return httperror.Method{Allowed: []string{"GET"}}
}
authenticatedUser, err := h.users.GetAuthenticated(req.Context())
if err != nil {
log.Println(err)
authenticatedUser = users.User{} // THINK: Should it be a fatal error or not? What about on frontend vs backend?
}
var nc uint64
if authenticatedUser.ID != 0 {
nc, err = h.notification.CountNotifications(req.Context())
if err != nil {
return err
}
}
t0 := time.Now()
openIssues, err := h.issues.Count(req.Context(), issues.RepoSpec{URI: h.Repo.Spec}, issues.IssueListOptions{State: issues.StateFilter(statepkg.IssueOpen)})
if err != nil {
return err
}
openChanges, err := h.change.Count(req.Context(), h.Repo.Spec, change.ListOptions{Filter: change.FilterOpen})
if err != nil {
return err
}
fmt.Println("counting open issues & changes took:", time.Since(t0).Nanoseconds(), "for:", h.Repo.Spec)
commitHash, err := verifyCommitHash(req.URL.Path[1:])
if err != nil {
return os.ErrNotExist
}
c, err := diffTree(req.Context(), h.Repo.Dir, commitHash, ":", h.gitUsers)
if err != nil {
return err
}
if commitHash != c.CommitHash {
return os.ErrNotExist
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
err = commitHTML.Execute(w, struct {
AnalyticsHTML template.HTML
FullName string
Hash string
}{
AnalyticsHTML: analyticsHTML,
FullName: "Repository " + path.Base(h.Repo.Spec),
Hash: shortSHA(c.CommitHash),
})
if err != nil {
return err
}
_, err = io.WriteString(w, `<div style="max-width: 800px; margin: 0 auto 100px auto;">`)
if err != nil {
return err
}
// Render the header.
header := homecomponent.Header{
CurrentUser: authenticatedUser,
NotificationCount: nc,
ReturnURL: req.RequestURI,
}
err = htmlg.RenderComponents(w, header)
if err != nil {
return err
}
err = html.Render(w, htmlg.H2(htmlg.Text(h.Repo.Spec+"/...")))
if err != nil {
return err
}
// Render the tabnav.
err = htmlg.RenderComponents(w, homecomponent.RepositoryTabNav(homecomponent.HistoryTab, h.Repo.Path, h.Repo.Packages, openIssues, openChanges))
if err != nil {
return err
}
err = commitHTML.ExecuteTemplate(w, "CommitMessage", commitMessage{
ImportPath: h.Repo.Spec,
CommitHash: c.CommitHash,
Subject: c.Subject,
Body: c.Body,
Author: c.Author,
AuthorTime: c.AuthorTime,
})
if err != nil {
return err
}
if len(c.Patch) == 0 {
// Empty commit. Let the user know via a blank slate.
err := htmlg.RenderComponents(w, homecomponent.BlankSlate{
Content: htmlg.Nodes{htmlg.Text("There are no affected files.")},
})
if err != nil {
return err
}
} else {
fileDiffs, err := diff.ParseMultiFileDiff(c.Patch)
if err != nil {
return err
}
for _, f := range fileDiffs {
err := commitHTML.ExecuteTemplate(w, "FileDiff", fileDiff{FileDiff: f})
if err != nil {
return err
}
}
}
_, err = io.WriteString(w, `</div>
</body>
</html>`)
return err
}
// commitHandlerPkg is a handler for displaying a commit of a single package.
type commitHandlerPkg struct {
Repo repoInfo
PkgPath string
Dir *code.Directory
notification notification.Service
users users.Service
gitUsers map[string]users.User // Key is lower git author email.
}
func (h *commitHandlerPkg) ServeHTTP(w http.ResponseWriter, req *http.Request) error {
if req.Method != "GET" {
return httperror.Method{Allowed: []string{"GET"}}
}
authenticatedUser, err := h.users.GetAuthenticated(req.Context())
if err != nil {
log.Println(err)
authenticatedUser = users.User{} // THINK: Should it be a fatal error or not? What about on frontend vs backend?
}
var nc uint64
if authenticatedUser.ID != 0 {
nc, err = h.notification.CountNotifications(req.Context())
if err != nil {
return err
}
}
commitHash, err := verifyCommitHash(req.URL.Path[1:])
if err != nil {
return os.ErrNotExist
}
c, err := diffTree(req.Context(), h.Repo.Dir, commitHash, directoryGitPathspec(h.Dir), h.gitUsers)
if err != nil {
return err
}
if commitHash != c.CommitHash {
return os.ErrNotExist
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
var fullName string
if h.Dir.Package == nil {
fullName = "Directory " + path.Base(h.Dir.ImportPath)
} else if h.Dir.Package.IsCommand() {
fullName = "Command " + path.Base(h.Dir.ImportPath)
} else {
fullName = "Package " + h.Dir.Package.Name
}
err = commitHTML.Execute(w, struct {
AnalyticsHTML template.HTML
FullName string
Hash string
}{
AnalyticsHTML: analyticsHTML,
FullName: fullName,
Hash: shortSHA(c.CommitHash),
})
if err != nil {
return err
}
_, err = io.WriteString(w, `<div style="max-width: 800px; margin: 0 auto 100px auto;">`)
if err != nil {
return err
}
// Render the header.
header := homecomponent.Header{
CurrentUser: authenticatedUser,
NotificationCount: nc,
ReturnURL: req.RequestURI,
}
err = htmlg.RenderComponents(w, header)
if err != nil {
return err
}
err = html.Render(w, htmlg.H2(htmlg.Text(h.Dir.ImportPath)))
if err != nil {
return err
}
// Render the tabnav.
err = htmlg.RenderComponents(w, directoryTabnav(homecomponent.HistoryTab, h.PkgPath))
if err != nil {
return err
}
err = commitHTML.ExecuteTemplate(w, "CommitMessage", commitMessage{
ImportPath: h.Dir.ImportPath,
CommitHash: c.CommitHash,
Subject: strings.TrimPrefix(c.Subject, pathWithinRepo(h.Dir)+": "), // THINK: Trim package prefix from subject better?
Body: c.Body,
Author: c.Author,
AuthorTime: c.AuthorTime,
})
if err != nil {
return err
}
// Show warning we're displaying a part of the commit, link to entire commit.
err = htmlg.RenderComponents(w, homecomponent.Flash{
Content: htmlg.Nodes{
htmlg.Text("Showing partial commit. "),
htmlg.A("Full Commit", route.RepoCommit(h.Repo.Path)+"/"+c.CommitHash),
},
})
if err != nil {
return err
}
if len(c.Patch) == 0 {
// Empty commit. Let the user know via a blank slate.
err := htmlg.RenderComponents(w, homecomponent.BlankSlate{
Content: htmlg.Nodes{htmlg.Text("There are no affected files.")},
})
if err != nil {
return err
}
} else {
fileDiffs, err := diff.ParseMultiFileDiff(c.Patch)
if err != nil {
return err
}
for _, f := range fileDiffs {
// THINK: Trim package prefix from file paths better?
f.OrigName = strings.TrimPrefix(f.OrigName, pathWithinRepo(h.Dir)+"/")
f.NewName = strings.TrimPrefix(f.NewName, pathWithinRepo(h.Dir)+"/")
err := commitHTML.ExecuteTemplate(w, "FileDiff", fileDiff{FileDiff: f})
if err != nil {
return err
}
}
}
_, err = io.WriteString(w, `</div>
</body>
</html>`)
return err
}
func verifyCommitHash(commitHash string) (string, error) {
if len(commitHash) != 40 {
return "", fmt.Errorf("length is %v instead of 40", len(commitHash))
}
for _, b := range []byte(commitHash) {
ok := ('0' <= b && b <= '9') || ('a' <= b && b <= 'f')
if !ok {
return "", fmt.Errorf("commit hash contains unexpected character %+q", b)
}
}
return commitHash, nil
}
func shortSHA(sha string) string {
return sha[:8]
}
func diffTree(ctx context.Context, gitDir, treeish, pathspec string, gitUsers map[string]users.User) (diffTreeResponse, error) {
cmd := exec.CommandContext(ctx, "git", "diff-tree",
"--unified=5",
"--format=tformat:%H%x00%s%x00%b%x00%an%x00%ae%x00%aI",
"-z",
"--no-prefix",
"--always",
"--root",
"--find-renames",
//"--break-rewrites",
treeish, "--", pathspec)
cmd.Dir = gitDir
var buf bytes.Buffer
cmd.Stdout = &buf
err := cmd.Start()
if os.IsNotExist(err) {
// TODO: Document when this happens. Is it when gitDir doesn't exist, or git doesn't exist, or?
return diffTreeResponse{}, err
} else if err != nil {
return diffTreeResponse{}, fmt.Errorf("could not start command: %v", err)
}
err = cmd.Wait()
if ee, _ := err.(*exec.ExitError); ee != nil && ee.Sys().(syscall.WaitStatus).ExitStatus() == 128 {
return diffTreeResponse{}, os.ErrNotExist // Commit doesn't exist.
} else if err != nil {
return diffTreeResponse{}, fmt.Errorf("%v: %v", cmd.Args, err)
}
b := buf.Bytes()
var (
// Calls to readLine match exactly what is specified in --format.
commitHash = readLine(&b)
subject = readLine(&b)
body = readLine(&b)
authorName = readLine(&b)
authorEmail = readLine(&b)
authorDate = readLine(&b)
patch = b // There may be a leading '\n', but diff.ParseMultiFileDiff ignores it anyway, so leave it. It's not there when commit is empty.
)
author, ok := gitUsers[strings.ToLower(authorEmail)]
if !ok {
author = users.User{
Name: authorName,
Email: authorEmail,
AvatarURL: "https://secure.gravatar.com/avatar?d=mm&f=y&s=96", // TODO: Use email.
}
}
authorTime, err := time.Parse(time.RFC3339, authorDate)
if err != nil {
return diffTreeResponse{}, err
}
return diffTreeResponse{
CommitHash: commitHash,
Subject: subject,
Body: body,
Author: author,
AuthorTime: authorTime,
Patch: patch,
}, nil
}
type diffTreeResponse struct {
CommitHash string
Subject string
Body string
Author users.User
AuthorTime time.Time
Patch []byte
}
// readLine reads a line until zero byte, then updates b to the byte that immediately follows.
// A zero byte must exist in b, otherwise readLine panics.
func readLine(b *[]byte) string {
i := bytes.IndexByte(*b, 0)
s := string((*b)[:i])
*b = (*b)[i+1:]
return s
}
type commitMessage struct {
ImportPath string // Import path corresponding to directory used for linking to source code view.
CommitHash string
Subject string
Body string
Author users.User
AuthorTime time.Time
}
func (c commitMessage) ViewCode() template.HTML {
return template.HTML(htmlg.Render(&html.Node{
Type: html.ElementNode, Data: atom.A.String(),
Attr: []html.Attribute{
{Key: atom.Class.String(), Val: "lightgray"},
{Key: atom.Style.String(), Val: "height: 16px;"},
{Key: atom.Href.String(), Val: "https://gotools.org/" + c.ImportPath + "?rev=" + c.CommitHash},
{Key: atom.Title.String(), Val: "View code at this revision."},
},
FirstChild: octicon.Code(),
}))
}
func (c commitMessage) Avatar() template.HTML {
return template.HTML(htmlg.RenderComponentsString(issuescomponent.Avatar{User: c.Author, Size: 24}))
}
func (c commitMessage) User() template.HTML {
return template.HTML(htmlg.RenderComponentsString(issuescomponent.User{User: c.Author}))
}
func (c commitMessage) Time() template.HTML {
return template.HTML(htmlg.RenderComponentsString(issuescomponent.Time{Time: c.AuthorTime}))
}
type fileDiff struct {
*diff.FileDiff
}
func (f fileDiff) Title() (template.HTML, error) {
switch new, old := f.NewName, f.OrigName; {
case old != "/dev/null" && new != "/dev/null" && old == new: // Modified.
return template.HTML(html.EscapeString(new)), nil
case old != "/dev/null" && new != "/dev/null" && old != new: // Renamed.
return template.HTML(html.EscapeString(old + " -> " + new)), nil
case old == "/dev/null" && new != "/dev/null": // Added.
return template.HTML(html.EscapeString(new)), nil
case old != "/dev/null" && new == "/dev/null": // Removed.
return template.HTML("<strikethrough>" + html.EscapeString(old) + "</strikethrough>"), nil
default:
return "", fmt.Errorf("unexpected *diff.FileDiff: %+v", f)
}
}
func (f fileDiff) Diff() (template.HTML, error) {
hunks, err := diff.PrintHunks(f.Hunks)
if err != nil {
return "", err
}
html, err := highlightDiff(hunks)
if err != nil {
log.Println("fileDiff.Diff: highlightDiff:", err)
var buf bytes.Buffer
template.HTMLEscape(&buf, hunks)
html = buf.Bytes()
}
return template.HTML(html), nil
}
// highlightDiff highlights the src diff, returning the annotated HTML.
func highlightDiff(src []byte) ([]byte, error) {
anns, err := highlight_diff.Annotate(src)
if err != nil {
return nil, err
}
lines := bytes.Split(src, []byte("\n"))
lineStarts := make([]int, len(lines))
var offset int
for lineIndex := 0; lineIndex < len(lines); lineIndex++ {
lineStarts[lineIndex] = offset
offset += len(lines[lineIndex]) + 1
}
lastDel, lastIns := -1, -1
for lineIndex := 0; lineIndex < len(lines); lineIndex++ {
var lineFirstChar byte
if len(lines[lineIndex]) > 0 {
lineFirstChar = lines[lineIndex][0]
}
switch lineFirstChar {
case '+':
if lastIns == -1 {
lastIns = lineIndex
}
case '-':
if lastDel == -1 {
lastDel = lineIndex
}
default:
if lastDel != -1 || lastIns != -1 {
if lastDel == -1 {
lastDel = lastIns
} else if lastIns == -1 {
lastIns = lineIndex
}
beginOffsetLeft := lineStarts[lastDel]
endOffsetLeft := lineStarts[lastIns]
beginOffsetRight := lineStarts[lastIns]
endOffsetRight := lineStarts[lineIndex]
anns = append(anns, &annotate.Annotation{Start: beginOffsetLeft, End: endOffsetLeft, Left: []byte(`<span class="gd input-block">`), Right: []byte(`</span>`), WantInner: 0})
anns = append(anns, &annotate.Annotation{Start: beginOffsetRight, End: endOffsetRight, Left: []byte(`<span class="gi input-block">`), Right: []byte(`</span>`), WantInner: 0})
if '@' != lineFirstChar {
//leftContent := string(src[beginOffsetLeft:endOffsetLeft])
//rightContent := string(src[beginOffsetRight:endOffsetRight])
// This is needed to filter out the "-" and "+" at the beginning of each line from being highlighted.
// TODO: Still not completely filtered out.
leftContent := ""
for line := lastDel; line < lastIns; line++ {
leftContent += "\x00" + string(lines[line][1:]) + "\n"
}
rightContent := ""
for line := lastIns; line < lineIndex; line++ {
rightContent += "\x00" + string(lines[line][1:]) + "\n"
}
var sectionSegments [2][]*annotate.Annotation
highlight_diff.HighlightedDiffFunc(leftContent, rightContent, §ionSegments, [2]int{beginOffsetLeft, beginOffsetRight})
anns = append(anns, sectionSegments[0]...)
anns = append(anns, sectionSegments[1]...)
}
}
lastDel, lastIns = -1, -1
}
}
sort.Sort(anns)
out, err := annotate.Annotate(src, anns, template.HTMLEscape)
if err != nil {
return nil, err
}
return out, nil
}