File: diff_test.rb

package info (click to toggle)
ruby-rugged 1.9.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,752 kB
  • sloc: ansic: 8,722; ruby: 7,473; sh: 99; makefile: 5
file content (1281 lines) | stat: -rw-r--r-- 40,797 bytes parent folder | download | duplicates (3)
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
require "test_helper"

class PatchFromStringsTest < Rugged::TestCase
  def test_from_strings_no_args
    patch = Rugged::Patch.from_strings()
    assert_equal 0, patch.size
    assert_equal "", patch.to_s
  end

  def test_from_strings_create_file
    patch = Rugged::Patch.from_strings(nil, "added\n")
    assert_equal 1, patch.size
    assert_equal <<-EOS, patch.to_s
diff --git a/file b/file
new file mode 100644
index 0000000..d5f7fc3
--- /dev/null
+++ b/file
@@ -0,0 +1 @@
+added
EOS
  end

  def test_from_strings_delete_file
    patch = Rugged::Patch.from_strings("deleted\n", nil)
    assert_equal 1, patch.size
    assert_equal <<-EOS, patch.to_s
diff --git a/file b/file
deleted file mode 100644
index 71779d2..0000000
--- a/file
+++ /dev/null
@@ -1 +0,0 @@
-deleted
EOS
  end

  def test_from_strings_without_paths
    patch = Rugged::Patch.from_strings("deleted\n", "added\n")
    assert_equal 1, patch.size
    assert_equal <<-EOS, patch.to_s
diff --git a/file b/file
index 71779d2..d5f7fc3 100644
--- a/file
+++ b/file
@@ -1 +1 @@
-deleted
+added
EOS
  end

  def test_from_strings_with_custom_paths
    patch = Rugged::Patch.from_strings("deleted\n", "added\n", old_path: "old", new_path: "new")
    assert_equal 1, patch.size
    assert_equal <<-EOS, patch.to_s
diff --git a/old b/new
index 71779d2..d5f7fc3 100644
--- a/old
+++ b/new
@@ -1 +1 @@
-deleted
+added
EOS
  end
end

class RepoDiffTest < Rugged::TestCase
  def test_new_from_buffer
    repo    = FixtureRepo.from_libgit2("attr")
    patch1  = Rugged::Patch.from_strings("deleted\n", "added\n", old_path: "old", new_path: "new").to_s
    diff1   = repo.diff_from_buffer(patch1)
    assert_equal diff1.patch, patch1
    
    diff2   = repo.diff("605812a", "370fe9ec22", :context_lines => 1, :interhunk_lines => 1)
    patch2  = diff2.patch
    diff3   = repo.diff_from_buffer(patch2)
    assert_equal diff3.patch, patch2
  end
  
  def test_with_oid_string
    repo = FixtureRepo.from_libgit2("attr")
    diff = repo.diff("605812a", "370fe9ec22", :context_lines => 1, :interhunk_lines => 1)

    deltas = diff.deltas
    patches = diff.patches
    hunks = patches.map(&:hunks).flatten
    lines = hunks.map(&:lines).flatten
    bytesize = patches.inject(0) {|n, p| n += p.bytesize(exclude_context: true)}

    assert_equal 5, diff.size
    assert_equal 5, deltas.size
    assert_equal 5, patches.size
    assert_equal 1589, bytesize

    assert_equal 2, deltas.select(&:added?).size
    assert_equal 1, deltas.select(&:deleted?).size
    assert_equal 2, deltas.select(&:modified?).size

    assert_equal 5, hunks.size

    assert_equal((7 + 24 + 1 + 6 + 6), lines.size)
    assert_equal((1), lines.select(&:context?).size)
    assert_equal((24 + 1 + 5 + 5), lines.select(&:addition?).size)
    assert_equal((7 + 1), lines.select(&:deletion?).size)
  end

  def test_delta_status_char
    repo = FixtureRepo.from_libgit2("attr")
    diff = repo.diff("605812a", "370fe9ec22", :context_lines => 1, :interhunk_lines => 1)

    deltas = diff.deltas

    assert_equal :D, deltas[0].status_char
    assert_equal :A, deltas[1].status_char
    assert_equal :A, deltas[2].status_char
    assert_equal :M, deltas[3].status_char
    assert_equal :M, deltas[4].status_char
  end

  def test_with_nil_on_left_side
    repo = FixtureRepo.from_libgit2("attr")
    diff = repo.diff("605812a", nil, :context_lines => 1, :interhunk_lines => 1)

    deltas = diff.deltas
    patches = diff.patches
    hunks = patches.map(&:hunks).flatten
    lines = hunks.map(&:lines).flatten

    assert_equal 16, diff.size
    assert_equal 16, deltas.size
    assert_equal 16, patches.size

    assert_equal 0, deltas.select(&:added?).size
    assert_equal 16, deltas.select(&:deleted?).size
    assert_equal 0, deltas.select(&:modified?).size

    assert_equal 15, hunks.size

    assert_equal 115, lines.size
    assert_equal 0, lines.select(&:context?).size
    assert_equal 0, lines.select(&:addition?).size
    assert_equal 113, lines.select(&:deletion?).size
  end

  def test_with_nil_on_right_side
    repo = FixtureRepo.from_libgit2("attr")
    diff = repo.diff(nil, "605812a", :context_lines => 1, :interhunk_lines => 1)

    deltas = diff.deltas
    patches = diff.patches
    hunks = patches.map(&:hunks).flatten
    lines = hunks.map(&:lines).flatten

    assert_equal 16, diff.size
    assert_equal 16, deltas.size
    assert_equal 16, patches.size

    assert_equal 16, deltas.select(&:added?).size
    assert_equal 0, deltas.select(&:deleted?).size
    assert_equal 0, deltas.select(&:modified?).size

    assert_equal 15, hunks.size

    assert_equal 115, lines.size
    assert_equal 0, lines.select(&:context?).size
    assert_equal 113, lines.select(&:addition?).size
    assert_equal 0, lines.select(&:deletion?).size
  end
