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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
|
/*
** Copyright (c) 2007 D. Richard Hipp
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the Simplified BSD License (also
** known as the "2-Clause License" or "FreeBSD License".)
**
** This program is distributed in the hope that it will be useful,
** but without any warranty; without even the implied warranty of
** merchantability or fitness for a particular purpose.
**
** Author contact information:
** drh@hwaci.com
** http://www.hwaci.com/drh/
**
*******************************************************************************
**
** This file contains code used to compute a "diff" between two
** text files.
*/
#include "config.h"
#include "diff.h"
#include <assert.h>
#include <errno.h>
#if INTERFACE
/*
** Flag parameters to the text_diff() routine used to control the formatting
** of the diff output.
*/
#define DIFF_IGNORE_EOLWS 0x00000001 /* Ignore end-of-line whitespace */
#define DIFF_IGNORE_ALLWS 0x00000003 /* Ignore all whitespace */
#define DIFF_SIDEBYSIDE 0x00000004 /* Generate a side-by-side diff */
#define DIFF_VERBOSE 0x00000008 /* Missing shown as empty files */
#define DIFF_BRIEF 0x00000010 /* Show filenames only */
#define DIFF_HTML 0x00000020 /* Render for HTML */
#define DIFF_LINENO 0x00000040 /* Show line numbers */
#define DIFF_NUMSTAT 0x00000080 /* Show line count of changes */
#define DIFF_NOOPT 0x00000100 /* Suppress optimizations (debug) */
#define DIFF_INVERT 0x00000200 /* Invert the diff (debug) */
#define DIFF_CONTEXT_EX 0x00000400 /* Use context even if zero */
#define DIFF_NOTTOOBIG 0x00000800 /* Only display if not too big */
#define DIFF_STRIP_EOLCR 0x00001000 /* Strip trailing CR */
#define DIFF_SLOW_SBS 0x00002000 /* Better but slower side-by-side */
#define DIFF_WEBPAGE 0x00004000 /* Complete webpage */
#define DIFF_BROWSER 0x00008000 /* The --browser option */
#define DIFF_JSON 0x00010000 /* JSON output */
#define DIFF_DEBUG 0x00020000 /* Debugging diff output */
#define DIFF_RAW 0x00040000 /* Raw triples - for debugging */
#define DIFF_TCL 0x00080000 /* For the --tk option */
#define DIFF_INCBINARY 0x00100000 /* The --diff-binary option */
#define DIFF_SHOW_VERS 0x00200000 /* Show compared versions */
#define DIFF_DARKMODE 0x00400000 /* Use dark mode for HTML */
#define DIFF_BY_TOKEN 0x01000000 /* Split on tokens, not lines */
/*
** Per file information that may influence output.
*/
#define DIFF_FILE_ADDED 0x40000000 /* Added or rename destination */
#define DIFF_FILE_DELETED 0x80000000 /* Deleted or rename source */
#define DIFF_FILE_MASK 0xc0000000 /* Used for clearing file flags */
/*
** These error messages are shared in multiple locations. They are defined
** here for consistency.
*/
#define DIFF_CANNOT_COMPUTE_BINARY \
"cannot compute difference between binary files\n"
#define DIFF_CANNOT_COMPUTE_SYMLINK \
"cannot compute difference between symlink and regular file\n"
#define DIFF_TOO_MANY_CHANGES \
"more than 10,000 changes\n"
#define DIFF_WHITESPACE_ONLY \
"whitespace changes only\n"
/*
** Maximum length of a line in a text file, in bytes. (2**15 = 32768 bytes)
*/
#define LENGTH_MASK_SZ 15
#define LENGTH_MASK ((1<<LENGTH_MASK_SZ)-1)
/*
** An instance of this object describes the formatting and processing
** details desired of a "diff" operation.
**
** Conceptually, this object is as an encoding of the command-line options
** for the "fossil diff" command. That is not a precise description, though,
** because not all diff operations are started from the command-line. But
** the idea is sound.
**
** Information encoded by this object includes but is not limited to:
**
** * The desired output format (unified vs. side-by-side,
** TCL, JSON, HTML vs. plain-text).
**
** * Number of lines of context surrounding each difference block
**
** * Width of output columns for text side-by-side diffop
*/
struct DiffConfig {
u64 diffFlags; /* Diff flags */
int nContext; /* Number of lines of context */
int wColumn; /* Column width in -y mode */
u32 nFile; /* Number of files diffed so far */
const char *zDiffCmd; /* External diff command to use instead of builtin */
const char *zBinGlob; /* GLOB pattern for binary files */
ReCompiled *pRe; /* Show only changes matching this pattern */
const char *zLeftHash; /* HASH-id of the left file */
const char *azLabel[2]; /* Optional labels for left and right files */
};
#endif /* INTERFACE */
/*
** Initialize memory for a DiffConfig based on just a diffFlags integer.
*/
DiffConfig *diff_config_init(DiffConfig *pCfg, u64 diffFlags){
memset(pCfg, 0, sizeof(*pCfg));
pCfg->diffFlags = diffFlags;
return pCfg;
}
/*
** Information about each line of a file being diffed.
**
** The lower LENGTH_MASK_SZ bits of the hash (DLine.h) are the length
** of the line. If any line is longer than LENGTH_MASK characters,
** the file is considered binary.
*/
typedef struct DLine DLine;
struct DLine {
const char *z; /* The text of the line */
u64 h; /* Hash of the line */
unsigned short indent; /* Index of first non-space */
unsigned short n; /* number of bytes */
unsigned short nw; /* number of bytes without leading/trailing space */
unsigned int iNext; /* 1+(Index of next line with same the same hash) */
/* an array of DLine elements serves two purposes. The fields
** above are one per line of input text. But each entry is also
** a bucket in a hash table, as follows: */
unsigned int iHash; /* 1+(first entry in the hash chain) */
};
/*
** Length of a dline
*/
#define LENGTH(X) ((X)->n)
/*
** Number of diff chunks generated
*/
static int nChunk = 0;
/*
** A context for running a raw diff.
**
** The aEdit[] array describes the raw diff. Each triple of integers in
** aEdit[] means:
**
** (1) COPY: Number of lines aFrom and aTo have in common
** (2) DELETE: Number of lines found only in aFrom
** (3) INSERT: Number of lines found only in aTo
**
** The triples repeat until all lines of both aFrom and aTo are accounted
** for.
*/
typedef struct DContext DContext;
struct DContext {
int *aEdit; /* Array of copy/delete/insert triples */
int nEdit; /* Number of integers (3x num of triples) in aEdit[] */
int nEditAlloc; /* Space allocated for aEdit[] */
DLine *aFrom; /* File on left side of the diff */
int nFrom; /* Number of lines in aFrom[] */
DLine *aTo; /* File on right side of the diff */
int nTo; /* Number of lines in aTo[] */
int (*xDiffer)(const DLine *,const DLine *); /* comparison function */
};
/* Fast isspace for use by diff */
static const char diffIsSpace[] = {
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
#define diff_isspace(X) (diffIsSpace[(unsigned char)(X)])
/*
** Count the number of lines in the input string. Include the last line
** in the count even if it lacks the \n terminator. If an empty string
** is specified, the number of lines is zero. For the purposes of this
** function, a string is considered empty if it contains no characters
** -OR- it contains only NUL characters.
**
** Returns true if the input seems to be plain text input, else false.
** If it returns false, pnLine is not modified, else it is set to the
** number of lines in z.
*/
int count_lines(
const char *z,
int n,
int *pnLine
){
int nLine;
const char *zNL, *z2;
for(nLine=0, z2=z; (zNL = strchr(z2,'\n'))!=0; z2=zNL+1, nLine++){}
if( z2[0]!='\0' ){
nLine++;
do{ z2++; }while( z2[0]!='\0' );
}
if( n!=(int)(z2-z) ) return 0;
if( pnLine ) *pnLine = nLine;
return 1;
}
/*
** Return an array of DLine objects containing a pointer to the
** start of each line and a hash of that line. The lower
** bits of the hash store the length of each line.
**
** Trailing whitespace is removed from each line. 2010-08-20: Not any
** more. If trailing whitespace is ignored, the "patch" command gets
** confused by the diff output. Ticket [a9f7b23c2e376af5b0e5b]
**
** Return 0 if the file is binary or contains a line that is
** too long.
**
** Profiling show that in most cases this routine consumes the bulk of
** the CPU time on a diff.
*/
static DLine *break_into_lines(
const char *z,
int n,
int *pnLine,
u64 diffFlags
){
int nLine, i, k, nn, s, x;
u64 h, h2;
DLine *a;
const char *zNL;
if( count_lines(z, n, &nLine)==0 ){
return 0;
}
assert( nLine>0 || z[0]=='\0' );
a = fossil_malloc( sizeof(a[0])*nLine );
memset(a, 0, sizeof(a[0])*nLine);
if( nLine==0 ){
*pnLine = 0;
return a;
}
i = 0;
do{
zNL = strchr(z,'\n');
if( zNL==0 ) zNL = z+n;
nn = (int)(zNL - z);
if( nn>LENGTH_MASK ){
fossil_free(a);
return 0;
}
a[i].z = z;
k = nn;
if( diffFlags & DIFF_STRIP_EOLCR ){
if( k>0 && z[k-1]=='\r' ){ k--; }
}
a[i].n = k;
if( diffFlags & DIFF_IGNORE_EOLWS ){
while( k>0 && diff_isspace(z[k-1]) ){ k--; }
}
if( (diffFlags & DIFF_IGNORE_ALLWS)==DIFF_IGNORE_ALLWS ){
int numws = 0;
for(s=0; s<k && z[s]<=' '; s++){}
a[i].indent = s;
a[i].nw = k - s;
for(h=0, x=s; x<k; x++){
char c = z[x];
if( diff_isspace(c) ){
++numws;
}else{
h = (h^c)*9000000000000000041LL;
}
}
k -= numws;
}else{
int k2 = k & ~0x7;
u64 m;
for(h=x=s=0; x<k2; x += 8){
memcpy(&m, z+x, 8);
h = (h^m)*9000000000000000041LL;
}
m = 0;
memcpy(&m, z+x, k-k2);
h ^= m;
}
a[i].h = h = ((h%281474976710597LL)<<LENGTH_MASK_SZ) | (k-s);
h2 = h % nLine;
a[i].iNext = a[h2].iHash;
a[h2].iHash = i+1;
z += nn+1; n -= nn+1;
i++;
}while( zNL[0]!='\0' && zNL[1]!='\0' );
assert( i==nLine );
/* Return results */
*pnLine = nLine;
return a;
}
/*
** Character classes for the purpose of tokenization.
**
** 1 - alphanumeric
** 2 - whitespace
** 3 - punctuation
*/
static char aTCharClass[256] = {
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3,
3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3,
3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};
/*
** Count the number of tokens in the given string.
*/
static int count_tokens(const unsigned char *p, int n){
int nToken = 0;
int iPrev = 0;
int i;
for(i=0; i<n; i++){
char x = aTCharClass[p[i]];
if( x!=iPrev ){
iPrev = x;
nToken++;
}
}
return nToken;
}
/*
** Return an array of DLine objects containing a pointer to the
** start of each token and a hash of that token. The lower
** bits of the hash store the length of each token.
**
** This is like break_into_lines() except that it works with tokens
** instead of lines. A token is:
**
** * A contiguous sequence of alphanumeric characters.
** * A contiguous sequence of whitespace
** * A contiguous sequence of punctuation characters.
**
** Return 0 if the file is binary or contains a line that is
** too long.
*/
static DLine *break_into_tokens(
const char *z,
int n,
int *pnToken,
u64 diffFlags
){
int nToken, i, k;
u64 h, h2;
DLine *a;
unsigned char *p = (unsigned char*)z;
nToken = count_tokens(p, n);
a = fossil_malloc( sizeof(a[0])*(nToken+1) );
memset(a, 0, sizeof(a[0])*(nToken+1));
if( n==0 ){
*pnToken = 0;
return a;
}
i = 0;
while( n>0 ){
char x = aTCharClass[*p];
h = 0xcbf29ce484222325LL;
for(k=1; k<n && aTCharClass[p[k]]==x; k++){
h ^= p[k];
h *= 0x100000001b3LL;
}
a[i].z = (char*)p;
a[i].n = k;
a[i].h = h = ((h%281474976710597LL)<<LENGTH_MASK_SZ) | k;
h2 = h % nToken;
a[i].iNext = a[h2].iHash;
a[h2].iHash = i+1;
p += k; n -= k;
i++;
};
assert( i==nToken );
/* Return results */
*pnToken = nToken;
return a;
}
/*
** Return zero if two DLine elements are identical.
*/
static int compare_dline(const DLine *pA, const DLine *pB){
if( pA->h!=pB->h ) return 1;
return memcmp(pA->z,pB->z, pA->h&LENGTH_MASK);
}
/*
** Return zero if two DLine elements are identical, ignoring
** all whitespace. The indent field of pA/pB already points
** to the first non-space character in the string.
*/
static int compare_dline_ignore_allws(const DLine *pA, const DLine *pB){
if( pA->h==pB->h ){
int a, b;
if( memcmp(pA->z, pB->z, pA->h&LENGTH_MASK)==0 ) return 0;
a = pA->indent;
b = pB->indent;
while( a<pA->n || b<pB->n ){
if( a<pA->n && b<pB->n && pA->z[a++] != pB->z[b++] ) return 1;
while( a<pA->n && diff_isspace(pA->z[a])) ++a;
while( b<pB->n && diff_isspace(pB->z[b])) ++b;
}
return pA->n-a != pB->n-b;
}
return 1;
}
/*
** Return true if the regular expression *pRe matches any of the
** N dlines
*/
static int re_dline_match(
ReCompiled *pRe, /* The regular expression to be matched */
const DLine *aDLine, /* First of N DLines to compare against */
int N /* Number of DLines to check */
){
while( N-- ){
if( re_match(pRe, (const unsigned char *)aDLine->z, LENGTH(aDLine)) ){
return 1;
}
aDLine++;
}
return 0;
}
/*
** Append a single line of context-diff output to pOut.
*/
static void appendDiffLine(
Blob *pOut, /* Where to write the line of output */
char cPrefix, /* One of " ", "+", or "-" */
const DLine *pLine /* The line to be output */
){
blob_append_char(pOut, cPrefix);
blob_append(pOut, pLine->z, pLine->n);
blob_append_char(pOut, '\n');
}
/*
** Add two line numbers to the beginning of an output line for a context
** diff. One or the other of the two numbers might be zero, which means
** to leave that number field blank.
*/
static void appendDiffLineno(Blob *pOut, int lnA, int lnB){
if( lnA>0 ){
blob_appendf(pOut, "%6d ", lnA);
}else{
blob_append(pOut, " ", 7);
}
if( lnB>0 ){
blob_appendf(pOut, "%6d ", lnB);
}else{
blob_append(pOut, " ", 8);
}
}
/*
** Output a patch-style text diff.
*/
static void contextDiff(
DContext *p, /* The difference */
Blob *pOut, /* Output a context diff to here */
DiffConfig *pCfg /* Configuration options */
){
const DLine *A; /* Left side of the diff */
const DLine *B; /* Right side of the diff */
int a = 0; /* Index of next line in A[] */
int b = 0; /* Index of next line in B[] */
int *R; /* Array of COPY/DELETE/INSERT triples */
int r; /* Index into R[] */
int nr; /* Number of COPY/DELETE/INSERT triples to process */
int mxr; /* Maximum value for r */
int na, nb; /* Number of lines shown from A and B */
int i, j; /* Loop counters */
int m; /* Number of lines to output */
int skip; /* Number of lines to skip */
int nContext; /* Number of lines of context */
int showLn; /* Show line numbers */
int showDivider = 0; /* True to show the divider between diff blocks */
nContext = diff_context_lines(pCfg);
showLn = (pCfg->diffFlags & DIFF_LINENO)!=0;
A = p->aFrom;
B = p->aTo;
R = p->aEdit;
mxr = p->nEdit;
while( mxr>2 && R[mxr-1]==0 && R[mxr-2]==0 ){ mxr -= 3; }
for(r=0; r<mxr; r += 3*nr){
/* Figure out how many triples to show in a single block */
for(nr=1; 3*nr<mxr && R[r+nr*3]>0 && R[r+nr*3]<(int)nContext*2; nr++){}
/* printf("r=%d nr=%d\n", r, nr); */
/* For the current block comprising nr triples, figure out
** how many lines of A and B are to be displayed
*/
if( R[r]>nContext ){
na = nb = nContext;
skip = R[r] - nContext;
}else{
na = nb = R[r];
skip = 0;
}
for(i=0; i<nr; i++){
na += R[r+i*3+1];
nb += R[r+i*3+2];
}
if( R[r+nr*3]>nContext ){
na += nContext;
nb += nContext;
}else{
na += R[r+nr*3];
nb += R[r+nr*3];
}
for(i=1; i<nr; i++){
na += R[r+i*3];
nb += R[r+i*3];
}
/* Show the header for this block, or if we are doing a modified
** context diff that contains line numbers, show the separator from
** the previous block.
*/
nChunk++;
if( showLn ){
if( !showDivider ){
/* Do not show a top divider */
showDivider = 1;
}else{
blob_appendf(pOut, "%.80c\n", '.');
}
}else{
/*
* If the patch changes an empty file or results in an empty file,
* the block header must use 0,0 as position indicator and not 1,0.
* Otherwise, patch would be confused and may reject the diff.
*/
blob_appendf(pOut,"@@ -%d,%d +%d,%d @@",
na ? a+skip+1 : a+skip, na,
nb ? b+skip+1 : b+skip, nb);
blob_append(pOut, "\n", 1);
}
/* Show the initial common area */
a += skip;
b += skip;
m = R[r] - skip;
for(j=0; j<m; j++){
if( showLn ) appendDiffLineno(pOut, a+j+1, b+j+1);
appendDiffLine(pOut, ' ', &A[a+j]);
}
a += m;
b += m;
/* Show the differences */
for(i=0; i<nr; i++){
m = R[r+i*3+1];
for(j=0; j<m; j++){
if( showLn ) appendDiffLineno(pOut, a+j+1, 0);
appendDiffLine(pOut, '-', &A[a+j]);
}
a += m;
m = R[r+i*3+2];
for(j=0; j<m; j++){
if( showLn ) appendDiffLineno(pOut, 0, b+j+1);
appendDiffLine(pOut, '+', &B[b+j]);
}
b += m;
if( i<nr-1 ){
m = R[r+i*3+3];
for(j=0; j<m; j++){
if( showLn ) appendDiffLineno(pOut, a+j+1, b+j+1);
appendDiffLine(pOut, ' ', &A[a+j]);
}
b += m;
a += m;
}
}
/* Show the final common area */
assert( nr==i );
m = R[r+nr*3];
if( m>nContext ) m = nContext;
for(j=0; j<m; j++){
if( showLn ) appendDiffLineno(pOut, a+j+1, b+j+1);
appendDiffLine(pOut, ' ', &A[a+j]);
}
}
}
#define MX_CSN 8 /* Maximum number of change spans across a change region */
/*
** A description of zero or more (up to MX_CSN) areas of difference
** between two lines of text.
*/
typedef struct LineChange LineChange;
struct LineChange {
int n; /* Number of change spans */
struct Span {
int iStart1; /* Byte offset to start of a change on the left */
int iLen1; /* Length of the left change in bytes */
int iStart2; /* Byte offset to start of a change on the right */
int iLen2; /* Length of the change on the right in bytes */
int isMin; /* True if this change is known to have no useful subdivs */
} a[MX_CSN]; /* Array of change spans, sorted order */
};
/*
** The two text segments zLeft and zRight are known to be different on
** both ends, but they might have a common segment in the middle. If
** they do not have a common segment, return 0. If they do have a large
** common segment, return 1 and before doing so set:
**
** aLCS[0] = start of the common segment in zLeft
** aLCS[1] = end of the common segment in zLeft
** aLCS[2] = start of the common segment in zLeft
** aLCS[3] = end of the common segment in zLeft
**
** This computation is for display purposes only and does not have to be
** optimal or exact.
*/
static int textLCS(
const char *zLeft, int nA, /* String on the left */
const char *zRight, int nB, /* String on the right */
int *aLCS /* Identify bounds of LCS here */
){
const unsigned char *zA = (const unsigned char*)zLeft; /* left string */
const unsigned char *zB = (const unsigned char*)zRight; /* right string */
int i, j, k; /* Loop counters */
int lenBest = 0; /* Match length to beat */
for(i=0; i<nA-lenBest; i++){
unsigned char cA = zA[i];
if( (cA&0xc0)==0x80 ) continue;
for(j=0; j<nB-lenBest; j++ ){
if( zB[j]==cA ){
for(k=1; j+k<nB && i+k<nA && zB[j+k]==zA[i+k]; k++){}
while( (zB[j+k]&0xc0)==0x80 ){ k--; }
if( k>lenBest ){
lenBest = k;
aLCS[0] = i;
aLCS[1] = i+k;
aLCS[2] = j;
aLCS[3] = j+k;
}
}
}
}
return lenBest>0;
}
/*
** Find the smallest spans that are different between two text strings that
** are known to be different on both ends.
*/
static int textLineChanges(
const char *zLeft, int nA, /* String on the left */
const char *zRight, int nB, /* String on the right */
LineChange *p /* Write results here */
){
p->n = 1;
p->a[0].iStart1 = 0;
p->a[0].iLen1 = nA;
p->a[0].iStart2 = 0;
p->a[0].iLen2 = nB;
p->a[0].isMin = 0;
while( p->n<MX_CSN-1 ){
int mxi = -1;
int mxLen = -1;
int x, i;
int aLCS[4];
struct Span *a, *b;
memset(aLCS, 0, sizeof(aLCS));
for(i=0; i<p->n; i++){
if( p->a[i].isMin ) continue;
x = p->a[i].iLen1;
if( p->a[i].iLen2<x ) x = p->a[i].iLen2;
if( x>mxLen ){
mxLen = x;
mxi = i;
}
}
if( mxLen<6 ) break;
x = textLCS(zLeft + p->a[mxi].iStart1, p->a[mxi].iLen1,
zRight + p->a[mxi].iStart2, p->a[mxi].iLen2, aLCS);
if( x==0 ){
p->a[mxi].isMin = 1;
continue;
}
a = p->a+mxi;
b = a+1;
if( mxi<p->n-1 ){
memmove(b+1, b, sizeof(*b)*(p->n-mxi-1));
}
p->n++;
b->iStart1 = a->iStart1 + aLCS[1];
b->iLen1 = a->iLen1 - aLCS[1];
a->iLen1 = aLCS[0];
b->iStart2 = a->iStart2 + aLCS[3];
b->iLen2 = a->iLen2 - aLCS[3];
a->iLen2 = aLCS[2];
b->isMin = 0;
}
return p->n;
}
/*
** Return true if the string starts with n spaces
*/
static int allSpaces(const char *z, int n){
int i;
for(i=0; i<n && diff_isspace(z[i]); i++){}
return i==n;
}
/*
** Try to improve the human-readability of the LineChange p.
**
** (1) If the first change span shows a change of indentation, try to
** move that indentation change to the left margin.
**
** (2) Try to shift changes so that they begin or end with a space.
*/
static void improveReadability(
const char *zA, /* Left line of the change */
const char *zB, /* Right line of the change */
LineChange *p /* The LineChange to be adjusted */
){
int j, n, len;
if( p->n<1 ) return;
/* (1) Attempt to move indentation changes to the left margin */
if( p->a[0].iLen1==0
&& (len = p->a[0].iLen2)>0
&& (j = p->a[0].iStart2)>0
&& zB[0]==zB[j]
&& allSpaces(zB, j)
){
for(n=1; n<len && n<j && zB[j]==zB[j+n]; n++){}
if( n<len ){
memmove(&p->a[1], &p->a[0], sizeof(p->a[0])*p->n);
p->n++;
p->a[0] = p->a[1];
p->a[1].iStart2 += n;
p->a[1].iLen2 -= n;
p->a[0].iLen2 = n;
}
p->a[0].iStart1 = 0;
p->a[0].iStart2 = 0;
}else
if( p->a[0].iLen2==0
&& (len = p->a[0].iLen1)>0
&& (j = p->a[0].iStart1)>0
&& zA[0]==zA[j]
&& allSpaces(zA, j)
){
for(n=1; n<len && n<j && zA[j]==zA[j+n]; n++){}
if( n<len ){
memmove(&p->a[1], &p->a[0], sizeof(p->a[0])*p->n);
p->n++;
p->a[0] = p->a[1];
p->a[1].iStart1 += n;
p->a[1].iLen1 -= n;
p->a[0].iLen1 = n;
}
p->a[0].iStart1 = 0;
p->a[0].iStart2 = 0;
}
/* (2) Try to shift changes so that they begin or end with a
** space. (TBD) */
}
/*
** Given two lines of text, pFrom and pTo, compute a set of changes
** between those two lines, for enhanced display purposes.
**
** The result is written into the LineChange object given by the
** third parameter.
*/
static void oneLineChange(
const DLine *pLeft, /* Left line of the change */
const DLine *pRight, /* Right line of the change */
LineChange *p /* OUTPUT: Write the results here */
){
int nLeft; /* Length of left line in bytes */
int nRight; /* Length of right line in bytes */
int nShort; /* Shortest of left and right */
int nPrefix; /* Length of common prefix */
int nSuffix; /* Length of common suffix */
int nCommon; /* Total byte length of suffix and prefix */
const char *zLeft; /* Text of the left line */
const char *zRight; /* Text of the right line */
int nLeftDiff; /* nLeft - nPrefix - nSuffix */
int nRightDiff; /* nRight - nPrefix - nSuffix */
nLeft = pLeft->n;
zLeft = pLeft->z;
nRight = pRight->n;
zRight = pRight->z;
nShort = nLeft<nRight ? nLeft : nRight;
nPrefix = 0;
while( nPrefix<nShort && zLeft[nPrefix]==zRight[nPrefix] ){
nPrefix++;
}
if( nPrefix<nShort ){
while( nPrefix>0 && (zLeft[nPrefix]&0xc0)==0x80 ) nPrefix--;
}
nSuffix = 0;
if( nPrefix<nShort ){
while( nSuffix<nShort
&& zLeft[nLeft-nSuffix-1]==zRight[nRight-nSuffix-1] ){
nSuffix++;
}
if( nSuffix<nShort ){
while( nSuffix>0 && (zLeft[nLeft-nSuffix]&0xc0)==0x80 ) nSuffix--;
}
if( nSuffix==nLeft || nSuffix==nRight ) nPrefix = 0;
}
nCommon = nPrefix + nSuffix;
/* If the prefix and suffix overlap, that means that we are dealing with
** a pure insertion or deletion of text that can have multiple alignments.
** Try to find an alignment to begins and ends on whitespace, or on
** punctuation, rather than in the middle of a name or number.
*/
if( nCommon > nShort ){
int iBest = -1;
int iBestVal = -1;
int i;
int nLong = nLeft<nRight ? nRight : nLeft;
int nGap = nLong - nShort;
for(i=nShort-nSuffix; i<=nPrefix; i++){
int iVal = 0;
char c = zLeft[i];
if( diff_isspace(c) ){
iVal += 5;
}else if( !fossil_isalnum(c) ){
iVal += 2;
}
c = zLeft[i+nGap-1];
if( diff_isspace(c) ){
iVal += 5;
}else if( !fossil_isalnum(c) ){
iVal += 2;
}
if( iVal>iBestVal ){
iBestVal = iVal;
iBest = i;
}
}
nPrefix = iBest;
nSuffix = nShort - nPrefix;
nCommon = nPrefix + nSuffix;
}
/* A single chunk of text inserted */
if( nCommon==nLeft ){
p->n = 1;
p->a[0].iStart1 = nPrefix;
p->a[0].iLen1 = 0;
p->a[0].iStart2 = nPrefix;
p->a[0].iLen2 = nRight - nCommon;
improveReadability(zLeft, zRight, p);
return;
}
/* A single chunk of text deleted */
if( nCommon==nRight ){
p->n = 1;
p->a[0].iStart1 = nPrefix;
p->a[0].iLen1 = nLeft - nCommon;
p->a[0].iStart2 = nPrefix;
p->a[0].iLen2 = 0;
improveReadability(zLeft, zRight, p);
return;
}
/* At this point we know that there is a chunk of text that has
** changed between the left and the right. Check to see if there
** is a large unchanged section in the middle of that changed block.
*/
nLeftDiff = nLeft - nCommon;
nRightDiff = nRight - nCommon;
if( nLeftDiff >= 4
&& nRightDiff >= 4
&& textLineChanges(&zLeft[nPrefix], nLeftDiff,
&zRight[nPrefix], nRightDiff, p)>1
){
int i;
for(i=0; i<p->n; i++){
p->a[i].iStart1 += nPrefix;
p->a[i].iStart2 += nPrefix;
}
improveReadability(zLeft, zRight, p);
return;
}
/* If all else fails, show a single big change between left and right */
p->n = 1;
p->a[0].iStart1 = nPrefix;
p->a[0].iLen1 = nLeft - nCommon;
p->a[0].iStart2 = nPrefix;
p->a[0].iLen2 = nRight - nCommon;
improveReadability(zLeft, zRight, p);
}
/*
** COMMAND: test-line-diff
** Usage: %fossil% test-line-diff STRING1 STRING2
**
** Show the differences between the two strings. Used for testing
** the oneLineChange() routine in the diff logic.
*/
void test_line_diff(void){
DLine a, b;
LineChange chng;
int i, j, x;
if( g.argc!=4 ) usage("STRING1 STRING2");
a.z = g.argv[2];
a.n = (int)strlen(a.z);
b.z = g.argv[3];
b.n = (int)strlen(b.z);
oneLineChange(&a, &b, &chng);
fossil_print("left: [%s]\n", a.z);
for(i=x=0; i<chng.n; i++){
int ofst = chng.a[i].iStart1;
int len = chng.a[i].iLen1;
if( len ){
if( x==0 ){ fossil_print("%*s", 8, ""); }
while( ofst > x ){
if( (a.z[x]&0xc0)!=0x80 ) fossil_print(" ");
x++;
}
for(j=0; j<len; j++, x++){
if( (a.z[x]&0xc0)!=0x80 ) fossil_print("%d",i);
}
}
}
if( x ) fossil_print("\n");
fossil_print("right: [%s]\n", b.z);
for(i=x=0; i<chng.n; i++){
int ofst = chng.a[i].iStart2;
int len = chng.a[i].iLen2;
if( len ){
if( x==0 ){ fossil_print("%*s", 8, ""); }
while( ofst > x ){
if( (b.z[x]&0xc0)!=0x80 ) fossil_print(" ");
x++;
}
for(j=0; j<len; j++, x++){
if( (b.z[x]&0xc0)!=0x80 ) fossil_print("%d",i);
}
}
}
if( x ) fossil_print("\n");
}
/*
** Minimum of two values
*/
static int minInt(int a, int b){ return a<b ? a : b; }
/*
** This is an abstract superclass for an object that accepts difference
** lines and formats them for display. Subclasses of this object format
** the diff output in different ways.
**
** To subclass, create an instance of the DiffBuilder object and fill
** in appropriate method implementations.
*/
typedef struct DiffBuilder DiffBuilder;
struct DiffBuilder {
void (*xSkip)(DiffBuilder*, unsigned int, int);
void (*xCommon)(DiffBuilder*,const DLine*);
void (*xInsert)(DiffBuilder*,const DLine*);
void (*xDelete)(DiffBuilder*,const DLine*);
void (*xReplace)(DiffBuilder*,const DLine*,const DLine*);
void (*xEdit)(DiffBuilder*,const DLine*,const DLine*);
void (*xEnd)(DiffBuilder*);
unsigned int lnLeft; /* Lines seen on the left (delete) side */
unsigned int lnRight; /* Lines seen on the right (insert) side */
unsigned int nPending; /* Number of pending lines */
int eState; /* State of the output */
int width; /* Display width */
Blob *pOut; /* Output blob */
Blob aCol[5]; /* Holding blobs */
DiffConfig *pCfg; /* Configuration information */
};
/************************* DiffBuilderDebug ********************************/
/* This version of DiffBuilder is used for debugging the diff and diff
** diff formatter logic. It is accessed using the (undocumented) --debug
** option to the diff command. The output is human-readable text that
** describes the various method calls that are invoked against the DiffBuilder
** object.
*/
static void dfdebugSkip(DiffBuilder *p, unsigned int n, int isFinal){
blob_appendf(p->pOut, "SKIP %d (%d..%d left and %d..%d right)%s\n",
n, p->lnLeft+1, p->lnLeft+n, p->lnRight+1, p->lnRight+n,
isFinal ? " FINAL" : "");
p->lnLeft += n;
p->lnRight += n;
}
static void dfdebugCommon(DiffBuilder *p, const DLine *pLine){
p->lnLeft++;
p->lnRight++;
blob_appendf(p->pOut, "COMMON %8u %8u %.*s\n",
p->lnLeft, p->lnRight, (int)pLine->n, pLine->z);
}
static void dfdebugInsert(DiffBuilder *p, const DLine *pLine){
p->lnRight++;
blob_appendf(p->pOut, "INSERT %8d %.*s\n",
p->lnRight, (int)pLine->n, pLine->z);
}
static void dfdebugDelete(DiffBuilder *p, const DLine *pLine){
p->lnLeft++;
blob_appendf(p->pOut, "DELETE %8u %.*s\n",
p->lnLeft, (int)pLine->n, pLine->z);
}
static void dfdebugReplace(DiffBuilder *p, const DLine *pX, const DLine *pY){
p->lnLeft++;
p->lnRight++;
blob_appendf(p->pOut, "REPLACE %8u %.*s\n",
p->lnLeft, (int)pX->n, pX->z);
blob_appendf(p->pOut, " %8u %.*s\n",
p->lnRight, (int)pY->n, pY->z);
}
static void dfdebugEdit(DiffBuilder *p, const DLine *pX, const DLine *pY){
int i, j;
int x;
LineChange chng;
p->lnLeft++;
p->lnRight++;
blob_appendf(p->pOut, "EDIT %8u %.*s\n",
p->lnLeft, (int)pX->n, pX->z);
oneLineChange(pX, pY, &chng);
for(i=x=0; i<chng.n; i++){
int ofst = chng.a[i].iStart1;
int len = chng.a[i].iLen1;
if( len ){
char c = '0' + i;
if( x==0 ){ blob_appendf(p->pOut, "%*s", 26, ""); }
while( ofst > x ){
if( (pX->z[x]&0xc0)!=0x80 ) blob_append_char(p->pOut, ' ');
x++;
}
for(j=0; j<len; j++, x++){
if( (pX->z[x]&0xc0)!=0x80 ) blob_append_char(p->pOut, c);
}
}
}
if( x ) blob_append_char(p->pOut, '\n');
blob_appendf(p->pOut, " %8u %.*s\n",
p->lnRight, (int)pY->n, pY->z);
for(i=x=0; i<chng.n; i++){
int ofst = chng.a[i].iStart2;
int len = chng.a[i].iLen2;
if( len ){
char c = '0' + i;
if( x==0 ){ blob_appendf(p->pOut, "%*s", 26, ""); }
while( ofst > x ){
if( (pY->z[x]&0xc0)!=0x80 ) blob_append_char(p->pOut, ' ');
x++;
}
for(j=0; j<len; j++, x++){
if( (pY->z[x]&0xc0)!=0x80 ) blob_append_char(p->pOut, c);
}
}
}
if( x ) blob_append_char(p->pOut, '\n');
}
static void dfdebugEnd(DiffBuilder *p){
blob_appendf(p->pOut, "END with %u lines left and %u lines right\n",
p->lnLeft, p->lnRight);
fossil_free(p);
}
static DiffBuilder *dfdebugNew(Blob *pOut){
DiffBuilder *p = fossil_malloc(sizeof(*p));
p->xSkip = dfdebugSkip;
p->xCommon = dfdebugCommon;
p->xInsert = dfdebugInsert;
p->xDelete = dfdebugDelete;
p->xReplace = dfdebugReplace;
p->xEdit = dfdebugEdit;
p->xEnd = dfdebugEnd;
p->lnLeft = p->lnRight = 0;
p->pOut = pOut;
return p;
}
/************************* DiffBuilderTcl ********************************/
/*
** This formatter outputs a description of the diff formatted as TCL, for
** use by the --tk option to "diff". See also the "diff.tcl" file. The
** output can be viewed directly using the --tcl option.
**
** There is one line per method call:
**
** SKIP n -- Skip "n" lines of input
** COM string -- "string" is an unchanged context line
** INS string -- "string" is in the right file only
** DEL string -- "string" is in the left file only
** EDIT string .... -- Complex edit between left and right
**
** The EDIT verb will be followed by 3*N or 3*N+1 strings. The triples
** each show:
**
** 1. Common text
** 2. Text from the left side
** 3. Text on the right that replaces (2) from the left
**
** For inserted text (2) will be an empty string. For deleted text, (3)
** will be an empty string. (1) might be empty for the first triple if
** the line begins with an edit. After all triples, there might be one
** additional string which is a common suffix.
*/
static void dftclSkip(DiffBuilder *p, unsigned int n, int isFinal){
blob_appendf(p->pOut, "SKIP %u\n", n);
}
static void dftclCommon(DiffBuilder *p, const DLine *pLine){
blob_appendf(p->pOut, "COM ");
blob_append_tcl_literal(p->pOut, pLine->z, pLine->n);
blob_append_char(p->pOut, '\n');
}
static void dftclInsert(DiffBuilder *p, const DLine *pLine){
blob_append(p->pOut, "INS ", -1);
blob_append_tcl_literal(p->pOut, pLine->z, pLine->n);
blob_append_char(p->pOut, '\n');
}
static void dftclDelete(DiffBuilder *p, const DLine *pLine){
blob_append(p->pOut, "DEL ", -1);
blob_append_tcl_literal(p->pOut, pLine->z, pLine->n);
blob_append_char(p->pOut, '\n');
}
static void dftclReplace(DiffBuilder *p, const DLine *pX, const DLine *pY){
blob_append(p->pOut, "EDIT \"\" ", -1);
blob_append_tcl_literal(p->pOut, pX->z, pX->n);
blob_append_char(p->pOut, ' ');
blob_append_tcl_literal(p->pOut, pY->z, pY->n);
blob_append_char(p->pOut, '\n');
}
static void dftclEdit(DiffBuilder *p, const DLine *pX, const DLine *pY){
int i, x;
LineChange chng;
blob_append(p->pOut, "EDIT", 4);
oneLineChange(pX, pY, &chng);
for(i=x=0; i<chng.n; i++){
blob_append_char(p->pOut, ' ');
blob_append_tcl_literal(p->pOut, pX->z + x, chng.a[i].iStart1 - x);
x = chng.a[i].iStart1;
blob_append_char(p->pOut, ' ');
blob_append_tcl_literal(p->pOut, pX->z + x, chng.a[i].iLen1);
x += chng.a[i].iLen1;
blob_append_char(p->pOut, ' ');
blob_append_tcl_literal(p->pOut,
pY->z + chng.a[i].iStart2, chng.a[i].iLen2);
}
if( x<pX->n ){
blob_append_char(p->pOut, ' ');
blob_append_tcl_literal(p->pOut, pX->z + x, pX->n - x);
}
blob_append_char(p->pOut, '\n');
}
static void dftclEnd(DiffBuilder *p){
fossil_free(p);
}
static DiffBuilder *dftclNew(Blob *pOut){
DiffBuilder *p = fossil_malloc(sizeof(*p));
p->xSkip = dftclSkip;
p->xCommon = dftclCommon;
p->xInsert = dftclInsert;
p->xDelete = dftclDelete;
p->xReplace = dftclReplace;
p->xEdit = dftclEdit;
p->xEnd = dftclEnd;
p->pOut = pOut;
return p;
}
/************************* DiffBuilderJson ********************************/
/*
** This formatter generates a JSON array that describes the difference.
**
** The Json array consists of integer opcodes with each opcode followed
** by zero or more arguments:
**
** Syntax Mnemonic Description
** ----------- -------- --------------------------
** 0 END This is the end of the diff
** 1 INTEGER SKIP Skip N lines from both files
** 2 STRING COMMON The line shown by STRING is in both files
** 3 STRING INSERT The line STRING is in only the right file
** 4 STRING DELETE The STRING line is in only the left file
** 5 SUBARRAY EDIT One line is different on left and right.
**
** The SUBARRAY is an array of 3*N+1 strings with N>=0. The triples
** represent common-text, left-text, and right-text. The last string
** in SUBARRAY is the common-suffix. Any string can be empty if it does
** not apply.
*/
static void dfjsonSkip(DiffBuilder *p, unsigned int n, int isFinal){
blob_appendf(p->pOut, "1,%u,\n", n);
}
static void dfjsonCommon(DiffBuilder *p, const DLine *pLine){
blob_append(p->pOut, "2,",2);
blob_append_json_literal(p->pOut, pLine->z, (int)pLine->n);
blob_append(p->pOut, ",\n",2);
}
static void dfjsonInsert(DiffBuilder *p, const DLine *pLine){
blob_append(p->pOut, "3,",2);
blob_append_json_literal(p->pOut, pLine->z, (int)pLine->n);
blob_append(p->pOut, ",\n",2);
}
static void dfjsonDelete(DiffBuilder *p, const DLine *pLine){
blob_append(p->pOut, "4,",2);
blob_append_json_literal(p->pOut, pLine->z, (int)pLine->n);
blob_append(p->pOut, ",\n",2);
}
static void dfjsonReplace(DiffBuilder *p, const DLine *pX, const DLine *pY){
blob_append(p->pOut, "5,[\"\",",-1);
blob_append_json_literal(p->pOut, pX->z, (int)pX->n);
blob_append(p->pOut, ",",1);
blob_append_json_literal(p->pOut, pY->z, (int)pY->n);
blob_append(p->pOut, ",\"\"],\n",-1);
}
static void dfjsonEdit(DiffBuilder *p, const DLine *pX, const DLine *pY){
int i, x;
LineChange chng;
blob_append(p->pOut, "5,[", 3);
oneLineChange(pX, pY, &chng);
for(i=x=0; i<chng.n; i++){
if(i>0){
blob_append_char(p->pOut, ',');
}
blob_append_json_literal(p->pOut, pX->z + x, chng.a[i].iStart1 - x);
x = chng.a[i].iStart1;
blob_append_char(p->pOut, ',');
blob_append_json_literal(p->pOut, pX->z + x, chng.a[i].iLen1);
x += chng.a[i].iLen1;
blob_append_char(p->pOut, ',');
blob_append_json_literal(p->pOut,
pY->z + chng.a[i].iStart2, chng.a[i].iLen2);
}
blob_append_char(p->pOut, ',');
blob_append_json_literal(p->pOut, pX->z + x, pX->n - x);
blob_append(p->pOut, "],\n",3);
}
static void dfjsonEnd(DiffBuilder *p){
blob_append(p->pOut, "0]}", 3);
fossil_free(p);
}
static DiffBuilder *dfjsonNew(Blob *pOut){
DiffBuilder *p = fossil_malloc(sizeof(*p));
p->xSkip = dfjsonSkip;
p->xCommon = dfjsonCommon;
p->xInsert = dfjsonInsert;
p->xDelete = dfjsonDelete;
p->xReplace = dfjsonReplace;
p->xEdit = dfjsonEdit;
p->xEnd = dfjsonEnd;
p->lnLeft = p->lnRight = 0;
p->pOut = pOut;
blob_append_char(pOut, '[');
return p;
}
/************************* DiffBuilderUnified********************************/
/* This formatter generates a unified diff for HTML.
**
** The result is a <table> with four columns. The four columns hold:
**
** 1. The line numbers for the first file.
** 2. The line numbers for the second file.
** 3. The "diff mark": "+" or "-" or just a space
** 4. Text of the line
**
** Inserted lines are marked with <ins> and deleted lines are marked
** with <del>. The whole line is marked this way, not just the part that
** changed. The part that change has an additional nested <ins> or <del>.
** The CSS needs to be set up such that a single <ins> or <del> gives a
** light background and a nested <ins> or <del> gives a darker background.
** Additional attributes (like bold font) might also be added to nested
** <ins> and <del> since those are the characters that have actually
** changed.
**
** Accumulator strategy:
**
** * Delete line numbers are output directly to p->pOut
** * Insert line numbers accumulate in p->aCol[0].
** * Separator marks accumulate in p->aCol[1].
** * Change text accumulates in p->aCol[2].
** * Pending insert line numbers go into p->aCol[3].
** * Pending insert text goes into p->aCol[4].
**
** eState is 1 if text has an open <del>
*/
static void dfunifiedFinishDelete(DiffBuilder *p){
if( p->eState==0 ) return;
blob_append(p->pOut, "</del>", 6);
blob_append(&p->aCol[2], "</del>", 6);
p->eState = 0;
}
static void dfunifiedFinishInsert(DiffBuilder *p){
unsigned int i;
if( p->nPending==0 ) return;
dfunifiedFinishDelete(p);
/* Blank lines for delete line numbers for each inserted line */
for(i=0; i<p->nPending; i++) blob_append_char(p->pOut, '\n');
/* Insert line numbers */
blob_append(&p->aCol[0], "<ins>", 5);
blob_append_xfer(&p->aCol[0], &p->aCol[3]);
blob_append(&p->aCol[0], "</ins>", 6);
/* "+" marks for the separator on inserted lines */
for(i=0; i<p->nPending; i++) blob_append(&p->aCol[1], "+\n", 2);
/* Text of the inserted lines */
blob_append(&p->aCol[2], "<ins>", 5);
blob_append_xfer(&p->aCol[2], &p->aCol[4]);
blob_append(&p->aCol[2], "</ins>", 6);
p->nPending = 0;
}
static void dfunifiedFinishRow(DiffBuilder *p){
dfunifiedFinishDelete(p);
dfunifiedFinishInsert(p);
if( blob_size(&p->aCol[0])==0 ) return;
blob_append(p->pOut, "</pre></td><td class=\"diffln difflnr\"><pre>\n", -1);
blob_append_xfer(p->pOut, &p->aCol[0]);
blob_append(p->pOut, "</pre></td><td class=\"diffsep\"><pre>\n", -1);
blob_append_xfer(p->pOut, &p->aCol[1]);
blob_append(p->pOut, "</pre></td><td class=\"difftxt difftxtu\"><pre>\n",-1);
blob_append_xfer(p->pOut, &p->aCol[2]);
blob_append(p->pOut, "</pre></td></tr>\n", -1);
}
static void dfunifiedStartRow(DiffBuilder *p){
if( blob_size(&p->aCol[0])>0 ) return;
blob_appendf(p->pOut,"<tr id=\"chunk%d\" class=\"diffchunk\">"
"<td class=\"diffln difflnl\"><pre>\n", ++nChunk);
}
static void dfunifiedSkip(DiffBuilder *p, unsigned int n, int isFinal){
dfunifiedFinishRow(p);
if( p->pCfg && p->pCfg->zLeftHash ){
blob_appendf(p->pOut,
"<tr class=\"diffskip\" data-startln=\"%d\" data-endln=\"%d\""
" id=\"skip%xh%xi%x\">\n",
p->lnLeft+1, p->lnLeft+n,
nChunk, p->lnLeft, n);
}else{
blob_append(p->pOut, "<tr>", 4);
}
blob_append(p->pOut, "<td class=\"diffln difflne\">"
"︙</td><td></td><td></td></tr>\n", -1);
p->lnLeft += n;
p->lnRight += n;
}
static void dfunifiedCommon(DiffBuilder *p, const DLine *pLine){
dfunifiedStartRow(p);
dfunifiedFinishDelete(p);
dfunifiedFinishInsert(p);
p->lnLeft++;
p->lnRight++;
blob_appendf(p->pOut,"%d\n", p->lnLeft);
blob_appendf(&p->aCol[0],"%d\n",p->lnRight);
blob_append_char(&p->aCol[1], '\n');
htmlize_to_blob(&p->aCol[2], pLine->z, (int)pLine->n);
blob_append_char(&p->aCol[2], '\n');
}
static void dfunifiedInsert(DiffBuilder *p, const DLine *pLine){
dfunifiedStartRow(p);
p->lnRight++;
blob_appendf(&p->aCol[3],"%d\n", p->lnRight);
blob_append(&p->aCol[4], "<ins>", 5);
htmlize_to_blob(&p->aCol[4], pLine->z, (int)pLine->n);
blob_append(&p->aCol[4], "</ins>\n", 7);
p->nPending++;
}
static void dfunifiedDelete(DiffBuilder *p, const DLine *pLine){
dfunifiedStartRow(p);
dfunifiedFinishInsert(p);
if( p->eState==0 ){
dfunifiedFinishInsert(p);
blob_append(p->pOut, "<del>", 5);
blob_append(&p->aCol[2], "<del>", 5);
p->eState = 1;
}
p->lnLeft++;
blob_appendf(p->pOut,"%d\n", p->lnLeft);
blob_append_char(&p->aCol[0],'\n');
blob_append(&p->aCol[1],"-\n",2);
blob_append(&p->aCol[2], "<del>", 5);
htmlize_to_blob(&p->aCol[2], pLine->z, (int)pLine->n);
blob_append(&p->aCol[2], "</del>\n", 7);
}
static void dfunifiedReplace(DiffBuilder *p, const DLine *pX, const DLine *pY){
dfunifiedStartRow(p);
if( p->eState==0 ){
dfunifiedFinishInsert(p);
blob_append(p->pOut, "<del>", 5);
blob_append(&p->aCol[2], "<del>", 5);
p->eState = 1;
}
p->lnLeft++;
p->lnRight++;
blob_appendf(p->pOut,"%d\n", p->lnLeft);
blob_append_char(&p->aCol[0], '\n');
blob_append(&p->aCol[1], "-\n", 2);
htmlize_to_blob(&p->aCol[2], pX->z, pX->n);
blob_append_char(&p->aCol[2], '\n');
blob_appendf(&p->aCol[3],"%d\n", p->lnRight);
htmlize_to_blob(&p->aCol[4], pY->z, pY->n);
blob_append_char(&p->aCol[4], '\n');
p->nPending++;
}
static void dfunifiedEdit(DiffBuilder *p, const DLine *pX, const DLine *pY){
int i;
int x;
LineChange chng;
oneLineChange(pX, pY, &chng);
dfunifiedStartRow(p);
if( p->eState==0 ){
dfunifiedFinishInsert(p);
blob_append(p->pOut, "<del>", 5);
blob_append(&p->aCol[2], "<del>", 5);
p->eState = 1;
}
p->lnLeft++;
p->lnRight++;
blob_appendf(p->pOut,"%d\n", p->lnLeft);
blob_append_char(&p->aCol[0], '\n');
blob_append(&p->aCol[1], "-\n", 2);
for(i=x=0; i<chng.n; i++){
int ofst = chng.a[i].iStart1;
int len = chng.a[i].iLen1;
if( len ){
htmlize_to_blob(&p->aCol[2], pX->z+x, ofst - x);
x = ofst;
blob_append(&p->aCol[2], "<del>", 5);
htmlize_to_blob(&p->aCol[2], pX->z+x, len);
x += len;
blob_append(&p->aCol[2], "</del>", 6);
}
}
htmlize_to_blob(&p->aCol[2], pX->z+x, pX->n - x);
blob_append_char(&p->aCol[2], '\n');
blob_appendf(&p->aCol[3],"%d\n", p->lnRight);
for(i=x=0; i<chng.n; i++){
int ofst = chng.a[i].iStart2;
int len = chng.a[i].iLen2;
if( len ){
htmlize_to_blob(&p->aCol[4], pY->z+x, ofst - x);
x = ofst;
blob_append(&p->aCol[4], "<ins>", 5);
htmlize_to_blob(&p->aCol[4], pY->z+x, len);
x += len;
blob_append(&p->aCol[4], "</ins>", 6);
}
}
htmlize_to_blob(&p->aCol[4], pY->z+x, pY->n - x);
blob_append_char(&p->aCol[4], '\n');
p->nPending++;
}
static void dfunifiedEnd(DiffBuilder *p){
dfunifiedFinishRow(p);
blob_append(p->pOut, "</table>\n",-1);
fossil_free(p);
}
static DiffBuilder *dfunifiedNew(Blob *pOut, DiffConfig *pCfg){
DiffBuilder *p = fossil_malloc(sizeof(*p));
p->xSkip = dfunifiedSkip;
p->xCommon = dfunifiedCommon;
p->xInsert = dfunifiedInsert;
p->xDelete = dfunifiedDelete;
p->xReplace = dfunifiedReplace;
p->xEdit = dfunifiedEdit;
p->xEnd = dfunifiedEnd;
p->lnLeft = p->lnRight = 0;
p->eState = 0;
p->nPending = 0;
p->pOut = pOut;
if( pCfg->zLeftHash ){
blob_appendf(pOut, "<table class=\"diff udiff\" data-lefthash=\"%s\">\n",
pCfg->zLeftHash);
}else{
blob_append(pOut, "<table class=\"diff udiff\">\n", -1);
}
blob_init(&p->aCol[0], 0, 0);
blob_init(&p->aCol[1], 0, 0);
blob_init(&p->aCol[2], 0, 0);
blob_init(&p->aCol[3], 0, 0);
blob_init(&p->aCol[4], 0, 0);
p->pCfg = pCfg;
return p;
}
/************************* DiffBuilderSplit ******************************/
/* This formatter creates a side-by-side diff in HTML. The output is a
** <table> with 5 columns:
**
** 1. Line numbers for the first file.
** 2. Text for the first file.
** 3. The difference mark. "<", ">", "|" or blank
** 4. Line numbers for the second file.
** 5. Text for the second file.
**
** The <ins> and <del> strategy is the same as for unified diff above.
** In fact, the same CSS can be used for both.
**
** Accumulator strategy:
**
** * Left line numbers are output directly to p->pOut
** * Left text accumulates in p->aCol[0].
** * Edit marks accumulates in p->aCol[1].
** * Right line numbers accumulate in p->aCol[2].
** * Right text accumulates in p->aCol[3].
**
** eState:
** 0 In common block
** 1 Have <del> on the left
** 2 Have <ins> on the right
** 3 Have <del> on left and <ins> on the right
*/
static void dfsplitChangeState(DiffBuilder *p, int newState){
if( p->eState == newState ) return;
if( (p->eState&1)==0 && (newState & 1)!=0 ){
blob_append(p->pOut, "<del>", 5);
blob_append(&p->aCol[0], "<del>", 5);
p->eState |= 1;
}else if( (p->eState&1)!=0 && (newState & 1)==0 ){
blob_append(p->pOut, "</del>", 6);
blob_append(&p->aCol[0], "</del>", 6);
p->eState &= ~1;
}
if( (p->eState&2)==0 && (newState & 2)!=0 ){
blob_append(&p->aCol[2], "<ins>", 5);
blob_append(&p->aCol[3], "<ins>", 5);
p->eState |= 2;
}else if( (p->eState&2)!=0 && (newState & 2)==0 ){
blob_append(&p->aCol[2], "</ins>", 6);
blob_append(&p->aCol[3], "</ins>", 6);
p->eState &= ~2;
}
}
static void dfsplitFinishRow(DiffBuilder *p){
if( blob_size(&p->aCol[0])==0 ) return;
dfsplitChangeState(p, 0);
blob_append(p->pOut, "</pre></td><td class=\"difftxt difftxtl\"><pre>\n",-1);
blob_append_xfer(p->pOut, &p->aCol[0]);
blob_append(p->pOut, "</pre></td><td class=\"diffsep\"><pre>\n", -1);
blob_append_xfer(p->pOut, &p->aCol[1]);
blob_append(p->pOut, "</pre></td><td class=\"diffln difflnr\"><pre>\n",-1);
blob_append_xfer(p->pOut, &p->aCol[2]);
blob_append(p->pOut, "</pre></td><td class=\"difftxt difftxtr\"><pre>\n",-1);
blob_append_xfer(p->pOut, &p->aCol[3]);
blob_append(p->pOut, "</pre></td></tr>\n", -1);
}
static void dfsplitStartRow(DiffBuilder *p){
if( blob_size(&p->aCol[0])>0 ) return;
blob_appendf(p->pOut,"<tr id=\"chunk%d\" class=\"diffchunk\">"
"<td class=\"diffln difflnl\"><pre>\n", ++nChunk);
p->eState = 0;
}
static void dfsplitSkip(DiffBuilder *p, unsigned int n, int isFinal){
dfsplitFinishRow(p);
if( p->pCfg && p->pCfg->zLeftHash ){
blob_appendf(p->pOut,
"<tr class=\"diffskip\" data-startln=\"%d\" data-endln=\"%d\""
" id=\"skip%xh%xi%x\">\n",
p->lnLeft+1, p->lnLeft+n,
nChunk,p->lnLeft,n);
}else{
blob_append(p->pOut, "<tr>", 4);
}
blob_append(p->pOut,
"<td class=\"diffln difflnl difflne\">︙</td>"
"<td></td><td></td>"
"<td class=\"diffln difflnr difflne\">︙</td>"
"<td/td></tr>\n", -1);
p->lnLeft += n;
p->lnRight += n;
}
static void dfsplitCommon(DiffBuilder *p, const DLine *pLine){
dfsplitStartRow(p);
dfsplitChangeState(p, 0);
p->lnLeft++;
p->lnRight++;
blob_appendf(p->pOut,"%d\n", p->lnLeft);
htmlize_to_blob(&p->aCol[0], pLine->z, (int)pLine->n);
blob_append_char(&p->aCol[0], '\n');
blob_append_char(&p->aCol[1], '\n');
blob_appendf(&p->aCol[2],"%d\n",p->lnRight);
htmlize_to_blob(&p->aCol[3], pLine->z, (int)pLine->n);
blob_append_char(&p->aCol[3], '\n');
}
static void dfsplitInsert(DiffBuilder *p, const DLine *pLine){
dfsplitStartRow(p);
dfsplitChangeState(p, 2);
p->lnRight++;
blob_append_char(p->pOut, '\n');
blob_append_char(&p->aCol[0], '\n');
blob_append(&p->aCol[1], ">\n", -1);
blob_appendf(&p->aCol[2],"%d\n", p->lnRight);
blob_append(&p->aCol[3], "<ins>", 5);
htmlize_to_blob(&p->aCol[3], pLine->z, (int)pLine->n);
blob_append(&p->aCol[3], "</ins>\n", 7);
}
static void dfsplitDelete(DiffBuilder *p, const DLine *pLine){
dfsplitStartRow(p);
dfsplitChangeState(p, 1);
p->lnLeft++;
blob_appendf(p->pOut,"%d\n", p->lnLeft);
blob_append(&p->aCol[0], "<del>", 5);
htmlize_to_blob(&p->aCol[0], pLine->z, (int)pLine->n);
blob_append(&p->aCol[0], "</del>\n", 7);
blob_append(&p->aCol[1], "<\n", -1);
blob_append_char(&p->aCol[2],'\n');
blob_append_char(&p->aCol[3],'\n');
}
static void dfsplitReplace(DiffBuilder *p, const DLine *pX, const DLine *pY){
dfsplitStartRow(p);
dfsplitChangeState(p, 3);
p->lnLeft++;
p->lnRight++;
blob_appendf(p->pOut,"%d\n", p->lnLeft);
htmlize_to_blob(&p->aCol[0], pX->z, pX->n);
blob_append_char(&p->aCol[0], '\n');
blob_append(&p->aCol[1], "|\n", 2);
blob_appendf(&p->aCol[2],"%d\n", p->lnRight);
htmlize_to_blob(&p->aCol[3], pY->z, pY->n);
blob_append_char(&p->aCol[3], '\n');
}
static void dfsplitEdit(DiffBuilder *p, const DLine *pX, const DLine *pY){
int i;
int x;
LineChange chng;
oneLineChange(pX, pY, &chng);
dfsplitStartRow(p);
dfsplitChangeState(p, 3);
p->lnLeft++;
p->lnRight++;
blob_appendf(p->pOut,"%d\n", p->lnLeft);
for(i=x=0; i<chng.n; i++){
int ofst = chng.a[i].iStart1;
int len = chng.a[i].iLen1;
if( len ){
htmlize_to_blob(&p->aCol[0], pX->z+x, ofst - x);
x = ofst;
if( chng.a[i].iLen2 ){
blob_append(&p->aCol[0], "<del class='edit'>", -1);
}else{
blob_append(&p->aCol[0], "<del>", 5);
}
htmlize_to_blob(&p->aCol[0], pX->z+x, len);
x += len;
blob_append(&p->aCol[0], "</del>", 6);
}
}
htmlize_to_blob(&p->aCol[0], pX->z+x, pX->n - x);
blob_append_char(&p->aCol[0], '\n');
blob_append(&p->aCol[1], "|\n", 2);
blob_appendf(&p->aCol[2],"%d\n", p->lnRight);
for(i=x=0; i<chng.n; i++){
int ofst = chng.a[i].iStart2;
int len = chng.a[i].iLen2;
if( len ){
htmlize_to_blob(&p->aCol[3], pY->z+x, ofst - x);
x = ofst;
if( chng.a[i].iLen1 ){
blob_append(&p->aCol[3], "<ins class='edit'>", -1);
}else{
blob_append(&p->aCol[3], "<ins>", 5);
}
htmlize_to_blob(&p->aCol[3], pY->z+x, len);
x += len;
blob_append(&p->aCol[3], "</ins>", 6);
}
}
htmlize_to_blob(&p->aCol[3], pY->z+x, pY->n - x);
blob_append_char(&p->aCol[3], '\n');
}
static void dfsplitEnd(DiffBuilder *p){
dfsplitFinishRow(p);
blob_append(p->pOut, "</table>\n",-1);
fossil_free(p);
}
static DiffBuilder *dfsplitNew(Blob *pOut, DiffConfig *pCfg){
DiffBuilder *p = fossil_malloc(sizeof(*p));
p->xSkip = dfsplitSkip;
p->xCommon = dfsplitCommon;
p->xInsert = dfsplitInsert;
p->xDelete = dfsplitDelete;
p->xReplace = dfsplitReplace;
p->xEdit = dfsplitEdit;
p->xEnd = dfsplitEnd;
p->lnLeft = p->lnRight = 0;
p->eState = 0;
p->pOut = pOut;
if( pCfg->zLeftHash ){
blob_appendf(pOut,
"<table class=\"diff splitdiff\" data-lefthash=\"%s\">\n",
pCfg->zLeftHash);
}else{
blob_append(pOut, "<table class=\"diff splitdiff\">\n", -1);
}
blob_init(&p->aCol[0], 0, 0);
blob_init(&p->aCol[1], 0, 0);
blob_init(&p->aCol[2], 0, 0);
blob_init(&p->aCol[3], 0, 0);
blob_init(&p->aCol[4], 0, 0);
p->pCfg = pCfg;
return p;
}
/************************* DiffBuilderSbs ******************************/
/* This formatter creates a side-by-side diff in text.
*/
static void dfsbsSkip(DiffBuilder *p, unsigned int n, int isFinal){
if( (p->lnLeft || p->lnRight) && !isFinal ){
blob_appendf(p->pOut, "%.*c\n", p->width*2 + 16, '.');
}
p->lnLeft += n;
p->lnRight += n;
}
/*
** Append at least iMin characters (not bytes) and at most iMax characters
** from pX onto the into of p.
**
** This comment contains multibyte unicode characters (ü, Æ, ð) in order
** to test the ability of the diff code to handle such characters.
*/
static void sbs_append_chars(Blob *p, int iMin, int iMax, const DLine *pX){
int i;
const char *z = pX->z;
for(i=0; i<iMax && i<pX->n; i++){
char c = z[i];
blob_append_char(p, c);
if( (c&0xc0)==0x80 ){ iMin++; iMax++; }
}
while( i<iMin ){
blob_append_char(p, ' ');
i++;
}
}
static void dfsbsCommon(DiffBuilder *p, const DLine *pLine){
p->lnLeft++;
p->lnRight++;
blob_appendf(p->pOut,"%6u ", p->lnLeft);
sbs_append_chars(p->pOut, p->width, p->width, pLine);
blob_appendf(p->pOut," %6u ", p->lnRight);
sbs_append_chars(p->pOut, 0, p->width, pLine);
blob_append_char(p->pOut, '\n');
}
static void dfsbsInsert(DiffBuilder *p, const DLine *pLine){
p->lnRight++;
blob_appendf(p->pOut,"%6s %*s > %6u ",
"", p->width, "", p->lnRight);
sbs_append_chars(p->pOut, 0, p->width, pLine);
blob_append_char(p->pOut, '\n');
}
static void dfsbsDelete(DiffBuilder *p, const DLine *pLine){
p->lnLeft++;
blob_appendf(p->pOut,"%6u ", p->lnLeft);
sbs_append_chars(p->pOut, p->width, p->width, pLine);
blob_append(p->pOut," <\n", 3);
}
static void dfsbsEdit(DiffBuilder *p, const DLine *pX, const DLine *pY){
p->lnLeft++;
p->lnRight++;
blob_appendf(p->pOut,"%6u ", p->lnLeft);
sbs_append_chars(p->pOut, p->width, p->width, pX);
blob_appendf(p->pOut, " | %6u ", p->lnRight);
sbs_append_chars(p->pOut, 0, p->width, pY);
blob_append_char(p->pOut, '\n');
}
static void dfsbsEnd(DiffBuilder *p){
fossil_free(p);
}
static DiffBuilder *dfsbsNew(Blob *pOut, DiffConfig *pCfg){
DiffBuilder *p = fossil_malloc(sizeof(*p));
p->xSkip = dfsbsSkip;
p->xCommon = dfsbsCommon;
p->xInsert = dfsbsInsert;
p->xDelete = dfsbsDelete;
p->xReplace = dfsbsEdit;
p->xEdit = dfsbsEdit;
p->xEnd = dfsbsEnd;
p->lnLeft = p->lnRight = 0;
p->width = diff_width(pCfg);
p->pOut = pOut;
return p;
}
/****************************************************************************/
/*
** Return the number between 0 and 100 that is smaller the closer pA and
** pB match. Return 0 for a perfect match. Return 100 if pA and pB are
** completely different.
**
** The current algorithm is as follows:
**
** (1) Remove leading and trailing whitespace.
** (2) Truncate both strings to at most 250 characters
** (3) If the two strings have a common prefix, measure that prefix
** (4) Find the length of the longest common subsequence that is
** at least 150% longer than the common prefix.
** (5) Longer common subsequences yield lower scores.
*/
static int match_dline(DLine *pA, DLine *pB){
const char *zA; /* Left string */
const char *zB; /* right string */
int nA; /* Bytes in zA[] */
int nB; /* Bytes in zB[] */
int nMin;
int nPrefix;
int avg; /* Average length of A and B */
int i, j, k; /* Loop counters */
int best = 0; /* Longest match found so far */
int score; /* Final score. 0..100 */
unsigned char c; /* Character being examined */
unsigned char aFirst[256]; /* aFirst[X] = index in zB[] of first char X */
unsigned char aNext[252]; /* aNext[i] = index in zB[] of next zB[i] char */
zA = pA->z;
if( pA->nw==0 && pA->n ){
for(i=0; i<pA->n && diff_isspace(zA[i]); i++){}
pA->indent = i;
for(j=pA->n-1; j>i && diff_isspace(zA[j]); j--){}
pA->nw = j - i + 1;
}
zA += pA->indent;
nA = pA->nw;
zB = pB->z;
if( pB->nw==0 && pB->n ){
for(i=0; i<pB->n && diff_isspace(zB[i]); i++){}
pB->indent = i;
for(j=pB->n-1; j>i && diff_isspace(zB[j]); j--){}
pB->nw = j - i + 1;
}
zB += pB->indent;
nB = pB->nw;
if( nA>250 ) nA = 250;
if( nB>250 ) nB = 250;
avg = (nA+nB)/2;
if( avg==0 ) return 0;
nMin = nA;
if( nB<nMin ) nMin = nB;
if( nMin==0 ) return 68;
for(nPrefix=0; nPrefix<nMin && zA[nPrefix]==zB[nPrefix]; nPrefix++){}
best = 0;
if( nPrefix>5 && nPrefix>nMin/2 ){
best = nPrefix*3/2;
if( best>=avg - 2 ) best = avg - 2;
}
if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
memset(aFirst, 0xff, sizeof(aFirst));
zA--; zB--; /* Make both zA[] and zB[] 1-indexed */
for(i=nB; i>0; i--){
c = (unsigned char)zB[i];
aNext[i] = aFirst[c];
aFirst[c] = i;
}
for(i=1; i<=nA-best; i++){
c = (unsigned char)zA[i];
for(j=aFirst[c]; j<nB-best && memcmp(&zA[i],&zB[j],best)==0; j = aNext[j]){
int limit = minInt(nA-i, nB-j);
for(k=best; k<=limit && zA[k+i]==zB[k+j]; k++){}
if( k>best ) best = k;
}
}
score = 5 + ((best>=avg) ? 0 : (avg - best)*95/avg);
#if 0
fprintf(stderr, "A: [%.*s]\nB: [%.*s]\nbest=%d avg=%d score=%d\n",
nA, zA+1, nB, zB+1, best, avg, score);
#endif
/* Return the result */
return score;
}
/*
** COMMAND: test-line-match
** Usage: %fossil test-line-match STRING1 STRING2
**
** Return a score from 0 to 100 that is how similar STRING1 is to
** STRING2. Smaller numbers mean more similar. 0 is an exact match.
**
** This command is used to test to match_dline() function in the
** internal Fossil diff logic.
*/
void test_dline_match(void){
DLine a, b;
int x;
if( g.argc!=4 ) usage("STRING1 STRING2");
a.z = g.argv[2];
a.n = (int)strlen(a.z);
b.z = g.argv[3];
b.n = (int)strlen(b.z);
x = match_dline(&a, &b);
fossil_print("%d\n", x);
}
/* Forward declarations for recursion */
static unsigned char *diffBlockAlignment(
DLine *aLeft, int nLeft, /* Text on the left */
DLine *aRight, int nRight, /* Text on the right */
DiffConfig *pCfg, /* Configuration options */
int *pNResult /* OUTPUT: Bytes of result */
);
static void longestCommonSequence(
DContext *p, /* Two files being compared */
int iS1, int iE1, /* Range of lines in p->aFrom[] */
int iS2, int iE2, /* Range of lines in p->aTo[] */
int *piSX, int *piEX, /* Write p->aFrom[] common segment here */
int *piSY, int *piEY /* Write p->aTo[] common segment here */
);
/*
** Make a copy of a list of nLine DLine objects from one array to
** another. Hash the new array to ignore whitespace.
*/
static void diffDLineXfer(
DLine *aTo,
const DLine *aFrom,
int nLine
){
int i, j, k;
u64 h, h2;
for(i=0; i<nLine; i++) aTo[i].iHash = 0;
for(i=0; i<nLine; i++){
const char *z = aFrom[i].z;
int n = aFrom[i].n;
for(j=0; j<n && diff_isspace(z[j]); j++){}
aTo[i].z = &z[j];
for(k=aFrom[i].n; k>j && diff_isspace(z[k-1]); k--){}
aTo[i].n = n = k-j;
aTo[i].indent = 0;
aTo[i].nw = 0;
for(h=0; j<k; j++){
char c = z[j];
if( !diff_isspace(c) ){
h = (h^c)*9000000000000000041LL;
}
}
aTo[i].h = h = ((h%281474976710597LL)<<LENGTH_MASK_SZ) | n;
h2 = h % nLine;
aTo[i].iNext = aTo[h2].iHash;
aTo[h2].iHash = i+1;
}
}
/*
** For a difficult diff-block alignment that was originally for
** the default consider-all-whitespace algorithm, try to find the
** longest common subsequence between the two blocks that involves
** only whitespace changes.
*/
static unsigned char *diffBlockAlignmentIgnoreSpace(
DLine *aLeft, int nLeft, /* Text on the left */
DLine *aRight, int nRight, /* Text on the right */
DiffConfig *pCfg, /* Configuration options */
int *pNResult /* OUTPUT: Bytes of result */
){
DContext dc;
int iSX, iEX; /* Start and end of LCS on the left */
int iSY, iEY; /* Start and end of the LCS on the right */
unsigned char *a1, *a2;
int n1, n2, nLCS;
dc.aEdit = 0;
dc.nEdit = 0;
dc.nEditAlloc = 0;
dc.nFrom = nLeft;
dc.nTo = nRight;
dc.xDiffer = compare_dline_ignore_allws;
dc.aFrom = fossil_malloc( sizeof(DLine)*(nLeft+nRight) );
dc.aTo = &dc.aFrom[dc.nFrom];
diffDLineXfer(dc.aFrom, aLeft, nLeft);
diffDLineXfer(dc.aTo, aRight, nRight);
longestCommonSequence(&dc,0,nLeft,0,nRight,&iSX,&iEX,&iSY,&iEY);
fossil_free(dc.aFrom);
nLCS = iEX - iSX;
if( nLCS<5 ) return 0; /* No good LCS was found */
if( pCfg->diffFlags & DIFF_DEBUG ){
fossil_print(" LCS size=%d\n"
" [%.*s]\n"
" [%.*s]\n",
nLCS, aLeft[iSX].n, aLeft[iSX].z,
aLeft[iEX-1].n, aLeft[iEX-1].z);
}
a1 = diffBlockAlignment(aLeft,iSX,aRight,iSY,pCfg,&n1);
a2 = diffBlockAlignment(aLeft+iEX, nLeft-iEX,
aRight+iEY, nRight-iEY,
pCfg, &n2);
a1 = fossil_realloc(a1, n1+nLCS+n2);
memcpy(a1+n1+nLCS,a2,n2);
memset(a1+n1,3,nLCS);
fossil_free(a2);
*pNResult = n1+n2+nLCS;
return a1;
}
/*
** This is a helper route for diffBlockAlignment(). In this case,
** a very large block is encountered that might be too expensive to
** use the O(N*N) Wagner edit distance algorithm. So instead, this
** block implements a less-precise but faster O(N*logN) divide-and-conquer
** approach.
*/
static unsigned char *diffBlockAlignmentDivideAndConquer(
DLine *aLeft, int nLeft, /* Text on the left */
DLine *aRight, int nRight, /* Text on the right */
DiffConfig *pCfg, /* Configuration options */
int *pNResult /* OUTPUT: Bytes of result */
){
DLine *aSmall; /* The smaller of aLeft and aRight */
DLine *aBig; /* The larger of aLeft and aRight */
int nSmall, nBig; /* Size of aSmall and aBig. nSmall<=nBig */
int iDivSmall, iDivBig; /* Divider point for aSmall and aBig */
int iDivLeft, iDivRight; /* Divider point for aLeft and aRight */
unsigned char *a1, *a2; /* Results of the alignments on two halves */
int n1, n2; /* Number of entries in a1 and a2 */
int score, bestScore; /* Score and best score seen so far */
int i; /* Loop counter */
if( nLeft>nRight ){
aSmall = aRight;
nSmall = nRight;
aBig = aLeft;
nBig = nLeft;
}else{
aSmall = aLeft;
nSmall = nLeft;
aBig = aRight;
nBig = nRight;
}
iDivBig = nBig/2;
iDivSmall = nSmall/2;
if( pCfg->diffFlags & DIFF_DEBUG ){
fossil_print(" Divide at [%.*s]\n",
aBig[iDivBig].n, aBig[iDivBig].z);
}
bestScore = 10000;
for(i=0; i<nSmall; i++){
score = match_dline(aBig+iDivBig, aSmall+i) + abs(i-nSmall/2)*2;
if( score<bestScore ){
bestScore = score;
iDivSmall = i;
}
}
if( aSmall==aRight ){
iDivRight = iDivSmall;
iDivLeft = iDivBig;
}else{
iDivRight = iDivBig;
iDivLeft = iDivSmall;
}
a1 = diffBlockAlignment(aLeft,iDivLeft,aRight,iDivRight,pCfg,&n1);
a2 = diffBlockAlignment(aLeft+iDivLeft, nLeft-iDivLeft,
aRight+iDivRight, nRight-iDivRight,
pCfg, &n2);
a1 = fossil_realloc(a1, n1+n2 );
memcpy(a1+n1,a2,n2);
fossil_free(a2);
*pNResult = n1+n2;
return a1;
}
/*
** The threshold at which diffBlockAlignment transitions from the
** O(N*N) Wagner minimum-edit-distance algorithm to a less process
** O(NlogN) divide-and-conquer approach.
*/
#define DIFF_ALIGN_MX 1225
/*
** There is a change block in which nLeft lines of text on the left are
** converted into nRight lines of text on the right. This routine computes
** how the lines on the left line up with the lines on the right.
**
** The return value is a buffer of unsigned characters, obtained from
** fossil_malloc(). (The caller needs to free the return value using
** fossil_free().) Entries in the returned array have values as follows:
**
** 1. Delete the next line of pLeft.
** 2. Insert the next line of pRight.
** 3. The next line of pLeft changes into the next line of pRight.
** 4. Delete one line from pLeft and add one line to pRight.
**
** The length of the returned array will be at most nLeft+nRight bytes.
** If the first bytes is 4, that means we could not compute reasonable
** alignment between the two blocks.
**
** Algorithm: Wagner's minimum edit-distance algorithm, modified by
** adding a cost to each match based on how well the two rows match
** each other. Insertion and deletion costs are 50. Match costs
** are between 0 and 100 where 0 is a perfect match 100 is a complete
** mismatch.
*/
static unsigned char *diffBlockAlignment(
DLine *aLeft, int nLeft, /* Text on the left */
DLine *aRight, int nRight, /* Text on the right */
DiffConfig *pCfg, /* Configuration options */
int *pNResult /* OUTPUT: Bytes of result */
){
int i, j, k; /* Loop counters */
int *a; /* One row of the Wagner matrix */
int *pToFree; /* Space that needs to be freed */
unsigned char *aM; /* Wagner result matrix */
int aBuf[100]; /* Stack space for a[] if nRight not to big */
if( nLeft==0 ){
aM = fossil_malloc( nRight + 2 );
memset(aM, 2, nRight);
*pNResult = nRight;
return aM;
}
if( nRight==0 ){
aM = fossil_malloc( nLeft + 2 );
memset(aM, 1, nLeft);
*pNResult = nLeft;
return aM;
}
if( pCfg->diffFlags & DIFF_DEBUG ){
fossil_print("BlockAlignment:\n [%.*s] + %d\n [%.*s] + %d\n",
aLeft[0].n, aLeft[0].z, nLeft,
aRight[0].n, aRight[0].z, nRight);
}
/* For large alignments, try to use alternative algorithms that are
** faster than the O(N*N) Wagner edit distance.
*/
if( (i64)nLeft*(i64)nRight>DIFF_ALIGN_MX
&& (pCfg->diffFlags & DIFF_SLOW_SBS)==0
){
if( (pCfg->diffFlags & DIFF_IGNORE_ALLWS)==0 ){
unsigned char *aRes;
aRes = diffBlockAlignmentIgnoreSpace(
aLeft, nLeft,aRight, nRight,pCfg,pNResult);
if( aRes ) return aRes;
}
return diffBlockAlignmentDivideAndConquer(
aLeft, nLeft,aRight, nRight,pCfg,pNResult);
}
/* If we reach this point, we will be doing an O(N*N) Wagner minimum
** edit distance to compute the alignment.
*/
if( nRight < count(aBuf)-1 ){
pToFree = 0;
a = aBuf;
}else{
a = pToFree = fossil_malloc( sizeof(a[0])*(nRight+1) );
}
aM = fossil_malloc( (nLeft+1)*(nRight+1) );
/* Compute the best alignment */
for(i=0; i<=nRight; i++){
aM[i] = 2;
a[i] = i*50;
}
aM[0] = 0;
for(j=1; j<=nLeft; j++){
int p = a[0];
a[0] = p+50;
aM[j*(nRight+1)] = 1;
for(i=1; i<=nRight; i++){
int m = a[i-1]+50;
int d = 2;
if( m>a[i]+50 ){
m = a[i]+50;
d = 1;
}
if( m>p ){
int score = match_dline(&aLeft[j-1], &aRight[i-1]);
if( (score<=90 || (i<j+1 && i>j-1)) && m>p+score ){
m = p+score;
d = 3 | score*4;
}
}
p = a[i];
a[i] = m;
aM[j*(nRight+1)+i] = d;
}
}
/* Compute the lowest-cost path back through the matrix */
i = nRight;
j = nLeft;
k = (nRight+1)*(nLeft+1)-1;
while( i+j>0 ){
unsigned char c = aM[k];
if( c>=3 ){
assert( i>0 && j>0 );
i--;
j--;
aM[k] = 3;
}else if( c==2 ){
assert( i>0 );
i--;
}else{
assert( j>0 );
j--;
}
k--;
aM[k] = aM[j*(nRight+1)+i];
}
k++;
i = (nRight+1)*(nLeft+1) - k;
memmove(aM, &aM[k], i);
*pNResult = i;
/* Return the result */
fossil_free(pToFree);
return aM;
}
/*
** R[] is an array of six integer, two COPY/DELETE/INSERT triples for a
** pair of adjacent differences. Return true if the gap between these
** two differences is so small that they should be rendered as a single
** edit.
*/
static int smallGap(const int *R, int ma, int mb){
int m = R[3];
ma += R[4] + m;
mb += R[5] + m;
if( ma*mb>DIFF_ALIGN_MX ) return 0;
return m<=2 || m<=(R[1]+R[2]+R[4]+R[5])/8;
}
/*
** Format a diff using a DiffBuilder object
*/
static void formatDiff(
DContext *p, /* The computed diff */
DiffConfig *pCfg, /* Configuration options */
DiffBuilder *pBuilder /* The formatter object */
){
DLine *A; /* Left side of the diff */
DLine *B; /* Right side of the diff */
unsigned int a = 0; /* Index of next line in A[] */
unsigned int b = 0; /* Index of next line in B[] */
const int *R; /* Array of COPY/DELETE/INSERT triples */
unsigned int r; /* Index into R[] */
unsigned int nr; /* Number of COPY/DELETE/INSERT triples to process */
unsigned int mxr; /* Maximum value for r */
unsigned int i, j; /* Loop counters */
unsigned int m, ma, mb;/* Number of lines to output */
signed int skip = 0; /* Number of lines to skip */
unsigned int nContext; /* Lines of context above and below each change */
nContext = diff_context_lines(pCfg);
A = p->aFrom;
B = p->aTo;
R = p->aEdit;
mxr = p->nEdit;
while( mxr>2 && R[mxr-1]==0 && R[mxr-2]==0 ){ mxr -= 3; }
for(r=0; r<mxr; r += 3*nr){
/* Figure out how many triples to show in a single block */
for(nr=1; 3*nr<mxr && R[r+nr*3]>0 && R[r+nr*3]<(int)nContext*2; nr++){}
/* If there is a regex, skip this block (generate no diff output)
** if the regex matches or does not match both insert and delete.
** Only display the block if one side matches but the other side does
** not.
*/
if( pCfg->pRe ){
int hideBlock = 1;
int xa = a, xb = b;
for(i=0; hideBlock && i<nr; i++){
int c1, c2;
xa += R[r+i*3];
xb += R[r+i*3];
c1 = re_dline_match(pCfg->pRe, &A[xa], R[r+i*3+1]);
c2 = re_dline_match(pCfg->pRe, &B[xb], R[r+i*3+2]);
hideBlock = c1==c2;
xa += R[r+i*3+1];
xb += R[r+i*3+2];
}
if( hideBlock ){
a = xa;
b = xb;
continue;
}
}
/* Figure out how many lines of A and B are to be displayed
** for this change block.
*/
if( R[r]>(int)nContext ){
skip = R[r] - nContext;
}else{
skip = 0;
}
/* Show the initial common area */
a += skip;
b += skip;
m = R[r] - skip;
if( r ) skip -= nContext;
if( skip>0 ){
if( skip<(int)nContext ){
/* If the amount to skip is less that the context band, then
** go ahead and show the skip band as it is not worth eliding */
for(j=0; (int)j<skip; j++){
pBuilder->xCommon(pBuilder, &A[a+j-skip]);
}
}else{
pBuilder->xSkip(pBuilder, skip, 0);
}
}
for(j=0; j<m; j++){
pBuilder->xCommon(pBuilder, &A[a+j]);
}
a += m;
b += m;
/* Show the differences */
for(i=0; i<nr; i++){
int nAlign;
unsigned char *alignment;
ma = R[r+i*3+1]; /* Lines on left but not on right */
mb = R[r+i*3+2]; /* Lines on right but not on left */
/* Try merging the current block with subsequent blocks, if the
** subsequent blocks are nearby and there result isn't too big.
*/
while( i<nr-1 && smallGap(&R[r+i*3],ma,mb) ){
i++;
m = R[r+i*3];
ma += R[r+i*3+1] + m;
mb += R[r+i*3+2] + m;
}
/* Try to find an alignment for the lines within this one block */
alignment = diffBlockAlignment(&A[a], ma, &B[b], mb, pCfg, &nAlign);
for(j=0; ma+mb>0; j++){
assert( (int)j<nAlign );
switch( alignment[j] ){
case 1: {
/* Delete one line from the left */
pBuilder->xDelete(pBuilder, &A[a]);
ma--;
a++;
break;
}
case 2: {
/* Insert one line on the right */
pBuilder->xInsert(pBuilder, &B[b]);
assert( mb>0 );
mb--;
b++;
break;
}
case 3: {
/* The left line is changed into the right line */
if( p->xDiffer(&A[a], &B[b])==0 ){
pBuilder->xCommon(pBuilder, &A[a]);
}else{
pBuilder->xEdit(pBuilder, &A[a], &B[b]);
}
assert( ma>0 && mb>0 );
ma--;
mb--;
a++;
b++;
break;
}
case 4: {
/* Delete from left then separately insert on the right */
pBuilder->xReplace(pBuilder, &A[a], &B[b]);
ma--;
a++;
mb--;
b++;
break;
}
}
}
assert( nAlign==(int)j );
fossil_free(alignment);
if( i<nr-1 ){
m = R[r+i*3+3];
for(j=0; j<m; j++){
pBuilder->xCommon(pBuilder, &A[a+j]);
}
b += m;
a += m;
}
}
/* Show the final common area */
assert( nr==i );
m = R[r+nr*3];
if( m>nContext ) m = nContext;
for(j=0; j<m && j<nContext; j++){
pBuilder->xCommon(pBuilder, &A[a+j]);
}
}
if( R[r]>(int)nContext ){
pBuilder->xSkip(pBuilder, R[r] - nContext, 1);
}
pBuilder->xEnd(pBuilder);
}
/*
** Compute the optimal longest common subsequence (LCS) using an
** exhaustive search. This version of the LCS is only used for
** shorter input strings since runtime is O(N*N) where N is the
** input string length.
*/
static void optimalLCS(
DContext *p, /* Two files being compared */
int iS1, int iE1, /* Range of lines in p->aFrom[] */
int iS2, int iE2, /* Range of lines in p->aTo[] */
int *piSX, int *piEX, /* Write p->aFrom[] common segment here */
int *piSY, int *piEY /* Write p->aTo[] common segment here */
){
int mxLength = 0; /* Length of longest common subsequence */
int i, j; /* Loop counters */
int k; /* Length of a candidate subsequence */
int iSXb = iS1; /* Best match so far */
int iSYb = iS2; /* Best match so far */
for(i=iS1; i<iE1-mxLength; i++){
for(j=iS2; j<iE2-mxLength; j++){
if( p->xDiffer(&p->aFrom[i], &p->aTo[j]) ) continue;
if( mxLength && p->xDiffer(&p->aFrom[i+mxLength], &p->aTo[j+mxLength]) ){
continue;
}
k = 1;
while( i+k<iE1 && j+k<iE2 && p->xDiffer(&p->aFrom[i+k],&p->aTo[j+k])==0 ){
k++;
}
if( k>mxLength ){
iSXb = i;
iSYb = j;
mxLength = k;
}
}
}
*piSX = iSXb;
*piEX = iSXb + mxLength;
*piSY = iSYb;
*piEY = iSYb + mxLength;
}
/*
** Compare two blocks of text on lines iS1 through iE1-1 of the aFrom[]
** file and lines iS2 through iE2-1 of the aTo[] file. Locate a sequence
** of lines in these two blocks that are exactly the same. Return
** the bounds of the matching sequence.
**
** If there are two or more possible answers of the same length, the
** returned sequence should be the one closest to the center of the
** input range.
**
** Ideally, the common sequence should be the longest possible common
** sequence. However, an exact computation of LCS is O(N*N) which is
** way too slow for larger files. So this routine uses an O(N)
** heuristic approximation based on hashing that usually works about
** as well. But if the O(N) algorithm doesn't get a good solution
** and N is not too large, we fall back to an exact solution by
** calling optimalLCS().
*/
static void longestCommonSequence(
DContext *p, /* Two files being compared */
int iS1, int iE1, /* Range of lines in p->aFrom[] */
int iS2, int iE2, /* Range of lines in p->aTo[] */
int *piSX, int *piEX, /* Write p->aFrom[] common segment here */
int *piSY, int *piEY /* Write p->aTo[] common segment here */
){
int i, j, k; /* Loop counters */
int n; /* Loop limit */
DLine *pA, *pB; /* Pointers to lines */
int iSX, iSY, iEX, iEY; /* Current match */
int skew = 0; /* How lopsided is the match */
int dist = 0; /* Distance of match from center */
int mid; /* Center of the chng */
int iSXb, iSYb, iEXb, iEYb; /* Best match so far */
int iSXp, iSYp, iEXp, iEYp; /* Previous match */
sqlite3_int64 bestScore; /* Best score so far */
sqlite3_int64 score; /* Score for current candidate LCS */
int span; /* combined width of the input sequences */
int cutoff = 4; /* Max hash chain entries to follow */
int nextCutoff = -1; /* Value of cutoff for next iteration */
span = (iE1 - iS1) + (iE2 - iS2);
bestScore = -9223300000*(sqlite3_int64)1000000000;
score = 0;
iSXb = iSXp = iS1;
iEXb = iEXp = iS1;
iSYb = iSYp = iS2;
iEYb = iEYp = iS2;
mid = (iE1 + iS1)/2;
do{
nextCutoff = 0;
for(i=iS1; i<iE1; i++){
int limit = 0;
j = p->aTo[p->aFrom[i].h % p->nTo].iHash;
while( j>0
&& (j-1<iS2 || j>=iE2 || p->xDiffer(&p->aFrom[i], &p->aTo[j-1]))
){
if( limit++ > cutoff ){
j = 0;
nextCutoff = cutoff*4;
break;
}
j = p->aTo[j-1].iNext;
}
if( j==0 ) continue;
assert( i>=iSXb && i>=iSXp );
if( i<iEXb && j>=iSYb && j<iEYb ) continue;
if( i<iEXp && j>=iSYp && j<iEYp ) continue;
iSX = i;
iSY = j-1;
pA = &p->aFrom[iSX-1];
pB = &p->aTo[iSY-1];
n = minInt(iSX-iS1, iSY-iS2);
for(k=0; k<n && p->xDiffer(pA,pB)==0; k++, pA--, pB--){}
iSX -= k;
iSY -= k;
iEX = i+1;
iEY = j;
pA = &p->aFrom[iEX];
pB = &p->aTo[iEY];
n = minInt(iE1-iEX, iE2-iEY);
for(k=0; k<n && p->xDiffer(pA,pB)==0; k++, pA++, pB++){}
iEX += k;
iEY += k;
skew = (iSX-iS1) - (iSY-iS2);
if( skew<0 ) skew = -skew;
dist = (iSX+iEX)/2 - mid;
if( dist<0 ) dist = -dist;
score = (iEX - iSX)*(sqlite3_int64)span - (skew + dist);
if( score>bestScore ){
bestScore = score;
iSXb = iSX;
iSYb = iSY;
iEXb = iEX;
iEYb = iEY;
}else if( iEX>iEXp ){
iSXp = iSX;
iSYp = iSY;
iEXp = iEX;
iEYp = iEY;
}
}
}while( iSXb==iEXb && nextCutoff && (cutoff=nextCutoff)<=64 );
if( iSXb==iEXb && (sqlite3_int64)(iE1-iS1)*(iE2-iS2)<2500 ){
/* If no common sequence is found using the hashing heuristic and
** the input is not too big, use the expensive exact solution */
optimalLCS(p, iS1, iE1, iS2, iE2, piSX, piEX, piSY, piEY);
}else{
*piSX = iSXb;
*piSY = iSYb;
*piEX = iEXb;
*piEY = iEYb;
}
}
/*
** Expand the size of aEdit[] array to hold at least nEdit elements.
*/
static void expandEdit(DContext *p, int nEdit){
p->aEdit = fossil_realloc(p->aEdit, nEdit*sizeof(int));
p->nEditAlloc = nEdit;
}
/*
** Append a new COPY/DELETE/INSERT triple.
*/
static void appendTriple(DContext *p, int nCopy, int nDel, int nIns){
/* printf("APPEND %d/%d/%d\n", nCopy, nDel, nIns); */
if( p->nEdit>=3 ){
if( p->aEdit[p->nEdit-1]==0 ){
if( p->aEdit[p->nEdit-2]==0 ){
p->aEdit[p->nEdit-3] += nCopy;
p->aEdit[p->nEdit-2] += nDel;
p->aEdit[p->nEdit-1] += nIns;
return;
}
if( nCopy==0 ){
p->aEdit[p->nEdit-2] += nDel;
p->aEdit[p->nEdit-1] += nIns;
return;
}
}
if( nCopy==0 && nDel==0 ){
p->aEdit[p->nEdit-1] += nIns;
return;
}
}
if( p->nEdit+3>p->nEditAlloc ){
expandEdit(p, p->nEdit*2 + 15);
if( p->aEdit==0 ) return;
}
p->aEdit[p->nEdit++] = nCopy;
p->aEdit[p->nEdit++] = nDel;
p->aEdit[p->nEdit++] = nIns;
}
/*
** A common subsequence between p->aFrom and p->aTo has been found.
** This routine tries to judge if the subsequence really is a valid
** match or rather is just an artifact of an indentation change.
**
** Return non-zero if the subsequence is valid. Return zero if the
** subsequence seems likely to be an editing artifact and should be
** ignored.
**
** This routine is a heuristic optimization intended to give more
** intuitive diff results following an indentation change it code that
** is formatted similarly to C/C++, Javascript, Go, TCL, and similar
** languages that use {...} for nesting. A correct diff is computed
** even if this routine always returns true (non-zero). But sometimes
** a more intuitive diff can result if this routine returns false.
**
** The subsequences consists of the rows iSX through iEX-1 (inclusive)
** in p->aFrom[]. The total sequences is iS1 through iE1-1 (inclusive)
** of p->aFrom[].
**
** Example where this heuristic is useful, see the diff at
** https://www.sqlite.org/src/fdiff?v1=0e79dd15cbdb4f48&v2=33955a6fd874dd97
**
** See also discussion at https://fossil-scm.org/forum/forumpost/9ba3284295
**
** ALGORITHM (subject to change and refinement):
**
** 1. If the subsequence is larger than 1/7th of the original span,
** then consider it valid. --> return 1
**
** 2. If no lines of the subsequence contains more than one
** non-whitespace character, --> return 0
**
** 3. If any line of the subsequence contains more than one non-whitespace
** character and is unique across the entire sequence after ignoring
** leading and trailing whitespace --> return 1
**
** 4. Otherwise, it is potentially an artifact of an indentation
** change. --> return 0
*/
static int likelyNotIndentChngArtifact(
DContext *p, /* The complete diff context */
int iS1, /* Start of the main segment */
int iSX, /* Start of the subsequence */
int iEX, /* First row past the end of the subsequence */
int iE1 /* First row past the end of the main segment */
){
int i, j, n;
/* Rule (1) */
if( (iEX-iSX)*7 >= (iE1-iS1) ) return 1;
/* Compute DLine.indent and DLine.nw for all lines of the subsequence.
** If no lines contain more than one non-whitespace character return
** 0 because the subsequence could be due to an indentation change.
** Rule (2).
*/
n = 0;
for(i=iSX; i<iEX; i++){
DLine *pA = &p->aFrom[i];
if( pA->nw==0 && pA->n ){
const char *zA = pA->z;
const int nn = pA->n;
int ii, jj;
for(ii=0; ii<nn && diff_isspace(zA[ii]); ii++){}
pA->indent = ii;
for(jj=nn-1; jj>ii && diff_isspace(zA[jj]); jj--){}
pA->nw = jj - ii + 1;
}
if( pA->nw>1 ) n++;
}
if( n==0 ) return 0;
/* Compute DLine.indent and DLine.nw for the entire sequence */
for(i=iS1; i<iE1; i++){
DLine *pA;
if( i==iSX ){
i = iEX;
if( i>=iE1 ) break;
}
pA = &p->aFrom[i];
if( pA->nw==0 && pA->n ){
const char *zA = pA->z;
const int nn = pA->n;
int ii, jj;
for(ii=0; ii<nn && diff_isspace(zA[ii]); ii++){}
pA->indent = ii;
for(jj=nn-1; jj>ii && diff_isspace(zA[jj]); jj--){}
pA->nw = jj - ii + 1;
}
}
/* Check to see if any subsequence line that has more than one
** non-whitespace character is unique across the entire sequence.
** Rule (3)
*/
for(i=iSX; i<iEX; i++){
const char *z = p->aFrom[i].z + p->aFrom[i].indent;
const int nw = p->aFrom[i].nw;
if( nw<=1 ) continue;
for(j=iS1; j<iSX; j++){
if( p->aFrom[j].nw!=nw ) continue;
if( memcmp(p->aFrom[j].z+p->aFrom[j].indent,z,nw)==0 ) break;
}
if( j<iSX ) continue;
for(j=iEX; j<iE1; j++){
if( p->aFrom[j].nw!=nw ) continue;
if( memcmp(p->aFrom[j].z+p->aFrom[j].indent,z,nw)==0 ) break;
}
if( j>=iE1 ) break;
}
return i<iEX;
}
/*
** Do a single step in the difference. Compute a sequence of
** copy/delete/insert steps that will convert lines iS1 through iE1-1 of
** the input into lines iS2 through iE2-1 of the output and write
** that sequence into the difference context.
**
** The algorithm is to find a block of common text near the middle
** of the two segments being diffed. Then recursively compute
** differences on the blocks before and after that common segment.
** Special cases apply if either input segment is empty or if the
** two segments have no text in common.
*/
static void diff_step(DContext *p, int iS1, int iE1, int iS2, int iE2){
int iSX, iEX, iSY, iEY;
if( iE1<=iS1 ){
/* The first segment is empty */
if( iE2>iS2 ){
appendTriple(p, 0, 0, iE2-iS2);
}
return;
}
if( iE2<=iS2 ){
/* The second segment is empty */
appendTriple(p, 0, iE1-iS1, 0);
return;
}
/* Find the longest matching segment between the two sequences */
longestCommonSequence(p, iS1, iE1, iS2, iE2, &iSX, &iEX, &iSY, &iEY);
if( iEX>iSX+5
|| (iEX>iSX && likelyNotIndentChngArtifact(p,iS1,iSX,iEX,iE1) )
){
/* A common segment has been found.
** Recursively diff either side of the matching segment */
diff_step(p, iS1, iSX, iS2, iSY);
if( iEX>iSX ){
appendTriple(p, iEX - iSX, 0, 0);
}
diff_step(p, iEX, iE1, iEY, iE2);
}else{
/* The two segments have nothing in common. Delete the first then
** insert the second. */
appendTriple(p, 0, iE1-iS1, iE2-iS2);
}
}
/*
** Compute the differences between two files already loaded into
** the DContext structure.
**
** A divide and conquer technique is used. We look for a large
** block of common text that is in the middle of both files. Then
** compute the difference on those parts of the file before and
** after the common block. This technique is fast, but it does
** not necessarily generate the minimum difference set. On the
** other hand, we do not need a minimum difference set, only one
** that makes sense to human readers, which this algorithm does.
**
** Any common text at the beginning and end of the two files is
** removed before starting the divide-and-conquer algorithm.
*/
static void diff_all(DContext *p){
int mnE, iS, iE1, iE2;
/* Carve off the common header and footer */
iE1 = p->nFrom;
iE2 = p->nTo;
while( iE1>0 && iE2>0 && p->xDiffer(&p->aFrom[iE1-1], &p->aTo[iE2-1])==0 ){
iE1--;
iE2--;
}
mnE = iE1<iE2 ? iE1 : iE2;
for(iS=0; iS<mnE && p->xDiffer(&p->aFrom[iS],&p->aTo[iS])==0; iS++){}
/* do the difference */
if( iS>0 ){
appendTriple(p, iS, 0, 0);
}
diff_step(p, iS, iE1, iS, iE2);
if( iE1<p->nFrom ){
appendTriple(p, p->nFrom - iE1, 0, 0);
}
/* Terminate the COPY/DELETE/INSERT triples with three zeros */
expandEdit(p, p->nEdit+3);
if( p->aEdit ){
p->aEdit[p->nEdit++] = 0;
p->aEdit[p->nEdit++] = 0;
p->aEdit[p->nEdit++] = 0;
}
}
/*
** Attempt to shift insertion or deletion blocks so that they begin and
** end on lines that are pure whitespace. In other words, try to transform
** this:
**
** int func1(int x){
** return x*10;
** +}
** +
** +int func2(int x){
** + return x*20;
** }
**
** int func3(int x){
** return x/5;
** }
**
** Into one of these:
**
** int func1(int x){ int func1(int x){
** return x*10; return x*10;
** } }
** +
** +int func2(int x){ +int func2(int x){
** + return x*20; + return x*20;
** +} +}
** +
** int func3(int x){ int func3(int x){
** return x/5; return x/5;
** } }
*/
static void diff_optimize(DContext *p){
int r; /* Index of current triple */
int lnFrom; /* Line number in p->aFrom */
int lnTo; /* Line number in p->aTo */
int cpy, del, ins;
lnFrom = lnTo = 0;
for(r=0; r<p->nEdit; r += 3){
cpy = p->aEdit[r];
del = p->aEdit[r+1];
ins = p->aEdit[r+2];
lnFrom += cpy;
lnTo += cpy;
/* Shift insertions toward the beginning of the file */
while( cpy>0 && del==0 && ins>0 ){
DLine *pTop = &p->aFrom[lnFrom-1]; /* Line before start of insert */
DLine *pBtm = &p->aTo[lnTo+ins-1]; /* Last line inserted */
if( p->xDiffer(pTop, pBtm) ) break;
if( LENGTH(pTop+1)+LENGTH(pBtm)<=LENGTH(pTop)+LENGTH(pBtm-1) ) break;
lnFrom--;
lnTo--;
p->aEdit[r]--;
p->aEdit[r+3]++;
cpy--;
}
/* Shift insertions toward the end of the file */
while( r+3<p->nEdit && p->aEdit[r+3]>0 && del==0 && ins>0 ){
DLine *pTop = &p->aTo[lnTo]; /* First line inserted */
DLine *pBtm = &p->aTo[lnTo+ins]; /* First line past end of insert */
if( p->xDiffer(pTop, pBtm) ) break;
if( LENGTH(pTop)+LENGTH(pBtm-1)<=LENGTH(pTop+1)+LENGTH(pBtm) ) break;
lnFrom++;
lnTo++;
p->aEdit[r]++;
p->aEdit[r+3]--;
cpy++;
}
/* Shift deletions toward the beginning of the file */
while( cpy>0 && del>0 && ins==0 ){
DLine *pTop = &p->aFrom[lnFrom-1]; /* Line before start of delete */
DLine *pBtm = &p->aFrom[lnFrom+del-1]; /* Last line deleted */
if( p->xDiffer(pTop, pBtm) ) break;
if( LENGTH(pTop+1)+LENGTH(pBtm)<=LENGTH(pTop)+LENGTH(pBtm-1) ) break;
lnFrom--;
lnTo--;
p->aEdit[r]--;
p->aEdit[r+3]++;
cpy--;
}
/* Shift deletions toward the end of the file */
while( r+3<p->nEdit && p->aEdit[r+3]>0 && del>0 && ins==0 ){
DLine *pTop = &p->aFrom[lnFrom]; /* First line deleted */
DLine *pBtm = &p->aFrom[lnFrom+del]; /* First line past end of delete */
if( p->xDiffer(pTop, pBtm) ) break;
if( LENGTH(pTop)+LENGTH(pBtm-1)<=LENGTH(pTop)+LENGTH(pBtm) ) break;
lnFrom++;
lnTo++;
p->aEdit[r]++;
p->aEdit[r+3]--;
cpy++;
}
lnFrom += del;
lnTo += ins;
}
}
/*
** Extract the number of lines of context from diffFlags. Supply an
** appropriate default if no context width is specified. If pCfg is
** NULL then the compile-time default is used (which gets propagated
** to JS-side state by certain pages).
*/
int diff_context_lines(DiffConfig *pCfg){
const int dflt = 5;
if(pCfg!=0){
int n = pCfg->nContext;
if( n==0 && (pCfg->diffFlags & DIFF_CONTEXT_EX)==0 ) n = dflt;
return n<0 ? 0x7ffffff : n;
}else{
return dflt;
}
}
/*
** Extract the width of columns for side-by-side diff. Supply an
** appropriate default if no width is given.
**
** Calculate the default automatically, based on terminal's current width:
** term-width = 2*diff-col + diff-marker + 1
** diff-col = lineno + lmargin + text-width + rmargin
**
** text-width = (term-width - diff-marker - 1)/2 - lineno - lmargin - rmargin
*/
int diff_width(DiffConfig *pCfg){
int w = pCfg->wColumn;
if( w==0 ){
static struct {
unsigned int lineno, lmargin, text, rmargin, marker;
} sbsW = { 5, 2, 0, 0, 3 };
const unsigned int wMin = 24, wMax = 132;
unsigned int tw = terminal_get_width(80);
unsigned int twMin =
(wMin + sbsW.lineno + sbsW.lmargin + sbsW.rmargin)*2 + sbsW.marker + 1;
unsigned int twMax =
(wMax + sbsW.lineno + sbsW.lmargin + sbsW.rmargin)*2 + sbsW.marker + 1;
if( tw<twMin ){
tw = twMin;
}else if( tw>twMax ){
tw = twMax;
}
sbsW.text =
(tw - sbsW.marker - 1)/2 - sbsW.lineno - sbsW.lmargin - sbsW.rmargin;
w = sbsW.text;
}
return w;
}
/*
** Append the error message to pOut.
*/
void diff_errmsg(Blob *pOut, const char *msg, int diffFlags){
if( diffFlags & DIFF_HTML ){
blob_appendf(pOut, "<p class=\"generalError\">%s</p>", msg);
}else{
blob_append(pOut, msg, -1);
}
}
/*
** Generate a report of the differences between files pA_Blob and pB_Blob.
**
** If pOut!=NULL then append text to pOut that will be the difference,
** formatted according to flags in diffFlags. The pOut Blob must have
** already been initialized.
**
** If pOut==NULL then no formatting occurs. Instead, this routine
** returns a pointer to an array of integers. The integers come in
** triples. The elements of each triple are:
**
** 1. The number of lines to copy
** 2. The number of lines to delete
** 3. The number of lines to insert
**
** The return vector is terminated by a triple of all zeros. The caller
** should free the returned vector using fossil_free().
**
** This diff utility does not work on binary files. If a binary
** file is encountered, 0 is returned and pOut is written with
** text "cannot compute difference between binary files".
*/
int *text_diff(
Blob *pA_Blob, /* FROM file */
Blob *pB_Blob, /* TO file */
Blob *pOut, /* Write diff here if not NULL */
DiffConfig *pCfg /* Configuration options */
){
int ignoreWs; /* Ignore whitespace */
DContext c;
int nDel = 0, nIns = 0;
if( pCfg->diffFlags & DIFF_INVERT ){
Blob *pTemp = pA_Blob;
pA_Blob = pB_Blob;
pB_Blob = pTemp;
}
ignoreWs = (pCfg->diffFlags & DIFF_IGNORE_ALLWS)!=0;
blob_to_utf8_no_bom(pA_Blob, 0);
blob_to_utf8_no_bom(pB_Blob, 0);
/* Prepare the input files */
memset(&c, 0, sizeof(c));
if( (pCfg->diffFlags & DIFF_IGNORE_ALLWS)==DIFF_IGNORE_ALLWS ){
c.xDiffer = compare_dline_ignore_allws;
}else{
c.xDiffer = compare_dline;
}
if( pCfg->diffFlags & DIFF_BY_TOKEN ){
c.aFrom = break_into_tokens(blob_str(pA_Blob), blob_size(pA_Blob),
&c.nFrom, pCfg->diffFlags);
c.aTo = break_into_tokens(blob_str(pB_Blob), blob_size(pB_Blob),
&c.nTo, pCfg->diffFlags);
}else{
c.aFrom = break_into_lines(blob_str(pA_Blob), blob_size(pA_Blob),
&c.nFrom, pCfg->diffFlags);
c.aTo = break_into_lines(blob_str(pB_Blob), blob_size(pB_Blob),
&c.nTo, pCfg->diffFlags);
}
if( c.aFrom==0 || c.aTo==0 ){
fossil_free(c.aFrom);
fossil_free(c.aTo);
if( pOut ){
diff_errmsg(pOut, DIFF_CANNOT_COMPUTE_BINARY, pCfg->diffFlags);
}
return 0;
}
/* Compute the difference */
diff_all(&c);
if( ignoreWs && c.nEdit==6 && c.aEdit[1]==0 && c.aEdit[2]==0 ){
fossil_free(c.aFrom);
fossil_free(c.aTo);
fossil_free(c.aEdit);
if( pOut ) diff_errmsg(pOut, DIFF_WHITESPACE_ONLY, pCfg->diffFlags);
return 0;
}
if( (pCfg->diffFlags & DIFF_NOTTOOBIG)!=0 ){
int i, m, n;
int *a = c.aEdit;
int mx = c.nEdit;
for(i=m=n=0; i<mx; i+=3){ m += a[i]; n += a[i+1]+a[i+2]; }
if( n>10000 ){
fossil_free(c.aFrom);
fossil_free(c.aTo);
fossil_free(c.aEdit);
if( pOut ) diff_errmsg(pOut, DIFF_TOO_MANY_CHANGES, pCfg->diffFlags);
return 0;
}
}
if( (pCfg->diffFlags & DIFF_NOOPT)==0 ){
diff_optimize(&c);
}
if( (pCfg->diffFlags & DIFF_BY_TOKEN)!=0 ){
/* Convert token counts into byte counts. */
int i;
int iA = 0;
int iB = 0;
for(i=0; c.aEdit[i] || c.aEdit[i+1] || c.aEdit[i+2]; i+=3){
int k, sum;
for(k=0, sum=0; k<c.aEdit[i]; k++) sum += c.aFrom[iA++].n;
iB += c.aEdit[i];
c.aEdit[i] = sum;
for(k=0, sum=0; k<c.aEdit[i+1]; k++) sum += c.aFrom[iA++].n;
c.aEdit[i+1] = sum;
for(k=0, sum=0; k<c.aEdit[i+2]; k++) sum += c.aTo[iB++].n;
c.aEdit[i+2] = sum;
}
}
if( pCfg->diffFlags & DIFF_NUMSTAT ){
int i;
for(i=0; c.aEdit[i] || c.aEdit[i+1] || c.aEdit[i+2]; i+=3){
nDel += c.aEdit[i+1];
nIns += c.aEdit[i+2];
}
g.diffCnt[1] += nIns;
g.diffCnt[2] += nDel;
if( nIns+nDel ){
g.diffCnt[0]++;
}
}
if( pOut ){
if( pCfg->diffFlags & DIFF_NUMSTAT && !(pCfg->diffFlags & DIFF_HTML)){
if( nIns+nDel ){
if( !(pCfg->diffFlags & DIFF_BRIEF) ){
blob_appendf(pOut, "%10d %10d", nIns, nDel);
}
}
}else if( pCfg->diffFlags & (DIFF_RAW|DIFF_BY_TOKEN) ){
const int *R = c.aEdit;
unsigned int r;
for(r=0; R[r] || R[r+1] || R[r+2]; r += 3){
blob_appendf(pOut, " copy %6d delete %6d insert %6d\n",
R[r], R[r+1], R[r+2]);
}
}else if( pCfg->diffFlags & DIFF_JSON ){
DiffBuilder *pBuilder = dfjsonNew(pOut);
formatDiff(&c, pCfg, pBuilder);
blob_append_char(pOut, '\n');
}else if( pCfg->diffFlags & DIFF_TCL ){
DiffBuilder *pBuilder = dftclNew(pOut);
formatDiff(&c, pCfg, pBuilder);
}else if( pCfg->diffFlags & DIFF_SIDEBYSIDE ){
DiffBuilder *pBuilder;
if( pCfg->diffFlags & DIFF_HTML ){
pBuilder = dfsplitNew(pOut, pCfg);
}else{
pBuilder = dfsbsNew(pOut, pCfg);
}
formatDiff(&c, pCfg, pBuilder);
}else if( pCfg->diffFlags & DIFF_DEBUG ){
DiffBuilder *pBuilder = dfdebugNew(pOut);
formatDiff(&c, pCfg, pBuilder);
}else if( pCfg->diffFlags & DIFF_HTML ){
DiffBuilder *pBuilder = dfunifiedNew(pOut, pCfg);
formatDiff(&c, pCfg, pBuilder);
}else{
contextDiff(&c, pOut, pCfg);
}
fossil_free(c.aFrom);
fossil_free(c.aTo);
fossil_free(c.aEdit);
return 0;
}else{
/* If a context diff is not requested, then return the
** array of COPY/DELETE/INSERT triples.
*/
free(c.aFrom);
free(c.aTo);
return c.aEdit;
}
}
/*
** Initialize the DiffConfig object using command-line options.
**
** Process diff-related command-line options and return an appropriate
** "diffFlags" integer.
**
** -b|--browser Show the diff output in a web-browser
** --brief Show filenames only DIFF_BRIEF
** --by Shorthand for "--browser -y"
** -c|--context N N lines of context. nContext
** --dark Use dark mode for Tcl/Tk and HTML output
** --html Format for HTML DIFF_HTML
** -i|--internal Use built-in diff, not an external tool
** --invert Invert the diff DIFF_INVERT
** --json Output formatted as JSON
** --label NAME Column label. Can be repeated once.
** -n|--linenum Show line numbers DIFF_LINENO
** -N|--new-file Alias for --verbose
** --noopt Disable optimization DIFF_NOOPT
** -s|--numstat Show change counts DIFF_NUMSTAT
** --strip-trailing-cr Strip trailing CR DIFF_STRIP_EOLCR
** --tcl Tcl-formatted output used internally by --tk
** --unified Unified diff. ~DIFF_SIDEBYSIDE
** -v|--verbose Show complete text of added or deleted files
** -w|--ignore-all-space Ignore all whitespaces DIFF_IGNORE_ALLWS
** --webpage Format output as a stand-alone HTML webpage
** -W|--width N N character lines. wColumn
** -y|--side-by-side Side-by-side diff. DIFF_SIDEBYSIDE
** -Z|--ignore-trailing-space Ignore eol-whitespaces DIFF_IGNORE_EOLWS
*/
void diff_options(DiffConfig *pCfg, int isGDiff, int bUnifiedTextOnly){
u64 diffFlags = 0;
const char *z;
int f;
memset(pCfg, 0, sizeof(*pCfg));
if( find_option("ignore-trailing-space","Z",0)!=0 ){
diffFlags = DIFF_IGNORE_EOLWS;
}
if( find_option("ignore-all-space","w",0)!=0 ){
diffFlags = DIFF_IGNORE_ALLWS; /* stronger than DIFF_IGNORE_EOLWS */
}
if( find_option("strip-trailing-cr",0,0)!=0 ){
diffFlags |= DIFF_STRIP_EOLCR;
}
if( !bUnifiedTextOnly ){
if( find_option("side-by-side","y",0)!=0 ) diffFlags |= DIFF_SIDEBYSIDE;
if( find_option("yy",0,0)!=0 ){
diffFlags |= DIFF_SIDEBYSIDE | DIFF_SLOW_SBS;
}
if( find_option("html",0,0)!=0 ) diffFlags |= DIFF_HTML;
if( find_option("unified",0,0)!=0 ) diffFlags &= ~DIFF_SIDEBYSIDE;
if( find_option("webpage",0,0)!=0 ){
diffFlags |= DIFF_HTML|DIFF_WEBPAGE|DIFF_LINENO;
}
if( find_option("browser","b",0)!=0 ){
diffFlags |= DIFF_HTML|DIFF_WEBPAGE|DIFF_LINENO|DIFF_BROWSER;
}
if( find_option("by",0,0)!=0 ){
diffFlags |= DIFF_HTML|DIFF_WEBPAGE|DIFF_LINENO|DIFF_BROWSER
|DIFF_SIDEBYSIDE;
}
if( find_option("json",0,0)!=0 ){
diffFlags |= DIFF_JSON;
}
if( find_option("tcl",0,0)!=0 ){
diffFlags |= DIFF_TCL;
}
/* Undocumented and unsupported flags used for development
** debugging and analysis: */
if( find_option("debug",0,0)!=0 ) diffFlags |= DIFF_DEBUG;
if( find_option("raw",0,0)!=0 ) diffFlags |= DIFF_RAW;
if( find_option("bytoken",0,0)!=0 ){
diffFlags = DIFF_RAW|DIFF_BY_TOKEN;
}
}
if( (z = find_option("context","c",1))!=0 ){
char *zEnd;
f = (int)strtol(z, &zEnd, 10);
if( zEnd[0]==0 && errno!=ERANGE ){
pCfg->nContext = f;
diffFlags |= DIFF_CONTEXT_EX;
}
}
if( (z = find_option("width","W",1))!=0 && (f = atoi(z))>0 ){
pCfg->wColumn = f;
}
pCfg->azLabel[0] = find_option("label",0,1);
if( pCfg->azLabel[0] ){
pCfg->azLabel[1] = find_option("label",0,1);
}
if( find_option("linenum","n",0)!=0 ) diffFlags |= DIFF_LINENO;
if( find_option("noopt",0,0)!=0 ) diffFlags |= DIFF_NOOPT;
if( find_option("numstat","s",0)!=0 ) diffFlags |= DIFF_NUMSTAT;
if( find_option("versions","h",0)!=0 ) diffFlags |= DIFF_SHOW_VERS;
if( find_option("dark",0,0)!=0 ) diffFlags |= DIFF_DARKMODE;
if( find_option("invert",0,0)!=0 ) diffFlags |= DIFF_INVERT;
if( find_option("brief",0,0)!=0 ) diffFlags |= DIFF_BRIEF;
if( find_option("internal","i",0)==0
&& (diffFlags & (DIFF_HTML|DIFF_TCL|DIFF_DEBUG|DIFF_JSON))==0
){
pCfg->zDiffCmd = find_option("command", 0, 1);
if( pCfg->zDiffCmd==0 ) pCfg->zDiffCmd = diff_command_external(isGDiff);
if( pCfg->zDiffCmd ){
const char *zDiffBinary;
pCfg->zBinGlob = diff_get_binary_glob();
zDiffBinary = find_option("diff-binary", 0, 1);
if( zDiffBinary ){
if( is_truth(zDiffBinary) ) diffFlags |= DIFF_INCBINARY;
}else if( db_get_boolean("diff-binary", 1) ){
diffFlags |= DIFF_INCBINARY;
}
}else if( isGDiff) {
/* No external gdiff command found, using --by */
diffFlags |= DIFF_HTML|DIFF_WEBPAGE|DIFF_LINENO|DIFF_BROWSER
|DIFF_SIDEBYSIDE;
}
}
if( find_option("verbose","v",0)!=0 ) diffFlags |= DIFF_VERBOSE;
/* Deprecated, but retained for script compatibility. */
else if( find_option("new-file","N",0)!=0 ) diffFlags |= DIFF_VERBOSE;
pCfg->diffFlags = diffFlags;
}
/*
** COMMAND: test-diff
** COMMAND: xdiff
**
** Usage: %fossil xdiff [options] FILE1 FILE2
**
** Compute an "external diff" between two files. By "external diff" we mean
** a diff between two disk files that are not necessarily under management.
** In other words, this command provides a mechanism to use Fossil's file
** difference engine on arbitrary disk files. See the "diff" command for
** computing differences between files that are under management.
**
** This command prints the differences between the two files FILE1 and FILE2.
** all of the usual diff formatting options (--tk, --by, -c N, etc.) apply.
** See the "diff" command for a full list of command-line options.
*/
void xdiff_cmd(void){
Blob a, b, out;
const char *zRe; /* Regex filter for diff output */
DiffConfig DCfg;
if( find_option("tk",0,0)!=0 ){
diff_tk("xdiff", 2);
return;
}
find_option("i",0,0);
find_option("v",0,0);
diff_options(&DCfg, 0, 0);
zRe = find_option("regexp","e",1);
if( zRe ){
const char *zErr = fossil_re_compile(&DCfg.pRe, zRe, 0);
if( zErr ) fossil_fatal("regex error: %s", zErr);
}
verify_all_options();
if( g.argc!=4 ) usage("FILE1 FILE2");
blob_zero(&out);
diff_begin(&DCfg);
diff_print_filenames(g.argv[2], g.argv[3], &DCfg, &out);
blob_read_from_file(&a, g.argv[2], ExtFILE);
blob_read_from_file(&b, g.argv[3], ExtFILE);
text_diff(&a, &b, &out, &DCfg);
blob_write_to_file(&out, "-");
diff_end(&DCfg, 0);
re_free(DCfg.pRe);
}
/*
** COMMAND: fdiff
**
** Usage: %fossil fdiff [options] HASH1 HASH2
**
** Compute a diff between two artifacts in a repository (either a repository
** identified by the "-R FILENAME" option, or the repository that contains
** the working directory).
**
** All of the usual diff formatting options (--tk, --by, -c N, etc.) apply.
** See the "diff" command for a full list of command-line options.
*/
void fdiff_cmd(void){
Blob a, b, out;
const char *zRe; /* Regex filter for diff output */
int rid;
DiffConfig DCfg;
if( find_option("tk",0,0)!=0 ){
diff_tk("fdiff", 2);
return;
}
find_option("i",0,0);
find_option("v",0,0);
diff_options(&DCfg, 0, 0);
zRe = find_option("regexp","e",1);
if( zRe ){
const char *zErr = fossil_re_compile(&DCfg.pRe, zRe, 0);
if( zErr ) fossil_fatal("regex error: %s", zErr);
}
db_find_and_open_repository(0, 0);
verify_all_options();
if( g.argc!=4 ) usage("HASH1 HASH2");
blob_zero(&out);
diff_begin(&DCfg);
diff_print_filenames(g.argv[2], g.argv[3], &DCfg, &out);
rid = name_to_typed_rid(g.argv[2], 0);
content_get(rid, &a);
rid = name_to_typed_rid(g.argv[3], 0);
content_get(rid, &b);
text_diff(&a, &b, &out, &DCfg);
blob_write_to_file(&out, "-");
diff_end(&DCfg, 0);
re_free(DCfg.pRe);
}
/**************************************************************************
** The basic difference engine is above. What follows is the annotation
** engine. Both are in the same file since they share many components.
*/
/*
** The status of an annotation operation is recorded by an instance
** of the following structure.
*/
typedef struct Annotator Annotator;
struct Annotator {
DContext c; /* The diff-engine context */
struct AnnLine { /* Lines of the original files... */
const char *z; /* The text of the line */
short int n; /* Number of bytes (omitting trailing \n) */
short int iVers; /* Level at which tag was set */
} *aOrig;
int nOrig; /* Number of elements in aOrig[] */
int nVers; /* Number of versions analyzed */
int bMoreToDo; /* True if the limit was reached */
int origId; /* RID for the zOrigin version */
int showId; /* RID for the version being analyzed */
struct AnnVers {
const char *zFUuid; /* File being analyzed */
const char *zMUuid; /* Check-in containing the file */
const char *zDate; /* Date of the check-in */
const char *zBgColor; /* Suggested background color */
const char *zUser; /* Name of user who did the check-in */
unsigned cnt; /* Number of lines contributed by this check-in */
} *aVers; /* For each check-in analyzed */
char **azVers; /* Names of versions analyzed */
};
/*
** Initialize the annotation process by specifying the file that is
** to be annotated. The annotator takes control of the input Blob and
** will release it when it is finished with it.
*/
static int annotation_start(Annotator *p, Blob *pInput, u64 diffFlags){
int i;
memset(p, 0, sizeof(*p));
if( (diffFlags & DIFF_IGNORE_ALLWS)==DIFF_IGNORE_ALLWS ){
p->c.xDiffer = compare_dline_ignore_allws;
}else{
p->c.xDiffer = compare_dline;
}
p->c.aTo = break_into_lines(blob_str(pInput), blob_size(pInput),&p->c.nTo,
diffFlags);
if( p->c.aTo==0 ){
return 1;
}
p->aOrig = fossil_malloc( sizeof(p->aOrig[0])*p->c.nTo );
for(i=0; i<p->c.nTo; i++){
p->aOrig[i].z = p->c.aTo[i].z;
p->aOrig[i].n = p->c.aTo[i].n;
p->aOrig[i].iVers = -1;
}
p->nOrig = p->c.nTo;
return 0;
}
/*
** The input pParent is the next most recent ancestor of the file
** being annotated. Do another step of the annotation. Return true
** if additional annotation is required.
*/
static int annotation_step(
Annotator *p,
Blob *pParent,
int iVers,
u64 diffFlags
){
int i, j;
int lnTo;
/* Prepare the parent file to be diffed */
p->c.aFrom = break_into_lines(blob_str(pParent), blob_size(pParent),
&p->c.nFrom, diffFlags);
if( p->c.aFrom==0 ){
return 1;
}
/* Compute the differences going from pParent to the file being
** annotated. */
diff_all(&p->c);
/* Where new lines are inserted on this difference, record the
** iVers as the source of the new line.
*/
for(i=lnTo=0; i<p->c.nEdit; i+=3){
int nCopy = p->c.aEdit[i];
int nIns = p->c.aEdit[i+2];
lnTo += nCopy;
for(j=0; j<nIns; j++, lnTo++){
if( p->aOrig[lnTo].iVers<0 ){
p->aOrig[lnTo].iVers = iVers;
}
}
}
/* Clear out the diff results */
fossil_free(p->c.aEdit);
p->c.aEdit = 0;
p->c.nEdit = 0;
p->c.nEditAlloc = 0;
/* Clear out the from file */
free(p->c.aFrom);
/* Return no errors */
return 0;
}
/* Return the current time as milliseconds since the Julian epoch */
static sqlite3_int64 current_time_in_milliseconds(void){
static sqlite3_vfs *clockVfs = 0;
sqlite3_int64 t;
if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
clockVfs->xCurrentTimeInt64(clockVfs, &t);
}else{
double r;
clockVfs->xCurrentTime(clockVfs, &r);
t = (sqlite3_int64)(r*86400000.0);
}
return t;
}
/*
** Compute a complete annotation on a file. The file is identified by its
** filename and check-in name (NULL for current check-in).
*/
static void annotate_file(
Annotator *p, /* The annotator */
const char *zFilename, /* The name of the file to be annotated */
const char *zRevision, /* Use the version of the file in this check-in */
const char *zLimit, /* Limit the number of versions analyzed */
const char *zOrigin, /* The origin check-in, or NULL for root-of-tree */
u64 annFlags /* Flags to alter the annotation */
){
Blob toAnnotate; /* Text of the final (mid) version of the file */
Blob step; /* Text of previous revision */
int cid; /* Selected check-in ID */
int origid = 0; /* The origin ID or zero */
int rid; /* Artifact ID of the file being annotated */
int fnid; /* Filename ID */
Stmt q; /* Query returning all ancestor versions */
int cnt = 0; /* Number of versions analyzed */
int iLimit; /* Maximum number of versions to analyze */
sqlite3_int64 mxTime; /* Halt at this time if not already complete */
memset(p, 0, sizeof(*p));
if( zLimit ){
if( strcmp(zLimit,"none")==0 ){
iLimit = 0;
mxTime = 0;
}else if( sqlite3_strglob("*[0-9]s", zLimit)==0 ){
iLimit = 0;
mxTime =
(sqlite3_int64)(current_time_in_milliseconds() + 1000.0*atof(zLimit));
}else{
iLimit = atoi(zLimit);
if( iLimit<=0 ) iLimit = 30;
mxTime = 0;
}
}else{
/* Default limit is as much as we can do in 1.000 seconds */
iLimit = 0;
mxTime = current_time_in_milliseconds()+1000;
}
db_begin_transaction();
/* Get the artifact ID for the check-in being analyzed */
if( zRevision ){
cid = name_to_typed_rid(zRevision, "ci");
}else{
db_must_be_within_tree();
cid = db_lget_int("checkout", 0);
}
origid = zOrigin ? name_to_typed_rid(zOrigin, "ci") : 0;
/* Compute all direct ancestors of the check-in being analyzed into
** the "ancestor" table. */
if( origid ){
path_shortest_stored_in_ancestor_table(origid, cid);
}else{
compute_direct_ancestors(cid);
}
/* Get filename ID */
fnid = db_int(0, "SELECT fnid FROM filename WHERE name=%Q", zFilename);
if( fnid==0 ){
fossil_fatal("no such file: %Q", zFilename);
}
db_prepare(&q,
"SELECT DISTINCT"
" (SELECT uuid FROM blob WHERE rid=mlink.fid),"
" (SELECT uuid FROM blob WHERE rid=mlink.mid),"
" date(event.mtime),"
" coalesce(event.euser,event.user),"
" mlink.fid"
" FROM mlink, event, ancestor"
" WHERE mlink.fnid=%d"
" AND ancestor.rid=mlink.mid"
" AND event.objid=mlink.mid"
" AND mlink.mid!=mlink.pid"
" ORDER BY ancestor.generation;",
fnid
);
while( db_step(&q)==SQLITE_ROW ){
if( cnt>=3 ){ /* Process at least 3 rows before imposing limits */
if( (iLimit>0 && cnt>=iLimit)
|| (cnt>0 && mxTime>0 && current_time_in_milliseconds()>mxTime)
){
p->bMoreToDo = 1;
break;
}
}
rid = db_column_int(&q, 4);
if( cnt==0 ){
if( !content_get(rid, &toAnnotate) ){
fossil_fatal("unable to retrieve content of artifact #%d", rid);
}
blob_to_utf8_no_bom(&toAnnotate, 0);
annotation_start(p, &toAnnotate, annFlags);
p->bMoreToDo = origid!=0;
p->origId = origid;
p->showId = cid;
}
p->aVers = fossil_realloc(p->aVers, (p->nVers+1)*sizeof(p->aVers[0]));
p->aVers[p->nVers].zFUuid = fossil_strdup(db_column_text(&q, 0));
p->aVers[p->nVers].zMUuid = fossil_strdup(db_column_text(&q, 1));
p->aVers[p->nVers].zDate = fossil_strdup(db_column_text(&q, 2));
p->aVers[p->nVers].zUser = fossil_strdup(db_column_text(&q, 3));
if( cnt>0 ){
content_get(rid, &step);
blob_to_utf8_no_bom(&step, 0);
annotation_step(p, &step, p->nVers-1, annFlags);
blob_reset(&step);
}
p->nVers++;
cnt++;
}
if( p->nVers==0 ){
if( zRevision ){
fossil_fatal("file %s does not exist in check-in %s",
zFilename, zRevision);
}else{
fossil_fatal("no history for file: %s", zFilename);
}
}
db_finalize(&q);
db_end_transaction(0);
}
/*
** Return a color from a gradient.
*/
unsigned gradient_color(unsigned c1, unsigned c2, int n, int i){
unsigned c; /* Result color */
unsigned x1, x2;
if( i==0 || n==0 ) return c1;
else if(i>=n) return c2;
x1 = (c1>>16)&0xff;
x2 = (c2>>16)&0xff;
c = (x1*(n-i) + x2*i)/n<<16 & 0xff0000;
x1 = (c1>>8)&0xff;
x2 = (c2>>8)&0xff;
c |= (x1*(n-i) + x2*i)/n<<8 & 0xff00;
x1 = c1&0xff;
x2 = c2&0xff;
c |= (x1*(n-i) + x2*i)/n & 0xff;
return c;
}
/*
** WEBPAGE: annotate
** WEBPAGE: blame
** WEBPAGE: praise
**
** URL: /annotate?checkin=ID&filename=FILENAME
** URL: /blame?checkin=ID&filename=FILENAME
** URL: /praise?checkin=ID&filename=FILENAME
**
** Show the most recent change to each line of a text file. /annotate shows
** the date of the changes and the check-in hash (with a link to the
** check-in). /blame and /praise also show the user who made the check-in.
**
** Reverse Annotations: Normally, these web pages look at versions of
** FILENAME moving backwards in time back toward the root check-in. However,
** if the origin= query parameter is used to specify some future check-in
** (example: "origin=trunk") then these pages show changes moving towards
** that alternative origin. Thus using "origin=trunk" on an historical
** version of the file shows the first time each line in the file was changed
** or removed by any subsequent check-in.
**
** Query parameters:
**
** checkin=ID The check-in at which to start the annotation
** filename=FILENAME The filename.
** filevers=BOOLEAN Show file versions rather than check-in versions
** limit=LIMIT Limit the amount of analysis. LIMIT can be one of:
** none No limit
** Xs As much as can be computed in X seconds
** N N versions
** log=BOOLEAN Show a log of versions analyzed
** origin=ID The origin check-in. If unspecified, the root
** check-in over the entire repository is used.
** Specify "origin=trunk" or similar for a reverse
** annotation
** w=BOOLEAN Ignore whitespace
*/
void annotation_page(void){
int i;
const char *zLimit; /* Depth limit */
u64 annFlags = DIFF_STRIP_EOLCR;
int showLog; /* True to display the log */
int fileVers; /* Show file version instead of check-in versions */
int ignoreWs; /* Ignore whitespace */
const char *zFilename; /* Name of file to annotate */
const char *zRevision; /* Name of check-in from which to start annotation */
const char *zCI; /* The check-in containing zFilename */
const char *zOrigin; /* The origin of the analysis */
int szHash; /* Number of characters in %S display */
char *zLink;
Annotator ann;
HQuery url;
struct AnnVers *p;
unsigned clr1, clr2, clr;
int bBlame = g.zPath[0]!='a';/* True for BLAME output. False for ANNOTATE. */
/* Gather query parameters */
login_check_credentials();
if( !g.perm.Read ){ login_needed(g.anon.Read); return; }
if( robot_restrict("annotate") ) return;
fossil_nice_default();
zFilename = P("filename");
zRevision = PD("checkin",0);
zOrigin = P("origin");
zLimit = P("limit");
showLog = PB("log");
fileVers = PB("filevers");
ignoreWs = PB("w");
if( ignoreWs ) annFlags |= DIFF_IGNORE_ALLWS;
cgi_check_for_malice();
/* compute the annotation */
annotate_file(&ann, zFilename, zRevision, zLimit, zOrigin, annFlags);
zCI = ann.aVers[0].zMUuid;
/* generate the web page */
style_set_current_feature("annotate");
style_header("Annotation For %h", zFilename);
if( bBlame ){
url_initialize(&url, "blame");
}else{
url_initialize(&url, "annotate");
}
url_add_parameter(&url, "checkin", P("checkin"));
url_add_parameter(&url, "filename", zFilename);
if( zLimit ){
url_add_parameter(&url, "limit", zLimit);
}
url_add_parameter(&url, "w", ignoreWs ? "1" : "0");
url_add_parameter(&url, "log", showLog ? "1" : "0");
url_add_parameter(&url, "filevers", fileVers ? "1" : "0");
style_submenu_checkbox("w", "Ignore Whitespace", 0, 0);
style_submenu_checkbox("log", "Log", 0, "toggle_annotation_log");
style_submenu_checkbox("filevers", "Link to Files", 0, 0);
if( ann.bMoreToDo ){
style_submenu_element("All Ancestors", "%s",
url_render(&url, "limit", "none", 0, 0));
}
if( skin_detail_boolean("white-foreground") ){
clr1 = 0xa04040;
clr2 = 0x4059a0;
}else{
clr1 = 0xffb5b5; /* Recent changes: red (hot) */
clr2 = 0xb5e0ff; /* Older changes: blue (cold) */
}
for(p=ann.aVers, i=0; i<ann.nVers; i++, p++){
clr = gradient_color(clr1, clr2, ann.nVers-1, i);
ann.aVers[i].zBgColor = mprintf("#%06x", clr);
}
@ <div id="annotation_log" style='display:%s(showLog?"block":"none");'>
if( zOrigin ){
zLink = href("%R/finfo?name=%t&from=%!S&to=%!S",zFilename,zCI,zOrigin);
}else{
zLink = href("%R/finfo?name=%t&from=%!S",zFilename,zCI);
}
@ <h2>Versions of %z(zLink)%h(zFilename)</a> analyzed:</h2>
@ <ol>
for(p=ann.aVers, i=0; i<ann.nVers; i++, p++){
@ <li><span style='background-color:%s(p->zBgColor);'>%s(p->zDate)
@ check-in %z(href("%R/info/%!S",p->zMUuid))%S(p->zMUuid)</a>
@ artifact %z(href("%R/artifact/%!S",p->zFUuid))%S(p->zFUuid)</a>
@ </span>
}
@ </ol>
@ <hr>
@ </div>
if( !ann.bMoreToDo ){
assert( ann.origId==0 ); /* bMoreToDo always set for a point-to-point */
@ <h2>Origin for each line in
@ %z(href("%R/finfo?name=%h&from=%!S", zFilename, zCI))%h(zFilename)</a>
@ from check-in %z(href("%R/info/%!S",zCI))%S(zCI)</a>:</h2>
}else if( ann.origId>0 ){
@ <h2>Lines of
@ %z(href("%R/finfo?name=%h&from=%!S", zFilename, zCI))%h(zFilename)</a>
@ from check-in %z(href("%R/info/%!S",zCI))%S(zCI)</a>
@ that are changed by the sequence of edits moving toward
@ check-in %z(href("%R/info/%!S",zOrigin))%S(zOrigin)</a>:</h2>
}else{
@ <h2>Lines added by the %d(ann.nVers) most recent ancestors of
@ %z(href("%R/finfo?name=%h&from=%!S", zFilename, zCI))%h(zFilename)</a>
@ from check-in %z(href("%R/info/%!S",zCI))%S(zCI)</a>:</h2>
}
@ <pre>
szHash = 10;
for(i=0; i<ann.nOrig; i++){
int iVers = ann.aOrig[i].iVers;
char *z = (char*)ann.aOrig[i].z;
int n = ann.aOrig[i].n;
char zPrefix[300];
z[n] = 0;
if( iVers<0 && !ann.bMoreToDo ) iVers = ann.nVers-1;
if( bBlame ){
if( iVers>=0 ){
struct AnnVers *p = ann.aVers+iVers;
const char *zUuid = fileVers ? p->zFUuid : p->zMUuid;
char *zLink = xhref("target='infowindow'", "%R/info/%!S", zUuid);
sqlite3_snprintf(sizeof(zPrefix), zPrefix,
"<span style='background-color:%s'>"
"%s%.10s</a> %s</span> %13.13s:",
p->zBgColor, zLink, zUuid, p->zDate, p->zUser);
fossil_free(zLink);
}else{
sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%*s", szHash+26, "");
}
}else{
if( iVers>=0 ){
struct AnnVers *p = ann.aVers+iVers;
const char *zUuid = fileVers ? p->zFUuid : p->zMUuid;
char *zLink = xhref("target='infowindow'", "%R/info/%!S", zUuid);
sqlite3_snprintf(sizeof(zPrefix), zPrefix,
"<span style='background-color:%s'>"
"%s%.10s</a> %s</span> %4d:",
p->zBgColor, zLink, zUuid, p->zDate, i+1);
fossil_free(zLink);
}else{
sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%*s%4d:",szHash+12,"",i+1);
}
}
@ %s(zPrefix) %h(z)
}
@ </pre>
style_finish_page();
}
/*
** COMMAND: annotate
** COMMAND: blame
** COMMAND: praise*
**
** Usage: %fossil annotate|blame|praise ?OPTIONS? FILENAME
**
** Output the text of a file with markings to show when each line of the file
** was last modified. The version currently checked out is shown by default.
** Other versions may be specified using the -r option. The "annotate" command
** shows line numbers and omits the username. The "blame" and "praise" commands
** show the user who made each check-in.
**
** Reverse Annotations: Normally, these commands look at versions of
** FILENAME moving backwards in time back toward the root check-in, and
** thus the output shows the most recent change to each line. However,
** if the -o|--origin option is used to specify some future check-in
** (example: "-o trunk") then these commands show changes moving towards
** that alternative origin. Thus using "-o trunk" on an historical version
** of the file shows the first time each line in the file was changed or
** removed by any subsequent check-in.
**
** With -t or -T, the "blame" and "praise" commands show for each file the
** latest (relative to the revision given by -r) check-in that modified it and
** the check-in's author. If not given, the revision defaults to "current" for
** a check-out. Option -T additionally shows a comment snippet for the check-in.
**
** Options:
** --filevers Show file version numbers rather than
** check-in versions
** -r|--revision VERSION The specific check-in containing the file
** -l|--log List all versions analyzed
** -n|--limit LIMIT LIMIT can be one of:
** N Up to N versions
** Xs As much as possible in X seconds
** none No limit
** -o|--origin VERSION The origin check-in. By default this is the
** root of the repository. Set to the name of
** the main branch (usually "trunk") or
** similar for a reverse annotation.
** -t Show latest check-in and its author for each
** tracked file in the tree as of VERSION
** -T Like -t, plus comment snippet
** -w|--ignore-all-space Ignore white space when comparing lines
** -Z|--ignore-trailing-space Ignore whitespace at line end
**
** See also: [[info]], [[finfo]], [[timeline]]
*/
void annotate_cmd(void){
const char *zRevision; /* Revision name, or NULL for current check-in */
Annotator ann; /* The annotation of the file */
int i; /* Loop counter */
const char *zLimit; /* The value to the -n|--limit option */
const char *zOrig; /* The value for -o|--origin */
int showLog; /* True to show the log */
int fileVers; /* Show file version instead of check-in versions */
u64 annFlags = 0; /* Flags to control annotation properties */
int bBlame = 0; /* True for BLAME output. False for ANNOTATE. */
int bTreeInfo = 0; /* Show for the entire tree: 1=checkin, 2=with comment */
int szHash; /* Display size of a version hash */
Blob treename; /* Name of file to be annotated */
char *zFilename; /* Name of file to be annotated */
bBlame = g.argv[1][0]!='a';
if( find_option("t","t",0)!=0 ) bTreeInfo = 1;
if( find_option("T","T",0)!=0 ) bTreeInfo = 2;
zRevision = find_option("revision","r",1);
if( bBlame && bTreeInfo ){
if( find_repository_option()!=0 && zRevision==0 ){
fossil_fatal("the -r is required in addition to -R");
}
db_find_and_open_repository(0, 0);
if( zRevision==0 ) zRevision = "current";
ls_cmd_rev(zRevision,1,1,0,1,bTreeInfo,0,0);
return;
}
zLimit = find_option("limit","n",1);
zOrig = find_option("origin","o",1);
showLog = find_option("log","l",0)!=0;
if( find_option("ignore-trailing-space","Z",0)!=0 ){
annFlags = DIFF_IGNORE_EOLWS;
}
if( find_option("ignore-all-space","w",0)!=0 ){
annFlags = DIFF_IGNORE_ALLWS; /* stronger than DIFF_IGNORE_EOLWS */
}
fileVers = find_option("filevers",0,0)!=0;
db_must_be_within_tree();
/* We should be done with options.. */
verify_all_options();
if( g.argc<3 ) {
usage("FILENAME");
}
annFlags |= DIFF_STRIP_EOLCR;
file_tree_name(g.argv[2], &treename, 0, 1);
zFilename = blob_str(&treename);
annotate_file(&ann, zFilename, zRevision, zLimit, zOrig, annFlags);
if( showLog ){
struct AnnVers *p;
for(p=ann.aVers, i=0; i<ann.nVers; i++, p++){
fossil_print("version %3d: %s %S file %S\n",
i+1, p->zDate, p->zMUuid, p->zFUuid);
}
fossil_print("---------------------------------------------------\n");
}
szHash = length_of_S_display();
for(i=0; i<ann.nOrig; i++){
int iVers = ann.aOrig[i].iVers;
char *z = (char*)ann.aOrig[i].z;
int n = ann.aOrig[i].n;
struct AnnVers *p;
if( iVers<0 && !ann.bMoreToDo ) iVers = ann.nVers-1;
if( bBlame ){
if( iVers>=0 ){
p = ann.aVers + iVers;
fossil_print("%S %s %13.13s: %.*s\n",
fileVers ? p->zFUuid : p->zMUuid, p->zDate, p->zUser, n, z);
}else{
fossil_print("%*s %.*s\n", szHash+26, "", n, z);
}
}else{
if( iVers>=0 ){
p = ann.aVers + iVers;
fossil_print("%S %s %5d: %.*s\n",
fileVers ? p->zFUuid : p->zMUuid, p->zDate, i+1, n, z);
}else{
fossil_print("%*s %5d: %.*s\n", szHash+11, "", i+1, n, z);
}
}
}
}
|