Commit e68e33f33d98c171d31dac33257250b5ecded4c9

Vicent Martí 2013-02-27T14:50:32

Merge pull request #1233 from arrbee/file-similarity-metric Add file similarity scoring to diff rename/copy detection

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
diff --git a/include/git2/diff.h b/include/git2/diff.h
index 3a88902..ca34843 100644
--- a/include/git2/diff.h
+++ b/include/git2/diff.h
@@ -88,10 +88,9 @@ typedef enum {
 	GIT_DIFF_INCLUDE_UNTRACKED = (1 << 8),
 	/** Include unmodified files in the diff list */
 	GIT_DIFF_INCLUDE_UNMODIFIED = (1 << 9),
-	/** Even with the GIT_DIFF_INCLUDE_UNTRACKED flag, when an untracked
-	 *  directory is found, only a single entry for the directory is added
-	 *  to the diff list; with this flag, all files under the directory will
-	 *  be included, too.
+	/** Even with GIT_DIFF_INCLUDE_UNTRACKED, an entire untracked directory
+	 *  will be marked with only a single entry in the diff list; this flag
+	 *  adds all files under the directory as UNTRACKED entries, too.
 	 */
 	GIT_DIFF_RECURSE_UNTRACKED_DIRS = (1 << 10),
 	/** If the pathspec is set in the diff options, this flags means to
@@ -120,6 +119,11 @@ typedef enum {
 	GIT_DIFF_INCLUDE_TYPECHANGE_TREES  = (1 << 16),
 	/** Ignore file mode changes */
 	GIT_DIFF_IGNORE_FILEMODE = (1 << 17),
+	/** Even with GIT_DIFF_INCLUDE_IGNORED, an entire ignored directory
+	 *  will be marked with only a single entry in the diff list; this flag
+	 *  adds all files under the directory as IGNORED entries, too.
+	 */
+	GIT_DIFF_RECURSE_IGNORED_DIRS = (1 << 10),
 } git_diff_option_t;
 
 /**
@@ -133,20 +137,18 @@ typedef enum {
 typedef struct git_diff_list git_diff_list;
 
 /**
- * Flags for the file object on each side of a diff.
+ * Flags for the delta object and the file objects on each side.
  *
- * Note: most of these flags are just for **internal** consumption by
- * libgit2, but some of them may be interesting to external users.
+ * These flags are used for both the `flags` value of the `git_diff_delta`
+ * and the flags for the `git_diff_file` objects representing the old and
+ * new sides of the delta.  Values outside of this public range should be
+ * considered reserved for internal or future use.
  */
 typedef enum {
-	GIT_DIFF_FILE_VALID_OID  = (1 << 0), /** `oid` value is known correct */
-	GIT_DIFF_FILE_FREE_PATH  = (1 << 1), /** `path` is allocated memory */
-	GIT_DIFF_FILE_BINARY     = (1 << 2), /** should be considered binary data */
-	GIT_DIFF_FILE_NOT_BINARY = (1 << 3), /** should be considered text data */
-	GIT_DIFF_FILE_FREE_DATA  = (1 << 4), /** internal file data is allocated */
-	GIT_DIFF_FILE_UNMAP_DATA = (1 << 5), /** internal file data is mmap'ed */
-	GIT_DIFF_FILE_NO_DATA    = (1 << 6), /** file data should not be loaded */
-} git_diff_file_flag_t;
+	GIT_DIFF_FLAG_BINARY     = (1 << 0), /** file(s) treated as binary data */
+	GIT_DIFF_FLAG_NOT_BINARY = (1 << 1), /** file(s) treated as text data */
+	GIT_DIFF_FLAG_VALID_OID  = (1 << 2), /** `oid` value is known correct */
+} git_diff_flag_t;
 
 /**
  * What type of change is described by a git_diff_delta?
@@ -186,18 +188,17 @@ typedef enum {
  *
  * `size` is the size of the entry in bytes.
  *
- * `flags` is a combination of the `git_diff_file_flag_t` types, but those
- * are largely internal values.
+ * `flags` is a combination of the `git_diff_flag_t` types
  *
  * `mode` is, roughly, the stat() `st_mode` value for the item.  This will
  * be restricted to one of the `git_filemode_t` values.
  */
 typedef struct {
-	git_oid oid;
+	git_oid     oid;
 	const char *path;
-	git_off_t size;
-	unsigned int flags;
-	uint16_t mode;
+	git_off_t   size;
+	uint32_t    flags;
+	uint16_t    mode;
 } git_diff_file;
 
 /**
@@ -219,16 +220,17 @@ typedef struct {
  *
  * Under some circumstances, in the name of efficiency, not all fields will
  * be filled in, but we generally try to fill in as much as possible.  One
- * example is that the "binary" field will not examine file contents if you
- * do not pass in hunk and/or line callbacks to the diff foreach iteration
- * function.  It will just use the git attributes for those files.
+ * example is that the "flags" field may not have either the `BINARY` or the
+ * `NOT_BINARY` flag set to avoid examining file contents if you do not pass
+ * in hunk and/or line callbacks to the diff foreach iteration function.  It
+ * will just use the git attributes for those files.
  */
 typedef struct {
 	git_diff_file old_file;
 	git_diff_file new_file;
 	git_delta_t   status;
-	unsigned int  similarity; /**< for RENAMED and COPIED, value 0-100 */
-	int           binary;
+	uint32_t      similarity; /**< for RENAMED and COPIED, value 0-100 */
+	uint32_t      flags;
 } git_diff_delta;
 
 /**
@@ -377,7 +379,7 @@ typedef struct git_diff_patch git_diff_patch;
 typedef enum {
 	/** look for renames? (`--find-renames`) */
 	GIT_DIFF_FIND_RENAMES = (1 << 0),
-	/** consider old size of modified for renames? (`--break-rewrites=N`) */
+	/** consider old side of modified for renames? (`--break-rewrites=N`) */
 	GIT_DIFF_FIND_RENAMES_FROM_REWRITES = (1 << 1),
 
 	/** look for copies? (a la `--find-copies`) */
@@ -387,10 +389,49 @@ typedef enum {
 
 	/** split large rewrites into delete/add pairs (`--break-rewrites=/M`) */
 	GIT_DIFF_FIND_AND_BREAK_REWRITES = (1 << 4),
+
+	/** turn on all finding features */
+	GIT_DIFF_FIND_ALL = (0x1f),
+
+	/** measure similarity ignoring leading whitespace (default) */
+	GIT_DIFF_FIND_IGNORE_LEADING_WHITESPACE = 0,
+	/** measure similarity ignoring all whitespace */
+	GIT_DIFF_FIND_IGNORE_WHITESPACE = (1 << 6),
+	/** measure similarity including all data */
+	GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE = (1 << 7),
 } git_diff_find_t;
 
 /**
+ * Pluggable similarity metric
+ */
+typedef struct {
+	int (*file_signature)(
+		void **out, const git_diff_file *file,
+		const char *fullpath, void *payload);
+	int (*buffer_signature)(
+		void **out, const git_diff_file *file,
+		const char *buf, size_t buflen, void *payload);
+	void (*free_signature)(void *sig, void *payload);
+	int (*similarity)(int *score, void *siga, void *sigb, void *payload);
+	void *payload;
+} git_diff_similarity_metric;
+
+/**
  * Control behavior of rename and copy detection
+ *
+ * These options mostly mimic parameters that can be passed to git-diff.
+ *
+ * - `rename_threshold` is the same as the -M option with a value
+ * - `copy_threshold` is the same as the -C option with a value
+ * - `rename_from_rewrite_threshold` matches the top of the -B option
+ * - `break_rewrite_threshold` matches the bottom of the -B option
+ * - `target_limit` matches the -l option
+ *
+ * The `metric` option allows you to plug in a custom similarity metric.
+ * Set it to NULL for the default internal metric which is based on sampling
+ * hashes of ranges of data in the file.  The default metric is a pretty
+ * good similarity approximation that should work fairly well for both text
+ * and binary data, and is pretty fast with fixed memory overhead.
  */
 typedef struct {
 	unsigned int version;
@@ -411,6 +452,9 @@ typedef struct {
 	 *  the `diff.renameLimit` config) (default 200)
 	 */
 	unsigned int target_limit;
+
+	/** Pluggable similarity metric; pass NULL to use internal metric */
+	git_diff_similarity_metric *metric;
 } git_diff_find_options;
 
 #define GIT_DIFF_FIND_OPTIONS_VERSION 1