end

class RepoWorkdirDiffTest < Rugged::TestCase
  def test_basic_diff
    repo = FixtureRepo.from_libgit2("status")
    diff = repo.diff_workdir("26a125ee1bf",
      :context_lines => 3,
      :interhunk_lines => 1,
      :include_ignored => true,
      :include_untracked => true
    )

    deltas = diff.deltas
    patches = diff.patches
    hunks = patches.map(&:hunks).flatten
    lines = hunks.map(&:lines).flatten

    assert_equal 14, deltas.size
    assert_equal 14, patches.size

    assert_equal 0, deltas.select(&:added?).size
    assert_equal 4, deltas.select(&:deleted?).size
    assert_equal 4, deltas.select(&:modified?).size
    assert_equal 1, deltas.select(&:ignored?).size
    assert_equal 5, deltas.select(&:untracked?).size

    assert_equal 8, hunks.size

    assert_equal 13, lines.size
    assert_equal 4, lines.select(&:context?).size
    assert_equal 5, lines.select(&:addition?).size
    assert_equal 4, lines.select(&:deletion?).size
  end
end

class CommitDiffTest < Rugged::TestCase
  def test_diff_with_parent
    repo = FixtureRepo.from_libgit2("attr")
    commit = Rugged::Commit.lookup(repo, "605812a")

    diff = commit.diff(:context_lines => 1, :interhunk_lines => 1)

    deltas = diff.deltas
    patches = diff.patches
    hunks = patches.map(&:hunks).flatten
    lines = hunks.map(&:lines).flatten

    assert_equal 5, diff.size
    assert_equal 5, deltas.size
    assert_equal 5, patches.size

    assert_equal 3, deltas.select(&:added?).size
    assert_equal 0, deltas.select(&:deleted?).size
    assert_equal 2, deltas.select(&:modified?).size

    assert_equal 4, hunks.size

    assert_equal(51, lines.size)
    assert_equal(2, lines.select(&:context?).size)
    assert_equal(46, lines.select(&:addition?).size)
    assert_equal(3, lines.select(&:deletion?).size)
  end

  def test_diff_with_parent_no_options
    repo = FixtureRepo.from_libgit2("attr")
    commit = Rugged::Commit.lookup(repo, "605812a")

    diff = commit.diff

    deltas = diff.deltas
    patches = diff.patches
    hunks = patches.map(&:hunks).flatten
    lines = hunks.map(&:lines).flatten

    assert_equal 5, diff.size
    assert_equal 5, deltas.size
    assert_equal 5, patches.size

    assert_equal 3, deltas.select(&:added?).size
    assert_equal 0, deltas.select(&:deleted?).size
    assert_equal 2, deltas.select(&:modified?).size

    assert_equal 4, hunks.size

    assert_equal(53, lines.size)
    assert_equal(4, lines.select(&:context?).size)
    assert_equal(46, lines.select(&:addition?).size)
    assert_equal(3, lines.select(&:deletion?).size)
  end

  def test_diff_with_parent_for_initial_commit
    repo = FixtureRepo.from_libgit2("attr")
    commit = Rugged::Commit.lookup(repo, "6bab5c79cd5140d0f800917f550eb2a3dc32b0da")

    diff = commit.diff(:context_lines => 1, :interhunk_lines => 1)

    deltas = diff.deltas
    patches = diff.patches
    hunks = patches.map(&:hunks).flatten
    lines = hunks.map(&:lines).flatten

    assert_equal 13, diff.size
    assert_equal 13, deltas.size
    assert_equal 13, patches.size

    assert_equal 13, deltas.select(&:added?).size
    assert_equal 0, deltas.select(&:deleted?).size
    assert_equal 0, deltas.select(&:modified?).size

    assert_equal 13, hunks.size

    assert_equal(72, lines.size)
    assert_equal(0, lines.select(&:context?).size)
    assert_equal(70, lines.select(&:addition?).size)
    assert_equal(0, lines.select(&:deletion?).size)
  end
end

class CommitToWorkdirDiffTest < Rugged::TestCase
  def test_basic_diff
    repo = FixtureRepo.from_libgit2("status")
    a = Rugged::Commit.lookup(repo, "26a125ee1bf")

    diff = a.diff_workdir(
      :context_lines => 3,
      :interhunk_lines => 1,
      :include_ignored => true,
      :include_untracked => true
    )

    deltas = diff.deltas
    patches = diff.patches
    hunks = patches.map(&:hunks).flatten
    lines = hunks.map(&:lines).flatten

    assert_equal 14, deltas.size
    assert_equal 14, patches.size

    assert_equal 0, deltas.select(&:added?).size
    assert_equal 4, deltas.select(&:deleted?).size
    assert_equal 4, deltas.select(&:modified?).size
    assert_equal 1, deltas.select(&:ignored?).size
    assert_equal 5, deltas.select(&:untracked?).size

    assert_equal 8, hunks.size

    assert_equal 13, lines.size
    assert_equal 4, lines.select(&:context?).size
    assert_equal 5, lines.select(&:addition?).size
    assert_equal 4, lines.select(&:deletion?).size
  end
end

