Skip to content

Commit 7c21d58

Browse files
rolfbjarneCopilot
andauthored
[tools] Convert tools/diff-to-html to a C# script in scripts/diff-to-html (#24756)
Replace the old Mono csharp script with a proper .NET project following the scripts/ directory pattern. Update compare-commits.sh to use the new script via dotnet run. This fixes a problem with api-diff not working --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent e04e9e5 commit 7c21d58

File tree

5 files changed

+108
-99
lines changed

5 files changed

+108
-99
lines changed

scripts/diff-to-html/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# diff-to-html
2+
3+
Converts a unified diff file into a styled HTML page for easy viewing.
4+
5+
Usage:
6+
7+
$(DIFF_TO_HTML_EXEC) <input.diff> <output.html>
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,25 @@
1-
#!/usr/bin/env /Library/Frameworks/Mono.framework/Commands/csharp -s
2-
31
using System.IO;
4-
using System.Reflection;
2+
using System.Net;
53
using System.Text;
6-
using System.Web;
7-
8-
LoadAssembly ("System.Web.dll");
94

10-
Func<string, string> ParseDiff = (string diff) =>
11-
{
12-
StringBuilder result = new StringBuilder (diff.Length);
13-
string line;
14-
int old_ln = 0, new_ln = 0;
15-
int old_d = 0, new_d = 0;
16-
bool started = false;
17-
result.Append ("<div>");
18-
using (StringReader reader = new StringReader (diff)) {
19-
while ((line = reader.ReadLine ()) != null) {
20-
if (line.StartsWith ("diff --git")) {
21-
// New file
22-
if (started)
23-
result.AppendLine ("</table>");
24-
started = true;
25-
result.AppendLine ("<table class='diff_view_table'>");
26-
var fn = line.Substring (10).Trim ().Split (' ') [0];
27-
var fn_split = fn.Split ('/').ToList ();
28-
fn_split.RemoveAt (0);
29-
fn_split.RemoveAt (0);
30-
fn = string.Join ("/", fn_split);
31-
32-
result.AppendFormat ("<tr><td class='diff_view_header_td' colspan='3'>{0}</td></tr>\n", fn);
33-
} else if (line.StartsWith ("index")) {
34-
// Not sure what this is
35-
result.AppendFormat ("<tr><td class='diff_view_line_number'><pre class='diff_view_pre'>&nbsp;</pre></td><td class='diff_view_line_number'><pre class='diff_view_pre'>&nbsp;</pre></td><td class='diff_view_index_td'><pre class='diff_view_pre'>{0}</pre></td></tr>\n", line);
36-
} else if (line.StartsWith ("---") || line.StartsWith ("+++")) {
37-
// Ignore this for now
38-
// style = "background-color: white";
39-
// result.AppendFormat ("<tr style='{1}'><td colspan='3'>{0}</td></tr>", line, style);
40-
} else if (line.StartsWith ("@@")) {
41-
// line numbers
42-
string [] nl = line.Replace ("@@", "").Trim ().Split (' ');
43-
var oldc = nl [0].IndexOf (',');
44-
var newc = nl [1].IndexOf (',');
45-
old_ln = int.Parse (nl [0].Substring (1, oldc > 0 ? oldc - 1 : nl [0].Length - 1));
46-
new_ln = int.Parse (nl [1].Substring (1, newc > 0 ? newc - 1 : nl [1].Length - 1));
47-
result.AppendFormat ("<tr><td class='diff_view_line_number'><pre class='diff_view_pre'>&nbsp;</pre></td><td class='diff_view_line_number'><pre class='diff_view_pre'>&nbsp;</pre></td><td class='diff_view_at_td'><pre class='diff_view_pre'>{0}</pre></td></tr>\n", line);
48-
} else {
49-
string cl;
50-
if (line.StartsWith ("-")) {
51-
cl = "diff_view_removed_line";
52-
old_d = 1;
53-
new_d = 0;
54-
} else if (line.StartsWith ("+")) {
55-
cl = "diff_view_added_line";
56-
old_d = 0;
57-
new_d = 1;
58-
} else {
59-
cl = "diff_view_normal_line";
60-
old_d = 1;
61-
new_d = 1;
62-
}
63-
result.AppendFormat ("<tr><td class='diff_view_line_number'><pre class='diff_view_pre'>{2}</pre></td><td class='diff_view_line_number'><pre class='diff_view_pre'>{3}</pre></td><td class='{1}'><pre class='diff_view_pre'>{0}</pre></td></tr>\n",
64-
HttpUtility.HtmlEncode (line), cl, old_d == 0 ? string.Empty : old_ln.ToString (), new_d == 0 ? string.Empty : new_ln.ToString ());
65-
old_ln += old_d;
66-
new_ln += new_d;
67-
}
68-
}
5+
try {
6+
var expectedArgumentCount = 2;
7+
if (args.Length != expectedArgumentCount) {
8+
Console.WriteLine ($"Need {expectedArgumentCount} arguments, got {args.Length} arguments");
9+
return 1;
6910
}
70-
result.AppendLine ("</table>");
71-
result.AppendLine ("</div>");
72-
return result.ToString ();
73-
};
74-
75-
var args = Args;
76-
var expectedArgumentCount = 2;
77-
if (args.Length != expectedArgumentCount) {
78-
Console.WriteLine ($"Need {expectedArgumentCount} arguments, got {args.Length}");
79-
Environment.Exit (1);
80-
return;
81-
}
8211

83-
var idx = 0;
84-
var input = args [idx++];
85-
var output = args [idx++];
12+
var input = args [0];
13+
var output = args [1];
8614

87-
var diff = ParseDiff (File.ReadAllText (input));
88-
diff = @"
15+
var diff = ParseDiff (File.ReadAllText (input));
16+
diff = @"
8917
<html>
9018
<head>
9119
<style>
9220
/* diff view */
9321
94-
.diff_view_line_number
22+
.diff_view_line_number
9523
{
9624
background-color: #ececec;
9725
text-align: right;
@@ -106,19 +34,19 @@
10634
padding-right: 5px;
10735
}
10836
109-
.diff_view_normal_line
37+
.diff_view_normal_line
11038
{
11139
background-color: white;
11240
width: 100%;
11341
}
11442
115-
.diff_view_removed_line
43+
.diff_view_removed_line
11644
{
11745
background-color: #ffd0d0;
11846
width: 100%;
11947
}
12048
121-
.diff_view_added_line
49+
.diff_view_added_line
12250
{
12351
background-color: #d0ffd0;
12452
width: 100%;
@@ -133,38 +61,38 @@
13361
border-bottom-width: 1px;
13462
border-right-width: 0px;
13563
border-right-color: #777777;
136-
padding-top: 5px;
64+
padding-top: 5px;
13765
padding-bottom: 5px;
138-
padding-left: 5px;
66+
padding-left: 5px;
13967
padding-right: 5px;
14068
}
14169
14270
td.diff_view_td
14371
{
14472
border-width: 0px;
145-
padding-top: 0px;
73+
padding-top: 0px;
14674
padding-bottom: 0px;
147-
padding-left: 5px;
75+
padding-left: 5px;
14876
padding-right: 5px;
14977
}
15078
15179
td.diff_view_at_td
15280
{
15381
background-color: #f0f0f0;
15482
border-width: 0px;
155-
padding-top: 0px;
83+
padding-top: 0px;
15684
padding-bottom: 0px;
157-
padding-left: 5px;
85+
padding-left: 5px;
15886
padding-right: 5px;
15987
}
16088
16189
td.diff_view_index_td
16290
{
16391
background-color: #f0f0f0;
16492
border-width: 0px;
165-
padding-top: 0px;
93+
padding-top: 0px;
16694
padding-bottom: 0px;
167-
padding-left: 5px;
95+
padding-left: 5px;
16896
padding-right: 5px;
16997
}
17098
@@ -181,8 +109,8 @@
181109
margin-top: 5px;
182110
margin-bottom: 5px;
183111
width: 100%;
184-
border-width: 1px;
185-
border-style: outset;
112+
border-width: 1px;
113+
border-style: outset;
186114
border-color: #808080;
187115
border-collapse: separate;
188116
}
@@ -193,6 +121,73 @@
193121
" + diff + @"
194122
</html>
195123
";
196-
File.WriteAllText (output, diff);
124+
File.WriteAllText (output, diff);
125+
126+
return 0;
127+
} catch (Exception e) {
128+
Console.WriteLine ("Failed: {0}", e);
129+
return 1;
130+
}
197131

