diff options
author | June McEnroe | 2021-01-17 19:27:59 +0000 |
---|---|---|
committer | June McEnroe | 2022-02-13 11:56:12 -0500 |
commit | be6a526a74199d1ff1cf69e5dff3171a63ffc7f7 (patch) | |
tree | a5acb376ca4f9928f76a3b65528e5588236c63c8 | |
parent | Use buffered stdio (diff) | |
download | cgit-be6a526a74199d1ff1cf69e5dff3171a63ffc7f7.tar.gz cgit-be6a526a74199d1ff1cf69e5dff3171a63ffc7f7.zip |
Use <pre> and <span> to print diffs
This correctly preserves whitespace in browsers without CSS, as an alternative to [1]. [1]: https://80x24.org/cgit.git/commit/?id=7c692e6137697de8a8473c4de5c3de4fb03a2989
-rw-r--r-- | cgit.css | 14 | ||||
-rw-r--r-- | ui-diff.c | 30 |
2 files changed, 21 insertions, 23 deletions
diff --git a/cgit.css b/cgit.css index dac791b..36826b0 100644 --- a/cgit.css +++ b/cgit.css | |||
@@ -533,26 +533,20 @@ div#cgit table.diff { | |||
533 | width: 100%; | 533 | width: 100%; |
534 | } | 534 | } |
535 | 535 | ||
536 | div#cgit table.diff td { | 536 | div#cgit table.diff td span.head { |
537 | font-family: monospace; | ||
538 | white-space: pre; | ||
539 | } | ||
540 | |||
541 | div#cgit table.diff td div.head { | ||
542 | font-weight: bold; | 537 | font-weight: bold; |
543 | margin-top: 1em; | ||
544 | color: black; | 538 | color: black; |
545 | } | 539 | } |
546 | 540 | ||
547 | div#cgit table.diff td div.hunk { | 541 | div#cgit table.diff td span.hunk { |
548 | color: #009; | 542 | color: #009; |
549 | } | 543 | } |
550 | 544 | ||
551 | div#cgit table.diff td div.add { | 545 | div#cgit table.diff td span.add { |
552 | color: green; | 546 | color: green; |
553 | } | 547 | } |
554 | 548 | ||
555 | div#cgit table.diff td div.del { | 549 | div#cgit table.diff td span.del { |
556 | color: red; | 550 | color: red; |
557 | } | 551 | } |
558 | 552 | ||
diff --git a/ui-diff.c b/ui-diff.c index 329c350..97c1ca0 100644 --- a/ui-diff.c +++ b/ui-diff.c | |||
@@ -231,11 +231,11 @@ static void print_line(char *line, int len) | |||
231 | else if (line[0] == '@') | 231 | else if (line[0] == '@') |
232 | class = "hunk"; | 232 | class = "hunk"; |
233 | 233 | ||
234 | htmlf("<div class='%s'>", class); | 234 | htmlf("<span class='%s'>", class); |
235 | line[len-1] = '\0'; | 235 | line[len-1] = '\0'; |
236 | html_txt(line); | 236 | html_txt(line); |
237 | html("</div>"); | ||
238 | line[len-1] = c; | 237 | line[len-1] = c; |
238 | html("</span>\n"); | ||
239 | } | 239 | } |
240 | 240 | ||
241 | static void header(const struct object_id *oid1, char *path1, int mode1, | 241 | static void header(const struct object_id *oid1, char *path1, int mode1, |
@@ -245,22 +245,23 @@ static void header(const struct object_id *oid1, char *path1, int mode1, | |||
245 | int subproject; | 245 | int subproject; |
246 | 246 | ||
247 | subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2)); | 247 | subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2)); |
248 | html("<div class='head'>"); | 248 | html("<span class='head'>"); |
249 | html("diff --git a/"); | 249 | html("diff --git a/"); |
250 | html_txt(path1); | 250 | html_txt(path1); |
251 | html(" b/"); | 251 | html(" b/"); |
252 | html_txt(path2); | 252 | html_txt(path2); |
253 | html("\n"); | ||
253 | 254 | ||
254 | if (mode1 == 0) | 255 | if (mode1 == 0) |
255 | htmlf("<br/>new file mode %.6o", mode2); | 256 | htmlf("new file mode %.6o\n", mode2); |
256 | 257 | ||
257 | if (mode2 == 0) | 258 | if (mode2 == 0) |
258 | htmlf("<br/>deleted file mode %.6o", mode1); | 259 | htmlf("deleted file mode %.6o\n", mode1); |
259 | 260 | ||
260 | if (!subproject) { | 261 | if (!subproject) { |
261 | abbrev1 = xstrdup(find_unique_abbrev(oid1, DEFAULT_ABBREV)); | 262 | abbrev1 = xstrdup(find_unique_abbrev(oid1, DEFAULT_ABBREV)); |
262 | abbrev2 = xstrdup(find_unique_abbrev(oid2, DEFAULT_ABBREV)); | 263 | abbrev2 = xstrdup(find_unique_abbrev(oid2, DEFAULT_ABBREV)); |
263 | htmlf("<br/>index %s..%s", abbrev1, abbrev2); | 264 | htmlf("index %s..%s", abbrev1, abbrev2); |
264 | free(abbrev1); | 265 | free(abbrev1); |
265 | free(abbrev2); | 266 | free(abbrev2); |
266 | if (mode1 != 0 && mode2 != 0) { | 267 | if (mode1 != 0 && mode2 != 0) { |
@@ -268,28 +269,31 @@ static void header(const struct object_id *oid1, char *path1, int mode1, | |||
268 | if (mode2 != mode1) | 269 | if (mode2 != mode1) |
269 | htmlf("..%.6o", mode2); | 270 | htmlf("..%.6o", mode2); |
270 | } | 271 | } |
272 | html("\n"); | ||
271 | if (is_null_oid(oid1)) { | 273 | if (is_null_oid(oid1)) { |
272 | path1 = "dev/null"; | 274 | path1 = "dev/null"; |
273 | html("<br/>--- /"); | 275 | html("--- /"); |
274 | } else | 276 | } else |
275 | html("<br/>--- a/"); | 277 | html("--- a/"); |
276 | if (mode1 != 0) | 278 | if (mode1 != 0) |
277 | cgit_tree_link(path1, NULL, NULL, ctx.qry.head, | 279 | cgit_tree_link(path1, NULL, NULL, ctx.qry.head, |
278 | oid_to_hex(old_rev_oid), path1); | 280 | oid_to_hex(old_rev_oid), path1); |
279 | else | 281 | else |
280 | html_txt(path1); | 282 | html_txt(path1); |
283 | html("\n"); | ||
281 | if (is_null_oid(oid2)) { | 284 | if (is_null_oid(oid2)) { |
282 | path2 = "dev/null"; | 285 | path2 = "dev/null"; |
283 | html("<br/>+++ /"); | 286 | html("+++ /"); |
284 | } else | 287 | } else |
285 | html("<br/>+++ b/"); | 288 | html("+++ b/"); |
286 | if (mode2 != 0) | 289 | if (mode2 != 0) |
287 | cgit_tree_link(path2, NULL, NULL, ctx.qry.head, | 290 | cgit_tree_link(path2, NULL, NULL, ctx.qry.head, |
288 | oid_to_hex(new_rev_oid), path2); | 291 | oid_to_hex(new_rev_oid), path2); |
289 | else | 292 | else |
290 | html_txt(path2); | 293 | html_txt(path2); |
294 | html("\n"); | ||
291 | } | 295 | } |
292 | html("</div>"); | 296 | html("</span>"); |
293 | } | 297 | } |
294 | 298 | ||
295 | static void filepair_cb(struct diff_filepair *pair) | 299 | static void filepair_cb(struct diff_filepair *pair) |
@@ -488,12 +492,12 @@ void cgit_print_diff(const char *new_rev, const char *old_rev, | |||
488 | html("<table summary='ssdiff' class='ssdiff'>"); | 492 | html("<table summary='ssdiff' class='ssdiff'>"); |
489 | } else { | 493 | } else { |
490 | html("<table summary='diff' class='diff'>"); | 494 | html("<table summary='diff' class='diff'>"); |
491 | html("<tr><td>"); | 495 | html("<tr><td><pre>"); |
492 | } | 496 | } |
493 | cgit_diff_tree(old_rev_oid, new_rev_oid, filepair_cb, prefix, | 497 | cgit_diff_tree(old_rev_oid, new_rev_oid, filepair_cb, prefix, |
494 | ctx.qry.ignorews); | 498 | ctx.qry.ignorews); |
495 | if (!use_ssdiff) | 499 | if (!use_ssdiff) |
496 | html("</td></tr>"); | 500 | html("</pre></td></tr>"); |
497 | html("</table>"); | 501 | html("</table>"); |
498 | 502 | ||
499 | if (show_ctrls) | 503 | if (show_ctrls) |