class TreeToWorkdirDiffTest < Rugged::TestCase
  def test_basic_diff
    repo = FixtureRepo.from_libgit2("status")
    a = Rugged::Commit.lookup(repo, "26a125ee1bf").tree

    diff = a.diff_workdir(
      :context_lines => 3,
      :interhunk_lines => 1,
      :include_ignored => true,
      :include_untracked => true
    )

    deltas = diff.deltas
    patches = diff.patches
    hunks = patches.map(&:hunks).flatten
    lines = hunks.map(&:lines).flatten

    assert_equal 14, deltas.size
    assert_equal 14, patches.size

    assert_equal 0, deltas.select(&:added?).size
    assert_equal 4, deltas.select(&:deleted?).size
    assert_equal 4, deltas.select(&:modified?).size
    assert_equal 1, deltas.select(&:ignored?).size
    assert_equal 5, deltas.select(&:untracked?).size

    assert_equal 8, hunks.size

    assert_equal 13, lines.size
    assert_equal 4, lines.select(&:context?).size
    assert_equal 5, lines.select(&:addition?).size
    assert_equal 4, lines.select(&:deletion?).size
  end

  def test_diff_merge
    repo = FixtureRepo.from_libgit2("status")
    index = repo.index

    a = Rugged::Commit.lookup(repo, "26a125ee1bf").tree

    # merge diffs to simulate "git diff 26a125ee1bf"

    diff  = a.diff(index, :include_ignored => true, :include_untracked => true)
    diff2 = index.diff(:include_ignored => true, :include_untracked => true)
    diff.merge!(diff2)

    deltas = diff.deltas
    patches = diff.patches
    hunks = patches.map(&:hunks).flatten
    lines = hunks.map(&:lines).flatten

    # expected values differ from "git diff --porcelain 26a125ee1bf"
    # because that includes the file "staged_delete_modified_file" twice,
    # once in the deleted list and again the untracked list and libgit2
    # does not do that with a merged diff (though it would with status)

    assert_equal 15, deltas.size
    assert_equal 15, patches.size

    assert_equal 2, deltas.select(&:added?).size
    assert_equal 5, deltas.select(&:deleted?).size
    assert_equal 4, deltas.select(&:modified?).size
    assert_equal 1, deltas.select(&:ignored?).size
    assert_equal 3, deltas.select(&:untracked?).size

    assert_equal 11, hunks.size

    assert_equal 17, lines.size
    assert_equal 4, lines.select(&:context?).size
    assert_equal 8, lines.select(&:addition?).size
    assert_equal 5, lines.select(&:deletion?).size
  end

  def test_stats
    repo = FixtureRepo.from_libgit2("status")
    index = repo.index

    a = Rugged::Commit.lookup(repo, "26a125ee1bf").tree

    # merge diffs to simulate "git diff 26a125ee1bf"

    diff  = a.diff(index, :include_ignored => true, :include_untracked => true)
    diff2 = index.diff(:include_ignored => true, :include_untracked => true)
    diff.merge!(diff2)

    # expected values from: git diff --stat 26a125ee1bf
    files, adds, dels = diff.stat
    assert_equal 11, files
    assert_equal 8, adds
    assert_equal 5, dels

    # expected per-file values from the diff --stat output plus total lines
    expected_patch_stat = [
      [ 0, 1, 1, 1 ], [ 1, 0, 2, 1 ], [ 1, 0, 2, 1 ], [ 0, 1, 1, 1 ], [ 2, 0, 3, 2 ],
      [ 0, 1, 1, 1 ], [ 0, 1, 1, 1 ], [ 1, 0, 1, 1 ], [ 2, 0, 2, 2 ], [ 0, 1, 1, 1 ],
      [ 1, 0, 2, 1 ]
    ]

    diff.each_patch do |patch|
      next if [:unmodified, :ignored, :untracked].include? patch.delta.status

      expected_adds, expected_dels, expected_lines, lines_wo_context = expected_patch_stat.shift

      actual_adds, actual_dels = patch.stat

      assert_equal expected_adds, actual_adds
      assert_equal expected_dels, actual_dels
      assert_equal expected_adds + expected_dels, patch.changes

      assert_equal expected_lines, patch.lines
      assert_equal lines_wo_context, patch.lines(exclude_context: true)
    end
  end
end

