all repos — elgit @ 5ce8c1235f20ffcf31de84e198b7ccb3c8569c35

fork of legit: web frontend for git, written in go

templates/commit.go (view raw)

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
package templates

import (
	"fmt"

	"github.com/bluekeyes/go-gitdiff/gitdiff"
	"github.com/go-git/go-git/v5/plumbing/object"
	"go.alanpearce.eu/elgit/git"
	g "go.alanpearce.eu/gomponents"
	. "go.alanpearce.eu/gomponents/html"
)

// CommitPage renders a commit diff page
func CommitPage(data PageData, diff *git.NiceDiff) g.Node {
	return Page(data, []g.Node{
		RepoHeader(data),
		RenderNav(data),
		Main(
			Section(Class("commit"),
				Pre(g.Text(diff.Commit.Message)),
				CommitInfo(diff.Commit.Author),
				Div(
					Strong(g.Text("commit")),
					P(A(Href(fmt.Sprintf("/%s/commit/%s", data.Name, diff.Commit.This)),
						Class("commit-hash"),
						g.Text(diff.Commit.This))),
				),
				g.If(diff.Commit.Parent != "",
					Div(
						Strong(g.Text("parent")),
						P(A(Href(fmt.Sprintf("/%s/commit/%s", data.Name, diff.Commit.Parent)),
							Class("commit-hash"),
							g.Text(diff.Commit.Parent))),
					),
				),
				Div(Class("diff-stat"),
					Div(g.Textf("%d file%s changed, %d insertion%s(+), %d deletion%s(-)",
						diff.Stat.FilesChanged, pluralise(diff.Stat.FilesChanged),
						diff.Stat.Insertions, pluralise(diff.Stat.Insertions),
						diff.Stat.Deletions, pluralise(diff.Stat.Deletions))),
					Div(
						Br(),
						Strong(g.Text("jump to")),
						Ul(g.Map(diff.Diff, func(d git.Diff) g.Node {
							return Li(A(Href("#"+d.Name.New), g.Text(d.Name.New)))
						})),
					),
				),
			),
			Section(
				g.Map(diff.Diff, func(d git.Diff) g.Node {
					return Div(ID(d.Name.New),
						Div(Class("diff"),
							g.If(d.IsNew,
								Span(Class("diff-type"), g.Text("A")),
								g.If(d.IsDelete,
									Span(Class("diff-type"), g.Text("D")),
									Span(Class("diff-type"), g.Text("M")),
								),
							),
							g.Text(" "),
							g.If(d.Name.Old != "",
								g.Group{
									A(Href(fmt.Sprintf("/%s/blob/%s/%s", data.Name, diff.Commit.Parent, d.Name.Old)),
										g.Text(d.Name.Old)),
									g.If(d.Name.New != "",
										g.Group{
											g.Text(" → "),
											A(
												Href(
													fmt.Sprintf("/%s/blob/%s/%s", data.Name, diff.Commit.This, d.Name.New),
												),
												g.Text(d.Name.New),
											),
										},
									),
								},
								A(Href(fmt.Sprintf("/%s/blob/%s/%s", data.Name, diff.Commit.This, d.Name.New)),
									g.Text(d.Name.New)),
							),
							g.If(d.IsBinary,
								P(g.Text("Not showing binary file.")),
								Pre(
									g.Group(g.Map(d.TextFragments, func(tf git.TextFragment) g.Node {
										return g.Group(
											[]g.Node{
												Header(Class("diff-hunk-header"), g.Text(tf.Header)),
												g.Map(tf.Lines, func(line gitdiff.Line) g.Node {
													switch line.Op.String() {
													case "+":
														return Span(Class("diff-add"), g.Text(line.String()))
													case "-":
														return Span(Class("diff-del"), g.Text(line.String()))
													default:
														return Span(Class("diff-noop"), g.Text(line.String()))
													}
												}),
											})
									})),
								),
							),
						),
					)
				})...,
			),
		),
	})
}

func CommitInfo(author object.Signature) g.Node {
	return Div(Class("commit-info"),
		g.Text(author.Name+" "),
		A(
			Href(fmt.Sprintf("mailto:%s", author.Email)),
			Class("commit-email"),
			g.Text(author.Email),
		),
		Div(g.Text(author.When.Format("Mon, 02 Jan 2006 15:04:05 -0700"))),
	)
}

func pluralise(n int) string {
	if n == 1 {
		return ""
	}
	return "s"
}