Repository navigation
Expand file tree
/
Copy pathdiff2html.py
More file actions
executable file
·1197 lines (1038 loc) · 46.2 KB
/
Copy pathdiff2html.py
File metadata and controls
executable file
·1197 lines (1038 loc) · 46.2 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
diff2html - Generate GitHub-style HTML diff from a git diff.
Usage:
python diff2html.py [-o output.html] diff current branch vs its gh stack parent or main/master
python diff2html.py <commit-hash> [-o output.html] diff a specific commit
python diff2html.py <diff-file> [-o output.html] render an existing diff file
python diff2html.py --stdin [-o output.html] read diff from stdin
"""
import argparse
import json
import os
import re
import subprocess
import sys
from html import escape
from pathlib import Path
try:
from pygments import lex
from pygments.lexers import get_lexer_for_filename
from pygments.lexers.special import TextLexer
from pygments.token import Token
HAS_PYGMENTS = True
except ImportError:
HAS_PYGMENTS = False
# ---------------------------------------------------------------------------
# Diff parsing
# ---------------------------------------------------------------------------
class DiffFile:
def __init__(self):
self.old_path = ""
self.new_path = ""
self.status = "modified" # added | deleted | modified | renamed | copied
self.similarity = 100
self.binary = False
self.hunks: list[Hunk] = []
class Hunk:
def __init__(self, old_start: int, old_count: int, new_start: int, new_count: int, header: str = ""):
self.old_start = old_start
self.old_count = old_count
self.new_start = new_start
self.new_count = new_count
self.header = header
self.lines: list[DiffLine] = []
class DiffLine:
def __init__(self, kind: str, content: str, old_ln: int = 0, new_ln: int = 0):
# kind: ' ', '-', '+', or '\' (for no newline messages)
self.kind = kind
self.content = content
self.old_ln = old_ln
self.new_ln = new_ln
def _finalize_hunk(f: DiffFile, h: Hunk | None) -> Hunk | None:
"""Append current hunk to file and return None."""
if h is not None:
f.hunks.append(h)
return None
def _finalize_file(files: list[DiffFile], f: DiffFile | None, h: Hunk | None
) -> tuple[DiffFile | None, Hunk | None]:
"""Finalize hunk and file, appending to list if it has changes."""
if f is None:
return None, None
h = _finalize_hunk(f, h)
if f.hunks or f.binary:
files.append(f)
return None, None
def parse_diff(text: str) -> list[DiffFile]:
files: list[DiffFile] = []
current_file: DiffFile | None = None
current_hunk: Hunk | None = None
old_ln = 0
new_ln = 0
for raw_line in text.splitlines():
line = raw_line
# --- File header ---
if line.startswith("diff --git "):
current_file, current_hunk = _finalize_file(files, current_file, current_hunk)
current_file = DiffFile()
in_git_diff = True
parts = line.split()
if len(parts) >= 4:
current_file.old_path = parts[2][2:]
current_file.new_path = parts[3][2:]
continue
if current_file is None:
continue
if line.startswith("index "):
continue
if line.startswith("new file mode "):
current_file.status = "added"
continue
if line.startswith("deleted file mode "):
current_file.status = "deleted"
continue
if line.startswith("rename from "):
current_file.status = "renamed"
current_file.old_path = line[len("rename from "):]
continue
if line.startswith("rename to "):
current_file.new_path = line[len("rename to "):]
continue
if line.startswith("copy from "):
current_file.status = "copied"
current_file.old_path = line[len("copy from "):]
continue
if line.startswith("copy to "):
current_file.new_path = line[len("copy to "):]
continue
m = re.match(r"similarity index (\d+)%", line)
if m:
current_file.similarity = int(m.group(1))
continue
if line.startswith("Binary files ") and "differ" in line:
current_file.binary = True
continue
if line.startswith("--- "):
path = line[4:]
if path != "/dev/null":
current_file.old_path = path[2:] if path.startswith("a/") else path
continue
if line.startswith("+++ "):
path = line[4:]
if path != "/dev/null":
current_file.new_path = path[2:] if path.startswith("b/") else path
continue
# --- Hunk header ---
m = re.match(r"^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@(.*)", line)
if m:
current_hunk = _finalize_hunk(current_file, current_hunk)
old_s = int(m.group(1))
old_c = int(m.group(2)) if m.group(2) else 1
new_s = int(m.group(3))
new_c = int(m.group(4)) if m.group(4) else 1
current_hunk = Hunk(old_s, old_c, new_s, new_c, m.group(5).strip())
old_ln = old_s
new_ln = new_s
continue
if current_hunk is None:
continue
if line.startswith("\\ "):
current_hunk.lines.append(DiffLine("\\", line[1:]))
continue
if line.startswith("+"):
dl = DiffLine("+", line[1:], old_ln=0, new_ln=new_ln)
current_hunk.lines.append(dl)
new_ln += 1
elif line.startswith("-"):
dl = DiffLine("-", line[1:], old_ln=old_ln, new_ln=0)
current_hunk.lines.append(dl)
old_ln += 1
elif line.startswith(" "):
dl = DiffLine(" ", line[1:], old_ln=old_ln, new_ln=new_ln)
current_hunk.lines.append(dl)
old_ln += 1
new_ln += 1
# Finalize last file
_finalize_file(files, current_file, current_hunk)
return files
def _file_full_text(df: DiffFile, side: str) -> list[str]:
"""Reconstruct full file text from a delete/add diff.
side 'old' collects '-' plus context lines; 'new' collects '+' plus context.
"""
want = ("-", " ") if side == "old" else ("+", " ")
lines: list[str] = []
for h in df.hunks:
for dl in h.lines:
if dl.kind in want:
lines.append(dl.content)
return lines
def _is_pure_add(df: DiffFile) -> bool:
# Only explicit adds participate in rename detection (like git/GitHub).
# A *modified* file with only '+' lines is still a modify, not an add.
return df.status == "added"
def _is_pure_delete(df: DiffFile) -> bool:
# Only explicit deletes participate in rename detection (like git/GitHub).
return df.status == "deleted"
def _build_hunks_from_unified(unified: list[str]) -> list[Hunk]:
"""Parse `difflib.unified_diff` output (without ---/+++ headers) into Hunks."""
hunks: list[Hunk] = []
cur: Hunk | None = None
old_ln = 0
new_ln = 0
for raw in unified:
m = re.match(r"^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@(.*)", raw)
if m:
if cur is not None:
hunks.append(cur)
old_s = int(m.group(1))
old_c = int(m.group(2)) if m.group(2) else 1
new_s = int(m.group(3))
new_c = int(m.group(4)) if m.group(4) else 1
cur = Hunk(old_s, old_c, new_s, new_c, m.group(5).strip())
old_ln = old_s
new_ln = new_s
continue
if cur is None:
continue
if raw.startswith("\\ "):
cur.lines.append(DiffLine("\\", raw[1:]))
elif raw.startswith("+"):
cur.lines.append(DiffLine("+", raw[1:], old_ln=0, new_ln=new_ln))
new_ln += 1
elif raw.startswith("-"):
cur.lines.append(DiffLine("-", raw[1:], old_ln=old_ln, new_ln=0))
old_ln += 1
elif raw.startswith(" "):
cur.lines.append(DiffLine(" ", raw[1:], old_ln=old_ln, new_ln=new_ln))
old_ln += 1
new_ln += 1
if cur is not None:
hunks.append(cur)
return hunks
def reconstruct_renames(files: list[DiffFile], threshold: float = 0.5) -> list[DiffFile]:
"""Merge delete+add pairs with high content similarity into renames.
Matches GitHub's behaviour of rendering a rename as a single entry instead
of a separate delete (-N) and add (+M). Only applies to diffs that lack
rename metadata (e.g. generated with --no-renames); diffs where git already
emitted `rename from/to` are left untouched.
Uses difflib content similarity, mirroring git's default 50% threshold.
"""
import difflib
dels = [f for f in files if _is_pure_delete(f)]
adds = [f for f in files if _is_pure_add(f)]
if not dels or not adds:
return files
# Score every candidate pair.
scored: list[tuple[float, DiffFile, DiffFile, list[str], list[str]]] = []
for d in dels:
old_path = d.old_path or d.new_path
old_lines = _file_full_text(d, "old")
if not old_lines:
continue
for a in adds:
new_path = a.new_path or a.old_path
if (old_path and new_path) and old_path == new_path:
continue
new_lines = _file_full_text(a, "new")
if not new_lines:
continue
ratio = difflib.SequenceMatcher(None, old_lines, new_lines).ratio()
if ratio >= threshold:
scored.append((ratio, d, a, old_lines, new_lines))
if not scored:
return files
# Greedy best-first matching; each file merges at most once.
scored.sort(key=lambda t: t[0], reverse=True)
used: set[int] = set()
merged: dict[int, DiffFile] = {}
consumed_add: set[int] = set()
for ratio, d, a, old_lines, new_lines in scored:
if id(d) in used or id(a) in used:
continue
old_path = d.old_path or d.new_path
new_path = a.new_path or a.old_path
unified = list(difflib.unified_diff(
old_lines, new_lines,
fromfile=f"a/{old_path}", tofile=f"b/{new_path}",
n=3, lineterm="",
))
# Drop the ---/+++ file headers; keep hunk headers + body.
body = [ln for ln in unified if not ln.startswith("--- ") and not ln.startswith("+++ ")]
hunks = _build_hunks_from_unified(body)
if not hunks:
continue
nf = DiffFile()
nf.old_path = old_path
nf.new_path = new_path
nf.status = "renamed"
nf.similarity = int(ratio * 100)
nf.hunks = hunks
merged[id(d)] = nf
used.add(id(d))
used.add(id(a))
consumed_add.add(id(a))
if not merged:
return files
out: list[DiffFile] = []
emitted_rename: set[int] = set()
for f in files:
if id(f) in merged:
nf = merged[id(f)]
if id(nf) not in emitted_rename:
out.append(nf)
emitted_rename.add(id(nf))
elif id(f) in consumed_add:
continue # folded into a rename; skip the standalone add entry
else:
out.append(f)
return out
# ---------------------------------------------------------------------------
# HTML generation
# ---------------------------------------------------------------------------
CSS = """\
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif; background: #ffffff; color: #1f2328; overflow-x: hidden; font-weight: 500; }
.container { max-width: 1280px; margin: 0 auto; padding: 20px; }
/* Header */
.commit-header { background: #ffffff; border: 1px solid #d0d7de; border-radius: 6px; padding: 16px; margin-bottom: 16px; }
.commit-title { font-size: 16px; font-weight: 600; margin-bottom: 8px; }
.commit-meta { font-size: 12px; color: #656d76; }
.commit-meta span { margin-right: 16px; }
/* File list */
.file-list { background: #ffffff; border: 1px solid #d0d7de; border-radius: 6px; margin-bottom: 16px; overflow: hidden; }
.file-list-item { display: flex; align-items: center; padding: 8px 16px; border-bottom: 1px solid #d0d7de; text-decoration: none; color: #1f2328; font-size: 14px; font-weight: 500; }
.file-list-item:last-child { border-bottom: none; }
.file-list-item:hover { background: #e8eaed; }
.file-status { display: inline-block; width: 20px; height: 20px; border-radius: 3px; text-align: center; line-height: 20px; font-size: 11px; font-weight: 600; margin-right: 8px; flex-shrink: 0; }
.file-status-A { background: #2da44e; color: #fff; }
.file-status-M { background: #9a6700; color: #fff; }
.file-status-D { background: #cf222e; color: #fff; }
.file-status-R { background: #0969da; color: #fff; }
.file-path { flex: 1; }
.file-stats { font-size: 12px; }
.add-stat { color: #1a7f37; margin-right: 4px; }
.del-stat { color: #cf222e; }
/* Toggle all */
.toggle-all { margin-left: auto; font-size: 12px; }
.toggle-all a { color: #0969da; cursor: pointer; text-decoration: none; }
.toggle-all a:hover { text-decoration: underline; }
.toggle-all .sep { color: #d0d7de; margin: 0 6px; }
/* Collapsible file */
.diff-file { background: #ffffff; border: 1px solid #d0d7de; border-radius: 6px; margin-bottom: 16px; overflow: hidden; }
.diff-file[open] { border-radius: 6px; }
.diff-file-header { display: flex; align-items: center; padding: 8px 16px; background: #f6f8fa; font-size: 14px; font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace; cursor: pointer; user-select: none; font-weight: 500; }
.diff-file-header::-webkit-details-marker { display: none; }
.diff-file-header::before { content: "\\25B6"; color: #656d76; font-size: 10px; margin-right: 8px; transition: transform .15s; }
.diff-file[open] > .diff-file-header::before { transform: rotate(90deg); }
.diff-file-header .file-status { margin-right: 8px; }
.diff-file-header .file-path { flex: 1; }
.diff-file-header .file-stats { margin-left: 8px; }
.diff-file-header .copy-btn { background: none; border: 1px solid #d0d7de; color: #656d76; border-radius: 3px; cursor: pointer; font-size: 11px; padding: 1px 6px; margin-left: 8px; font-family: inherit; }
.diff-file-header .copy-btn:hover { background: #e8eaed; color: #1f2328; }
.diff-file-header .copy-btn.copied { border-color: #1a7f37; color: #1a7f37; }
.diff-binary { padding: 24px; text-align: center; color: #656d76; font-size: 14px; }
/* Unified diff table */
.diff-table-wrapper { overflow-x: auto; }
.diff-table { width: 100%; border-collapse: collapse; font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace; font-size: 12px; line-height: 20px; }
.diff-table td { padding: 0 8px; vertical-align: top; white-space: pre; font-weight: 500; }
.diff-table .ln { width: 50px; min-width: 50px; text-align: right; padding-right: 10px; color: #6e7781; -webkit-user-select: none; -moz-user-select: none; user-select: none; }
.diff-table .ln::before { content: attr(data-ln); }
.diff-table .kind::before { content: attr(data-kind); }
.diff-table .ln-old { border-right: 1px solid #d0d7de; }
.diff-table .ln-new { border-right: 1px solid #d0d7de; }
.diff-table .kind { width: 16px; min-width: 16px; text-align: center; padding: 0; -webkit-user-select: none; -moz-user-select: none; user-select: none; color: #6e7781; }
.diff-table .content { padding-left: 10px; padding-right: 10px; }
/* Line colours */
.line-add { background: #e6ffec; }
.line-add .ln-old { background: #ffffff; }
.line-add .ln-new { background: #e6ffec; }
.line-add .content { background: #e6ffec; }
.line-del { background: #ffebe9; }
.line-del .ln-old { background: #ffebe9; }
.line-del .ln-new { background: #ffffff; }
.line-del .content { background: #ffebe9; }
.line-ctx { background: #ffffff; }
.line-hunk-header { background: #f6f8fa; }
.line-hunk-header td { color: #656d76; padding: 4px 8px; font-weight: 500; }
/* File diff stats summary */
.stats-summary { display: flex; align-items: center; padding: 8px 16px; background: #f6f8fa; border-bottom: 1px solid #d0d7de; font-size: 12px; color: #656d76; flex-wrap: wrap; gap: 4px 16px; }
.stats-summary .additions { color: #1a7f37; }
.stats-summary .deletions { color: #cf222e; }
/* Syntax highlight token colours */
{token_css}
/* Sidebar */
.sidebar { position: fixed; top: 0; left: 0; width: 300px; height: 100vh; background: #f6f8fa; border-right: 1px solid #d0d7de; z-index: 100; transform: translateX(-100%); transition: transform .2s ease; display: flex; flex-direction: column; }
.sidebar.open { transform: translateX(0); }
.sidebar-header { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid #d0d7de; font-size: 14px; font-weight: 600; flex-shrink: 0; }
.sidebar-close { background: none; border: none; color: #656d76; cursor: pointer; font-size: 18px; padding: 0 4px; line-height: 1; }
.sidebar-close:hover { color: #1f2328; }
.sidebar-tree { padding: 8px 0; flex: 1; overflow-y: auto; min-height: 0; }
/* Sidebar toggle button */
.sidebar-toggle-btn { position: fixed; top: 12px; left: 12px; z-index: 99; background: #ffffff; border: 1px solid #d0d7de; color: #656d76; border-radius: 6px; cursor: pointer; font-size: 14px; padding: 4px 10px; line-height: 1.5; display: flex; align-items: center; gap: 6px; transition: opacity .2s; font-weight: 500; }
.sidebar-toggle-btn:hover { background: #e8eaed; color: #1f2328; }
.sidebar-toggle-btn.hidden { opacity: 0; pointer-events: none; }
/* Main content shift */
.main-content { transition: margin-left .2s ease; }
.main-content.shifted { margin-left: 300px; }
/* Tree folder */
.tree-folder { font-size: 13px; }
.tree-folder-header { display: flex; align-items: center; padding: 4px 16px; cursor: pointer; user-select: none; color: #1f2328; font-weight: 500; }
.tree-folder-header::-webkit-details-marker { display: none; }
.tree-folder-header:hover { background: #e8eaed; }
.tree-folder-icon { display: inline-block; width: 16px; font-size: 10px; color: #656d76; transition: transform .15s; flex-shrink: 0; text-align: center; }
.tree-folder[open] > .tree-folder-header .tree-folder-icon { transform: rotate(90deg); }
.tree-folder-name { margin-left: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-folder-contents { padding-left: 16px; }
/* Tree file */
.tree-file { display: flex; align-items: center; padding: 4px 16px; text-decoration: none; color: #1f2328; font-size: 13px; font-weight: 500; }
.tree-file:hover { background: #e8eaed; }
.tree-file .file-status { width: 16px; height: 16px; line-height: 16px; font-size: 9px; margin-right: 6px; flex-shrink: 0; }
.tree-file-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-file-stats { font-size: 11px; margin-left: auto; white-space: nowrap; }
.tree-file.active { background: #e8eaed; border-left: 2px solid #0969da; padding-left: 14px; }
.tree-file.active .tree-file-name { color: #0969da; }
"""
# ---------------------------------------------------------------------------
# Syntax highlighting (via pygments)
# ---------------------------------------------------------------------------
TOKEN_COLORS: dict = {}
if HAS_PYGMENTS:
TOKEN_COLORS = {
Token.Keyword: "#cf222e",
Token.Keyword.Constant: "#cf222e",
Token.Keyword.Declaration: "#cf222e",
Token.Keyword.Namespace: "#cf222e",
Token.Keyword.Pseudo: "#cf222e",
Token.Keyword.Reserved: "#cf222e",
Token.Keyword.Reserved: "#cf222e",
Token.Keyword.Type: "#cf222e",
Token.Name.Builtin: "#953800",
Token.Name.Builtin.Pseudo: "#953800",
Token.Name.Class: "#8250df",
Token.Name.Decorator: "#8250df",
Token.Name.Entity: "#8250df",
Token.Name.Exception: "#cf222e",
Token.Name.Function: "#8250df",
Token.Name.Label: "#cf222e",
Token.Name.Namespace: "#8250df",
Token.Name.Tag: "#116329",
Token.Name.Variable: "#953800",
Token.String: "#0550ae",
Token.String.Char: "#0550ae",
Token.String.Doc: "#0550ae",
Token.String.Double: "#0550ae",
Token.String.Escape: "#0550ae",
Token.String.Heredoc: "#0550ae",
Token.String.Interpol: "#0550ae",
Token.String.Other: "#0550ae",
Token.String.Single: "#0550ae",
Token.Number: "#0550ae",
Token.Number.Bin: "#0550ae",
Token.Number.Float: "#0550ae",
Token.Number.Hex: "#0550ae",
Token.Number.Integer: "#0550ae",
Token.Number.Oct: "#0550ae",
Token.Comment: "#6e7781",
Token.Comment.Hashbang: "#6e7781",
Token.Comment.Multiline: "#6e7781",
Token.Comment.Preproc: "#6e7781",
Token.Comment.Single: "#6e7781",
Token.Comment.Special: "#6e7781",
Token.Operator: "#cf222e",
Token.Operator.Word: "#cf222e",
Token.Punctuation: "#1f2328",
Token.Generic.Deleted: "#cf222e",
Token.Generic.Inserted: "#1a7f37",
Token.Generic.Emph: "#1f2328",
}
def _token_css() -> str:
lines = []
seen = set()
for ttype, color in TOKEN_COLORS.items():
cls = token_cls(ttype)
if cls in seen:
continue
seen.add(cls)
lines.append(f".{cls} {{ color: {color}; }}")
return "\n".join(lines)
def token_cls(ttype) -> str:
return "tok-" + str(ttype).replace(".", "-").replace(" ", "").lower().lstrip("token")
def get_lexer(path: str):
try:
return get_lexer_for_filename(path, stripnl=False, ensurenl=False)
except Exception:
return TextLexer(stripnl=False, ensurenl=False)
_LEXER_CACHE: dict[str, object] = {}
def get_cached_lexer(path: str):
if path not in _LEXER_CACHE:
_LEXER_CACHE[path] = get_lexer(path)
return _LEXER_CACHE[path]
def highlight_line(text: str, path: str) -> str:
lexer = get_cached_lexer(path)
parts = []
for ttype, value in lex(text, lexer):
cls = token_cls(ttype)
if cls and cls in _TOKEN_CLASSES_SET:
parts.append(f'<span class="{cls}">{escape(value)}</span>')
else:
parts.append(escape(value))
return "".join(parts)
# Pre-compute set of known token classes
_TOKEN_CLASSES_SET: set[str] = set()
for ttype in TOKEN_COLORS:
_TOKEN_CLASSES_SET.add(token_cls(ttype))
SYNTAX_CSS = _token_css()
else:
SYNTAX_CSS = ""
def highlight_line(text: str, path: str) -> str:
return escape(text)
def status_char(s: str) -> str:
return {"added": "A", "deleted": "D", "modified": "M", "renamed": "R", "copied": "C"}.get(s, "?")
def _build_file_tree(files: list[DiffFile]) -> dict:
"""Build a nested tree dict from file list."""
tree: dict = {"__files__": [], "__dirs__": {}}
for df in files:
path = df.new_path if df.new_path else df.old_path
parts = path.replace("\\", "/").split("/")
add_c = sum(1 for h in df.hunks for l in h.lines if l.kind == "+")
del_c = sum(1 for h in df.hunks for l in h.lines if l.kind == "-")
status = status_char(df.status)
anchor = path.replace("\\", "/")
node = tree
for i, part in enumerate(parts):
if i == len(parts) - 1:
node["__files__"].append({
"name": part,
"anchor": anchor,
"status": status,
"add": add_c,
"del": del_c,
})
else:
if part not in node["__dirs__"]:
node["__dirs__"][part] = {"__files__": [], "__dirs__": {}}
node = node["__dirs__"][part]
return tree
def _flatten_tree(node: dict) -> None:
"""Collapse single-child directory chains into compound names.
e.g. a/ → b/ → c/ becomes a/b/c/ when each has only that one child.
"""
for dirname in list(node["__dirs__"]):
child = node["__dirs__"][dirname]
_flatten_tree(child)
while len(child["__dirs__"]) == 1 and not child["__files__"]:
sub_name = next(iter(child["__dirs__"]))
sub_child = child["__dirs__"][sub_name]
merged = dirname + "/" + sub_name
node["__dirs__"][merged] = sub_child
del node["__dirs__"][dirname]
dirname = merged
child = sub_child
def _render_tree_node(node: dict) -> str:
"""Recursively render tree node to HTML."""
items = []
for dirname in sorted(node["__dirs__"]):
items.append(("dir", dirname, node["__dirs__"][dirname]))
for f in sorted(node["__files__"], key=lambda x: x["name"]):
items.append(("file", f["name"], f))
parts = []
for typ, name, data in items:
if typ == "dir":
child = _render_tree_node(data)
display_name = name + "/" if "/" in name else name
parts.append(
f'<details class="tree-folder" open>'
f'<summary class="tree-folder-header">'
f'<span class="tree-folder-icon">▾</span>'
f'<span class="tree-folder-name">{escape(display_name)}</span>'
f'</summary>'
f'<div class="tree-folder-contents">'
f'{child}'
f'</div>'
f'</details>'
)
else:
stats = ""
if data["add"] or data["del"]:
sp = []
if data["add"]:
sp.append(f'<span class="add-stat">+{data["add"]}</span>')
if data["del"]:
sp.append(f'<span class="del-stat">−{data["del"]}</span>')
stats = " " + " ".join(sp)
parts.append(
f'<a class="tree-file" href="#{escape(data["anchor"])}" data-file="{escape(data["anchor"])}">'
f'<span class="file-status file-status-{data["status"]}">{data["status"]}</span>'
f'<span class="tree-file-name">{escape(data["name"])}</span>'
f'<span class="tree-file-stats">{stats}</span>'
f'</a>'
)
return "\n".join(parts)
def render_hunk(hunk: Hunk, path: str = "") -> str:
rows = []
header_text = f"@@ -{hunk.old_start},{hunk.old_count} +{hunk.new_start},{hunk.new_count} @@"
if hunk.header:
header_text += f" {escape(hunk.header)}"
rows.append(
f'<tr class="line-hunk-header"><td class="ln ln-old"></td>'
f'<td class="ln ln-new"></td>'
f'<td class="kind"></td>'
f'<td class="content" colspan="2">{escape(header_text)}</td></tr>'
)
for dl in hunk.lines:
cls = {" ": "line-ctx", "+": "line-add", "-": "line-del", "\\": "line-ctx"}.get(dl.kind, "line-ctx")
old_ln_str = str(dl.old_ln) if dl.old_ln > 0 else ""
new_ln_str = str(dl.new_ln) if dl.new_ln > 0 else ""
kind_sym = dl.kind if dl.kind in ("+", "-", " ") else " "
if HAS_PYGMENTS and dl.kind in ("+", "-", " "):
content = highlight_line(dl.content, path)
else:
content = escape(dl.content)
rows.append(
f'<tr class="{cls}">'
f'<td class="ln ln-old" data-ln="{old_ln_str}"></td>'
f'<td class="ln ln-new" data-ln="{new_ln_str}"></td>'
f'<td class="kind" data-kind="{kind_sym}"></td>'
f'<td class="content">{content}</td>'
f'</tr>'
)
return "\n".join(rows)
def render_file(df: DiffFile, repo_dir: str, open_by_default: bool = False) -> str:
title = df.new_path if df.new_path else df.old_path
status = status_char(df.status)
anchor = title.replace("\\", "/")
parts = []
if df.binary:
parts.append(f'<details class="diff-file" id="{escape(anchor)}"{" open" if open_by_default else ""}>')
parts.append(
f'<summary class="diff-file-header">'
f'<span class="file-status file-status-{status}">{status}</span>'
f'<span class="file-path">{escape(title)}</span>'
f'<button class="copy-btn" data-path="{escape(title)}" onclick="event.stopPropagation();copyPath(this)">copy</button>'
f'</summary>'
f'<div class="diff-binary">Binary file not shown.</div>'
f'</details>'
)
return "\n".join(parts)
add_count = sum(1 for h in df.hunks for l in h.lines if l.kind == "+")
del_count = sum(1 for h in df.hunks for l in h.lines if l.kind == "-")
stats = ""
if add_count or del_count:
stats_parts = []
if add_count:
stats_parts.append(f'<span class="add-stat">+{add_count}</span>')
if del_count:
stats_parts.append(f'<span class="del-stat">−{del_count}</span>')
stats = " ".join(stats_parts)
path_label = title
if df.status == "renamed":
path_label = f"{df.old_path} → {df.new_path}"
parts.append(f'<details class="diff-file" id="{escape(anchor)}"{" open" if open_by_default else ""}>')
parts.append(
f'<summary class="diff-file-header">'
f'<span class="file-status file-status-{status}">{status}</span>'
f'<span class="file-path">{escape(path_label)}</span>'
f'<span class="file-stats">{stats}</span>'
f'<button class="copy-btn" data-path="{escape(title)}" onclick="event.stopPropagation();copyPath(this)">copy</button>'
f'</summary>'
)
parts.append('<div class="diff-table-wrapper"><table class="diff-table">')
for hunk in df.hunks:
parts.append(render_hunk(hunk, title))
parts.append('</table></div>')
parts.append('</details>')
return "\n".join(parts)
JS = """\
function toggleAll(expand) {
document.querySelectorAll('.diff-file').forEach(function(d) {
if (expand) d.setAttribute('open', ''); else d.removeAttribute('open');
});
}
function toggleSidebar() {
document.getElementById('sidebar').classList.toggle('open');
document.getElementById('main-content').classList.toggle('shifted');
document.getElementById('sidebar-toggle-btn').classList.toggle('hidden');
}
document.addEventListener('click', function(e) {
var link = e.target.closest('.file-list-item, .tree-file');
if (!link) return;
var href = link.getAttribute('href');
if (!href || !href.startsWith('#')) return;
var target = document.getElementById(href.slice(1));
if (target && target.tagName === 'DETAILS') {
target.setAttribute('open', '');
setTimeout(function() { target.scrollIntoView({ behavior: 'smooth', block: 'start' }); }, 50);
}
if (link.classList.contains('tree-file')) {
document.querySelectorAll('.tree-file.active').forEach(function(el) { el.classList.remove('active'); });
link.classList.add('active');
}
});
function copyPath(btn) {
var path = btn.getAttribute('data-path');
navigator.clipboard.writeText(path).then(function() {
btn.classList.add('copied');
btn.textContent = 'copied!';
setTimeout(function() {
btn.classList.remove('copied');
btn.textContent = 'copy';
}, 1500);
}).catch(function() {
// fallback
var ta = document.createElement('textarea');
ta.value = path;
document.body.appendChild(ta);
ta.select();
document.execCommand('copy');
document.body.removeChild(ta);
btn.classList.add('copied');
btn.textContent = 'copied!';
setTimeout(function() {
btn.classList.remove('copied');
btn.textContent = 'copy';
}, 1500);
});
}
"""
def generate_html(files: list[DiffFile], repo_dir: str = "", commit_info: dict | None = None) -> str:
total_add = sum(1 for f in files for h in f.hunks for l in h.lines if l.kind == "+")
total_del = sum(1 for f in files for h in f.hunks for l in h.lines if l.kind == "-")
# Build file list
file_list_rows = []
for df in files:
status = status_char(df.status)
path = df.new_path if df.new_path else df.old_path
anchor = path.replace("\\", "/")
label = path
if df.status == "renamed":
label = f"{df.old_path} → {df.new_path}"
add_c = sum(1 for h in df.hunks for l in h.lines if l.kind == "+")
del_c = sum(1 for h in df.hunks for l in h.lines if l.kind == "-")
stats = ""
if add_c or del_c:
sp = []
if add_c:
sp.append(f'<span class="add-stat">+{add_c}</span>')
if del_c:
sp.append(f'<span class="del-stat">-{del_c}</span>')
stats = " ".join(sp)
file_list_rows.append(
f'<a class="file-list-item" href="#{escape(anchor)}">'
f'<span class="file-status file-status-{status}">{status}</span>'
f'<span class="file-path">{escape(label)}</span>'
f'<span class="file-stats">{stats}</span>'
f'</a>'
)
# Commit header
commit_html = ""
if commit_info:
commit_html = f"""\
<div class="commit-header">
<div class="commit-title">{escape(commit_info.get("title", ""))}</div>
<div class="commit-meta">
<span>commit {commit_info.get("hash", "")}</span>
<span>author: {escape(commit_info.get("author", ""))}</span>
<span>date: {commit_info.get("date", "")}</span>
</div>
</div>"""
toggle_links = '<span class="toggle-all"><a onclick="toggleAll(true)">expand all</a><span class="sep">|</span><a onclick="toggleAll(false)">collapse all</a></span>'
stats_line = ""
if len(files) or total_add or total_del:
sp = []
sp.append(f'<span class="changed-files">{len(files)} file{"s" if len(files) != 1 else ""} changed</span>')
if total_add:
sp.append(f'<span class="additions">+{total_add} additions</span>')
if total_del:
sp.append(f'<span class="deletions">-{total_del} deletions</span>')
stats_line = f'<div class="stats-summary">{" | ".join(sp)}{toggle_links}</div>'
file_list_html = ""
if file_list_rows:
file_list_html = f"""\
<div class="file-list">
{stats_line}
{''.join(file_list_rows)}
</div>"""
diff_html = "\n".join(render_file(df, repo_dir) for df in files)
# Build sidebar tree
tree = _build_file_tree(files)
_flatten_tree(tree)
tree_html = _render_tree_node(tree)
return f"""\
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{escape(os.path.basename(repo_dir or "diff"))} — Diff</title>
<style>{CSS.replace("{token_css}", SYNTAX_CSS)}</style>
</head>
<body>
<aside class="sidebar" id="sidebar">
<div class="sidebar-header">
<span>Files changed ({len(files)})</span>
<button class="sidebar-close" onclick="toggleSidebar()">✕</button>
</div>
<div class="sidebar-tree">
{tree_html}
</div>
</aside>
<button class="sidebar-toggle-btn" id="sidebar-toggle-btn" onclick="toggleSidebar()">☰ Files</button>
<div class="main-content" id="main-content">
<div class="container">
{commit_html}
{file_list_html}
{diff_html}
</div>
</div>
<script>{JS}</script>
</body>
</html>"""
# ---------------------------------------------------------------------------
# Git helpers
# ---------------------------------------------------------------------------
# Git diff commands always enable rename detection so output matches GitHub,
# even if the user has diff.renames=false in their gitconfig.
GIT_DIFF_ARGS = ["-c", "diff.renames=true", "diff", "--find-renames"]
def get_diff_from_commit(repo_dir: str, commit: str) -> str:
"""Get diff for a commit (compared to parent)."""
# Check if it's a commit hash or ref
try:
# For merge commits, show diff against first parent
result = subprocess.run(
["git", *GIT_DIFF_ARGS, f"{commit}^..{commit}"],
capture_output=True, text=True, cwd=repo_dir
)
if result.returncode == 0 and result.stdout.strip():
return result.stdout
# Fallback: maybe it's the initial commit
result = subprocess.run(
["git", "-c", "diff.renames=true", "show",
"--find-renames", "--format=format:%b", commit],
capture_output=True, text=True, cwd=repo_dir
)
return result.stdout
except Exception as e:
print(f"Error getting diff: {e}", file=sys.stderr)
sys.exit(1)
def get_commit_info(repo_dir: str, commit: str) -> dict:
"""Get commit metadata."""
info = {}
try:
result = subprocess.run(
["git", "log", "-1", "--format=%H|%an|%ai|%s", commit],
capture_output=True, text=True, cwd=repo_dir
)
if result.returncode == 0:
parts = result.stdout.strip().split("|", 3)
if len(parts) >= 4:
info["hash"] = parts[0]
info["author"] = parts[1]
info["date"] = parts[2]
info["title"] = parts[3]
except Exception:
pass
return info
def read_diff_from_file(path: str) -> str:
with open(path, "r", encoding="utf-8", errors="replace") as f:
return f.read()
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
def _get_current_branch(repo_dir: str) -> str:
result = subprocess.run(
["git", "rev-parse", "--abbrev-ref", "HEAD"],
capture_output=True, text=True, cwd=repo_dir
)
return result.stdout.strip()
def _find_base_branch(repo_dir: str) -> str | None:
for branch in ["main", "master"]:
result = subprocess.run(
["git", "rev-parse", "--verify", branch],
capture_output=True, text=True, cwd=repo_dir
)
if result.returncode == 0:
return branch
return None
def _ref_exists(repo_dir: str, ref: str) -> bool:
# NOTE: `git rev-parse --verify` alone is not a reliable existence check:
# a full-length hex SHA passes verification even when the object is absent.
# `git cat-file -e` actually probes the object store.
result = subprocess.run(
["git", "cat-file", "-e", ref],
capture_output=True, text=True, cwd=repo_dir
)
return result.returncode == 0
def _is_ancestor(repo_dir: str, maybe_ancestor: str, ref: str) -> bool:
"""True when maybe_ancestor is an ancestor of ref (i.e. ref was built on it)."""
result = subprocess.run(
["git", "merge-base", "--is-ancestor", maybe_ancestor, ref],
capture_output=True, text=True, cwd=repo_dir
)
return result.returncode == 0