class TreeToTreeDiffTest < Rugged::TestCase
  def test_basic_diff
    repo = FixtureRepo.from_libgit2("attr")
    a = Rugged::Commit.lookup(repo, "605812a").tree
    b = Rugged::Commit.lookup(repo, "370fe9ec22").tree
    c = Rugged::Commit.lookup(repo, "f5b0af1fb4f5c").tree

    diff = a.diff(b, :context_lines => 1, :interhunk_lines => 1)

    deltas = diff.deltas
    patches = diff.patches
    hunks = patches.map(&:hunks).flatten
    lines = hunks.map(&:lines).flatten

    assert_equal 5, diff.size
    assert_equal 5, deltas.size
    assert_equal 5, patches.size

    assert_equal 2, deltas.select(&:added?).size
    assert_equal 1, deltas.select(&:deleted?).size
    assert_equal 2, deltas.select(&:modified?).size

    assert_equal 5, hunks.size

    assert_equal((7 + 24 + 1 + 6 + 6), lines.size)
    assert_equal((1), lines.select(&:context?).size)
    assert_equal((24 + 1 + 5 + 5), lines.select(&:addition?).size)
    assert_equal((7 + 1), lines.select(&:deletion?).size)


    diff = c.diff(b, :context_lines => 1, :interhunk_lines => 1)
    deltas = diff.deltas
    patches = diff.patches
    hunks = patches.map(&:hunks).flatten
    lines = hunks.map(&:lines).flatten

    assert_equal 2, deltas.size
    assert_equal 2, patches.size

    assert_equal 0, deltas.select(&:added?).size
    assert_equal 0, deltas.select(&:deleted?).size
    assert_equal 2, deltas.select(&:modified?).size

    assert_equal 2, hunks.size

    assert_equal((8 + 15), lines.size)
    assert_equal((1), lines.select(&:context?).size)
    assert_equal((1), lines.select(&:addition?).size)
    assert_equal((7 + 14), lines.select(&:deletion?).size)
  end

  def test_diff_with_empty_tree
    repo = FixtureRepo.from_libgit2("attr")
    a = Rugged::Commit.lookup(repo, "605812a").tree

    diff = a.diff(nil, :context_lines => 1, :interhunk_lines => 1)

    deltas = diff.deltas
    patches = diff.patches
    hunks = patches.map(&:hunks).flatten
    lines = hunks.map(&:lines).flatten

    assert_equal 16, diff.size
    assert_equal 16, deltas.size
    assert_equal 16, patches.size

    assert_equal 0, deltas.select(&:added?).size
    assert_equal 16, deltas.select(&:deleted?).size
    assert_equal 0, deltas.select(&:modified?).size

    assert_equal 15, hunks.size

    assert_equal 115, lines.size
    assert_equal 0, lines.select(&:context?).size
    assert_equal 0, lines.select(&:addition?).size
    assert_equal 113, lines.select(&:deletion?).size
  end

  def test_diff_with_rev_string
    repo = FixtureRepo.from_libgit2("attr")
    a = Rugged::Commit.lookup(repo, "605812a").tree

    diff = a.diff("370fe9ec22", :context_lines => 1, :interhunk_lines => 1)

    deltas = diff.deltas
    patches = diff.patches
    hunks = patches.map(&:hunks).flatten
    lines = hunks.map(&:lines).flatten

    assert_equal 5, diff.size
    assert_equal 5, deltas.size
    assert_equal 5, patches.size

    assert_equal 2, deltas.select(&:added?).size
    assert_equal 1, deltas.select(&:deleted?).size
    assert_equal 2, deltas.select(&:modified?).size

    assert_equal 5, hunks.size

    assert_equal((7 + 24 + 1 + 6 + 6), lines.size)
    assert_equal((1), lines.select(&:context?).size)
    assert_equal((24 + 1 + 5 + 5), lines.select(&:addition?).size)
    assert_equal((7 + 1), lines.select(&:deletion?).size)
  end

  def test_diff_merge
    repo = FixtureRepo.from_libgit2("attr")
    a = Rugged::Commit.lookup(repo, "605812a").tree
    b = Rugged::Commit.lookup(repo, "370fe9ec22").tree
    c = Rugged::Commit.lookup(repo, "f5b0af1fb4f5c").tree

    diff = a.diff(b).merge!(c.diff(b))

    deltas = diff.deltas
    patches = diff.patches
    hunks = patches.map(&:hunks).flatten
    lines = hunks.map(&:lines).flatten

    assert_equal 6, diff.size
    assert_equal 6, patches.size
    assert_equal 6, deltas.size

    assert_equal 2, deltas.select(&:added?).size
    assert_equal 1, deltas.select(&:deleted?).size
    assert_equal 3, deltas.select(&:modified?).size

    assert_equal 6, hunks.size

    assert_equal 59, lines.size
    assert_equal 1, lines.select(&:context?).size
    assert_equal 36, lines.select(&:addition?).size
    assert_equal 22, lines.select(&:deletion?).size
  end

  def test_symlink_blob_mode_changed_to_regular_file
    repo = FixtureRepo.from_libgit2("unsymlinked.git")

    a = Rugged::Commit.lookup(repo, "7fccd7").tree
    b = Rugged::Commit.lookup(repo, "806999").tree

    diff = a.diff(b)

    deltas = diff.deltas
    patches = diff.patches

    assert_equal 3, diff.size
    assert_equal 3, patches.size
    assert_equal 3, deltas.size

    assert_equal 1, deltas.select(&:added?).size
    assert_equal 2, deltas.select(&:deleted?).size
    assert_equal 0, deltas.select(&:modified?).size
    assert_equal 0, deltas.select(&:typechange?).size
  end

  def test_symlink_blob_mode_changed_to_regular_file_as_typechange
    repo = FixtureRepo.from_libgit2("unsymlinked.git")

    a = Rugged::Commit.lookup(repo, "7fccd7").tree
    b = Rugged::Commit.lookup(repo, "806999").tree

    diff = a.diff(b, :include_typechange => true)

    deltas = diff.deltas
    patches = diff.patches

    assert_equal 2, diff.size
    assert_equal 2, patches.size
    assert_equal 2, deltas.size

    assert_equal 0, deltas.select(&:added?).size
    assert_equal 1, deltas.select(&:deleted?).size
    assert_equal 0, deltas.select(&:modified?).size
    assert_equal 1, deltas.select(&:typechange?).size
  end

  def test_regular_blob_mode_changed_to_executable_file
    repo = FixtureRepo.from_libgit2("unsymlinked.git")

    a = Rugged::Commit.lookup(repo, "806999").tree
    b = Rugged::Commit.lookup(repo, "a8595c").tree

    diff = a.diff(b)

    deltas = diff.deltas
    patches = diff.patches

    assert_equal 1, diff.size
    assert_equal 1, patches.size
    assert_equal 1, deltas.size

    assert_equal 0, deltas.select(&:added?).size
    assert_equal 0, deltas.select(&:deleted?).size
    assert_equal 1, deltas.select(&:modified?).size
    assert_equal 0, deltas.select(&:typechange?).size
  end

  def test_size
    repo = FixtureRepo.from_libgit2("diff")

    a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69")
    b = repo.lookup("7a9e0b02e63179929fed24f0a3e0f19168114d10")

    diff = a.tree.diff(b.tree)
    assert_equal 2, diff.size
  end

  def test_each_delta
    repo = FixtureRepo.from_libgit2("diff")

    a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69")
    b = repo.lookup("7a9e0b02e63179929fed24f0a3e0f19168114d10")

    diff = a.tree.diff(b.tree)

    deltas = []
    diff.each_delta do |delta|
      assert_instance_of Rugged::Diff::Delta, delta
      deltas << delta
    end

    assert_equal 2, deltas.size

    assert_equal "another.txt", deltas[0].old_file[:path]
    assert_equal "another.txt", deltas[0].new_file[:path]

    refute deltas[0].binary?

    assert_equal "readme.txt", deltas[1].old_file[:path]
    assert_equal "readme.txt", deltas[1].new_file[:path]

    refute deltas[1].binary?
  end

  def test_diff_treats_files_bigger_as_max_size_as_binary
    repo = FixtureRepo.from_libgit2("diff")

    a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69")
    b = repo.lookup("7a9e0b02e63179929fed24f0a3e0f19168114d10")

    diff = a.tree.diff(b.tree, :max_size => 10)
    assert_equal 2, diff.patches.size
    assert_equal <<-EOS, diff.patch
