Blog/Components/Pages/Git/GitCommit.razor 8.4 K · 227 lines · raw · history

1 @page "/git/{Repository}/commit/{Sha}"
2 @inherits GitPage
3
4 <PageTitle>@Title — git</PageTitle>
5
6 <GitBar Repository="@Repository" Reference="@Reference" Tab="log"
7 Path="@(new[] { new GitBar.Crumb(Short, null) })"/>
8
9 <main class="git-main">
10 @if (View is not { } view)
11 {
12 <section class="block">
13 <p class="empty">No commit @Sha in @Repository.</p>
14 </section>
15 }
16 else
17 {
18 <section class="block">
19 <h2 class="block-head">
20 <span class="subject">@view.Commit.Summary</span>
21 <span class="block-note">
22 @foreach (var name in view.Commit.Refs)
23 {
24 <span class="ref">@name</span>
25 }
26 </span>
27 </h2>
28
29 <div class="block-body">
30 @if (view.Commit.Body is { Length: > 0 } body)
31 {
32 <p class="message">@body</p>
33 }
34
35 <dl class="pairs">
36 <dt>author</dt>
37 <dd>
38 @view.Commit.Author.Name <span class="dim">&lt;@view.Commit.Author.Email&gt;</span>
39 <span class="dim">· @Exact(view.Commit.Author.When)</span>
40 </dd>
41
42 @if (Differs(view.Commit))
43 {
44 <dt>committer</dt>
45 <dd>
46 @view.Commit.Committer.Name <span class="dim">&lt;@view.Commit.Committer.Email&gt;</span>
47 <span class="dim">· @Exact(view.Commit.Committer.When)</span>
48 </dd>
49 }
50
51 <dt>commit</dt>
52 <dd class="sha copyable">@view.Commit.Sha</dd>
53
54 @if (view.Commit.Parents.Count > 0)
55 {
56 <dt>parent@(view.Commit.Parents.Count == 1 ? "" : "s")</dt>
57 <dd>
58 @foreach (var parent in view.Commit.Parents)
59 {
60 <a class="sha" href="@CommitLink(parent)">@Shorten(parent)</a>
61 <text> </text>
62 }
63 </dd>
64 }
65
66 <dt>tree</dt>
67 <dd><a href="@TreeLink("", view.Commit.Sha)">browse at this commit</a></dd>
68 </dl>
69 </div>
70 </section>
71
72 <section class="block">
73 <h2 class="block-head">
74 <span>@Changed(view.Diff)</span>
75 <span class="block-note stat">
76 <span class="plus">+@view.Diff.Added</span>
77 <span class="minus">-@view.Diff.Deleted</span>
78 </span>
79 </h2>
80
81 @if (view.Diff.Files.Count == 0)
82 {
83 <p class="empty">
84 @(view.Commit.IsMerge
85 ? "Nothing changed against the first parent — the other side brought it all."
86 : "No changes.")
87 </p>
88 }
89 else
90 {
91 <div class="scroll">
92 <table>
93 <tbody>
94 @for (var index = 0; index < view.Diff.Files.Count; index++)
95 {
96 var file = view.Diff.Files[index];
97 <tr>
98 <td class="wide"><a href="#@Anchor(index)">@file.Path</a></td>
99 <td class="dim">@ChangeLabel(file.Change)</td>
100 <td class="num stat">
101 <span class="plus">+@file.Added</span>
102 <span class="minus">-@file.Deleted</span>
103 </td>
104 </tr>
105 }
106 </tbody>
107 </table>
108 </div>
109 }
110
111 @if (view.Diff.Truncated)
112 {
113 <p class="empty">
114 This diff is longer than one page will show. The files below it keep their
115 counts; read them at
116 <a href="@TreeLink("", view.Commit.Sha)">this commit's tree</a> instead.
117 </p>
118 }
119 </section>
120
121 @for (var index = 0; index < view.Diff.Files.Count; index++)
122 {
123 var file = view.Diff.Files[index];
124
125 <section class="block diff-file" id="@Anchor(index)">
126 <h2 class="block-head">
127 <span>
128 @if (file.OldPath is { } old)
129 {
130 <span class="dim">@old → </span>
131 }
132 <a href="@TreeLink(file.Path, view.Commit.Sha)">@file.Path</a>
133 </span>
134 <span class="block-note stat">
135 <span class="plus">+@file.Added</span>
136 <span class="minus">-@file.Deleted</span>
137 </span>
138 </h2>
139
140 @if (file.IsBinary)
141 {
142 <p class="empty">
143 Binary file.
144 <a href="@RawLink(file.Path, view.Commit.Sha)">Download it</a> as it was here.
145 </p>
146 }
147 else if (file.Skipped)
148 {
149 <p class="empty">Too far down a long diff to render. @file.Added added, @file.Deleted removed.</p>
150 }
151 else if (file.Hunks.Count == 0)
152 {
153 <p class="empty">@(ChangeLabel(file.Change) is { Length: > 0 } label ? label : "No textual change") — no lines changed.</p>
154 }
155 else
156 {
157 <div class="scroll">
158 <table class="code-table diff">
159 <tbody>
160 @foreach (var hunk in file.Hunks)
161 {
162 <tr class="hunk">
163 <td colspan="3">@hunk.Header</td>
164 </tr>
165
166 @foreach (var line in hunk.Lines)
167 {
168 @if (line.Origin == '\\')
169 {
170 <tr class="note">
171 <td colspan="3">@line.Text</td>
172 </tr>
173 }
174 else
175 {
176 <tr class="@Row(line.Origin)">
177 <td class="ln">@line.OldNumber</td>
178 <td class="ln">@line.NewNumber</td>
179 <td class="code"><span class="sign">@line.Origin</span>@line.Text</td>
180 </tr>
181 }
182 }
183 }
184 </tbody>
185 </table>
186 </div>
187 }
188 </section>
189 }
190 }
191 </main>
192
193 @code {
194 [Parameter]
195 public string Sha { get; set; } = "";
196
197 private GitCommitView? View { get; set; }
198
199 private string Short => Shorten(View?.Commit.Sha ?? Sha);
200
201 private string Title => View is { } view ? $"{view.Commit.Summary} · {Repository}" : Repository;
202
203 protected override void OnParametersSet()
204 {
205 View = Service.GetCommit(Repository, Sha);
206
207 if (View is null) NotFound();
208 }
209
210 private static string Shorten(string sha) => sha.Length >= 10 ? sha[..10] : sha;
211
212 /// <summary>A committer line worth printing is one that says something the author's didn't.</summary>
213 private static bool Differs(GitCommitInfo commit) =>
214 commit.Committer.Email != commit.Author.Email || commit.Committer.When != commit.Author.When;
215
216 private static string Changed(GitDiff diff) =>
217 diff.Files.Count == 1 ? "1 file changed" : $"{diff.Files.Count} files changed";
218
219 private static string Anchor(int index) => $"f{index}";
220
221 private static string Row(char origin) => origin switch
222 {
223 '+' => "add",
224 '-' => "del",
225 _ => "ctx"
226 };
227 }