| 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"><@view.Commit.Author.Email></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"><@view.Commit.Committer.Email></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 |
var grammar = Highlighted ? SyntaxHighlighter.GrammarFor(file.Path) : null; |
| 125 |
|
| 126 |
<section class="block diff-file" id="@Anchor(index)"> |
| 127 |
<h2 class="block-head"> |
| 128 |
<span> |
| 129 |
@if (file.OldPath is { } old) |
| 130 |
{ |
| 131 |
<span class="dim">@old → </span> |
| 132 |
} |
| 133 |
<a href="@TreeLink(file.Path, view.Commit.Sha)">@file.Path</a> |
| 134 |
</span> |
| 135 |
<span class="block-note stat"> |
| 136 |
<span class="plus">+@file.Added</span> |
| 137 |
<span class="minus">-@file.Deleted</span> |
| 138 |
</span> |
| 139 |
</h2> |
| 140 |
|
| 141 |
@if (file.IsBinary) |
| 142 |
{ |
| 143 |
<p class="empty"> |
| 144 |
Binary file. |
| 145 |
<a href="@RawLink(file.Path, view.Commit.Sha)">Download it</a> as it was here. |
| 146 |
</p> |
| 147 |
} |
| 148 |
else if (file.Skipped) |
| 149 |
{ |
| 150 |
<p class="empty">Too far down a long diff to render. @file.Added added, @file.Deleted removed.</p> |
| 151 |
} |
| 152 |
else if (file.Hunks.Count == 0) |
| 153 |
{ |
| 154 |
<p class="empty">@(ChangeLabel(file.Change) is { Length: > 0 } label ? label : "No textual change") — no lines changed.</p> |
| 155 |
} |
| 156 |
else |
| 157 |
{ |
| 158 |
<div class="scroll"> |
| 159 |
<table class="code-table diff"> |
| 160 |
<tbody> |
| 161 |
@foreach (var hunk in file.Hunks) |
| 162 |
{ |
| 163 |
var tokens = Highlight(hunk, grammar); |
| 164 |
|
| 165 |
<tr class="hunk"> |
| 166 |
<td colspan="3">@hunk.Header</td> |
| 167 |
</tr> |
| 168 |
|
| 169 |
@for (var row = 0; row < hunk.Lines.Count; row++) |
| 170 |
{ |
| 171 |
var line = hunk.Lines[row]; |
| 172 |
|
| 173 |
@if (line.Origin == '\\') |
| 174 |
{ |
| 175 |
<tr class="note"> |
| 176 |
<td colspan="3">@line.Text</td> |
| 177 |
</tr> |
| 178 |
} |
| 179 |
else |
| 180 |
{ |
| 181 |
<tr class="@Row(line.Origin)"> |
| 182 |
<td class="ln">@line.OldNumber</td> |
| 183 |
<td class="ln">@line.NewNumber</td> |
| 184 |
<td class="code"><GitCode Tokens="@(tokens[row])" Sign="@line.Origin"/></td> |
| 185 |
</tr> |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
</tbody> |
| 190 |
</table> |
| 191 |
</div> |
| 192 |
} |
| 193 |
</section> |
| 194 |
} |
| 195 |
} |
| 196 |
</main> |
| 197 |
|
| 198 |
@code { |
| 199 |
/// <summary> |
| 200 |
/// A diff longer than this is shown without highlighting. Every token in a highlighted line |
| 201 |
/// becomes a span, which is several times the length of the line itself, and a diff of |
| 202 |
/// machine-generated markup is almost entirely tokens — the commit that vendored the Warframe |
| 203 |
/// drop tables turned a 0.5 MB page into a 2.3 MB one. Nobody is reading a diff that size |
| 204 |
/// line by line, so it keeps its numbers and loses its spans. |
| 205 |
/// </summary> |
| 206 |
private const int MaxHighlightCharacters = 60_000; |
| 207 |
|
| 208 |
[Parameter] |
| 209 |
public string Sha { get; set; } = ""; |
| 210 |
|
| 211 |
private GitCommitView? View { get; set; } |
| 212 |
|
| 213 |
/// <summary>Whether this diff is small enough to be worth marking up. See above.</summary> |
| 214 |
private bool Highlighted { get; set; } |
| 215 |
|
| 216 |
private string Short => Shorten(View?.Commit.Sha ?? Sha); |
| 217 |
|
| 218 |
private string Title => View is { } view ? $"{view.Commit.Summary} · {Repository}" : Repository; |
| 219 |
|
| 220 |
protected override void OnParametersSet() |
| 221 |
{ |
| 222 |
View = Service.GetCommit(Repository, Sha); |
| 223 |
|
| 224 |
if (View is null) |
| 225 |
{ |
| 226 |
NotFound(); |
| 227 |
return; |
| 228 |
} |
| 229 |
|
| 230 |
Highlighted = View.Diff.Files |
| 231 |
.Sum(file => file.Hunks.Sum(hunk => hunk.Lines.Sum(line => line.Text.Length))) |
| 232 |
<= MaxHighlightCharacters; |
| 233 |
} |
| 234 |
|
| 235 |
private static string Shorten(string sha) => sha.Length >= 10 ? sha[..10] : sha; |
| 236 |
|
| 237 |
/// <summary>A committer line worth printing is one that says something the author's didn't.</summary> |
| 238 |
private static bool Differs(GitCommitInfo commit) => |
| 239 |
commit.Committer.Email != commit.Author.Email || commit.Committer.When != commit.Author.When; |
| 240 |
|
| 241 |
private static string Changed(GitDiff diff) => |
| 242 |
diff.Files.Count == 1 ? "1 file changed" : $"{diff.Files.Count} files changed"; |
| 243 |
|
| 244 |
private static string Anchor(int index) => $"f{index}"; |
| 245 |
|
| 246 |
/// <summary>Tokens for every line of a hunk, in the order the hunk lists them.</summary> |
| 247 |
/// <remarks> |
| 248 |
/// A diff line on its own is not enough to highlight with. A block comment or a template |
| 249 |
/// literal that opened three lines above it would go unnoticed, and the prose inside it would |
| 250 |
/// come back out as keywords. |
| 251 |
/// |
| 252 |
/// A hunk is enough, because a hunk is two contiguous runs of text: the lines the new file has |
| 253 |
/// (context and added) and the lines the old file had (context and removed). Each side is |
| 254 |
/// highlighted whole and each line takes its share back. Context lines belong to both, so the |
| 255 |
/// new side runs second and wins — the text is the same either way. |
| 256 |
/// |
| 257 |
/// A comment that opened before the hunk began is still lost. Git did not send those lines, |
| 258 |
/// so there is nothing here that could know about it. |
| 259 |
/// </remarks> |
| 260 |
private static IReadOnlyList<IReadOnlyList<SyntaxToken>> Highlight(GitDiffHunk hunk, SyntaxGrammar? grammar) |
| 261 |
{ |
| 262 |
var tokens = new IReadOnlyList<SyntaxToken>[hunk.Lines.Count]; |
| 263 |
Array.Fill(tokens, []); |
| 264 |
|
| 265 |
Side('-'); |
| 266 |
Side('+'); |
| 267 |
|
| 268 |
return tokens; |
| 269 |
|
| 270 |
void Side(char origin) |
| 271 |
{ |
| 272 |
var rows = Enumerable.Range(0, hunk.Lines.Count) |
| 273 |
.Where(row => hunk.Lines[row].Origin == origin || hunk.Lines[row].Origin == ' ') |
| 274 |
.ToArray(); |
| 275 |
|
| 276 |
var lines = SyntaxHighlighter.Highlight( |
| 277 |
string.Join('\n', rows.Select(row => hunk.Lines[row].Text)), |
| 278 |
grammar); |
| 279 |
|
| 280 |
for (var row = 0; row < rows.Length && row < lines.Count; row++) tokens[rows[row]] = lines[row]; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
private static string Row(char origin) => origin switch |
| 285 |
{ |
| 286 |
'+' => "add", |
| 287 |
'-' => "del", |
| 288 |
_ => "ctx" |
| 289 |
}; |
| 290 |
} |