diff --git a/another.txt b/another.txt
index 3e5bcba..546c735 100644
Binary files a/another.txt and b/another.txt differ
diff --git a/readme.txt b/readme.txt
index 7b808f7..29ab705 100644
Binary files a/readme.txt and b/readme.txt differ
EOS
  end

  def test_contraining_paths
    repo = FixtureRepo.from_libgit2("diff")

    a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69")
    b = repo.lookup("7a9e0b02e63179929fed24f0a3e0f19168114d10")

    diff = a.tree.diff(b.tree, :paths => ["readme.txt"])
    assert_equal "M\treadme.txt\n", diff.patch(:compact => true)

    diff = a.tree.diff(b.tree, :paths => ["r*.txt"])
    assert_equal "M\treadme.txt\n", diff.patch(:compact => true)

    diff = a.tree.diff(b.tree, :paths => ["*.txt"])
    assert_equal "M\tanother.txt\nM\treadme.txt\n", diff.patch(:compact => true)

    diff = a.tree.diff(b.tree, :paths => ["*.txt"], :disable_pathspec_match => true)
    assert_equal "", diff.patch(:compact => true)

    diff = a.tree.diff(b.tree, :paths => ["readme.txt"], :disable_pathspec_match => true)
    assert_equal "M\treadme.txt\n", diff.patch(:compact => true)
  end

  def test_options
    repo = FixtureRepo.from_libgit2("diff")

    a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69")
    b = repo.lookup("7a9e0b02e63179929fed24f0a3e0f19168114d10")

    diff = a.tree.diff(b.tree, :context_lines => 0)

    assert_equal <<-EOS, diff.patch
