-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathdiff_test.rb
1281 lines (992 loc) · 39.8 KB
/
diff_test.rb
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
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)