198-
Environment.Exit (0);
132+
static string ParseDiff (string diff)
133+
{
134+
StringBuilder result = new StringBuilder (diff.Length);
135+
string? line;
136+
int old_ln = 0, new_ln = 0;
137+
int old_d = 0, new_d = 0;
138+
bool started = false;
139+
result.Append ("<div>");
140+
using (StringReader reader = new StringReader (diff)) {
141+
while ((line = reader.ReadLine ()) is not null) {
142+
if (line.StartsWith ("diff --git")) {
143+
// New file
144+
if (started)
145+
result.AppendLine ("</table>");
146+
started = true;
147+
result.AppendLine ("<table class='diff_view_table'>");
148+
var fn = line.Substring (10).Trim ().Split (' ') [0];
149+
var fn_split = fn.Split ('/').ToList ();
150+
fn_split.RemoveAt (0);
151+
fn_split.RemoveAt (0);
152+
fn = string.Join ("/", fn_split);
153+
154+
result.AppendFormat ("<tr><td class='diff_view_header_td' colspan='3'>{0}</td></tr>\n", fn);
155+
} else if (line.StartsWith ("index")) {
156+
// Not sure what this is
157+
result.AppendFormat ("<tr><td class='diff_view_line_number'><pre class='diff_view_pre'>&nbsp;</pre></td><td class='diff_view_line_number'><pre class='diff_view_pre'>&nbsp;</pre></td><td class='diff_view_index_td'><pre class='diff_view_pre'>{0}</pre></td></tr>\n", line);
158+
} else if (line.StartsWith ("---") || line.StartsWith ("+++")) {
159+
// Ignore this for now
160+
} else if (line.StartsWith ("@@")) {
161+
// line numbers
162+
string [] nl = line.Replace ("@@", "").Trim ().Split (' ');
163+
var oldc = nl [0].IndexOf (',');
164+
var newc = nl [1].IndexOf (',');
165+
old_ln = int.Parse (nl [0].Substring (1, oldc > 0 ? oldc - 1 : nl [0].Length - 1));
166+
new_ln = int.Parse (nl [1].Substring (1, newc > 0 ? newc - 1 : nl [1].Length - 1));
167+
result.AppendFormat ("<tr><td class='diff_view_line_number'><pre class='diff_view_pre'>&nbsp;</pre></td><td class='diff_view_line_number'><pre class='diff_view_pre'>&nbsp;</pre></td><td class='diff_view_at_td'><pre class='diff_view_pre'>{0}</pre></td></tr>\n", line);
168+
} else {
169+
string cl;
170+
if (line.StartsWith ("-")) {
171+
cl = "diff_view_removed_line";
172+
old_d = 1;
173+
new_d = 0;
174+
} else if (line.StartsWith ("+")) {
175+
cl = "diff_view_added_line";
176+
old_d = 0;
177+
new_d = 1;
178+
} else {
179+
cl = "diff_view_normal_line";
180+
old_d = 1;
181+
new_d = 1;
182+
}
183+
result.AppendFormat ("<tr><td class='diff_view_line_number'><pre class='diff_view_pre'>{2}</pre></td><td class='diff_view_line_number'><pre class='diff_view_pre'>{3}</pre></td><td class='{1}'><pre class='diff_view_pre'>{0}</pre></td></tr>\n",
184+
WebUtility.HtmlEncode (line), cl, old_d == 0 ? string.Empty : old_ln.ToString (), new_d == 0 ? string.Empty : new_ln.ToString ());
185+
old_ln += old_d;
186+
new_ln += new_d;
187+
}
188+
}
189+
}
190+
result.AppendLine ("</table>");
191+
result.AppendLine ("</div>");
192+
return result.ToString ();
193+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net$(BundledNETCoreAppTargetFrameworkVersion)</TargetFramework>
4+
</PropertyGroup>
5+
</Project>

scripts/diff-to-html/fragment.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include $(TOP)/scripts/template.mk
2+
$(eval $(call TemplateScript,DIFF_TO_HTML,diff-to-html))

tools/compare-commits.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ if test -n "$ENABLE_GENERATOR_DIFF"; then
373373
echo " ${BLUE}Computing diff of the generated sources into ${WHITE}$GENERATOR_DIFF_FILE${BLUE}...${CLEAR}"
374374
cd "$OUTPUT_TMP_DIR/generator/build"
375375
git diff --no-index old new > "$GENERATOR_DIFF_FILE" || true
376-
"$ROOT_DIR/tools/diff-to-html" "$GENERATOR_DIFF_FILE" "$GENERATOR_HTML_FILE"
376+
dotnet run --project "$ROOT_DIR/scripts/diff-to-html/diff-to-html.csproj" -- "$GENERATOR_DIFF_FILE" "$GENERATOR_HTML_FILE"
377377
echo " ${BLUE}Computed generator diff: ${WHITE}$GENERATOR_HTML_FILE${BLUE}.${CLEAR}"
378378
echo ""
379379
else

0 commit comments

Comments
 (0)