diff --git a/another.txt b/another.txt
index 3e5bcba..546c735 100644
--- a/another.txt
+++ b/another.txt
@@ -2 +2 @@ Git is fast. With Git, nearly all operations are performed locally, giving
-it a huge speed advantage on centralized systems that constantly have to
+it an huge speed advantage on centralized systems that constantly have to
@@ -11,4 +10,0 @@ from the start.
-Let's see how common operations stack up against Subversion, a common
-centralized version control system that is similar to CVS or
-Perforce. Smaller is faster.
-
@@ -34,0 +31,4 @@ SVN.
+Let's see how common operations stack up against Subversion, a common
+centralized version control system that is similar to CVS or
+Perforce. Smaller is faster.
+
diff --git a/readme.txt b/readme.txt
index 7b808f7..29ab705 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1 +1 @@
-The Git feature that really makes it stand apart from nearly every other SCM
+The Git feature that r3ally mak3s it stand apart from n3arly 3v3ry other SCM
@@ -10,4 +9,0 @@ This means that you can do things like:
-Frictionless Context Switching. Create a branch to try out an idea, commit a
-few times, switch back to where you branched from, apply a patch, switch
-back to where you are experimenting, and merge it in.
-
@@ -27,3 +22,0 @@ Notably, when you push to a remote repository, you do not have to push all
-of your branches. You can choose to share just one of your branches, a few
-of them, or all of them. This tends to free people to try new ideas without
-worrying about having to plan how and when they are going to merge it in or
@@ -35 +28 @@ incredibly easy and it changes the way most developers work when they learn
-it.
+it.!
\\ No newline at end of file
EOS
  end

  def test_iteration
    repo = FixtureRepo.from_libgit2("diff")

    a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69")
    b = repo.lookup("7a9e0b02e63179929fed24f0a3e0f19168114d10")

    diff = a.tree.diff(b.tree)

    patches = []

    diff.each_patch do |patch|
      assert_instance_of Rugged::Patch, patch
      patches << patch
    end

    assert_equal 2, patches.size

    assert_equal "another.txt", patches[0].delta.old_file[:path]
    assert_equal "another.txt", patches[0].delta.new_file[:path]

    refute patches[0].delta.binary?

    hunks = []
    patches[0].each_hunk do |hunk|
      assert_instance_of Rugged::Diff::Hunk, hunk
      hunks << hunk
    end
    assert_equal 3, hunks.size

    assert hunks[0].header.start_with? "@@ -1,5 +1,5 @@"
    assert_equal 6, hunks[0].line_count
    assert_equal 1, hunks[0].old_start
    assert_equal 5, hunks[0].old_lines
    assert_equal 1, hunks[0].new_start
    assert_equal 5, hunks[0].new_lines

    assert hunks[1].header.start_with? "@@ -8,10 +8,6 @@"
    assert_equal 10, hunks[1].line_count
    assert_equal 8, hunks[1].old_start
    assert_equal 10, hunks[1].old_lines
    assert_equal 8, hunks[1].new_start
    assert_equal 6, hunks[1].new_lines

    assert hunks[2].header.start_with? "@@ -32,6 +28,10 @@"
    assert_equal 10, hunks[2].line_count
    assert_equal 32, hunks[2].old_start
    assert_equal 6, hunks[2].old_lines
    assert_equal 28, hunks[2].new_start
    assert_equal 10, hunks[2].new_lines

    assert_equal "readme.txt", patches[1].delta.old_file[:path]
    assert_equal "readme.txt", patches[1].delta.new_file[:path]

    refute patches[1].delta.binary?

    hunks = []
    patches[1].each_hunk do |hunk|
      assert_instance_of Rugged::Diff::Hunk, hunk
      hunks << hunk
    end
    assert_equal 3, hunks.size

    assert hunks[0].header.start_with? "@@ -1,4 +1,4 @@"
    assert hunks[1].header.start_with? "@@ -7,10 +7,6 @@"
    assert hunks[2].header.start_with? "@@ -24,12 +20,9 @@"

    lines = []
    hunks[0].each_line do |line|
      lines << line
    end
    assert_equal 5, lines.size

    assert_equal :deletion, lines[0].line_origin
    assert_equal "The Git feature that really makes it stand apart from nearly every other SCM\n", lines[0].content
    assert_equal 0, lines[0].content_offset

    assert_equal :addition, lines[1].line_origin
    assert_equal "The Git feature that r3ally mak3s it stand apart from n3arly 3v3ry other SCM\n", lines[1].content
    assert_equal 0, lines[1].content_offset

    assert_equal :context, lines[2].line_origin
    assert_equal "out there is its branching model.\n", lines[2].content
    assert_nil lines[2].content_offset

    assert_equal :context, lines[3].line_origin
    assert_equal "\n", lines[3].content

    assert_equal :context, lines[4].line_origin
    assert_equal "Git allows and encourages you to have multiple local branches that can be\n", lines[4].content

    lines = hunks[2].each_line.to_a

    assert_equal 14, lines.size

    assert_equal :deletion, lines[3].line_origin
    assert_equal "of your branches. You can choose to share just one of your branches, a few\n", lines[3].content
    assert_equal 1248, lines[3].content_offset

    assert_equal :deletion, lines[4].line_origin
    assert_equal "of them, or all of them. This tends to free people to try new ideas without\n", lines[4].content
    assert_equal 1323, lines[4].content_offset

    assert_equal :deletion, lines[11].line_origin
    assert_equal "it.\n", lines[11].content
    assert_equal 1721, lines[11].content_offset

    assert_equal :addition, lines[12].line_origin
    assert_equal "it.!", lines[12].content
    assert_equal 1289, lines[12].content_offset
  end

  def test_each_line_patch
    repo = FixtureRepo.from_libgit2("diff")

    a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69")
    b = repo.lookup("7a9e0b02e63179929fed24f0a3e0f19168114d10")

    diff = a.tree.diff(b.tree)

    lines = diff.each_line.to_a
    assert_equal 63, lines.size

    assert_equal(:file_header, lines[0].line_origin)
    assert_equal("diff --git a/another.txt b/another.txt\nindex 3e5bcba..546c735 100644\n--- a/another.txt\n+++ b/another.txt\n", lines[0].content)
    assert_equal(0, lines[0].content_offset)
    assert_equal(-1, lines[0].old_lineno)
    assert_equal(-1, lines[0].new_lineno)

    assert_equal(:hunk_header, lines[1].line_origin)
    assert_equal("@@ -1,5 +1,5 @@\n", lines[1].content)
    assert_equal(0, lines[1].content_offset)
    assert_equal(-1, lines[1].old_lineno)
    assert_equal(-1, lines[1].new_lineno)

    assert_equal(:context, lines[2].line_origin)
    assert_equal("Git is fast. With Git, nearly all operations are performed locally, giving\n", lines[2].content)
    assert_nil(lines[2].content_offset)
    assert_equal(1, lines[2].old_lineno)
    assert_equal(1, lines[2].new_lineno)

    assert_equal(:deletion, lines[3].line_origin)
    assert_equal("it a huge speed advantage on centralized systems that constantly have to\n", lines[3].content)
    assert_equal(75, lines[3].content_offset)
    assert_equal(2, lines[3].old_lineno)
    assert_equal(-1, lines[3].new_lineno)

    assert_equal(:addition, lines[4].line_origin)
    assert_equal("it an huge speed advantage on centralized systems that constantly have to\n", lines[4].content)
    assert_equal(75, lines[4].content_offset)
    assert_equal(-1, lines[4].old_lineno)
    assert_equal(2, lines[4].new_lineno)
  end

  def test_each_line_patch_header
    repo = FixtureRepo.from_libgit2("diff")

    a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69")
    b = repo.lookup("7a9e0b02e63179929fed24f0a3e0f19168114d10")

    diff = a.tree.diff(b.tree)

    lines = diff.each_line(:patch_header).to_a
    assert_equal 2, lines.size

    assert_equal(:file_header, lines[0].line_origin)
    assert_equal("diff --git a/another.txt b/another.txt\nindex 3e5bcba..546c735 100644\n--- a/another.txt\n+++ b/another.txt\n", lines[0].content)
    assert_equal(0, lines[0].content_offset)
    assert_equal(-1, lines[0].old_lineno)
    assert_equal(-1, lines[0].new_lineno)

    assert_equal(:file_header, lines[1].line_origin)
    assert_equal("diff --git a/readme.txt b/readme.txt\nindex 7b808f7..29ab705 100644\n--- a/readme.txt\n+++ b/readme.txt\n", lines[1].content)
    assert_equal(0, lines[1].content_offset)
    assert_equal(-1, lines[1].old_lineno)
    assert_equal(-1, lines[1].new_lineno)
  end

  def test_each_line_raw
    repo = FixtureRepo.from_libgit2("diff")

    a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69")
    b = repo.lookup("7a9e0b02e63179929fed24f0a3e0f19168114d10")

    diff = a.tree.diff(b.tree)

    lines = diff.each_line(:raw).to_a
    assert_equal 2, lines.size

    assert_equal(:file_header, lines[0].line_origin)
    assert_equal(":100644 100644 3e5bcba... 546c735... M\tanother.txt\n", lines[0].content)
    assert_equal(0, lines[0].content_offset)
    assert_equal(-1, lines[0].old_lineno)
    assert_equal(-1, lines[0].new_lineno)

    assert_equal(:file_header, lines[1].line_origin)
    assert_equal(":100644 100644 7b808f7... 29ab705... M\treadme.txt\n", lines[1].content)
    assert_equal(0, lines[1].content_offset)
    assert_equal(-1, lines[1].old_lineno)
    assert_equal(-1, lines[1].new_lineno)
  end

  def test_each_line_name_only
    repo = FixtureRepo.from_libgit2("diff")

    a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69")
    b = repo.lookup("7a9e0b02e63179929fed24f0a3e0f19168114d10")

    diff = a.tree.diff(b.tree)

    lines = diff.each_line(:name_only).to_a
    assert_equal 2, lines.size

    assert_equal(:file_header, lines[0].line_origin)
    assert_equal("another.txt\n", lines[0].content)
    assert_equal(0, lines[0].content_offset)
    assert_equal(-1, lines[0].old_lineno)
    assert_equal(-1, lines[0].new_lineno)

    assert_equal(:file_header, lines[1].line_origin)
    assert_equal("readme.txt\n", lines[1].content)
    assert_equal(0, lines[1].content_offset)
    assert_equal(-1, lines[1].old_lineno)
    assert_equal(-1, lines[1].new_lineno)
  end

  def test_each_line_name_status
    repo = FixtureRepo.from_libgit2("diff")

    a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69")
    b = repo.lookup("7a9e0b02e63179929fed24f0a3e0f19168114d10")

    diff = a.tree.diff(b.tree)

    lines = diff.each_line(:name_status).to_a
    assert_equal 2, lines.size

    assert_equal(:file_header, lines[0].line_origin)
    assert_equal("M\tanother.txt\n", lines[0].content)
    assert_equal(0, lines[0].content_offset)
    assert_equal(-1, lines[0].old_lineno)
    assert_equal(-1, lines[0].new_lineno)

    assert_equal(:file_header, lines[1].line_origin)
    assert_equal("M\treadme.txt\n", lines[1].content)
    assert_equal(0, lines[1].content_offset)
    assert_equal(-1, lines[1].old_lineno)
    assert_equal(-1, lines[1].new_lineno)
  end

  def test_each_line_unknown_format_raises_error
    repo = FixtureRepo.from_libgit2("diff")

    a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69")
    b = repo.lookup("7a9e0b02e63179929fed24f0a3e0f19168114d10")

    diff = a.tree.diff(b.tree)

    assert_raises ArgumentError do
      diff.each_line(:foobar).to_a
    end
  end

  def test_each_patch_returns_enumerator
    repo = FixtureRepo.from_libgit2("diff")

    a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69")
    b = repo.lookup("7a9e0b02e63179929fed24f0a3e0f19168114d10")

    diff = a.tree.diff(b.tree)

    assert_instance_of Enumerator, diff.each_patch

    patches = []
    diff.each_patch.each do |patch|
      assert_instance_of Rugged::Patch, patch
      patches << patch
    end
    assert_equal 2, patches.size
  end

  def test_each_hunk_returns_enumerator
    repo = FixtureRepo.from_libgit2("diff")

    a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69")
    b = repo.lookup("7a9e0b02e63179929fed24f0a3e0f19168114d10")

    diff = a.tree.diff(b.tree)

    diff.each_patch do |patch|
      assert_instance_of Enumerator, patch.each_hunk
    end
  end

  def test_each_line_returns_enumerator
    repo = FixtureRepo.from_libgit2("diff")

    a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69")
    b = repo.lookup("7a9e0b02e63179929fed24f0a3e0f19168114d10")

    diff = a.tree.diff(b.tree)

    diff.each_patch do |patch|
      patch.each_hunk do |hunk|
        assert_instance_of Enumerator, hunk.each_line
      end
    end
  end

  def test_patch
    repo = FixtureRepo.from_libgit2("diff")

    a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69")
    b = repo.lookup("7a9e0b02e63179929fed24f0a3e0f19168114d10")

    diff = a.tree.diff(b.tree)

    expected = <<-EOS