@@ -856,11 +900,12 @@ GIT_EXTERN(int) git_diff_patch_to_str(
  *
  * NULL is allowed for either `old_blob` or `new_blob` and will be treated
  * as an empty blob, with the `oid` set to NULL in the `git_diff_file` data.
+ * Passing NULL for both blobs is a noop; no callbacks will be made at all.
  *
- * We do run a binary content check on the two blobs and if either of the
- * blobs looks like binary data, the `git_diff_delta` binary attribute will
- * be set to 1 and no call to the hunk_cb nor line_cb will be made (unless
- * you pass `GIT_DIFF_FORCE_TEXT` of course).
+ * We do run a binary content check on the blob content and if either blob
+ * looks like binary data, the `git_diff_delta` binary attribute will be set
+ * to 1 and no call to the hunk_cb nor line_cb will be made (unless you pass
+ * `GIT_DIFF_FORCE_TEXT` of course).
  *
  * @return 0 on success, GIT_EUSER on non-zero callback, or error code
  */
@@ -880,6 +925,11 @@ GIT_EXTERN(int) git_diff_blobs(
  * so the `git_diff_file` parameters to the callbacks will be faked a la the
  * rules for `git_diff_blobs()`.
  *
+ * Passing NULL for `old_blob` will be treated as an empty blob (i.e. the
+ * `file_cb` will be invoked with GIT_DELTA_ADDED and the diff will be the
+ * entire content of the buffer added).  Passing NULL to the buffer will do
+ * the reverse, with GIT_DELTA_REMOVED and blob content removed.
+ *
  * @return 0 on success, GIT_EUSER on non-zero callback, or error code
  */
 GIT_EXTERN(int) git_diff_blob_to_buffer(
diff --git a/src/checkout.c b/src/checkout.c
index eda3e0b..040ead2 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -78,7 +78,7 @@ static int checkout_notify(
 		git_oid_cpy(&wdfile.oid, &wditem->oid);
 		wdfile.path = wditem->path;
 		wdfile.size = wditem->file_size;
-		wdfile.flags = GIT_DIFF_FILE_VALID_OID;
+		wdfile.flags = GIT_DIFF_FLAG_VALID_OID;
 		wdfile.mode = wditem->mode;
 
 		workdir = &wdfile;
diff --git a/src/diff.c b/src/diff.c
index d9bc32a..0861b13 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -92,11 +92,11 @@ static int diff_delta__from_one(
 		git_oid_cpy(&delta->new_file.oid, &entry->oid);
 	}
 
-	delta->old_file.flags |= GIT_DIFF_FILE_VALID_OID;
+	delta->old_file.flags |= GIT_DIFF_FLAG_VALID_OID;
 
 	if (delta->status == GIT_DELTA_DELETED ||
 		!git_oid_iszero(&delta->new_file.oid))
-		delta->new_file.flags |= GIT_DIFF_FILE_VALID_OID;
+		delta->new_file.flags |= GIT_DIFF_FLAG_VALID_OID;
 
 	notify_res = diff_notify(diff, delta, matched_pathspec);
 
@@ -142,7 +142,7 @@ static int diff_delta__from_two(
 	git_oid_cpy(&delta->old_file.oid, &old_entry->oid);
 	delta->old_file.size = old_entry->file_size;
 	delta->old_file.mode = old_mode;
-	delta->old_file.flags |= GIT_DIFF_FILE_VALID_OID;
+	delta->old_file.flags |= GIT_DIFF_FLAG_VALID_OID;
 
 	git_oid_cpy(&delta->new_file.oid, &new_entry->oid);
 	delta->new_file.size = new_entry->file_size;
@@ -156,7 +156,7 @@ static int diff_delta__from_two(
 	}
 
 	if (new_oid || !git_oid_iszero(&new_entry->oid))
-		delta->new_file.flags |= GIT_DIFF_FILE_VALID_OID;
+		delta->new_file.flags |= GIT_DIFF_FLAG_VALID_OID;
 
 	notify_res = diff_notify(diff, delta, matched_pathspec);
 
diff --git a/src/diff.h b/src/diff.h
index 16fbf71..8e3cbcd 100644
--- a/src/diff.h
+++ b/src/diff.h
@@ -28,8 +28,14 @@ enum {
 	GIT_DIFFCAPS_USE_DEV          = (1 << 4), /* use st_dev? */
 };
 
-#define GIT_DELTA__TO_DELETE 10
-#define GIT_DELTA__TO_SPLIT  11
+enum {
+	GIT_DIFF_FLAG__FREE_PATH  = (1 << 7),  /* `path` is allocated memory */
+	GIT_DIFF_FLAG__FREE_DATA  = (1 << 8),  /* internal file data is allocated */
+	GIT_DIFF_FLAG__UNMAP_DATA = (1 << 9),  /* internal file data is mmap'ed */
+	GIT_DIFF_FLAG__NO_DATA    = (1 << 10), /* file data should not be loaded */
+	GIT_DIFF_FLAG__TO_DELETE  = (1 << 11), /* delete entry during rename det. */
+	GIT_DIFF_FLAG__TO_SPLIT   = (1 << 12), /* split entry during rename det. */
+};
 
 struct git_diff_list {
 	git_refcount     rc;
diff --git a/src/diff_output.c b/src/diff_output.c
index 88ccc9d..13434be 100644
--- a/src/diff_output.c
+++ b/src/diff_output.c
@@ -52,8 +52,8 @@ static int parse_hunk_header(git_diff_range *range, const char *header)
 	return 0;
 }
 
-#define KNOWN_BINARY_FLAGS (GIT_DIFF_FILE_BINARY|GIT_DIFF_FILE_NOT_BINARY)
-#define NOT_BINARY_FLAGS   (GIT_DIFF_FILE_NOT_BINARY|GIT_DIFF_FILE_NO_DATA)
+#define KNOWN_BINARY_FLAGS (GIT_DIFF_FLAG_BINARY|GIT_DIFF_FLAG_NOT_BINARY)
+#define NOT_BINARY_FLAGS   (GIT_DIFF_FLAG_NOT_BINARY|GIT_DIFF_FLAG__NO_DATA)
 
 static int update_file_is_binary_by_attr(
 	git_repository *repo, git_diff_file *file)
@@ -68,9 +68,9 @@ static int update_file_is_binary_by_attr(
 		return -1;
 
 	if (GIT_ATTR_FALSE(value))
-		file->flags |= GIT_DIFF_FILE_BINARY;
+		file->flags |= GIT_DIFF_FLAG_BINARY;
 	else if (GIT_ATTR_TRUE(value))
-		file->flags |= GIT_DIFF_FILE_NOT_BINARY;
+		file->flags |= GIT_DIFF_FLAG_NOT_BINARY;
 	/* otherwise leave file->flags alone */
 
 	return 0;
@@ -78,15 +78,15 @@ static int update_file_is_binary_by_attr(
 
 static void update_delta_is_binary(git_diff_delta *delta)
 {
-	if ((delta->old_file.flags & GIT_DIFF_FILE_BINARY) != 0 ||
-		(delta->new_file.flags & GIT_DIFF_FILE_BINARY) != 0)
-		delta->binary = 1;
+	if ((delta->old_file.flags & GIT_DIFF_FLAG_BINARY) != 0 ||
+		(delta->new_file.flags & GIT_DIFF_FLAG_BINARY) != 0)
+		delta->flags |= GIT_DIFF_FLAG_BINARY;
 
 	else if ((delta->old_file.flags & NOT_BINARY_FLAGS) != 0 &&
 			 (delta->new_file.flags & NOT_BINARY_FLAGS) != 0)
-		delta->binary = 0;
+		delta->flags |= GIT_DIFF_FLAG_NOT_BINARY;
 
-	/* otherwise leave delta->binary value untouched */
+	/* otherwise leave delta->flags binary value untouched */
 }
 
 /* returns if we forced binary setting (and no further checks needed) */
@@ -95,24 +95,24 @@ static bool diff_delta_is_binary_forced(
 	git_diff_delta *delta)
 {
 	/* return true if binary-ness has already been settled */
-	if (delta->binary != -1)
+	if ((delta->flags & KNOWN_BINARY_FLAGS) != 0)
 		return true;
 
 	/* make sure files are conceivably mmap-able */
 	if ((git_off_t)((size_t)delta->old_file.size) != delta->old_file.size ||
 		(git_off_t)((size_t)delta->new_file.size) != delta->new_file.size)
 	{
-		delta->old_file.flags |= GIT_DIFF_FILE_BINARY;
-		delta->new_file.flags |= GIT_DIFF_FILE_BINARY;
-		delta->binary = 1;
+		delta->old_file.flags |= GIT_DIFF_FLAG_BINARY;
+		delta->new_file.flags |= GIT_DIFF_FLAG_BINARY;
+		delta->flags |= GIT_DIFF_FLAG_BINARY;
 		return true;
 	}
 
 	/* check if user is forcing us to text diff these files */
 	if (ctxt->opts && (ctxt->opts->flags & GIT_DIFF_FORCE_TEXT) != 0) {
-		delta->old_file.flags |= GIT_DIFF_FILE_NOT_BINARY;
-		delta->new_file.flags |= GIT_DIFF_FILE_NOT_BINARY;
-		delta->binary = 0;
+		delta->old_file.flags |= GIT_DIFF_FLAG_NOT_BINARY;
+		delta->new_file.flags |= GIT_DIFF_FLAG_NOT_BINARY;
+		delta->flags |= GIT_DIFF_FLAG_NOT_BINARY;
 		return true;
 	}
 
@@ -125,8 +125,6 @@ static int diff_delta_is_binary_by_attr(
 	int error = 0, mirror_new;
 	git_diff_delta *delta = patch->delta;
 
-	delta->binary = -1;
-
 	if (diff_delta_is_binary_forced(ctxt, delta))
 		return 0;
 
@@ -152,23 +150,21 @@ static int diff_delta_is_binary_by_content(
 	git_diff_file *file,
 	const git_map *map)
 {
+	const git_buf search = { map->data, 0, min(map->len, 4000) };
+
 	if (diff_delta_is_binary_forced(ctxt, delta))
 		return 0;
 
-	if ((file->flags & KNOWN_BINARY_FLAGS) == 0) {
-		const git_buf search = { map->data, 0, min(map->len, 4000) };
-
-		/* TODO: provide encoding / binary detection callbacks that can
-		 * be UTF-8 aware, etc.  For now, instead of trying to be smart,
-		 * let's just use the simple NUL-byte detection that core git uses.
-		 */
+	/* TODO: provide encoding / binary detection callbacks that can
+	 * be UTF-8 aware, etc.  For now, instead of trying to be smart,
+	 * let's just use the simple NUL-byte detection that core git uses.
+	 */
 
-		/* previously was: if (git_buf_text_is_binary(&search)) */
-		if (git_buf_text_contains_nul(&search))
-			file->flags |= GIT_DIFF_FILE_BINARY;
-		else
-			file->flags |= GIT_DIFF_FILE_NOT_BINARY;
-	}
+	/* previously was: if (git_buf_text_is_binary(&search)) */
+	if (git_buf_text_contains_nul(&search))
+		file->flags |= GIT_DIFF_FLAG_BINARY;
+	else
+		file->flags |= GIT_DIFF_FLAG_NOT_BINARY;
 
 	update_delta_is_binary(delta);
 
@@ -192,7 +188,7 @@ static int diff_delta_is_binary_by_size(
 	}
 
 	if (file->size > threshold)
-		file->flags |= GIT_DIFF_FILE_BINARY;
+		file->flags |= GIT_DIFF_FLAG_BINARY;
 
 	update_delta_is_binary(delta);
 
@@ -247,7 +243,7 @@ static int get_blob_content(
 		map->data = git_buf_detach(&content);
 		map->len = strlen(map->data);
 
-		file->flags |= GIT_DIFF_FILE_FREE_DATA;
+		file->flags |= GIT_DIFF_FLAG__FREE_DATA;
 		return 0;
 	}
 
@@ -270,7 +266,7 @@ static int get_blob_content(
 	/* if blob is too large to diff, mark as binary */
 	if ((error = diff_delta_is_binary_by_size(ctxt, delta, file)) < 0)
 		return error;
-	if (delta->binary == 1)
+	if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
 		return 0;
 
 	if (odb_obj != NULL) {
@@ -306,14 +302,14 @@ static int get_workdir_sm_content(
 		return error;
 
 	/* update OID if we didn't have it previously */
-	if ((file->flags & GIT_DIFF_FILE_VALID_OID) == 0) {
+	if ((file->flags & GIT_DIFF_FLAG_VALID_OID) == 0) {
 		const git_oid* sm_head;
 
 		if ((sm_head = git_submodule_wd_id(sm)) != NULL ||
 			(sm_head = git_submodule_head_id(sm)) != NULL)
 		{
 			git_oid_cpy(&file->oid, sm_head);
-			file->flags |= GIT_DIFF_FILE_VALID_OID;
+			file->flags |= GIT_DIFF_FLAG_VALID_OID;
 		}
 	}
 
@@ -329,7 +325,7 @@ static int get_workdir_sm_content(
 	map->data = git_buf_detach(&content);
 	map->len = strlen(map->data);
 
-	file->flags |= GIT_DIFF_FILE_FREE_DATA;
+	file->flags |= GIT_DIFF_FLAG__FREE_DATA;
 
 	return 0;
 }
@@ -356,8 +352,8 @@ static int get_workdir_content(
 	if (S_ISLNK(file->mode)) {
 		ssize_t alloc_len, read_len;
 
-		file->flags |= GIT_DIFF_FILE_FREE_DATA;
-		file->flags |= GIT_DIFF_FILE_BINARY;
+		file->flags |= GIT_DIFF_FLAG__FREE_DATA;
+		file->flags |= GIT_DIFF_FLAG_BINARY;
 
 		/* link path on disk could be UTF-16, so prepare a buffer that is
 		 * big enough to handle some UTF-8 data expansion
@@ -389,7 +385,7 @@ static int get_workdir_content(
 			file->size = git_futils_filesize(fd);
 
 		if ((error = diff_delta_is_binary_by_size(ctxt, delta, file)) < 0 ||
-			delta->binary == 1)
+			(delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
 			goto close_and_cleanup;
 
 		error = git_filters_load(
@@ -402,7 +398,7 @@ static int get_workdir_content(
 				goto close_and_cleanup;
 
 			error = git_futils_mmap_ro(map, fd, 0, (size_t)file->size);
-			file->flags |= GIT_DIFF_FILE_UNMAP_DATA;
+			file->flags |= GIT_DIFF_FLAG__UNMAP_DATA;
 		} else {
 			git_buf raw = GIT_BUF_INIT, filtered = GIT_BUF_INIT;
 
@@ -412,7 +408,7 @@ static int get_workdir_content(
 				map->len  = git_buf_len(&filtered);
 				map->data = git_buf_detach(&filtered);
 
-				file->flags |= GIT_DIFF_FILE_FREE_DATA;
+				file->flags |= GIT_DIFF_FLAG__FREE_DATA;
 			}
 
 			git_buf_free(&raw);
@@ -425,11 +421,11 @@ close_and_cleanup:
 	}
 
 	/* once data is loaded, update OID if we didn't have it previously */
-	if (!error && (file->flags & GIT_DIFF_FILE_VALID_OID) == 0) {
+	if (!error && (file->flags & GIT_DIFF_FLAG_VALID_OID) == 0) {
 		error = git_odb_hash(
 			&file->oid, map->data, map->len, GIT_OBJ_BLOB);
 		if (!error)
-			file->flags |= GIT_DIFF_FILE_VALID_OID;
+			file->flags |= GIT_DIFF_FLAG_VALID_OID;
 	}
 
 	if (!error)
@@ -445,22 +441,22 @@ static void release_content(git_diff_file *file, git_map *map, git_blob *blob)
 	if (blob != NULL)
 		git_blob_free(blob);
 
-	if (file->flags & GIT_DIFF_FILE_FREE_DATA) {
+	if (file->flags & GIT_DIFF_FLAG__FREE_DATA) {
 		git__free(map->data);
 		map->data = "";
 		map->len  = 0;
-		file->flags &= ~GIT_DIFF_FILE_FREE_DATA;
+		file->flags &= ~GIT_DIFF_FLAG__FREE_DATA;
 	}
-	else if (file->flags & GIT_DIFF_FILE_UNMAP_DATA) {
+	else if (file->flags & GIT_DIFF_FLAG__UNMAP_DATA) {
 		git_futils_mmap_free(map);
 		map->data = "";
 		map->len  = 0;
-		file->flags &= ~GIT_DIFF_FILE_UNMAP_DATA;
+		file->flags &= ~GIT_DIFF_FLAG__UNMAP_DATA;
 	}
 }
 
 
-static void diff_context_init(
+static int diff_context_init(
 	diff_context *ctxt,
 	git_diff_list *diff,
 	git_repository *repo,
@@ -472,6 +468,12 @@ static void diff_context_init(
 {
 	memset(ctxt, 0, sizeof(diff_context));
 
+	if (!repo && diff)
+		repo = diff->repo;
+
+	if (!opts && diff)
+		opts = &diff->opts;
+
 	ctxt->repo = repo;
 	ctxt->diff = diff;
 	ctxt->opts = opts;
@@ -482,6 +484,8 @@ static void diff_context_init(
 	ctxt->error = 0;
 
 	setup_xdiff_options(ctxt->opts, &ctxt->xdiff_config, &ctxt->xdiff_params);
+
+	return 0;
 }
 
 static int diff_delta_file_callback(
@@ -555,7 +559,7 @@ static int diff_patch_load(
 	patch->new_data.len  = 0;
 	patch->new_blob      = NULL;
 
-	if (delta->binary == 1)
+	if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
 		goto cleanup;
 
 	if (!ctxt->hunk_cb &&
@@ -565,25 +569,25 @@ static int diff_patch_load(
 
 	switch (delta->status) {
 	case GIT_DELTA_ADDED:
-		delta->old_file.flags |= GIT_DIFF_FILE_NO_DATA;
+		delta->old_file.flags |= GIT_DIFF_FLAG__NO_DATA;
 		break;
 	case GIT_DELTA_DELETED:
-		delta->new_file.flags |= GIT_DIFF_FILE_NO_DATA;
+		delta->new_file.flags |= GIT_DIFF_FLAG__NO_DATA;
 		break;
 	case GIT_DELTA_MODIFIED:
 		break;
 	case GIT_DELTA_UNTRACKED:
-		delta->old_file.flags |= GIT_DIFF_FILE_NO_DATA;
+		delta->old_file.flags |= GIT_DIFF_FLAG__NO_DATA;
 		if ((ctxt->opts->flags & GIT_DIFF_INCLUDE_UNTRACKED_CONTENT) == 0)
-			delta->new_file.flags |= GIT_DIFF_FILE_NO_DATA;
+			delta->new_file.flags |= GIT_DIFF_FLAG__NO_DATA;
 		break;
 	default:
-		delta->new_file.flags |= GIT_DIFF_FILE_NO_DATA;
-		delta->old_file.flags |= GIT_DIFF_FILE_NO_DATA;
+		delta->new_file.flags |= GIT_DIFF_FLAG__NO_DATA;
+		delta->old_file.flags |= GIT_DIFF_FLAG__NO_DATA;
 		break;
 	}
 
-#define CHECK_UNMODIFIED (GIT_DIFF_FILE_NO_DATA | GIT_DIFF_FILE_VALID_OID)
+#define CHECK_UNMODIFIED (GIT_DIFF_FLAG__NO_DATA | GIT_DIFF_FLAG_VALID_OID)
 
 	check_if_unmodified =
 		(delta->old_file.flags & CHECK_UNMODIFIED) == 0 &&
@@ -594,41 +598,41 @@ static int diff_patch_load(
 	 * memory footprint during diff.
 	 */
 
-	if ((delta->old_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
+	if ((delta->old_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0 &&
 		patch->old_src == GIT_ITERATOR_TYPE_WORKDIR) {
 		if ((error = get_workdir_content(
 				ctxt, delta, &delta->old_file, &patch->old_data)) < 0)
 			goto cleanup;
-		if (delta->binary == 1)
+		if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
 			goto cleanup;
 	}
 
-	if ((delta->new_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
+	if ((delta->new_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0 &&
 		patch->new_src == GIT_ITERATOR_TYPE_WORKDIR) {
 		if ((error = get_workdir_content(
 				ctxt, delta, &delta->new_file, &patch->new_data)) < 0)
 			goto cleanup;
-		if (delta->binary == 1)
+		if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
 			goto cleanup;
 	}
 
-	if ((delta->old_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
+	if ((delta->old_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0 &&
 		patch->old_src != GIT_ITERATOR_TYPE_WORKDIR) {
 		if ((error = get_blob_content(
 				ctxt, delta, &delta->old_file,
 				&patch->old_data, &patch->old_blob)) < 0)
 			goto cleanup;
-		if (delta->binary == 1)
+		if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
 			goto cleanup;
 	}
 
-	if ((delta->new_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
+	if ((delta->new_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0 &&
 		patch->new_src != GIT_ITERATOR_TYPE_WORKDIR) {
 		if ((error = get_blob_content(
 				ctxt, delta, &delta->new_file,
 				&patch->new_data, &patch->new_blob)) < 0)
 			goto cleanup;
-		if (delta->binary == 1)
+		if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
 			goto cleanup;
 	}
 
@@ -646,13 +650,13 @@ static int diff_patch_load(
 	}
 
 cleanup:
-	if (delta->binary == -1)
+	if ((delta->flags & KNOWN_BINARY_FLAGS) == 0)
 		update_delta_is_binary(delta);
 
 	if (!error) {
 		patch->flags |= GIT_DIFF_PATCH_LOADED;
 
-		if (delta->binary != 1 &&
+		if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0 &&
 			delta->status != GIT_DELTA_UNMODIFIED &&
 			(patch->old_data.len || patch->new_data.len) &&
 			!git_oid_equal(&delta->old_file.oid, &delta->new_file.oid))
@@ -926,6 +930,15 @@ static int diff_patch_line_cb(
 	return 0;
 }
 
+static int diff_required(git_diff_list *diff, const char *action)
+{
+	if (!diff) {
+		giterr_set(GITERR_INVALID, "Must provide valid diff to %s", action);
+		return -1;
+	}
+
+	return 0;
+}
 
 int git_diff_foreach(
 	git_diff_list *diff,
@@ -939,9 +952,12 @@ int git_diff_foreach(
 	size_t idx;
 	git_diff_patch patch;
 
-	diff_context_init(
-		&ctxt, diff, diff->repo, &diff->opts,
-		file_cb, hunk_cb, data_cb, payload);
+	if (diff_required(diff, "git_diff_foreach") < 0)
+		return -1;
+
+	if (diff_context_init(
+			&ctxt, diff, NULL, NULL, file_cb, hunk_cb, data_cb, payload) < 0)
+		return -1;
 
 	diff_patch_init(&ctxt, &patch);
 
@@ -1138,7 +1154,7 @@ static int print_patch_file(
 		newpath = "/dev/null";
 	}
 
-	if (delta->binary != 1) {
+	if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0) {
 		git_buf_printf(pi->buf, "--- %s%s\n", oldpfx, oldpath);
 		git_buf_printf(pi->buf, "+++ %s%s\n", newpfx, newpath);
 	}
@@ -1153,7 +1169,7 @@ static int print_patch_file(
 		return GIT_EUSER;
 	}
 
-	if (delta->binary != 1)
+	if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0)
 		return 0;
 
 	git_buf_clear(pi->buf);
@@ -1268,7 +1284,7 @@ static void set_data_from_blob(
 		map->data  = (char *)git_blob_rawcontent(blob);
 	} else {
 		file->size = 0;
-		file->flags |= GIT_DIFF_FILE_NO_DATA;
+		file->flags |= GIT_DIFF_FLAG__NO_DATA;
 
 		map->len   = 0;
 		map->data  = "";
@@ -1283,7 +1299,7 @@ static void set_data_from_buffer(
 	map->len   = buffer_len;
 
 	if (!buffer) {
-		file->flags |= GIT_DIFF_FILE_NO_DATA;
+		file->flags |= GIT_DIFF_FLAG__NO_DATA;
 		map->data = NULL;
 	} else {
 		map->data = (char *)buffer;
@@ -1310,8 +1326,10 @@ static int diff_single_init(
 
 	memset(data, 0, sizeof(*data));
 
-	diff_context_init(
-		&data->ctxt, NULL, repo, opts, file_cb, hunk_cb, data_cb, payload);
+	if (diff_context_init(
+			&data->ctxt, NULL, repo, opts,
+			file_cb, hunk_cb, data_cb, payload) < 0)
+		return -1;
 
 	diff_patch_init(&data->ctxt, &data->patch);
 
@@ -1322,13 +1340,13 @@ static int diff_single_apply(diff_single_data *data)
 {
 	int error;
 	git_diff_delta *delta = &data->delta;
-	bool has_old = ((delta->old_file.flags & GIT_DIFF_FILE_NO_DATA) == 0);
-	bool has_new = ((delta->new_file.flags & GIT_DIFF_FILE_NO_DATA) == 0);
+	bool has_old = ((delta->old_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
+	bool has_new = ((delta->new_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
 
 	/* finish setting up fake git_diff_delta record and loaded data */
 
 	data->patch.delta = delta;
-	delta->binary = -1;
+	delta->flags = delta->flags & ~KNOWN_BINARY_FLAGS;
 
 	delta->status = has_new ?
 		(has_old ? GIT_DELTA_MODIFIED : GIT_DELTA_ADDED) :
@@ -1345,7 +1363,8 @@ static int diff_single_apply(diff_single_data *data)
 
 	data->patch.flags |= GIT_DIFF_PATCH_LOADED;
 
-	if (delta->binary != 1 && delta->status != GIT_DELTA_UNMODIFIED)
+	if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0 &&
+		delta->status != GIT_DELTA_UNMODIFIED)
 		data->patch.flags |= GIT_DIFF_PATCH_DIFFABLE;
 
 	/* do diffs */
@@ -1377,6 +1396,9 @@ int git_diff_blobs(
 		new_blob ? git_object_owner((const git_object *)new_blob) :
 		old_blob ? git_object_owner((const git_object *)old_blob) : NULL;
 
+	if (!repo) /* Hmm, given two NULL blobs, silently do no callbacks? */
+		return 0;
+
 	if ((error = diff_single_init(
 			&d, repo, options, file_cb, hunk_cb, data_cb, payload)) < 0)
 		return error;
@@ -1408,6 +1430,9 @@ int git_diff_blob_to_buffer(
 	git_repository *repo =
 		old_blob ? git_object_owner((const git_object *)old_blob) : NULL;
 
+	if (!repo && !buf) /* Hmm, given NULLs, silently do no callbacks? */
+		return 0;
+
 	if ((error = diff_single_init(
 			&d, repo, options, file_cb, hunk_cb, data_cb, payload)) < 0)
 		return error;
@@ -1456,11 +1481,19 @@ int git_diff_get_patch(
 
 	if (patch_ptr)
 		*patch_ptr = NULL;
+	if (delta_ptr)
+		*delta_ptr = NULL;
+
+	if (diff_required(diff, "git_diff_get_patch") < 0)
+		return -1;
+
+	if (diff_context_init(
+			&ctxt, diff, NULL, NULL,
+			NULL, diff_patch_hunk_cb, diff_patch_line_cb, NULL) < 0)
+		return -1;
 
 	delta = git_vector_get(&diff->deltas, idx);
 	if (!delta) {
-		if (delta_ptr)
-			*delta_ptr = NULL;
 		giterr_set(GITERR_INVALID, "Index out of range for delta in diff");
 		return GIT_ENOTFOUND;
 	}
@@ -1469,14 +1502,10 @@ int git_diff_get_patch(
 		*delta_ptr = delta;
 
 	if (!patch_ptr &&
-		(delta->binary != -1 ||
+		((delta->flags & KNOWN_BINARY_FLAGS) != 0 ||
 		 (diff->opts.flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0))
 		return 0;
 
-	diff_context_init(
-		&ctxt, diff, diff->repo, &diff->opts,
-		NULL, diff_patch_hunk_cb, diff_patch_line_cb, NULL);
-
 	if (git_diff_delta__should_skip(ctxt.opts, delta))
 		return 0;
 
diff --git a/src/diff_tform.c b/src/diff_tform.c
index 2c2e1fb..958d2bf 100644
--- a/src/diff_tform.c
+++ b/src/diff_tform.c
@@ -7,6 +7,8 @@
 #include "common.h"
 #include "diff.h"
 #include "git2/config.h"
+#include "git2/blob.h"
+#include "hashsig.h"
 
 static git_diff_delta *diff_delta__dup(
 	const git_diff_delta *d, git_pool *pool)
@@ -168,6 +170,36 @@ int git_diff_merge(
 	return error;
 }
 
+static int find_similar__hashsig_for_file(
+	void **out, const git_diff_file *f, const char *path, void *p)
+{
+	git_hashsig_option_t opt = (git_hashsig_option_t)p;
+	GIT_UNUSED(f);
+	return git_hashsig_create_fromfile((git_hashsig **)out, path, opt);
+}
+
+static int find_similar__hashsig_for_buf(
+	void **out, const git_diff_file *f, const char *buf, size_t len, void *p)
+{
+	git_hashsig_option_t opt = (git_hashsig_option_t)p;
+	GIT_UNUSED(f);
+	return git_hashsig_create((git_hashsig **)out, buf, len, opt);
+}
+
+static void find_similar__hashsig_free(void *sig, void *payload)
+{
+	GIT_UNUSED(payload);
+	git_hashsig_free(sig);
+}
+
+static int find_similar__calc_similarity(
+	int *score, void *siga, void *sigb, void *payload)
+{
+	GIT_UNUSED(payload);
+	*score = git_hashsig_compare(siga, sigb);
+	return 0;
+}
+
 #define DEFAULT_THRESHOLD 50
 #define DEFAULT_BREAK_REWRITE_THRESHOLD 60
 #define DEFAULT_TARGET_LIMIT 200
@@ -178,7 +210,6 @@ static int normalize_find_opts(
 	git_diff_find_options *given)
 {
 	git_config *cfg = NULL;
-	const char *val;
 
 	if (diff->repo != NULL &&
 		git_repository_config__weakptr(&cfg, diff->repo) < 0)
@@ -187,8 +218,9 @@ static int normalize_find_opts(
 	if (given != NULL)
 		memcpy(opts, given, sizeof(*opts));
 	else {
-		git_diff_find_options init = GIT_DIFF_FIND_OPTIONS_INIT;
-		memmove(opts, &init, sizeof(init));
+		const char *val = NULL;
+
+		GIT_INIT_STRUCTURE(opts, GIT_DIFF_FIND_OPTIONS_VERSION);
 
 		opts->flags = GIT_DIFF_FIND_RENAMES;
 
@@ -236,6 +268,24 @@ static int normalize_find_opts(
 			opts->target_limit = limit;
 	}
 
+	/* assign the internal metric with whitespace flag as payload */
+	if (!opts->metric) {
+		opts->metric = git__malloc(sizeof(git_diff_similarity_metric));
+		GITERR_CHECK_ALLOC(opts->metric);
+
+		opts->metric->file_signature = find_similar__hashsig_for_file;
+		opts->metric->buffer_signature = find_similar__hashsig_for_buf;
+		opts->metric->free_signature = find_similar__hashsig_free;
+		opts->metric->similarity = find_similar__calc_similarity;
+
+		if (opts->flags & GIT_DIFF_FIND_IGNORE_WHITESPACE)
+			opts->metric->payload = (void *)GIT_HASHSIG_IGNORE_WHITESPACE;
+		else if (opts->flags & GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE)
+			opts->metric->payload = (void *)GIT_HASHSIG_NORMAL;
+		else
+			opts->metric->payload = (void *)GIT_HASHSIG_SMART_WHITESPACE;
+	}
+
 	return 0;
 }
 
@@ -250,10 +300,10 @@ static int apply_splits_and_deletes(git_diff_list *diff, size_t expected_size)
 
 	/* build new delta list without TO_DELETE and splitting TO_SPLIT */
 	git_vector_foreach(&diff->deltas, i, delta) {
-		if (delta->status == GIT_DELTA__TO_DELETE)
+		if ((delta->flags & GIT_DIFF_FLAG__TO_DELETE) != 0)
 			continue;
 
-		if (delta->status == GIT_DELTA__TO_SPLIT) {
+		if ((delta->flags & GIT_DIFF_FLAG__TO_SPLIT) != 0) {
 			git_diff_delta *deleted = diff_delta__dup(delta, &diff->pool);
 			if (!deleted)
 				goto on_error;
@@ -261,7 +311,7 @@ static int apply_splits_and_deletes(git_diff_list *diff, size_t expected_size)
 			deleted->status = GIT_DELTA_DELETED;
 			memset(&deleted->new_file, 0, sizeof(deleted->new_file));
 			deleted->new_file.path = deleted->old_file.path;
-			deleted->new_file.flags |= GIT_DIFF_FILE_VALID_OID;
+			deleted->new_file.flags |= GIT_DIFF_FLAG_VALID_OID;
 
 			if (git_vector_insert(&onto, deleted) < 0)
 				goto on_error;
@@ -269,7 +319,7 @@ static int apply_splits_and_deletes(git_diff_list *diff, size_t expected_size)
 			delta->status = GIT_DELTA_ADDED;
 			memset(&delta->old_file, 0, sizeof(delta->old_file));
 			delta->old_file.path = delta->new_file.path;
-			delta->old_file.flags |= GIT_DIFF_FILE_VALID_OID;
+			delta->old_file.flags |= GIT_DIFF_FLAG_VALID_OID;
 		}
 
 		if (git_vector_insert(&onto, delta) < 0)
@@ -278,7 +328,7 @@ static int apply_splits_and_deletes(git_diff_list *diff, size_t expected_size)
 
 	/* cannot return an error past this point */
 	git_vector_foreach(&diff->deltas, i, delta)
-		if (delta->status == GIT_DELTA__TO_DELETE)
+		if ((delta->flags & GIT_DIFF_FLAG__TO_DELETE) != 0)
 			git__free(delta);
 
 	/* swap new delta list into place */
@@ -297,17 +347,86 @@ on_error:
 	return -1;
 }
 
-static unsigned int calc_similarity(
-	void *cache, git_diff_file *old_file, git_diff_file *new_file)
+GIT_INLINE(git_diff_file *) similarity_get_file(git_diff_list *diff, size_t idx)
+{
+	git_diff_delta *delta = git_vector_get(&diff->deltas, idx / 2);
+	return (idx & 1) ? &delta->new_file : &delta->old_file;
+}
+
+static int similarity_calc(
+	git_diff_list *diff,
+	git_diff_find_options *opts,
+	size_t file_idx,
+	void **cache)
 {
-	GIT_UNUSED(cache);
+	int error = 0;
+	git_diff_file *file = similarity_get_file(diff, file_idx);
+	git_iterator_type_t src = (file_idx & 1) ? diff->old_src : diff->new_src;
+
+	if (src == GIT_ITERATOR_TYPE_WORKDIR) { /* compute hashsig from file */
+		git_buf path = GIT_BUF_INIT;
+
+		/* TODO: apply wd-to-odb filters to file data if necessary */
 
-	if (git_oid_cmp(&old_file->oid, &new_file->oid) == 0)
+		if (!(error = git_buf_joinpath(
+				&path, git_repository_workdir(diff->repo), file->path)))
+			error = opts->metric->file_signature(
+				&cache[file_idx], file, path.ptr, opts->metric->payload);
+
+		git_buf_free(&path);
+	} else { /* compute hashsig from blob buffer */
+		git_blob *blob = NULL;
+
+		/* TODO: add max size threshold a la diff? */
+
+		if ((error = git_blob_lookup(&blob, diff->repo, &file->oid)) < 0)
+			return error;
+
+		error = opts->metric->buffer_signature(
+			&cache[file_idx], file, git_blob_rawcontent(blob),
+			git_blob_rawsize(blob), opts->metric->payload);
+
+		git_blob_free(blob);
+	}
+
+	return error;
+}
+
+static int similarity_measure(
+	git_diff_list *diff,
+	git_diff_find_options *opts,
+	void **cache,
+	size_t a_idx,
+	size_t b_idx)
+{
+	int score = 0;
+	git_diff_file *a_file = similarity_get_file(diff, a_idx);
+	git_diff_file *b_file = similarity_get_file(diff, b_idx);
+
+	if (GIT_MODE_TYPE(a_file->mode) != GIT_MODE_TYPE(b_file->mode))
+		return 0;
+
+	if (git_oid_cmp(&a_file->oid, &b_file->oid) == 0)
 		return 100;
 
-	/* TODO: insert actual similarity algo here */
+	/* update signature cache if needed */
+	if (!cache[a_idx] && similarity_calc(diff, opts, a_idx, cache) < 0)
+		return -1;
+	if (!cache[b_idx] && similarity_calc(diff, opts, b_idx, cache) < 0)
+		return -1;
 
-	return 0;
+	/* compare signatures */
+	if (opts->metric->similarity(
+			&score, cache[a_idx], cache[b_idx], opts->metric->payload) < 0)
+		return -1;
+
+	/* clip score */
+	if (score < 0)
+		score = 0;
+	else if (score > 100)
+		score = 100;
+
+	return score;
 }
 
 #define FLAG_SET(opts,flag_name) ((opts.flags & flag_name) != 0)
@@ -316,109 +435,135 @@ int git_diff_find_similar(
 	git_diff_list *diff,
 	git_diff_find_options *given_opts)
 {
-	unsigned int i, j, similarity;
+	size_t i, j, cache_size, *matches;
+	int error = 0, similarity;
 	git_diff_delta *from, *to;
 	git_diff_find_options opts;
-	unsigned int tried_targets, num_changes = 0;
-	git_vector matches = GIT_VECTOR_INIT;
+	size_t tried_targets, num_rewrites = 0;
+	void **cache;
 
-	if (normalize_find_opts(diff, &opts, given_opts) < 0)
-		return -1;
+	if ((error = normalize_find_opts(diff, &opts, given_opts)) < 0)
+		return error;
 
-	/* first do splits if requested */
+	/* TODO: maybe abort if deltas.length > target_limit ??? */
+
+	cache_size = diff->deltas.length * 2; /* must store b/c length may change */
+	cache = git__calloc(cache_size, sizeof(void *));
+	GITERR_CHECK_ALLOC(cache);
+
+	matches = git__calloc(diff->deltas.length, sizeof(size_t));
+	GITERR_CHECK_ALLOC(matches);
+
+	/* first break MODIFIED records that are too different (if requested) */
 
 	if (FLAG_SET(opts, GIT_DIFF_FIND_AND_BREAK_REWRITES)) {
 		git_vector_foreach(&diff->deltas, i, from) {
 			if (from->status != GIT_DELTA_MODIFIED)
 				continue;
 
-			/* Right now, this doesn't work right because the similarity
-			 * algorithm isn't actually implemented...
-			 */
-			similarity = 100;
-			/* calc_similarity(NULL, &from->old_file, from->new_file); */
+			similarity = similarity_measure(
+				diff, &opts, cache, 2 * i, 2 * i + 1);
 
-			if (similarity < opts.break_rewrite_threshold) {
-				from->status = GIT_DELTA__TO_SPLIT;
-				num_changes++;
+			if (similarity < 0) {
+				error = similarity;
+				goto cleanup;
 			}
-		}
 
-		/* apply splits as needed */
-		if (num_changes > 0 &&
-			apply_splits_and_deletes(
-				diff, diff->deltas.length + num_changes) < 0)
-			return -1;
+			if ((unsigned int)similarity < opts.break_rewrite_threshold) {
+				from->flags |= GIT_DIFF_FLAG__TO_SPLIT;
+				num_rewrites++;
+			}
+		}
 	}
 
 	/* next find the most similar delta for each rename / copy candidate */
 
-	if (git_vector_init(&matches, diff->deltas.length, git_diff_delta__cmp) < 0)
-		return -1;
-
 	git_vector_foreach(&diff->deltas, i, from) {
 		tried_targets = 0;
 
+		/* skip things that aren't blobs */
+		if (GIT_MODE_TYPE(from->old_file.mode) !=
+			GIT_MODE_TYPE(GIT_FILEMODE_BLOB))
+			continue;
+
+		/* don't check UNMODIFIED files as source unless given option */
+		if (from->status == GIT_DELTA_UNMODIFIED &&
+			!FLAG_SET(opts, GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED))
+			continue;
+
+		/* skip all but DELETED files unless copy detection is on */
+		if (!FLAG_SET(opts, GIT_DIFF_FIND_COPIES) &&
+			from->status != GIT_DELTA_DELETED &&
+			(from->flags & GIT_DIFF_FLAG__TO_SPLIT) == 0)
+			continue;
+
 		git_vector_foreach(&diff->deltas, j, to) {
 			if (i == j)
 				continue;
 
+			/* skip things that aren't blobs */
+			if (GIT_MODE_TYPE(to->new_file.mode) !=
+				GIT_MODE_TYPE(GIT_FILEMODE_BLOB))
+				continue;
+
 			switch (to->status) {
 			case GIT_DELTA_ADDED:
 			case GIT_DELTA_UNTRACKED:
 			case GIT_DELTA_RENAMED:
 			case GIT_DELTA_COPIED:
 				break;
+			case GIT_DELTA_MODIFIED:
+				if ((to->flags & GIT_DIFF_FLAG__TO_SPLIT) == 0)
+					continue;
+				break;
 			default:
 				/* only the above status values should be checked */
 				continue;
 			}
 
-			/* skip all but DELETED files unless copy detection is on */
-			if (from->status != GIT_DELTA_DELETED &&
-				!FLAG_SET(opts, GIT_DIFF_FIND_COPIES))
-				continue;
-
-			/* don't check UNMODIFIED files as source unless given option */
-			if (from->status == GIT_DELTA_UNMODIFIED &&
-				!FLAG_SET(opts, GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED))
-				continue;
-
-			/* cap on maximum files we'll examine */
+			/* cap on maximum files we'll examine (per "from" file) */
 			if (++tried_targets > opts.target_limit)
 				break;
 
 			/* calculate similarity and see if this pair beats the
 			 * similarity score of the current best pair.
 			 */
-			similarity = calc_similarity(NULL, &from->old_file, &to->new_file);
+			similarity = similarity_measure(
+				diff, &opts, cache, 2 * i, 2 * j + 1);
+
+			if (similarity < 0) {
+				error = similarity;
+				goto cleanup;
+			}
 
-			if (to->similarity < similarity) {
-				to->similarity = similarity;
-				if (git_vector_set(NULL, &matches, j, from) < 0)
-					return -1;
+			if (to->similarity < (unsigned int)similarity) {
+				to->similarity = (unsigned int)similarity;
+				matches[j] = i + 1;
 			}
 		}
 	}
 
 	/* next rewrite the diffs with renames / copies */
 
-	num_changes = 0;
-
 	git_vector_foreach(&diff->deltas, j, to) {
-		from = GIT_VECTOR_GET(&matches, j);
-		if (!from) {
+		if (!matches[j]) {
 			assert(to->similarity == 0);
 			continue;
 		}
 
-		/* three possible outcomes here:
+		i = matches[j] - 1;
+		from = GIT_VECTOR_GET(&diff->deltas, i);
+		assert(from);
+
+		/* four possible outcomes here:
 		 * 1. old DELETED and if over rename threshold,
 		 *    new becomes RENAMED and old goes away
-		 * 2. old was MODIFIED but FIND_RENAMES_FROM_REWRITES is on and
+		 * 2. old SPLIT and if over rename threshold,
+		 *    new becomes RENAMED and old becomes ADDED (clear SPLIT)
+		 * 3. old was MODIFIED but FIND_RENAMES_FROM_REWRITES is on and
 		 *    old is more similar to new than it is to itself, in which
 		 *    case, new becomes RENAMED and old becomed ADDED
-		 * 3. otherwise if over copy threshold, new becomes COPIED
+		 * 4. otherwise if over copy threshold, new becomes COPIED
 		 */
 
 		if (from->status == GIT_DELTA_DELETED) {
@@ -430,8 +575,27 @@ int git_diff_find_similar(
 			to->status = GIT_DELTA_RENAMED;
 			memcpy(&to->old_file, &from->old_file, sizeof(to->old_file));
 
-			from->status = GIT_DELTA__TO_DELETE;
-			num_changes++;
+			from->flags |= GIT_DIFF_FLAG__TO_DELETE;
+			num_rewrites++;
+
+			continue;
+		}
+
+		if (from->status == GIT_DELTA_MODIFIED &&
+			(from->flags & GIT_DIFF_FLAG__TO_SPLIT) != 0)
+		{
+			if (to->similarity < opts.rename_threshold) {
+				to->similarity = 0;
+				continue;
+			}
+
+			to->status = GIT_DELTA_RENAMED;
+			memcpy(&to->old_file, &from->old_file, sizeof(to->old_file));
+
+			from->status = GIT_DELTA_ADDED;
+			from->flags &= ~GIT_DIFF_FLAG__TO_SPLIT;
+			memset(&from->old_file, 0, sizeof(from->old_file));
+			num_rewrites--;
 
 			continue;
 		}
@@ -440,17 +604,22 @@ int git_diff_find_similar(
 			FLAG_SET(opts, GIT_DIFF_FIND_RENAMES_FROM_REWRITES) &&
 			to->similarity > opts.rename_threshold)
 		{
-			similarity = 100;
-			/* calc_similarity(NULL, &from->old_file, from->new_file); */
+			similarity = similarity_measure(
+				diff, &opts, cache, 2 * i, 2 * i + 1);
+
+			if (similarity < 0) {
+				error = similarity;
+				goto cleanup;
+			}
 
-			if (similarity < opts.rename_from_rewrite_threshold) {
+			if ((unsigned int)similarity < opts.rename_from_rewrite_threshold) {
 				to->status = GIT_DELTA_RENAMED;
 				memcpy(&to->old_file, &from->old_file, sizeof(to->old_file));
 
 				from->status = GIT_DELTA_ADDED;
 				memset(&from->old_file, 0, sizeof(from->old_file));
 				from->old_file.path = to->old_file.path;
-				from->old_file.flags |= GIT_DIFF_FILE_VALID_OID;
+				from->old_file.flags |= GIT_DIFF_FLAG_VALID_OID;
 
 				continue;
 			}
@@ -466,17 +635,26 @@ int git_diff_find_similar(
 		memcpy(&to->old_file, &from->old_file, sizeof(to->old_file));
 	}
 
-	git_vector_free(&matches);
+	if (num_rewrites > 0) {
+		assert(num_rewrites < diff->deltas.length);
 
-	if (num_changes > 0) {
-		assert(num_changes < diff->deltas.length);
+		error = apply_splits_and_deletes(
+			diff, diff->deltas.length - num_rewrites);
+	}
+
+cleanup:
+	git__free(matches);
 
-		if (apply_splits_and_deletes(
-				diff, diff->deltas.length - num_changes) < 0)
-			return -1;
+	for (i = 0; i < cache_size; ++i) {
+		if (cache[i] != NULL)
+			opts.metric->free_signature(cache[i], opts.metric->payload);
 	}
+	git__free(cache);
 
-	return 0;
+	if (!given_opts || !given_opts->metric)
+		git__free(opts.metric);
+
+	return error;
 }
 
 #undef FLAG_SET
diff --git a/src/hashsig.c b/src/hashsig.c
new file mode 100644
index 0000000..60649fd
--- /dev/null
+++ b/src/hashsig.c
@@ -0,0 +1,365 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#include "hashsig.h"
+#include "fileops.h"
+
+typedef uint32_t hashsig_t;
+typedef uint64_t hashsig_state;
+
+#define HASHSIG_SCALE 100
+
+#define HASHSIG_HASH_WINDOW 32
+#define HASHSIG_HASH_START	0
+#define HASHSIG_HASH_SHIFT  5
+#define HASHSIG_HASH_MASK   0x7FFFFFFF
+
+#define HASHSIG_HEAP_SIZE ((1 << 7) - 1)
+
+typedef int (*hashsig_cmp)(const void *a, const void *b);
+
+typedef struct {
+	int size, asize;
+	hashsig_cmp cmp;
+	hashsig_t values[HASHSIG_HEAP_SIZE];
+} hashsig_heap;
+
+typedef struct {
+	hashsig_state state, shift_n;
+	char window[HASHSIG_HASH_WINDOW];
+	int win_len, win_pos, saw_lf;
+} hashsig_in_progress;
+
+#define HASHSIG_IN_PROGRESS_INIT { HASHSIG_HASH_START, 1, {0}, 0, 0, 1 }
+
+struct git_hashsig {
+	hashsig_heap mins;
+	hashsig_heap maxs;
+	git_hashsig_option_t opt;
+	int considered;
+};
+
+#define HEAP_LCHILD_OF(I) (((I)*2)+1)
+#define HEAP_RCHILD_OF(I) (((I)*2)+2)
+#define HEAP_PARENT_OF(I) (((I)-1)>>1)
+
+static void hashsig_heap_init(hashsig_heap *h, hashsig_cmp cmp)
+{
+	h->size  = 0;
+	h->asize = HASHSIG_HEAP_SIZE;
+	h->cmp   = cmp;
+}
+
+static int hashsig_cmp_max(const void *a, const void *b)
+{
+	hashsig_t av = *(const hashsig_t *)a, bv = *(const hashsig_t *)b;
+	return (av < bv) ? -1 : (av > bv) ? 1 : 0;
+}
+
+static int hashsig_cmp_min(const void *a, const void *b)
+{
+	hashsig_t av = *(const hashsig_t *)a, bv = *(const hashsig_t *)b;
+	return (av > bv) ? -1 : (av < bv) ? 1 : 0;
+}
+
+static void hashsig_heap_up(hashsig_heap *h, int el)
+{
+	int parent_el = HEAP_PARENT_OF(el);
+
+	while (el > 0 && h->cmp(&h->values[parent_el], &h->values[el]) > 0) {
+		hashsig_t t = h->values[el];
+		h->values[el] = h->values[parent_el];
+		h->values[parent_el] = t;
+
+		el = parent_el;
+		parent_el = HEAP_PARENT_OF(el);
+	}
+}
+
+static void hashsig_heap_down(hashsig_heap *h, int el)
+{
+	hashsig_t v, lv, rv;
+
+	/* 'el < h->size / 2' tests if el is bottom row of heap */
+
+	while (el < h->size / 2) {
+		int lel = HEAP_LCHILD_OF(el), rel = HEAP_RCHILD_OF(el), swapel;
+
+		v  = h->values[el];
+		lv = h->values[lel];
+		rv = h->values[rel];
+
+		if (h->cmp(&v, &lv) < 0 && h->cmp(&v, &rv) < 0)
+			break;
+
+		swapel = (h->cmp(&lv, &rv) < 0) ? lel : rel;
+
+		h->values[el] = h->values[swapel];
+		h->values[swapel] = v;
+
+		el = swapel;
+	}
+}
+
+static void hashsig_heap_sort(hashsig_heap *h)
+{
+	/* only need to do this at the end for signature comparison */
+	qsort(h->values, h->size, sizeof(hashsig_t), h->cmp);
+}
+
+static void hashsig_heap_insert(hashsig_heap *h, hashsig_t val)
+{
+	/* if heap is full, pop top if new element should replace it */
+	if (h->size == h->asize && h->cmp(&val, &h->values[0]) > 0) {
+		h->size--;
+		h->values[0] = h->values[h->size];
+		hashsig_heap_down(h, 0);
+	}
+
+	/* if heap is not full, insert new element */
+	if (h->size < h->asize) {
+		h->values[h->size++] = val;
+		hashsig_heap_up(h, h->size - 1);
+	}
+}
+
+GIT_INLINE(bool) hashsig_include_char(
+	char ch, git_hashsig_option_t opt, int *saw_lf)
+{
+	if ((opt & GIT_HASHSIG_IGNORE_WHITESPACE) && git__isspace(ch))
+		return false;
+
+	if (opt & GIT_HASHSIG_SMART_WHITESPACE) {
+		if (ch == '\r' || (*saw_lf && git__isspace(ch)))
+			return false;
+
+		*saw_lf = (ch == '\n');
+	}
+
+	return true;
+}
+
+static void hashsig_initial_window(
+	git_hashsig *sig,
+	const char **data,
+	size_t size,
+	hashsig_in_progress *prog)
+{
+	hashsig_state state, shift_n;
+	int win_len;
+	const char *scan, *end;
+
+	/* init until we have processed at least HASHSIG_HASH_WINDOW data */
+
+	if (prog->win_len >= HASHSIG_HASH_WINDOW)
+		return;
+
+	state   = prog->state;
+	win_len = prog->win_len;
+	shift_n = prog->shift_n;
+
+	scan = *data;
+	end  = scan + size;
+
+	while (scan < end && win_len < HASHSIG_HASH_WINDOW) {
+		char ch = *scan++;
+
+		if (!hashsig_include_char(ch, sig->opt, &prog->saw_lf))
+			continue;
+
+		state = (state * HASHSIG_HASH_SHIFT + ch) & HASHSIG_HASH_MASK;
+
+		if (!win_len)
+			shift_n = 1;
+		else
+			shift_n = (shift_n * HASHSIG_HASH_SHIFT) & HASHSIG_HASH_MASK;
+
+		prog->window[win_len++] = ch;
+	}
+
+	/* insert initial hash if we just finished */
+
+	if (win_len == HASHSIG_HASH_WINDOW) {
+		hashsig_heap_insert(&sig->mins, state);
+		hashsig_heap_insert(&sig->maxs, state);
+		sig->considered = 1;
+	}
+
+	prog->state   = state;
+	prog->win_len = win_len;
+	prog->shift_n = shift_n;
+
+	*data = scan;
+}
+
+static int hashsig_add_hashes(
+	git_hashsig *sig,
+	const char *data,
+	size_t size,
+	hashsig_in_progress *prog)
+{
+	const char *scan = data, *end = data + size;
+	hashsig_state state, shift_n, rmv;
+
+	if (prog->win_len < HASHSIG_HASH_WINDOW)
+		hashsig_initial_window(sig, &scan, size, prog);
+
+	state   = prog->state;
+	shift_n = prog->shift_n;
+
+	/* advance window, adding new chars and removing old */
+
+	for (; scan < end; ++scan) {
+		char ch = *scan;
+
+		if (!hashsig_include_char(ch, sig->opt, &prog->saw_lf))
+			continue;
+
+		rmv = shift_n * prog->window[prog->win_pos];
+
+		state = (state - rmv) & HASHSIG_HASH_MASK;
+		state = (state * HASHSIG_HASH_SHIFT) & HASHSIG_HASH_MASK;
+		state = (state + ch) & HASHSIG_HASH_MASK;
+
+		hashsig_heap_insert(&sig->mins, state);
+		hashsig_heap_insert(&sig->maxs, state);
+		sig->considered++;
+
+		prog->window[prog->win_pos] = ch;
+		prog->win_pos = (prog->win_pos + 1) % HASHSIG_HASH_WINDOW;
+	}
+
+	prog->state = state;
+
+	return 0;
+}
+
+static int hashsig_finalize_hashes(git_hashsig *sig)
+{
+	if (sig->mins.size < HASHSIG_HEAP_SIZE) {
+		giterr_set(GITERR_INVALID,
+			"File too small for similarity signature calculation");
+		return GIT_EBUFS;
+	}
+
+	hashsig_heap_sort(&sig->mins);
+	hashsig_heap_sort(&sig->maxs);
+
+	return 0;
+}
+
+static git_hashsig *hashsig_alloc(git_hashsig_option_t opts)
+{
+	git_hashsig *sig = git__calloc(1, sizeof(git_hashsig));
+	if (!sig)
+		return NULL;
+
+	hashsig_heap_init(&sig->mins, hashsig_cmp_min);
+	hashsig_heap_init(&sig->maxs, hashsig_cmp_max);
+	sig->opt = opts;
+
+	return sig;
+}
+
+int git_hashsig_create(
+	git_hashsig **out,
+	const char *buf,
+	size_t buflen,
+	git_hashsig_option_t opts)
+{
+	int error;
+	hashsig_in_progress prog = HASHSIG_IN_PROGRESS_INIT;
+	git_hashsig *sig = hashsig_alloc(opts);
+	GITERR_CHECK_ALLOC(sig);
+
+	error = hashsig_add_hashes(sig, buf, buflen, &prog);
+
+	if (!error)
+		error = hashsig_finalize_hashes(sig);
+
+	if (!error)
+		*out = sig;
+	else
+		git_hashsig_free(sig);
+
+	return error;
+}
+
+int git_hashsig_create_fromfile(
+	git_hashsig **out,
+	const char *path,
+	git_hashsig_option_t opts)
+{
+	char buf[4096];
+	ssize_t buflen = 0;
+	int error = 0, fd;
+	hashsig_in_progress prog = HASHSIG_IN_PROGRESS_INIT;
+	git_hashsig *sig = hashsig_alloc(opts);
+	GITERR_CHECK_ALLOC(sig);
+
+	if ((fd = git_futils_open_ro(path)) < 0) {
+		git__free(sig);
+		return fd;
+	}
+
+	while (!error) {
+		if ((buflen = p_read(fd, buf, sizeof(buf))) <= 0) {
+			if ((error = buflen) < 0)
+				giterr_set(GITERR_OS,
+					"Read error on '%s' calculating similarity hashes", path);
+			break;
+		}
+
+		error = hashsig_add_hashes(sig, buf, buflen, &prog);
+	}
+
+	p_close(fd);
+
+	if (!error)
+		error = hashsig_finalize_hashes(sig);
+
+	if (!error)
+		*out = sig;
+	else
+		git_hashsig_free(sig);
+
+	return error;
+}
+
+void git_hashsig_free(git_hashsig *sig)
+{
+	git__free(sig);
+}
+
+static int hashsig_heap_compare(const hashsig_heap *a, const hashsig_heap *b)
+{
+	int matches = 0, i, j, cmp;
+
+	assert(a->cmp == b->cmp);
+
+	/* hash heaps are sorted - just look for overlap vs total */
+
+	for (i = 0, j = 0; i < a->size && j < b->size; ) {
+		cmp = a->cmp(&a->values[i], &b->values[j]);
+
+		if (cmp < 0)
+			++i;
+		else if (cmp > 0)
+			++j;
+		else {
+			++i; ++j; ++matches;
+		}
+	}
+
+	return HASHSIG_SCALE * (matches * 2) / (a->size + b->size);
+}
+
+int git_hashsig_compare(const git_hashsig *a, const git_hashsig *b)
+{
+	return (hashsig_heap_compare(&a->mins, &b->mins) +
+			hashsig_heap_compare(&a->maxs, &b->maxs)) / 2;
+}
+
diff --git a/src/hashsig.h b/src/hashsig.h
new file mode 100644
index 0000000..8c920cb
--- /dev/null
+++ b/src/hashsig.h
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_hashsig_h__
+#define INCLUDE_hashsig_h__
+
+#include "common.h"
+
+/**
+ * Similarity signature of line hashes for a buffer
+ */
+typedef struct git_hashsig git_hashsig;
+
+typedef enum {
+	GIT_HASHSIG_NORMAL = 0, /* use all data */
+	GIT_HASHSIG_IGNORE_WHITESPACE = 1, /* ignore whitespace */
+	GIT_HASHSIG_SMART_WHITESPACE = 2, /* ignore \r and all space after \n */
+} git_hashsig_option_t;
+
+/**
+ * Build a similarity signature for a buffer
+ *
+ * If you have passed a whitespace-ignoring buffer, then the whitespace
+ * will be removed from the buffer while it is being processed, modifying
+ * the buffer in place.  Sorry about that!
+ *
+ * This will return an error if the buffer doesn't contain enough data to
+ * compute a valid signature.
+ *
+ * @param out The array of hashed runs representing the file content
+ * @param buf The contents of the file to hash
+ * @param buflen The length of the data at `buf`
+ * @param generate_pairwise_hashes Should pairwise runs be hashed
+ */
+extern int git_hashsig_create(
+	git_hashsig **out,
+	const char *buf,
+	size_t buflen,
+	git_hashsig_option_t opts);
+
+/**
+ * Build a similarity signature from a file
+ *
+ * This walks through the file, only loading a maximum of 4K of file data at
+ * a time.  Otherwise, it acts just like `git_hashsig_create`.
+ *
+ * This will return an error if the file doesn't contain enough data to
+ * compute a valid signature.
+ */
+extern int git_hashsig_create_fromfile(
+	git_hashsig **out,
+	const char *path,
+	git_hashsig_option_t opts);
+
+/**
+ * Release memory for a content similarity signature
+ */
+extern void git_hashsig_free(git_hashsig *sig);
+
+/**
+ * Measure similarity between two files
+ *
+ * @return <0 for error, [0 to 100] as similarity score
+ */
+extern int git_hashsig_compare(
+	const git_hashsig *a,
+	const git_hashsig *b);
+
+#endif
diff --git a/tests-clar/core/buffer.c b/tests-clar/core/buffer.c
index 49ab41f..cb5f016 100644
--- a/tests-clar/core/buffer.c
+++ b/tests-clar/core/buffer.c
@@ -1,6 +1,8 @@
 #include "clar_libgit2.h"
 #include "buffer.h"
 #include "buf_text.h"
+#include "hashsig.h"
+#include "fileops.h"
 
 #define TESTSTR "Have you seen that? Have you seeeen that??"
 const char *test_string = TESTSTR;
@@ -730,3 +732,175 @@ void test_core_buffer__classify_with_utf8(void)
 	cl_assert(git_buf_text_is_binary(&b));
 	cl_assert(git_buf_text_contains_nul(&b));
 }
+
+#define SIMILARITY_TEST_DATA_1 \
+	"test data\nright here\ninline\ntada\nneeds more data\nlots of data\n" \
+	"is this enough?\nthere has to be enough data to fill the hash array!\n" \
+	"Apparently 191 bytes is the minimum amount of data needed.\nHere goes!\n" \
+	"Let's make sure we've got plenty to go with here.\n   smile   \n"
+
+void test_core_buffer__similarity_metric(void)
+{
+	git_hashsig *a, *b;
+	git_buf buf = GIT_BUF_INIT;
+	int sim;
+
+	/* in the first case, we compare data to itself and expect 100% match */
+
+	cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
+	cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
+	cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
+
+	cl_assert_equal_i(100, git_hashsig_compare(a, b));
+
+	git_hashsig_free(a);
+	git_hashsig_free(b);
+
+	/* if we change just a single byte, how much does that change magnify? */
+
+	cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
+	cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
+	cl_git_pass(git_buf_sets(&buf,
+		"Test data\nright here\ninline\ntada\nneeds more data\nlots of data\n"
+		"is this enough?\nthere has to be enough data to fill the hash array!\n"
+		"Apparently 191 bytes is the minimum amount of data needed.\nHere goes!\n"
+		 "Let's make sure we've got plenty to go with here.\n   smile   \n"));
+	cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
+
+	sim = git_hashsig_compare(a, b);
+
+	cl_assert(95 < sim && sim < 100); /* expect >95% similarity */
+
+	git_hashsig_free(a);
+	git_hashsig_free(b);
+
+	/* let's try comparing data to a superset of itself */
+
+	cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
+	cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
+	cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1
+		"and if I add some more, it should still be pretty similar, yes?\n"));
+	cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
+
+	sim = git_hashsig_compare(a, b);
+
+	cl_assert(70 < sim && sim < 80); /* expect in the 70-80% similarity range */
+
+	git_hashsig_free(a);
+	git_hashsig_free(b);
+
+	/* what if we keep about half the original data and add half new */
+
+	cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
+	cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
+	cl_git_pass(git_buf_sets(&buf,
+		"test data\nright here\ninline\ntada\nneeds more data\nlots of data\n"
+		"is this enough?\nthere has to be enough data to fill the hash array!\n"
+		"okay, that's half the original\nwhat else can we add?\nmore data\n"
+		 "one more line will complete this\nshort\nlines\ndon't\nmatter\n"));
+	cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
+
+	sim = git_hashsig_compare(a, b);
+
+	cl_assert(40 < sim && sim < 60); /* expect in the 40-60% similarity range */
+
+	git_hashsig_free(a);
+	git_hashsig_free(b);
+
+	/* lastly, let's check that we can hash file content as well */
+
+	cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
+	cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
+
+	cl_git_pass(git_futils_mkdir("scratch", NULL, 0755, GIT_MKDIR_PATH));
+	cl_git_mkfile("scratch/testdata", SIMILARITY_TEST_DATA_1);
+	cl_git_pass(git_hashsig_create_fromfile(
+		&b, "scratch/testdata", GIT_HASHSIG_NORMAL));
+
+	cl_assert_equal_i(100, git_hashsig_compare(a, b));
+
+	git_hashsig_free(a);
+	git_hashsig_free(b);
+
+	git_buf_free(&buf);
+	git_futils_rmdir_r("scratch", NULL, GIT_RMDIR_REMOVE_FILES);
+}
+
+
+void test_core_buffer__similarity_metric_whitespace(void)
+{
+	git_hashsig *a, *b;
+	git_buf buf = GIT_BUF_INIT;
+	int sim, i, j;
+	git_hashsig_option_t opt;
+	const char *tabbed =
+		"	for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\n"
+		"		separator = sep[s];\n"
+		"		expect = expect_values[s];\n"
+		"\n"
+		"		for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\n"
+		"			for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\n"
+		"				git_buf_join(&buf, separator, a[i], b[j]);\n"
+		"				cl_assert_equal_s(*expect, buf.ptr);\n"
+		"				expect++;\n"
+		"			}\n"
+		"		}\n"
+		"	}\n";
+	const char *spaced =
+		"   for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\n"
+		"       separator = sep[s];\n"
+		"       expect = expect_values[s];\n"
+		"\n"
+		"       for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\n"
+		"           for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\n"
+		"               git_buf_join(&buf, separator, a[i], b[j]);\n"
+		"               cl_assert_equal_s(*expect, buf.ptr);\n"
+		"               expect++;\n"
+		"           }\n"
+		"       }\n"
+		"   }\n";
+	const char *crlf_spaced2 =
+		"  for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\r\n"
+		"    separator = sep[s];\r\n"
+		"    expect = expect_values[s];\r\n"
+		"\r\n"
+		"    for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\r\n"
+		"      for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\r\n"
+		"        git_buf_join(&buf, separator, a[i], b[j]);\r\n"
+		"        cl_assert_equal_s(*expect, buf.ptr);\r\n"
+		"        expect++;\r\n"
+		"      }\r\n"
+		"    }\r\n"
+		"  }\r\n";
+	const char *text[3] = { tabbed, spaced, crlf_spaced2 };
+
+	/* let's try variations of our own code with whitespace changes */
+
+	for (opt = GIT_HASHSIG_NORMAL; opt <= GIT_HASHSIG_SMART_WHITESPACE; ++opt) {
+		for (i = 0; i < 3; ++i) {
+			for (j = 0; j < 3; ++j) {
+				cl_git_pass(git_buf_sets(&buf, text[i]));
+				cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, opt));
+
+				cl_git_pass(git_buf_sets(&buf, text[j]));
+				cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, opt));
+
+				sim = git_hashsig_compare(a, b);
+
+				if (opt == GIT_HASHSIG_NORMAL) {
+					if (i == j)
+						cl_assert_equal_i(100, sim);
+					else
+						cl_assert(sim < 30); /* expect pretty different */
+				} else {
+					cl_assert_equal_i(100, sim);
+				}
+
+				git_hashsig_free(a);
+				git_hashsig_free(b);
+			}
+		}
+	}
+
+	git_buf_free(&buf);
+}
diff --git a/tests-clar/diff/blob.c b/tests-clar/diff/blob.c
index a6950b8..2ac8dbc 100644
--- a/tests-clar/diff/blob.c
+++ b/tests-clar/diff/blob.c
@@ -196,7 +196,7 @@ void test_diff_blob__can_compare_identical_blobs(void)
 		NULL, NULL, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
 
 	cl_assert_equal_i(0, expected.files_binary);
-	assert_identical_blobs_comparison(&expected);
+	cl_assert_equal_i(0, expected.files); /* NULLs mean no callbacks, period */
 
 	memset(&expected, 0, sizeof(expected));
 	cl_git_pass(git_diff_blobs(
@@ -399,7 +399,7 @@ void test_diff_blob__can_compare_blob_to_buffer(void)
 
 	assert_identical_blobs_comparison(&expected);
 
-	/* diff from NULL blob to content of b */
+	/* diff from NULL blob to content of a */
 	memset(&expected, 0, sizeof(expected));
 	cl_git_pass(git_diff_blob_to_buffer(
 		NULL, a_content, strlen(a_content),
diff --git a/tests-clar/diff/diff_helpers.c b/tests-clar/diff/diff_helpers.c
index 1436ada..a1f75ce 100644
--- a/tests-clar/diff/diff_helpers.c
+++ b/tests-clar/diff/diff_helpers.c
@@ -32,7 +32,7 @@ int diff_file_cb(
 
 	e->files++;
 
-	if (delta->binary)
+	if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
 		e->files_binary++;
 
 	cl_assert(delta->status <= GIT_DELTA_TYPECHANGE);
@@ -126,7 +126,8 @@ int diff_foreach_via_iterator(
 
 		/* if there are no changes, then the patch will be NULL */
 		if (!patch) {
-			cl_assert(delta->status == GIT_DELTA_UNMODIFIED || delta->binary == 1);
+			cl_assert(delta->status == GIT_DELTA_UNMODIFIED ||
+					  (delta->flags & GIT_DIFF_FLAG_BINARY) != 0);
 			continue;
 		}
 
diff --git a/tests-clar/diff/diffiter.c b/tests-clar/diff/diffiter.c
index 8d550ec..932d720 100644
--- a/tests-clar/diff/diffiter.c
+++ b/tests-clar/diff/diffiter.c
@@ -152,8 +152,8 @@ void test_diff_diffiter__max_size_threshold(void)
 		file_count++;
 		hunk_count += (int)git_diff_patch_num_hunks(patch);
 
-		assert(delta->binary == 0 || delta->binary == 1);
-		binary_count += delta->binary;
+		assert((delta->flags & (GIT_DIFF_FLAG_BINARY|GIT_DIFF_FLAG_NOT_BINARY)) != 0);
+		binary_count += ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0);
 
 		git_diff_patch_free(patch);
 	}
@@ -185,8 +185,8 @@ void test_diff_diffiter__max_size_threshold(void)
 		file_count++;
 		hunk_count += (int)git_diff_patch_num_hunks(patch);
 
-		assert(delta->binary == 0 || delta->binary == 1);
-		binary_count += delta->binary;
+		assert((delta->flags & (GIT_DIFF_FLAG_BINARY|GIT_DIFF_FLAG_NOT_BINARY)) != 0);
+		binary_count += ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0);
 
 		git_diff_patch_free(patch);
 	}
diff --git a/tests-clar/diff/patch.c b/tests-clar/diff/patch.c
index 18e90f9..5cb97fb 100644
--- a/tests-clar/diff/patch.c
+++ b/tests-clar/diff/patch.c
@@ -144,11 +144,11 @@ void test_diff_patch__hunks_have_correct_line_numbers(void)
 	size_t hdrlen, hunklen, textlen;
 	char origin;
 	int oldno, newno;
-	const char *new_content = "The Song of Seven Cities\n========================\n\nI WAS Lord of Cities very sumptuously builded.\nSeven roaring Cities paid me tribute from afar.\nIvory their outposts were—the guardrooms of them gilded,\nAnd garrisoned with Amazons invincible in war.\n\nThis is some new text;\nNot as good as the old text;\nBut here it is.\n\nSo they warred and trafficked only yesterday, my Cities.\nTo-day there is no mark or mound of where my Cities stood.\nFor the River rose at midnight and it washed away my Cities.\nThey are evened with Atlantis and the towns before the Flood.\n\nRain on rain-gorged channels raised the water-levels round them,\nFreshet backed on freshet swelled and swept their world from sight,\nTill the emboldened floods linked arms and, flashing forward, drowned them—\nDrowned my Seven Cities and their peoples in one night!\n\nLow among the alders lie their derelict foundations,\nThe beams wherein they trusted and the plinths whereon they built—\nMy rulers and their treasure and their unborn populations,\nDead, destroyed, aborted, and defiled with mud and silt!\n\nAnother replacement;\nBreaking up the poem;\nGenerating some hunks.\n\nTo the sound of trumpets shall their seed restore my Cities\nWealthy and well-weaponed, that once more may I behold\nAll the world go softly when it walks before my Cities,\nAnd the horses and the chariots fleeing from them as of old!\n\n                -- Rudyard Kipling\n";
+	const char *new_content = "The Song of Seven Cities\n------------------------\n\nI WAS Lord of Cities very sumptuously builded.\nSeven roaring Cities paid me tribute from afar.\nIvory their outposts were--the guardrooms of them gilded,\nAnd garrisoned with Amazons invincible in war.\n\nThis is some new text;\nNot as good as the old text;\nBut here it is.\n\nSo they warred and trafficked only yesterday, my Cities.\nTo-day there is no mark or mound of where my Cities stood.\nFor the River rose at midnight and it washed away my Cities.\nThey are evened with Atlantis and the towns before the Flood.\n\nRain on rain-gorged channels raised the water-levels round them,\nFreshet backed on freshet swelled and swept their world from sight,\nTill the emboldened floods linked arms and, flashing forward, drowned them--\nDrowned my Seven Cities and their peoples in one night!\n\nLow among the alders lie their derelict foundations,\nThe beams wherein they trusted and the plinths whereon they built--\nMy rulers and their treasure and their unborn populations,\nDead, destroyed, aborted, and defiled with mud and silt!\n\nAnother replacement;\nBreaking up the poem;\nGenerating some hunks.\n\nTo the sound of trumpets shall their seed restore my Cities\nWealthy and well-weaponed, that once more may I behold\nAll the world go softly when it walks before my Cities,\nAnd the horses and the chariots fleeing from them as of old!\n\n  -- Rudyard Kipling\n";
 
 	g_repo = cl_git_sandbox_init("renames");
 
-	cl_git_rewritefile("renames/songofseven.txt", new_content);
+	cl_git_rewritefile("renames/songof7cities.txt", new_content);
 
 	cl_git_pass(git_repository_head_tree(&head, g_repo));
 
@@ -178,14 +178,14 @@ void test_diff_patch__hunks_have_correct_line_numbers(void)
 	cl_git_pass(git_diff_patch_get_line_in_hunk(
 		&origin, &text, &textlen, &oldno, &newno, patch, 0, 0));
 	cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)origin);
-	cl_assert(strncmp("Ivory their outposts were—the guardrooms of them gilded,\n", text, textlen) == 0);
+	cl_assert(strncmp("Ivory their outposts were--the guardrooms of them gilded,\n", text, textlen) == 0);
 	cl_assert_equal_i(6, oldno);
 	cl_assert_equal_i(6, newno);
 
 	cl_git_pass(git_diff_patch_get_line_in_hunk(
 		&origin, &text, &textlen, &oldno, &newno, patch, 0, 3));
 	cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin);
-	cl_assert(strncmp("All the world went softly when it walked before my Cities—\n", text, textlen) == 0);
+	cl_assert(strncmp("All the world went softly when it walked before my Cities--\n", text, textlen) == 0);
 	cl_assert_equal_i(9, oldno);
 	cl_assert_equal_i(-1, newno);
 
@@ -271,32 +271,32 @@ void test_diff_patch__line_counts_with_eofnl(void)
 
 	g_repo = cl_git_sandbox_init("renames");
 
-	cl_git_pass(git_futils_readbuffer(&content, "renames/songofseven.txt"));
+	cl_git_pass(git_futils_readbuffer(&content, "renames/songof7cities.txt"));
 
 	/* remove first line */
 
 	end = git_buf_cstr(&content) + git_buf_find(&content, '\n') + 1;
 	git_buf_consume(&content, end);
-	cl_git_rewritefile("renames/songofseven.txt", content.ptr);
+	cl_git_rewritefile("renames/songof7cities.txt", content.ptr);
 
 	check_single_patch_stats(g_repo, 1, 0, 1);
 
 	/* remove trailing whitespace */
 
 	git_buf_rtrim(&content);
-	cl_git_rewritefile("renames/songofseven.txt", content.ptr);
+	cl_git_rewritefile("renames/songof7cities.txt", content.ptr);
 
 	check_single_patch_stats(g_repo, 2, 1, 2);
 
 	/* add trailing whitespace */
 
 	cl_git_pass(git_repository_index(&index, g_repo));
-	cl_git_pass(git_index_add_bypath(index, "songofseven.txt"));
+	cl_git_pass(git_index_add_bypath(index, "songof7cities.txt"));
 	cl_git_pass(git_index_write(index));
 	git_index_free(index);
 
 	cl_git_pass(git_buf_putc(&content, '\n'));
-	cl_git_rewritefile("renames/songofseven.txt", content.ptr);
+	cl_git_rewritefile("renames/songof7cities.txt", content.ptr);
 
 	check_single_patch_stats(g_repo, 1, 1, 1);
 
diff --git a/tests-clar/diff/rename.c b/tests-clar/diff/rename.c
index 2995b4e..5395125 100644
--- a/tests-clar/diff/rename.c
+++ b/tests-clar/diff/rename.c
@@ -23,8 +23,16 @@ void test_diff_rename__cleanup(void)
  *   serving.txt     -> sixserving.txt  (rename, no change, 100% match)
  *   sevencities.txt -> sevencities.txt (no change)
  *   sevencities.txt -> songofseven.txt (copy, no change, 100% match)
- *
- * TODO: add commits with various % changes of copy / rename
+ * commit 1c068dee5790ef1580cfc4cd670915b48d790084
+ *   songofseven.txt -> songofseven.txt (major rewrite, <20% match - split)
+ *   sixserving.txt  -> sixserving.txt  (indentation change)
+ *   sixserving.txt  -> ikeepsix.txt    (copy, add title, >80% match)
+ *   sevencities.txt                    (no change)
+ * commit 19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13
+ *   songofseven.txt -> untimely.txt    (rename, convert to crlf)
+ *   ikeepsix.txt    -> ikeepsix.txt    (reorder sections in file)
+ *   sixserving.txt  -> sixserving.txt  (whitespace change - not just indent)
+ *   sevencities.txt -> songof7cities.txt (rename, small text changes)
  */
 
 void test_diff_rename__match_oid(void)
@@ -133,3 +141,219 @@ void test_diff_rename__checks_options_version(void)
 	git_tree_free(old_tree);
 	git_tree_free(new_tree);
 }
+
+void test_diff_rename__not_exact_match(void)
+{
+	const char *sha0 = "2bc7f351d20b53f1c72c16c4b036e491c478c49a";
+	const char *sha1 = "1c068dee5790ef1580cfc4cd670915b48d790084";
+	const char *sha2 = "19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13";
+	git_tree *old_tree, *new_tree;
+	git_diff_list *diff;
+	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
+	git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
+	diff_expects exp;
+
+	/* == Changes =====================================================
+	 * songofseven.txt -> songofseven.txt (major rewrite, <20% match - split)
+	 * sixserving.txt  -> sixserving.txt  (indentation change)
+	 * sixserving.txt  -> ikeepsix.txt    (copy, add title, >80% match)
+	 * sevencities.txt                    (no change)
+	 */
+
+	old_tree = resolve_commit_oid_to_tree(g_repo, sha0);
+	new_tree = resolve_commit_oid_to_tree(g_repo, sha1);
+
+	/* Must pass GIT_DIFF_INCLUDE_UNMODIFIED if you expect to emulate
+	 * --find-copies-harder during rename transformion...
+	 */
+	diffopts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;
+
+	cl_git_pass(git_diff_tree_to_tree(
+		&diff, g_repo, old_tree, new_tree, &diffopts));
+
+	/* git diff --no-renames \
+	 *          2bc7f351d20b53f1c72c16c4b036e491c478c49a \
+	 *          1c068dee5790ef1580cfc4cd670915b48d790084
+	 */
+	memset(&exp, 0, sizeof(exp));
+	cl_git_pass(git_diff_foreach(
+		diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp));
+
+	cl_assert_equal_i(4, exp.files);
+	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
+	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
+	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]);
+
+	/* git diff -M 2bc7f351d20b53f1c72c16c4b036e491c478c49a \
+	 *          1c068dee5790ef1580cfc4cd670915b48d790084
+	 *
+	 * must not pass NULL for opts because it will pick up environment
+	 * values for "diff.renames" and test won't be consistent.
+	 */
+	opts.flags = GIT_DIFF_FIND_RENAMES;
+	cl_git_pass(git_diff_find_similar(diff, &opts));
+
+	memset(&exp, 0, sizeof(exp));
+	cl_git_pass(git_diff_foreach(
+		diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp));
+
+	cl_assert_equal_i(4, exp.files);
+	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
+	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
+	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]);
+
+	git_diff_list_free(diff);
+
+	/* git diff -M -C \
+	 *          2bc7f351d20b53f1c72c16c4b036e491c478c49a \
+	 *          1c068dee5790ef1580cfc4cd670915b48d790084
+	 */
+	cl_git_pass(git_diff_tree_to_tree(
+		&diff, g_repo, old_tree, new_tree, &diffopts));
+
+	opts.flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES;
+	cl_git_pass(git_diff_find_similar(diff, &opts));
+
+	memset(&exp, 0, sizeof(exp));
+	cl_git_pass(git_diff_foreach(
+		diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp));
+
+	cl_assert_equal_i(4, exp.files);
+	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
+	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
+	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]);
+
+	git_diff_list_free(diff);
+
+	/* git diff -M -C --find-copies-harder --break-rewrites \
+	 *          2bc7f351d20b53f1c72c16c4b036e491c478c49a \
+	 *          1c068dee5790ef1580cfc4cd670915b48d790084
+	 */
+	cl_git_pass(git_diff_tree_to_tree(
+		&diff, g_repo, old_tree, new_tree, &diffopts));
+
+	opts.flags = GIT_DIFF_FIND_ALL;
+	cl_git_pass(git_diff_find_similar(diff, &opts));
+
+	memset(&exp, 0, sizeof(exp));
+	cl_git_pass(git_diff_foreach(
+		diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp));
+
+	cl_assert_equal_i(5, exp.files);
+	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
+	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
+	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
+	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]);
+	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]);
+
+	git_diff_list_free(diff);
+
+	/* == Changes =====================================================
+	 * songofseven.txt -> untimely.txt    (rename, convert to crlf)
+	 * ikeepsix.txt    -> ikeepsix.txt    (reorder sections in file)
+	 * sixserving.txt  -> sixserving.txt  (whitespace - not just indent)
+	 * sevencities.txt -> songof7cities.txt (rename, small text changes)
+	 */
+
+	git_tree_free(old_tree);
+	old_tree = new_tree;
+	new_tree = resolve_commit_oid_to_tree(g_repo, sha2);
+
+	cl_git_pass(git_diff_tree_to_tree(
+		&diff, g_repo, old_tree, new_tree, &diffopts));
+
+	/* git diff --no-renames \
+	 *          1c068dee5790ef1580cfc4cd670915b48d790084 \
+	 *          19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13
+	 */
+	memset(&exp, 0, sizeof(exp));
+	cl_git_pass(git_diff_foreach(
+		diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp));
+
+	cl_assert_equal_i(6, exp.files);
+	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
+	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_ADDED]);
+	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]);
+
+	/* git diff -M -C \
+	 *          1c068dee5790ef1580cfc4cd670915b48d790084 \
+	 *          19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13
+	 */
+	cl_git_pass(git_diff_tree_to_tree(
+		&diff, g_repo, old_tree, new_tree, &diffopts));
+
+	opts.flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES;
+	cl_git_pass(git_diff_find_similar(diff, &opts));
+
+	memset(&exp, 0, sizeof(exp));
+	cl_git_pass(git_diff_foreach(
+		diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp));
+
+	cl_assert_equal_i(4, exp.files);
+	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
+	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_RENAMED]);
+
+	git_diff_list_free(diff);
+
+	/* git diff -M -C --find-copies-harder --break-rewrites \
+	 *          1c068dee5790ef1580cfc4cd670915b48d790084 \
+	 *          19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13
+	 * with libgit2 default similarity comparison...
+	 */
+	cl_git_pass(git_diff_tree_to_tree(
+		&diff, g_repo, old_tree, new_tree, &diffopts));
+
+	opts.flags = GIT_DIFF_FIND_ALL;
+	cl_git_pass(git_diff_find_similar(diff, &opts));
+
+	/* the default match algorithm is going to find the internal
+	 * whitespace differences in the lines of sixserving.txt to be
+	 * significant enough that this will decide to split it into
+	 * an ADD and a DELETE
+	 */
+
+	memset(&exp, 0, sizeof(exp));
+	cl_git_pass(git_diff_foreach(
+		diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp));
+
+	cl_assert_equal_i(5, exp.files);
+	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
+	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]);
+	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
+	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_RENAMED]);
+
+	git_diff_list_free(diff);
+
+	/* git diff -M -C --find-copies-harder --break-rewrites \
+	 *          1c068dee5790ef1580cfc4cd670915b48d790084 \
+	 *          19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13
+	 * with ignore_space whitespace comparision
+	 */
+	cl_git_pass(git_diff_tree_to_tree(
+		&diff, g_repo, old_tree, new_tree, &diffopts));
+
+	opts.flags = GIT_DIFF_FIND_ALL | GIT_DIFF_FIND_IGNORE_WHITESPACE;
+	cl_git_pass(git_diff_find_similar(diff, &opts));
+
+	/* Ignoring whitespace, this should no longer split sixserver.txt */
+
+	memset(&exp, 0, sizeof(exp));
+	cl_git_pass(git_diff_foreach(
+		diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp));
+
+	cl_assert_equal_i(4, exp.files);
+	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
+	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_RENAMED]);
+
+	git_diff_list_free(diff);
+
+	git_tree_free(old_tree);
+	git_tree_free(new_tree);
+}
+
+void test_diff_rename__working_directory_changes(void)
+{
+	/* let's rewrite some files in the working directory on demand */
+
+	/* and with / without CRLF changes */
+}
diff --git a/tests-clar/resources/renames/.gitted/index b/tests-clar/resources/renames/.gitted/index
index 1fc69fc..72363c0 100644
Binary files a/tests-clar/resources/renames/.gitted/index and b/tests-clar/resources/renames/.gitted/index differ
diff --git a/tests-clar/resources/renames/.gitted/logs/HEAD b/tests-clar/resources/renames/.gitted/logs/HEAD
index 34222ed..e697922 100644
--- a/tests-clar/resources/renames/.gitted/logs/HEAD
+++ b/tests-clar/resources/renames/.gitted/logs/HEAD
@@ -1,2 +1,4 @@
 0000000000000000000000000000000000000000 31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 Russell Belfer <rb@github.com> 1351024687 -0700	commit (initial): Initial commit
 31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 2bc7f351d20b53f1c72c16c4b036e491c478c49a Russell Belfer <rb@github.com> 1351024817 -0700	commit: copy and rename with no change
+2bc7f351d20b53f1c72c16c4b036e491c478c49a 1c068dee5790ef1580cfc4cd670915b48d790084 Russell Belfer <rb@github.com> 1361485758 -0800	commit: rewrites, copies with changes, etc.
+1c068dee5790ef1580cfc4cd670915b48d790084 19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13 Russell Belfer <rb@github.com> 1361486360 -0800	commit: more renames and smallish modifications
diff --git a/tests-clar/resources/renames/.gitted/logs/refs/heads/master b/tests-clar/resources/renames/.gitted/logs/refs/heads/master
index 34222ed..e697922 100644
--- a/tests-clar/resources/renames/.gitted/logs/refs/heads/master
+++ b/tests-clar/resources/renames/.gitted/logs/refs/heads/master
@@ -1,2 +1,4 @@
 0000000000000000000000000000000000000000 31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 Russell Belfer <rb@github.com> 1351024687 -0700	commit (initial): Initial commit
 31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 2bc7f351d20b53f1c72c16c4b036e491c478c49a Russell Belfer <rb@github.com> 1351024817 -0700	commit: copy and rename with no change
+2bc7f351d20b53f1c72c16c4b036e491c478c49a 1c068dee5790ef1580cfc4cd670915b48d790084 Russell Belfer <rb@github.com> 1361485758 -0800	commit: rewrites, copies with changes, etc.
+1c068dee5790ef1580cfc4cd670915b48d790084 19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13 Russell Belfer <rb@github.com> 1361486360 -0800	commit: more renames and smallish modifications
diff --git a/tests-clar/resources/renames/.gitted/objects/19/dd32dfb1520a64e5bbaae8dce6ef423dfa2f13 b/tests-clar/resources/renames/.gitted/objects/19/dd32dfb1520a64e5bbaae8dce6ef423dfa2f13
new file mode 100644
index 0000000..4be4c69
--- /dev/null
+++ b/tests-clar/resources/renames/.gitted/objects/19/dd32dfb1520a64e5bbaae8dce6ef423dfa2f13
@@ -0,0 +1 @@
+xM!Ei@3ސc,\XK{Nrbo,xzYC<h[&?=fcvyCW<Z#:J"vs%9PQ|ZKj#|u-pڐ;+[Z;L+l\k'J.WT;O
diff --git a/tests-clar/resources/renames/.gitted/objects/1c/068dee5790ef1580cfc4cd670915b48d790084 b/tests-clar/resources/renames/.gitted/objects/1c/068dee5790ef1580cfc4cd670915b48d790084
new file mode 100644
index 0000000..d65ab0a
Binary files /dev/null and b/tests-clar/resources/renames/.gitted/objects/1c/068dee5790ef1580cfc4cd670915b48d790084 differ
diff --git a/tests-clar/resources/renames/.gitted/objects/36/020db6cdacaa93497f31edcd8f242ff9bc366d b/tests-clar/resources/renames/.gitted/objects/36/020db6cdacaa93497f31edcd8f242ff9bc366d
new file mode 100644
index 0000000..f4f9303
Binary files /dev/null and b/tests-clar/resources/renames/.gitted/objects/36/020db6cdacaa93497f31edcd8f242ff9bc366d differ
diff --git a/tests-clar/resources/renames/.gitted/objects/3c/04741dd4b96c4ae4b00ec0f6e10c816a30aad2 b/tests-clar/resources/renames/.gitted/objects/3c/04741dd4b96c4ae4b00ec0f6e10c816a30aad2
new file mode 100644
index 0000000..c236022
Binary files /dev/null and b/tests-clar/resources/renames/.gitted/objects/3c/04741dd4b96c4ae4b00ec0f6e10c816a30aad2 differ
diff --git a/tests-clar/resources/renames/.gitted/objects/42/10ffd5c390b21dd5483375e75288dea9ede512 b/tests-clar/resources/renames/.gitted/objects/42/10ffd5c390b21dd5483375e75288dea9ede512
new file mode 100644
index 0000000..d351a6d
Binary files /dev/null and b/tests-clar/resources/renames/.gitted/objects/42/10ffd5c390b21dd5483375e75288dea9ede512 differ
diff --git a/tests-clar/resources/renames/.gitted/objects/4e/4cae3e7dd56ed74bff39526d0469e554432953 b/tests-clar/resources/renames/.gitted/objects/4e/4cae3e7dd56ed74bff39526d0469e554432953
new file mode 100644
index 0000000..5e6ebd5
Binary files /dev/null and b/tests-clar/resources/renames/.gitted/objects/4e/4cae3e7dd56ed74bff39526d0469e554432953 differ
diff --git a/tests-clar/resources/renames/.gitted/objects/5e/26abc56a5a84d89790f45416648899cbe13109 b/tests-clar/resources/renames/.gitted/objects/5e/26abc56a5a84d89790f45416648899cbe13109
new file mode 100644
index 0000000..2acd3d5
Binary files /dev/null and b/tests-clar/resources/renames/.gitted/objects/5e/26abc56a5a84d89790f45416648899cbe13109 differ
diff --git a/tests-clar/resources/renames/.gitted/objects/9a/69d960ae94b060f56c2a8702545e2bb1abb935 b/tests-clar/resources/renames/.gitted/objects/9a/69d960ae94b060f56c2a8702545e2bb1abb935
new file mode 100644
index 0000000..f75178c
Binary files /dev/null and b/tests-clar/resources/renames/.gitted/objects/9a/69d960ae94b060f56c2a8702545e2bb1abb935 differ
diff --git a/tests-clar/resources/renames/.gitted/objects/d7/9b202de198fa61b02424b9e25e840dc75e1323 b/tests-clar/resources/renames/.gitted/objects/d7/9b202de198fa61b02424b9e25e840dc75e1323
new file mode 100644
index 0000000..daa2b39
Binary files /dev/null and b/tests-clar/resources/renames/.gitted/objects/d7/9b202de198fa61b02424b9e25e840dc75e1323 differ
diff --git a/tests-clar/resources/renames/.gitted/objects/ea/f4a3e3bfe68585e90cada20736ace491cd100b b/tests-clar/resources/renames/.gitted/objects/ea/f4a3e3bfe68585e90cada20736ace491cd100b
new file mode 100644
index 0000000..f72df8d
--- /dev/null
+++ b/tests-clar/resources/renames/.gitted/objects/ea/f4a3e3bfe68585e90cada20736ace491cd100b
@@ -0,0 +1,5 @@
+x}R@<_QT.A[
+{t2Cez%L'!aj!x{'+;$)Ξ#yǘ#q#hc
QDXmR|aD*O+^ZI~i>3a!,R!-EaI>yo*4戁Rf	m&eIA*!;dݬ
+Ho
+OUDyTVHpwqH7Ʒ.ts6{Z+X\)C5Q9
+%t*&&v;|'4Du[7he!NK*"C-=`#؎$Ee2T|@NBsslW|/0¬aȥJNv)-ڡiۤ3bbO:uWMNX7T
\ No newline at end of file
diff --git a/tests-clar/resources/renames/.gitted/objects/f9/0d4fc20ecddf21eebe6a37e9225d244339d2b5 b/tests-clar/resources/renames/.gitted/objects/f9/0d4fc20ecddf21eebe6a37e9225d244339d2b5
new file mode 100644
index 0000000..f6d933b
Binary files /dev/null and b/tests-clar/resources/renames/.gitted/objects/f9/0d4fc20ecddf21eebe6a37e9225d244339d2b5 differ
diff --git a/tests-clar/resources/renames/.gitted/refs/heads/master b/tests-clar/resources/renames/.gitted/refs/heads/master
index 049b1f5..642c319 100644
--- a/tests-clar/resources/renames/.gitted/refs/heads/master
+++ b/tests-clar/resources/renames/.gitted/refs/heads/master
@@ -1 +1 @@
-2bc7f351d20b53f1c72c16c4b036e491c478c49a
+19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13
diff --git a/tests-clar/resources/renames/ikeepsix.txt b/tests-clar/resources/renames/ikeepsix.txt
new file mode 100644
index 0000000..eaf4a3e
--- /dev/null
+++ b/tests-clar/resources/renames/ikeepsix.txt
@@ -0,0 +1,27 @@
+I Keep Six Honest Serving-Men
+=============================
+
+She sends'em abroad on her own affairs,
+ From the second she opens her eyes—
+One million Hows, two million Wheres,
+And seven million Whys!
+
+I let them rest from nine till five,
+ For I am busy then,
+As well as breakfast, lunch, and tea,
+ For they are hungry men.
+But different folk have different views;
+I know a person small—
+She keeps ten million serving-men,
+Who get no rest at all!
+
+  -- Rudyard Kipling
+
+I KEEP six honest serving-men
+ (They taught me all I knew);
+Their names are What and Why and When
+ And How and Where and Who.
+I send them over land and sea,
+ I send them east and west;
+But after they have worked for me,
+ I give them all a rest.
diff --git a/tests-clar/resources/renames/sevencities.txt b/tests-clar/resources/renames/sevencities.txt
deleted file mode 100644
index 66311f5..0000000
--- a/tests-clar/resources/renames/sevencities.txt
+++ /dev/null
@@ -1,49 +0,0 @@
-The Song of Seven Cities
-========================
-
-I WAS Lord of Cities very sumptuously builded.
-Seven roaring Cities paid me tribute from afar.
-Ivory their outposts were—the guardrooms of them gilded,
-And garrisoned with Amazons invincible in war.
-
-All the world went softly when it walked before my Cities—
-Neither King nor Army vexed my peoples at their toil,
-Never horse nor chariot irked or overbore my Cities,
-Never Mob nor Ruler questioned whence they drew their spoil.
-
-Banded, mailed and arrogant from sunrise unto sunset;
-Singing while they sacked it, they possessed the land at large.
-Yet when men would rob them, they resisted, they made onset
-And pierced the smoke of battle with a thousand-sabred charge.
-
-So they warred and trafficked only yesterday, my Cities.
-To-day there is no mark or mound of where my Cities stood.
-For the River rose at midnight and it washed away my Cities.
-They are evened with Atlantis and the towns before the Flood.
-
-Rain on rain-gorged channels raised the water-levels round them,
-Freshet backed on freshet swelled and swept their world from sight,
-Till the emboldened floods linked arms and, flashing forward, drowned them—
-Drowned my Seven Cities and their peoples in one night!
-
-Low among the alders lie their derelict foundations,
-The beams wherein they trusted and the plinths whereon they built—
-My rulers and their treasure and their unborn populations,
-Dead, destroyed, aborted, and defiled with mud and silt!
-
-The Daughters of the Palace whom they cherished in my Cities,
-My silver-tongued Princesses, and the promise of their May—
-Their bridegrooms of the June-tide—all have perished in my Cities,
-With the harsh envenomed virgins that can neither love nor play.
-
-I was Lord of Cities—I will build anew my Cities,
-Seven, set on rocks, above the wrath of any flood.
-Nor will I rest from search till I have filled anew my Cities
-With peoples undefeated of the dark, enduring blood.
-
-To the sound of trumpets shall their seed restore my Cities
-Wealthy and well-weaponed, that once more may I behold
-All the world go softly when it walks before my Cities,
-And the horses and the chariots fleeing from them as of old!
-
-                -- Rudyard Kipling
diff --git a/tests-clar/resources/renames/sixserving.txt b/tests-clar/resources/renames/sixserving.txt
index ad0a8e5..f90d4fc 100644
--- a/tests-clar/resources/renames/sixserving.txt
+++ b/tests-clar/resources/renames/sixserving.txt
@@ -1,24 +1,25 @@
-I KEEP six honest serving-men
- (They taught me all I knew);
-Their names are What and Why and When
- And How and Where and Who.
-I send them over land and sea,
- I send them east and west;
-But after they have worked for me,
- I give them all a rest.
+I  KEEP  six  honest  serving-men
+  (They  taught  me  all  I  knew);
+Their  names  are  What  and  Why  and  When
+  And  How  and  Where  and  Who.
+I  send  them  over  land  and  sea,
+  I  send  them  east  and  west;
+But  after  they  have  worked  for  me,
+  I  give  them  all  a  rest.
 
-I let them rest from nine till five,
- For I am busy then,
-As well as breakfast, lunch, and tea,
- For they are hungry men.
-But different folk have different views;
-I know a person small—
-She keeps ten million serving-men,
-Who get no rest at all!
+I  let  them  rest  from  nine  till  five,
+  For  I  am  busy  then,
+As  well  as  breakfast,  lunch,  and  tea,
+  For  they  are  hungry  men.
+But  different  folk  have  different  views;
+I  know  a  person  small—
+She  keeps  ten  million  serving-men,
+Who  get  no  rest  at  all!
 
-She sends'em abroad on her own affairs,
- From the second she opens her eyes—
-One million Hows, two million Wheres,
-And seven million Whys!
+She  sends'em  abroad  on  her  own  affairs,
+  From  the  second  she  opens  her  eyes—
+One  million  Hows,  two  million  Wheres,
+And  seven  million  Whys!
+
+    --  Rudyard  Kipling
 
-                -- Rudyard Kipling
diff --git a/tests-clar/resources/renames/songof7cities.txt b/tests-clar/resources/renames/songof7cities.txt
new file mode 100644
index 0000000..4210ffd
--- /dev/null
+++ b/tests-clar/resources/renames/songof7cities.txt
@@ -0,0 +1,49 @@
+The Song of Seven Cities
+------------------------
+
+I WAS Lord of Cities very sumptuously builded.
+Seven roaring Cities paid me tribute from afar.
+Ivory their outposts were--the guardrooms of them gilded,
+And garrisoned with Amazons invincible in war.
+
+All the world went softly when it walked before my Cities--
+Neither King nor Army vexed my peoples at their toil,
+Never horse nor chariot irked or overbore my Cities,
+Never Mob nor Ruler questioned whence they drew their spoil.
+
+Banded, mailed and arrogant from sunrise unto sunset;
+Singing while they sacked it, they possessed the land at large.
+Yet when men would rob them, they resisted, they made onset
+And pierced the smoke of battle with a thousand-sabred charge.
+
+So they warred and trafficked only yesterday, my Cities.
+To-day there is no mark or mound of where my Cities stood.
+For the River rose at midnight and it washed away my Cities.
+They are evened with Atlantis and the towns before the Flood.
+
+Rain on rain-gorged channels raised the water-levels round them,
+Freshet backed on freshet swelled and swept their world from sight,
+Till the emboldened floods linked arms and, flashing forward, drowned them--
+Drowned my Seven Cities and their peoples in one night!
+
+Low among the alders lie their derelict foundations,
+The beams wherein they trusted and the plinths whereon they built--
+My rulers and their treasure and their unborn populations,
+Dead, destroyed, aborted, and defiled with mud and silt!
+
+The Daughters of the Palace whom they cherished in my Cities,
+My silver-tongued Princesses, and the promise of their May--
+Their bridegrooms of the June-tide--all have perished in my Cities,
+With the harsh envenomed virgins that can neither love nor play.
+
+I was Lord of Cities--I will build anew my Cities,
+Seven, set on rocks, above the wrath of any flood.
+Nor will I rest from search till I have filled anew my Cities
+With peoples undefeated of the dark, enduring blood.
+
+To the sound of trumpets shall their seed restore my Cities
+Wealthy and well-weaponed, that once more may I behold
+All the world go softly when it walks before my Cities,
+And the horses and the chariots fleeing from them as of old!
+
+  -- Rudyard Kipling
diff --git a/tests-clar/resources/renames/songofseven.txt b/tests-clar/resources/renames/songofseven.txt
deleted file mode 100644
index 66311f5..0000000
--- a/tests-clar/resources/renames/songofseven.txt
+++ /dev/null
@@ -1,49 +0,0 @@
-The Song of Seven Cities
-========================
-
-I WAS Lord of Cities very sumptuously builded.
-Seven roaring Cities paid me tribute from afar.
-Ivory their outposts were—the guardrooms of them gilded,
-And garrisoned with Amazons invincible in war.
-
-All the world went softly when it walked before my Cities—
-Neither King nor Army vexed my peoples at their toil,
-Never horse nor chariot irked or overbore my Cities,
-Never Mob nor Ruler questioned whence they drew their spoil.
-
-Banded, mailed and arrogant from sunrise unto sunset;
-Singing while they sacked it, they possessed the land at large.
-Yet when men would rob them, they resisted, they made onset
-And pierced the smoke of battle with a thousand-sabred charge.
-
-So they warred and trafficked only yesterday, my Cities.
-To-day there is no mark or mound of where my Cities stood.
-For the River rose at midnight and it washed away my Cities.
-They are evened with Atlantis and the towns before the Flood.
-
-Rain on rain-gorged channels raised the water-levels round them,
-Freshet backed on freshet swelled and swept their world from sight,
-Till the emboldened floods linked arms and, flashing forward, drowned them—
-Drowned my Seven Cities and their peoples in one night!
-
-Low among the alders lie their derelict foundations,
-The beams wherein they trusted and the plinths whereon they built—
-My rulers and their treasure and their unborn populations,
-Dead, destroyed, aborted, and defiled with mud and silt!
-
-The Daughters of the Palace whom they cherished in my Cities,
-My silver-tongued Princesses, and the promise of their May—
-Their bridegrooms of the June-tide—all have perished in my Cities,
-With the harsh envenomed virgins that can neither love nor play.
-
-I was Lord of Cities—I will build anew my Cities,
-Seven, set on rocks, above the wrath of any flood.
-Nor will I rest from search till I have filled anew my Cities
-With peoples undefeated of the dark, enduring blood.
-
-To the sound of trumpets shall their seed restore my Cities
-Wealthy and well-weaponed, that once more may I behold
-All the world go softly when it walks before my Cities,
-And the horses and the chariots fleeing from them as of old!
-
-                -- Rudyard Kipling
diff --git a/tests-clar/resources/renames/untimely.txt b/tests-clar/resources/renames/untimely.txt
new file mode 100644
index 0000000..9a69d96
--- /dev/null
+++ b/tests-clar/resources/renames/untimely.txt
@@ -0,0 +1,24 @@
+Untimely
+========
+
+Nothing in life has been made by man for man's using
+But it was shown long since to man in ages
+Lost as the name of the maker of it,
+Who received oppression and shame for his wages--
+Hate, avoidance, and scorn in his daily dealings--
+Until he perished, wholly confounded
+
+More to be pitied than he are the wise
+Souls which foresaw the evil of loosing
+Knowledge or Art before time, and aborted
+Noble devices and deep-wrought healings,
+Lest offense should arise.
+
+Heaven delivers on earth the Hour that cannot be
+  thwarted,
+Neither advanced, at the price of a world nor a soul,
+  and its Prophet
+Comes through the blood of the vanguards who
+  dreamed--too soon--it had sounded.
+
+                -- Rudyard Kipling