diff --git a/another.txt b/another.txt
index 3e5bcba..546c735 100644
--- a/another.txt
+++ b/another.txt
@@ -1,5 +1,5 @@
 Git is fast. With Git, nearly all operations are performed locally, giving
-it a huge speed advantage on centralized systems that constantly have to
+it an huge speed advantage on centralized systems that constantly have to
 communicate with a server somewhere.
 
 Git was built to work on the Linux kernel, meaning that it has had to
@@ -8,10 +8,6 @@ reducing the overhead of runtimes associated with higher-level
 languages. Speed and performance has been a primary design goal of the Git
 from the start.
 
-Let's see how common operations stack up against Subversion, a common
-centralized version control system that is similar to CVS or
-Perforce. Smaller is faster.
-
 For testing, large AWS instances were set up in the same availability
 zone. Git and SVN were installed on both machines, the Ruby repository was
 copied to both Git and SVN servers, and common operations were performed on
@@ -32,6 +28,10 @@ Clearly, in many of these common version control operations, Git is one or
 two orders of magnitude faster than SVN, even under ideal conditions for
 SVN.
 
+Let's see how common operations stack up against Subversion, a common
+centralized version control system that is similar to CVS or
+Perforce. Smaller is faster.
+
 One place where Git is slower is in the initial clone operation. Here, Git
 is downloading the entire history rather than only the latest version. As
 seen in the above charts, it's not considerably slower for an operation that
diff --git a/readme.txt b/readme.txt
index 7b808f7..29ab705 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,4 +1,4 @@
-The Git feature that really makes it stand apart from nearly every other SCM
+The Git feature that r3ally mak3s it stand apart from n3arly 3v3ry other SCM
 out there is its branching model.
 
 Git allows and encourages you to have multiple local branches that can be
@@ -7,10 +7,6 @@ those lines of development takes seconds.
 
 This means that you can do things like:
 
-Frictionless Context Switching. Create a branch to try out an idea, commit a
-few times, switch back to where you branched from, apply a patch, switch
-back to where you are experimenting, and merge it in.
-
 Role-Based Codelines. Have a branch that always contains only what goes to
 production, another that you merge work into for testing, and several
 smaller ones for day to day work.
@@ -24,12 +20,9 @@ not going to work, and just delete it - abandoning the work\xE2\x80\x94with nobody else
 ever seeing it (even if you've pushed other branches in the meantime).
 
 Notably, when you push to a remote repository, you do not have to push all
-of your branches. You can choose to share just one of your branches, a few
-of them, or all of them. This tends to free people to try new ideas without
-worrying about having to plan how and when they are going to merge it in or
 share it with others.
 
 There are ways to accomplish some of this with other systems, but the work
 involved is much more difficult and error-prone. Git makes this process
 incredibly easy and it changes the way most developers work when they learn
-it.
+it.!
\\ No newline at end of file
EOS

    expected.force_encoding('binary') if expected.respond_to?(:force_encoding)

    assert_equal expected, diff.patch
  end

  def test_patch_compact
    repo = FixtureRepo.from_libgit2("diff")

    a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69")
    b = repo.lookup("7a9e0b02e63179929fed24f0a3e0f19168114d10")

    diff = a.tree.diff(b.tree)

    assert_equal <<-EOS, diff.patch(:compact => true)
M\tanother.txt
M\treadme.txt
EOS
  end

  def test_stats
    repo = FixtureRepo.from_libgit2("diff")

    a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69")
    b = repo.lookup("7a9e0b02e63179929fed24f0a3e0f19168114d10")

    diff = a.tree.diff(b.tree)

    files, adds, dels = diff.stat
    assert_equal 2, files
    assert_equal 7, adds
    assert_equal 14, dels

    expected_patch_stat = [ [ 5, 5, 26 ], [ 2, 9, 28 ] ]

    diff.each_patch do |patch|
      expected_adds, expected_dels, expected_lines = expected_patch_stat.shift

      actual_adds, actual_dels = patch.stat

      assert_equal expected_adds, actual_adds
      assert_equal expected_dels, actual_dels
      assert_equal expected_adds + expected_dels, patch.changes

      assert_equal expected_lines, patch.lines(exclude_eofnl: true)
    end
  end
end

class TreeDiffRegression < Rugged::TestCase
  def test_nil_repo
    assert_raises TypeError do
      Rugged::Tree.diff nil, "foo"
    end
  end

  def test_self_is_not_tree
    repo = FixtureRepo.from_libgit2("diff")

    ex = assert_raises TypeError do
      Rugged::Tree.diff repo, "foo"
    end
    assert_equal "At least a Rugged::Tree object is required for diffing", ex.message
  end

  def test_self_or_other_must_be_present
    repo = FixtureRepo.from_libgit2("diff")

    ex = assert_raises TypeError do
      Rugged::Tree.diff repo, nil, nil
    end
    assert_equal "Need 'old' or 'new' for diffing", ex.message
  end

  def test_other_is_wrong_type
    repo = FixtureRepo.from_libgit2("diff")

    ex = assert_raises TypeError do
      Rugged::Tree.diff repo, nil, Object.new
    end
    assert_equal "A Rugged::Commit, Rugged::Tree or Rugged::Index instance is required", ex.message
  end

  def test_self_is_nil_other_is_tree_does_not_fail
    repo = FixtureRepo.from_libgit2("diff")

    a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69")

    diff = Rugged::Tree.diff(repo, nil, a.tree)
    assert_equal 2, diff.size
    assert_equal 2, diff.deltas.size

    delta = diff.deltas[0]
    assert_equal({
      oid: "0000000000000000000000000000000000000000",
      path: "another.txt",
      size: 0,
      flags: 4,
      mode: 0
    }, delta.old_file)

    assert_equal({
      oid: "3e5bcbad2a68e5bc60a53b8388eea53a1a7ab847",
      path: "another.txt",
      size: 0,
      flags: 12,
      mode: 0100644
    }, delta.new_file)

    delta = diff.deltas[1]
    assert_equal({
      oid: "0000000000000000000000000000000000000000",
      path: "readme.txt",
      size: 0,
      flags: 4,
      mode: 0
    }, delta.old_file)

    assert_equal({
      oid: "7b808f723a8ca90df319682c221187235af76693",
      path: "readme.txt",
      size: 0,
      flags: 12,
      mode: 0100644
    }, delta.new_file)
  end

  def test_other_tree_is_an_index_but_tree_is_nil
    repo = FixtureRepo.from_libgit2("diff")

    diff = Rugged::Tree.diff repo, nil, repo.index
    assert_equal 2, diff.size
    assert_equal 2, diff.deltas.size
  end
end