File: t-migrate-import.sh

package info (click to toggle)
git-lfs 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,808 kB
  • sloc: sh: 21,256; makefile: 507; ruby: 417
file content (1169 lines) | stat: -rwxr-xr-x 34,976 bytes parent folder | download
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
#!/usr/bin/env bash

. "$(dirname "$0")/fixtures/migrate.sh"
. "$(dirname "$0")/testlib.sh"

begin_test "migrate import (default branch)"
(
  set -e

  setup_multiple_local_branches

  md_oid="$(calc_oid "$(git cat-file -p :a.md)")"
  txt_oid="$(calc_oid "$(git cat-file -p :a.txt)")"
  md_feature_oid="$(calc_oid "$(git cat-file -p my-feature:a.md)")"

  git lfs migrate import

  assert_pointer "refs/heads/main" "a.md" "$md_oid" "140"
  assert_pointer "refs/heads/main" "a.txt" "$txt_oid" "120"

  assert_local_object "$md_oid" "140"
  assert_local_object "$txt_oid" "120"
  refute_local_object "$md_feature_oid" "30"

  main="$(git rev-parse refs/heads/main)"
  feature="$(git rev-parse refs/heads/my-feature)"

  main_attrs="$(git cat-file -p "$main:.gitattributes")"
  [ -z "$(git cat-file -p "$feature:.gitattributes")" ]

  echo "$main_attrs" | grep -q "*.md filter=lfs diff=lfs merge=lfs"
  echo "$main_attrs" | grep -q "*.txt filter=lfs diff=lfs merge=lfs"

  # Ensure that hooks are installed. If we find 'git-lfs' somewhere in
  # .git/hooks/pre-push we assume that the rest went correctly, too.
  grep -q "git-lfs" .git/hooks/pre-push
)
end_test

begin_test "migrate import (bare repository)"
(
  set -e

  setup_multiple_remote_branches

  git lfs migrate import --everything
)
end_test

begin_test "migrate import (given branch)"
(
  set -e

  setup_multiple_local_branches

  md_oid="$(calc_oid "$(git cat-file -p :a.md)")"
  txt_oid="$(calc_oid "$(git cat-file -p :a.txt)")"
  md_feature_oid="$(calc_oid "$(git cat-file -p my-feature:a.md)")"

  git lfs migrate import my-feature

  assert_pointer "refs/heads/my-feature" "a.md" "$md_feature_oid" "30"
  assert_pointer "refs/heads/my-feature" "a.txt" "$txt_oid" "120"
  assert_pointer "refs/heads/main" "a.md" "$md_oid" "140"
  assert_pointer "refs/heads/main" "a.txt" "$txt_oid" "120"

  assert_local_object "$md_oid" "140"
  assert_local_object "$md_feature_oid" "30"
  assert_local_object "$txt_oid" "120"

  main="$(git rev-parse refs/heads/main)"
  feature="$(git rev-parse refs/heads/my-feature)"

  main_attrs="$(git cat-file -p "$main:.gitattributes")"
  feature_attrs="$(git cat-file -p "$feature:.gitattributes")"

  echo "$main_attrs" | grep -q "*.md filter=lfs diff=lfs merge=lfs"
  echo "$main_attrs" | grep -q "*.txt filter=lfs diff=lfs merge=lfs"
  echo "$feature_attrs" | grep -q "*.md filter=lfs diff=lfs merge=lfs"
  echo "$feature_attrs" | grep -q "*.txt filter=lfs diff=lfs merge=lfs"
)
end_test

begin_test "migrate import (default branch with filter)"
(
  set -e

  setup_multiple_local_branches

  md_oid="$(calc_oid "$(git cat-file -p :a.md)")"
  txt_oid="$(calc_oid "$(git cat-file -p :a.txt)")"
  md_feature_oid="$(calc_oid "$(git cat-file -p my-feature:a.md)")"

  git lfs migrate import --include "*.md"

  assert_pointer "refs/heads/main" "a.md" "$md_oid" "140"

  assert_local_object "$md_oid" "140"
  refute_local_object "$txt_oid" "120"
  refute_local_object "$md_feature_oid" "30"

  main="$(git rev-parse refs/heads/main)"
  feature="$(git rev-parse refs/heads/my-feature)"

  main_attrs="$(git cat-file -p "$main:.gitattributes")"
  [ -z "$(git cat-file -p "$feature:.gitattributes")" ]

  echo "$main_attrs" | grep -q "*.md filter=lfs diff=lfs merge=lfs"
  echo "$main_attrs" | grep -vq "*.txt filter=lfs diff=lfs merge=lfs"
)
end_test

begin_test "migrate import (given branch with filter)"
(
  set -e

  setup_multiple_local_branches

  md_oid="$(calc_oid "$(git cat-file -p :a.md)")"
  txt_oid="$(calc_oid "$(git cat-file -p :a.txt)")"
  md_feature_oid="$(calc_oid "$(git cat-file -p my-feature:a.md)")"

  git lfs migrate import --include "*.md" my-feature

  assert_pointer "refs/heads/my-feature" "a.md" "$md_feature_oid" "30"
  assert_pointer "refs/heads/my-feature~1" "a.md" "$md_oid" "140"

  assert_local_object "$md_oid" "140"
  assert_local_object "$md_feature_oid" "30"
  refute_local_object "$txt_oid" "120"

  main="$(git rev-parse refs/heads/main)"
  feature="$(git rev-parse refs/heads/my-feature)"

  main_attrs="$(git cat-file -p "$main:.gitattributes")"
  feature_attrs="$(git cat-file -p "$feature:.gitattributes")"

  echo "$main_attrs" | grep -q "*.md filter=lfs diff=lfs merge=lfs"
  echo "$main_attrs" | grep -vq "*.txt filter=lfs diff=lfs merge=lfs"
  echo "$feature_attrs" | grep -q "*.md filter=lfs diff=lfs merge=lfs"
  echo "$feature_attrs" | grep -vq "*.txt filter=lfs diff=lfs merge=lfs"
)
end_test

begin_test "migrate import (default branch, exclude remote refs)"
(
  set -e

  setup_single_remote_branch

  md_remote_oid="$(calc_oid "$(git cat-file -p "refs/remotes/origin/main:a.md")")"
  txt_remote_oid="$(calc_oid "$(git cat-file -p "refs/remotes/origin/main:a.txt")")"
  md_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.md")")"
  txt_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.txt")")"

  git lfs migrate import

  assert_pointer "refs/heads/main" "a.md" "$md_oid" "50"
  assert_pointer "refs/heads/main" "a.txt" "$txt_oid" "30"

  assert_local_object "$md_oid" "50"
  assert_local_object "$txt_oid" "30"
  refute_local_object "$md_remote_oid" "140"
  refute_local_object "$txt_remote_oid" "120"

  main="$(git rev-parse refs/heads/main)"
  remote="$(git rev-parse refs/remotes/origin/main)"

  main_attrs="$(git cat-file -p "$main:.gitattributes")"
  [ -z "$(git cat-file -p "$remote:.gitattributes")" ]

  echo "$main_attrs" | grep -q "*.md filter=lfs diff=lfs merge=lfs"
  echo "$main_attrs" | grep -vq "*.txt filter=lfs diff=lfs merge=lfs"
)
end_test

begin_test "migrate import (given branch, exclude remote refs)"
(
  set -e

  setup_multiple_remote_branches

  md_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.md")")"
  md_remote_oid="$(calc_oid "$(git cat-file -p "refs/remotes/origin/main:a.md")")"
  md_feature_oid="$(calc_oid "$(git cat-file -p "refs/heads/my-feature:a.md")")"
  txt_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.txt")")"
  txt_remote_oid="$(calc_oid "$(git cat-file -p "refs/remotes/origin/main:a.txt")")"
  txt_feature_oid="$(calc_oid "$(git cat-file -p "refs/heads/my-feature:a.txt")")"

  git lfs migrate import my-feature

  assert_pointer "refs/heads/main" "a.md" "$md_main_oid" "21"
  assert_pointer "refs/heads/my-feature" "a.md" "$md_feature_oid" "31"
  assert_pointer "refs/heads/main" "a.txt" "$txt_main_oid" "20"
  assert_pointer "refs/heads/my-feature" "a.txt" "$txt_feature_oid" "30"

  assert_local_object "$md_feature_oid" "31"
  assert_local_object "$md_main_oid" "21"
  assert_local_object "$txt_feature_oid" "30"
  assert_local_object "$txt_main_oid" "20"
  refute_local_object "$md_remote_oid" "11"
  refute_local_object "$txt_remote_oid" "10"

  main="$(git rev-parse refs/heads/main)"
  feature="$(git rev-parse refs/heads/my-feature)"
  remote="$(git rev-parse refs/remotes/origin/main)"

  main_attrs="$(git cat-file -p "$main:.gitattributes")"
  [ -z "$(git cat-file -p "$remote:.gitattributes")" ]
  feature_attrs="$(git cat-file -p "$feature:.gitattributes")"

  echo "$main_attrs" | grep -q "*.md filter=lfs diff=lfs merge=lfs"
  echo "$main_attrs" | grep -q "*.txt filter=lfs diff=lfs merge=lfs"
  echo "$feature_attrs" | grep -q "*.md filter=lfs diff=lfs merge=lfs"
  echo "$feature_attrs" | grep -vq "*.txt filter=lfs diff=lfs merge=lfs"
)
end_test

begin_test "migrate import (given ref, --skip-fetch)"
(
  set -e

  setup_single_remote_branch

  md_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.md")")"
  md_remote_oid="$(calc_oid "$(git cat-file -p "refs/remotes/origin/main:a.md")")"
  txt_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.txt")")"
  txt_remote_oid="$(calc_oid "$(git cat-file -p "refs/remotes/origin/main:a.txt")")"

  git tag pseudo-remote "$(git rev-parse refs/remotes/origin/main)"
  # Remove the refs/remotes/origin/main ref, and instruct 'git lfs migrate' to
  # not fetch it.
  git update-ref -d refs/remotes/origin/main

  git lfs migrate import --skip-fetch

  assert_pointer "refs/heads/main" "a.md" "$md_main_oid" "50"
  assert_pointer "pseudo-remote" "a.md" "$md_remote_oid" "140"
  assert_pointer "refs/heads/main" "a.txt" "$txt_main_oid" "30"
  assert_pointer "pseudo-remote" "a.txt" "$txt_remote_oid" "120"

  assert_local_object "$md_main_oid" "50"
  assert_local_object "$txt_main_oid" "30"
  assert_local_object "$md_remote_oid" "140"
  assert_local_object "$txt_remote_oid" "120"

  main="$(git rev-parse refs/heads/main)"
  remote="$(git rev-parse pseudo-remote)"

  main_attrs="$(git cat-file -p "$main:.gitattributes")"
  remote_attrs="$(git cat-file -p "$remote:.gitattributes")"

  echo "$main_attrs" | grep -q "*.md filter=lfs diff=lfs merge=lfs"
  echo "$main_attrs" | grep -q "*.txt filter=lfs diff=lfs merge=lfs"
  echo "$remote_attrs" | grep -q "*.md filter=lfs diff=lfs merge=lfs"
  echo "$remote_attrs" | grep -q "*.txt filter=lfs diff=lfs merge=lfs"
)
end_test

begin_test "migrate import (un-annotated tags)"
(
  set -e

  setup_single_local_branch_with_tags

  txt_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.txt")")"

  git lfs migrate import --everything

  assert_pointer "refs/heads/main" "a.txt" "$txt_main_oid" "2"
  assert_local_object "$txt_main_oid" "2"

  git tag --points-at "$(git rev-parse HEAD)" | grep -q "v1.0.0"
)
end_test

begin_test "migrate import (annotated tags)"
(
  set -e

  setup_single_local_branch_with_annotated_tags

  txt_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.txt")")"

  git lfs migrate import --everything

  assert_pointer "refs/heads/main" "a.txt" "$txt_main_oid" "2"
  assert_local_object "$txt_main_oid" "2"

  git tag --points-at "$(git rev-parse HEAD)" | grep -q "v1.0.0"
)
end_test

begin_test "migrate import (include/exclude ref)"
(
  set -e

  setup_multiple_remote_branches

  md_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.md")")"
  md_remote_oid="$(calc_oid "$(git cat-file -p "refs/remotes/origin/main:a.md")")"
  md_feature_oid="$(calc_oid "$(git cat-file -p "refs/heads/my-feature:a.md")")"
  txt_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.txt")")"
  txt_remote_oid="$(calc_oid "$(git cat-file -p "refs/remotes/origin/main:a.txt")")"
  txt_feature_oid="$(calc_oid "$(git cat-file -p "refs/heads/my-feature:a.txt")")"

  git lfs migrate import \
    --include-ref=refs/heads/my-feature \
    --exclude-ref=refs/heads/main

  assert_pointer "refs/heads/my-feature" "a.md" "$md_feature_oid" "31"
  assert_pointer "refs/heads/my-feature" "a.txt" "$txt_feature_oid" "30"

  assert_local_object "$md_feature_oid" "31"
  refute_local_object "$md_main_oid" "21"
  assert_local_object "$txt_feature_oid" "30"
  refute_local_object "$txt_main_oid" "20"
  refute_local_object "$md_remote_oid" "11"
  refute_local_object "$txt_remote_oid" "10"

  main="$(git rev-parse refs/heads/main)"
  feature="$(git rev-parse refs/heads/my-feature)"
  remote="$(git rev-parse refs/remotes/origin/main)"

  [ -z "$(git cat-file -p "$main:.gitattributes")" ]
  [ -z "$(git cat-file -p "$remote:.gitattributes")" ]
  feature_attrs="$(git cat-file -p "$feature:.gitattributes")"

  echo "$feature_attrs" | grep -q "*.md filter=lfs diff=lfs merge=lfs"
  echo "$feature_attrs" | grep -q "*.txt filter=lfs diff=lfs merge=lfs"
)
end_test

begin_test "migrate import (include/exclude ref args)"
(
  set -e

  setup_multiple_remote_branches

  md_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.md")")"
  md_remote_oid="$(calc_oid "$(git cat-file -p "refs/remotes/origin/main:a.md")")"
  md_feature_oid="$(calc_oid "$(git cat-file -p "refs/heads/my-feature:a.md")")"
  txt_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.txt")")"
  txt_remote_oid="$(calc_oid "$(git cat-file -p "refs/remotes/origin/main:a.txt")")"
  txt_feature_oid="$(calc_oid "$(git cat-file -p "refs/heads/my-feature:a.txt")")"

  git lfs migrate import my-feature ^main

  assert_pointer "refs/heads/my-feature" "a.md" "$md_feature_oid" "31"
  assert_pointer "refs/heads/my-feature" "a.txt" "$txt_feature_oid" "30"

  assert_local_object "$md_feature_oid" "31"
  refute_local_object "$md_main_oid" "21"
  assert_local_object "$txt_feature_oid" "30"
  refute_local_object "$txt_main_oid" "20"
  refute_local_object "$md_remote_oid" "11"
  refute_local_object "$txt_remote_oid" "10"

  main="$(git rev-parse refs/heads/main)"
  feature="$(git rev-parse refs/heads/my-feature)"
  remote="$(git rev-parse refs/remotes/origin/main)"

  [ -z "$(git cat-file -p "$main:.gitattributes")" ]
  [ -z "$(git cat-file -p "$remote:.gitattributes")" ]
  feature_attrs="$(git cat-file -p "$feature:.gitattributes")"

  echo "$feature_attrs" | grep -q "*.md filter=lfs diff=lfs merge=lfs"
  echo "$feature_attrs" | grep -q "*.txt filter=lfs diff=lfs merge=lfs"
)
end_test

begin_test "migrate import (include/exclude ref with filter)"
(
  set -e

  setup_multiple_remote_branches

  md_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.md")")"
  md_remote_oid="$(calc_oid "$(git cat-file -p "refs/remotes/origin/main:a.md")")"
  md_feature_oid="$(calc_oid "$(git cat-file -p "refs/heads/my-feature:a.md")")"
  txt_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.txt")")"
  txt_remote_oid="$(calc_oid "$(git cat-file -p "refs/remotes/origin/main:a.txt")")"
  txt_feature_oid="$(calc_oid "$(git cat-file -p "refs/heads/my-feature:a.txt")")"

  git lfs migrate import \
    --include="*.txt" \
    --include-ref=refs/heads/my-feature \
    --exclude-ref=refs/heads/main

  assert_pointer "refs/heads/my-feature" "a.txt" "$txt_feature_oid" "30"

  refute_local_object "$md_feature_oid" "31"
  refute_local_object "$md_main_oid" "21"
  assert_local_object "$txt_feature_oid" "30"
  refute_local_object "$txt_main_oid" "20"
  refute_local_object "$md_remote_oid" "11"
  refute_local_object "$txt_remote_oid" "10"

  main="$(git rev-parse refs/heads/main)"
  feature="$(git rev-parse refs/heads/my-feature)"
  remote="$(git rev-parse refs/remotes/origin/main)"

  [ -z "$(git cat-file -p "$main:.gitattributes")" ]
  [ -z "$(git cat-file -p "$remote:.gitattributes")" ]
  feature_attrs="$(git cat-file -p "$feature:.gitattributes")"

  echo "$feature_attrs" | grep -vq "*.md filter=lfs diff=lfs merge=lfs"
  echo "$feature_attrs" | grep -q "*.txt filter=lfs diff=lfs merge=lfs"
)
end_test

begin_test "migrate import (invalid ref)"
(
  set -e
  remove_and_create_local_repo "migrate-import-invalid-ref"
  git commit --allow-empty -m "initial commit"

  git lfs migrate import --yes jibberish >migrate.log 2>&1 && exit 1
  grep "can't resolve ref" migrate.log
)
end_test

begin_test "migrate import (above)"
(
  set -e
  setup_single_local_branch_untracked

  md_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.md")")"
  txt_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.txt")")"

  git lfs migrate import --above 121B
  # Ensure that 'a.md', whose size is above our 121 byte threshold
  # was converted into a git-lfs pointer by the migration.
  assert_local_object "$md_main_oid" "140"
  assert_pointer "refs/heads/main" "a.md" "$md_main_oid" "140"
  refute_pointer "refs/heads/main" "a.txt" "$txt_main_oid" "120"
  refute_local_object "$txt_main_oid" "120"

  # The migration should have identified that *.md files are now
  # tracked because it migrated a.md
  main_attrs="$(git cat-file -p "$main:.gitattributes")"

  echo "$main_attrs" | grep -q "/a.md filter=lfs diff=lfs merge=lfs"
  echo "$main_attrs" | grep -vq "/a.txt filter=lfs diff=lfs merge=lfs"
  git check-attr filter -- a.txt | grep -vq lfs
)
end_test

begin_test "migrate import (above without extension)"
(
  set -e
  setup_single_local_branch_untracked "just-b"

  b_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:just-b")")"
  txt_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.txt")")"

  git lfs migrate import --above 121B
  # Ensure that 'b', whose size is above our 121 byte threshold
  # was converted into a git-lfs pointer by the migration.
  assert_local_object "$b_main_oid" "140"
  assert_pointer "refs/heads/main" "just-b" "$b_main_oid" "140"
  refute_pointer "refs/heads/main" "a.txt" "$txt_main_oid" "120"
  refute_local_object "$txt_main_oid" "120"

  # The migration should have identified that /b is now tracked
  # because it migrated it.
  main_attrs="$(git cat-file -p "$main:.gitattributes")"

  echo "$main_attrs" | grep -q "/just-b filter=lfs diff=lfs merge=lfs"
  echo "$main_attrs" | grep -vq "/a.txt filter=lfs diff=lfs merge=lfs"
  git check-attr filter -- a.txt | grep -vq lfs
)
end_test

begin_test "migrate import (above with multiple files)"
(
  set -e
  # It is important that this file sort after "a.txt".
  setup_single_local_branch_untracked "b.txt"

  a_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.txt")")"
  b_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:b.txt")")"

  git lfs migrate import --above 121B
  # Ensure that 'a.md', whose size is above our 121 byte threshold
  # was converted into a git-lfs pointer by the migration.
  assert_local_object "$b_main_oid" "140"
  assert_pointer "refs/heads/main" "b.txt" "$b_main_oid" "140"
  refute_pointer "refs/heads/main" "a.txt" "$a_main_oid" "120"
  refute_local_object "$a_main_oid" "120"

  # The migration should have identified that *.md files are now
  # tracked because it migrated a.md
  main_attrs="$(git cat-file -p "$main:.gitattributes")"

  echo "$main_attrs" | grep -q "/b.txt filter=lfs diff=lfs merge=lfs"
  git check-attr filter -- a.txt | grep -vq lfs
)
end_test

begin_test "migrate import (above with include or exclude)"
(
  set -e
  setup_single_local_branch_untracked

  md_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.md")")"
  txt_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.txt")")"

  git lfs migrate import --above 121B --include "*.md" && exit 1
  git lfs migrate import --above 121B --exclude "*.txt" && exit 1
  git lfs migrate import --above 121B --fixup && exit 1
  true
)
end_test

begin_test "migrate import (existing .gitattributes)"
(
  set -e

  setup_local_branch_with_gitattrs

  main="$(git rev-parse refs/heads/main)"

  txt_main_oid="$(calc_oid "$(git cat-file -p "$main:a.txt")")"

  git lfs migrate import --yes --include-ref=refs/heads/main --include="*.txt"

  assert_local_object "$txt_main_oid" "120"

  main="$(git rev-parse refs/heads/main)"
  prev="$(git rev-parse refs/heads/main^1)"

  diff -u <(git cat-file -p $main:.gitattributes) <(cat <<-EOF
*.txt filter=lfs diff=lfs merge=lfs -text
*.other filter=lfs diff=lfs merge=lfs -text
EOF)

  diff -u <(git cat-file -p $prev:.gitattributes) <(cat <<-EOF
*.txt filter=lfs diff=lfs merge=lfs -text
EOF)
)
end_test

begin_test "migrate import (--exclude with existing .gitattributes)"
(
  set -e

  setup_local_branch_with_gitattrs

  main="$(git rev-parse refs/heads/main)"

  txt_main_oid="$(calc_oid "$(git cat-file -p "$main:a.txt")")"

  git lfs migrate import --yes --include-ref=refs/heads/main --include="*.txt" --exclude="*.bin"

  assert_local_object "$txt_main_oid" "120"

  main="$(git rev-parse refs/heads/main)"
  prev="$(git rev-parse refs/heads/main^1)"

  diff -u <(git cat-file -p $main:.gitattributes) <(cat <<-EOF
*.txt filter=lfs diff=lfs merge=lfs -text
*.other filter=lfs diff=lfs merge=lfs -text
*.bin !text -filter -merge -diff
EOF)

  diff -u <(git cat-file -p $prev:.gitattributes) <(cat <<-EOF
*.txt filter=lfs diff=lfs merge=lfs -text
*.bin !text -filter -merge -diff
EOF)
)
end_test

begin_test "migrate import (existing .gitattributes with different permissions)"
(
  set -e

  # Windows lacks POSIX permissions.
  [ "$IS_WINDOWS" -eq 1 ] && exit 0

  setup_local_branch_with_gitattrs 0755

  main="$(git rev-parse refs/heads/main)"

  txt_main_oid="$(calc_oid "$(git cat-file -p "$main:a.txt")")"

  [ -x .gitattributes ]

  git lfs migrate import --yes --include-ref=refs/heads/main --include="*.txt"

  [ ! -x .gitattributes ]

  assert_local_object "$txt_main_oid" "120"

  main="$(git rev-parse refs/heads/main)"
  prev="$(git rev-parse refs/heads/main^1)"

  diff -u <(git cat-file -p $main:.gitattributes) <(cat <<-EOF
*.txt filter=lfs diff=lfs merge=lfs -text
*.other filter=lfs diff=lfs merge=lfs -text
EOF
  )

  diff -u <(git cat-file -p $prev:.gitattributes) <(cat <<-EOF
*.txt filter=lfs diff=lfs merge=lfs -text
EOF
  )

  attrs_main_sha="$(git show $main:.gitattributes | git hash-object --stdin)"
  txt_main_sha="$(git show $main:a.txt | git hash-object --stdin)"

  diff -u <(git ls-tree $main) <(cat <<-EOF
100644 blob $attrs_main_sha	.gitattributes
100644 blob $txt_main_sha	a.txt
EOF
  )
)
end_test

begin_test "migrate import (existing .gitattributes symlink)"
(
  set -e

  setup_local_branch_with_gitattrs link

  git lfs migrate import --yes --include-ref=refs/heads/main --include="*.txt" 2>&1 | tee migrate.log
  if [ ${PIPESTATUS[0]} -eq 0 ]; then
    echo >&2 "fatal: expected git lfs migrate import to fail, didn't"
    exit 1
  fi

  grep "migrate: expected '.gitattributes' to be a file, got a symbolic link" migrate.log

  main="$(git rev-parse refs/heads/main)"

  attrs_main_sha="$(git show $main:.gitattributes | git hash-object --stdin)"

  diff -u <(git ls-tree $main -- .gitattributes) <(cat <<-EOF
120000 blob $attrs_main_sha	.gitattributes
EOF
  )
)
end_test

begin_test "migrate import (identical contents, different permissions)"
(
  set -e

  # Windows lacks POSIX permissions.
  [ "$IS_WINDOWS" -eq 1 ] && exit 0

  setup_multiple_local_branches
  git checkout main

  echo "foo" >foo.dat
  git add .
  git commit -m "add file"

  chmod u+x foo.dat
  git add .
  git commit -m "make file executable"

  # Verify we have executable permissions.
  [ -x foo.dat ]

  git lfs migrate import --everything --include="*.dat"

  # Verify we have executable permissions.
  [ -x foo.dat ]
)
end_test

begin_test "migrate import (tags with same name as branches)"
(
  set -e

  setup_multiple_local_branches
  git checkout main

  contents="hello"
  oid=$(calc_oid "$contents")
  printf "$contents" >hello.dat
  git add .
  git commit -m "add file"

  git branch foo
  git tag foo
  git tag bar

  git lfs migrate import --everything --include="*.dat"

  [ "$(git rev-parse refs/heads/foo)" = "$(git rev-parse refs/tags/foo)" ]
  [ "$(git rev-parse refs/heads/foo)" = "$(git rev-parse refs/tags/bar)" ]

  assert_pointer "refs/heads/foo" hello.dat "$oid" 5
)
end_test

begin_test "migrate import (bare repository)"
(
  set -e

  setup_multiple_local_branches
  make_bare

  git lfs migrate import \
    --include-ref=main
)
end_test

begin_test "migrate import (nested sub-trees, no filter)"
(
  set -e

  setup_single_local_branch_deep_trees

  oid="$(calc_oid "$(git cat-file -p :foo/bar/baz/a.txt)")"
  size="$(git cat-file -p :foo/bar/baz/a.txt | wc -c | awk '{ print $1 }')"

  git lfs migrate import --everything

  assert_local_object "$oid" "$size"
)
end_test

begin_test "migrate import (prefix include(s))"
(
  set -e

  includes="foo/bar/baz/** foo/**/baz/a.txt *.txt"
  for include in $includes; do
    setup_single_local_branch_deep_trees

    oid="$(calc_oid "$(git cat-file -p :foo/bar/baz/a.txt)")"

    git lfs migrate import --include="$include"

    assert_local_object "$oid" 120

    cd ..
  done
)
end_test

begin_test "migrate import (--everything)"
(
  set -e

  setup_multiple_local_branches
  git checkout main

  main_txt_oid="$(calc_oid "$(git cat-file -p :a.txt)")"
  main_md_oid="$(calc_oid "$(git cat-file -p :a.md)")"
  feature_md_oid="$(calc_oid "$(git cat-file -p my-feature:a.md)")"
  main_txt_size="$(git cat-file -p :a.txt | wc -c | awk '{ print $1 }')"
  main_md_size="$(git cat-file -p :a.md | wc -c | awk '{ print $1 }')"
  feature_md_size="$(git cat-file -p my-feature:a.md | wc -c | awk '{ print $1 }')"

  git lfs migrate import --everything

  assert_pointer "main" "a.txt" "$main_txt_oid" "$main_txt_size"
  assert_pointer "main" "a.md" "$main_md_oid" "$main_md_size"
  assert_pointer "my-feature" "a.md" "$feature_md_oid" "$feature_md_size"
)
end_test

begin_test "migrate import (ambiguous reference)"
(
  set -e

  setup_multiple_local_branches

  # Create an ambiguously named reference sharing the name as the SHA-1 of
  # "HEAD".
  sha="$(git rev-parse HEAD)"
  git tag "$sha"

  git lfs migrate import --everything
)
end_test

begin_test "migrate import (--everything with args)"
(
  set -e

  setup_multiple_local_branches

  [ "$(git lfs migrate import --everything main 2>&1)" = \
    "Cannot use --everything with explicit reference arguments" ]
)
end_test

begin_test "migrate import (--everything with --include-ref)"
(
  set -e

  setup_multiple_local_branches

  [ "$(git lfs migrate import --everything --include-ref=refs/heads/main 2>&1)" = \
    "Cannot use --everything with --include-ref or --exclude-ref" ]
)
end_test

begin_test "migrate import (--everything with --exclude-ref)"
(
  set -e

  setup_multiple_local_branches

  [ "$(git lfs migrate import --everything --exclude-ref=refs/heads/main 2>&1)" = \
    "Cannot use --everything with --include-ref or --exclude-ref" ]
)
end_test

begin_test "migrate import (--everything and --include with glob pattern)"
(
  set -e

  setup_multiple_local_branches

  md_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.md")")"
  txt_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.txt")")"
  md_feature_oid="$(calc_oid "$(git cat-file -p "refs/heads/my-feature:a.md")")"
  txt_feature_oid="$(calc_oid "$(git cat-file -p "refs/heads/my-feature:a.txt")")"

  git lfs migrate import --verbose --everything --include='*.[mM][dD]'

  assert_pointer "refs/heads/main" "a.md" "$md_main_oid" "140"
  assert_pointer "refs/heads/my-feature" "a.md" "$md_feature_oid" "30"

  assert_local_object "$md_main_oid" "140"
  assert_local_object "$md_feature_oid" "30"
  refute_local_object "$txt_main_oid"
  refute_local_object "$txt_feature_oid"
)
end_test

begin_test "migrate import (--everything with tag pointing to tag)"
(
  set -e

  setup_multiple_local_branches

  md_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.md")")"
  txt_main_oid="$(calc_oid "$(git cat-file -p "refs/heads/main:a.txt")")"
  md_feature_oid="$(calc_oid "$(git cat-file -p "refs/heads/my-feature:a.md")")"
  txt_feature_oid="$(calc_oid "$(git cat-file -p "refs/heads/my-feature:a.txt")")"

  git tag -a -m abc abc refs/heads/main
  git tag -a -m def def refs/tags/abc

  git lfs migrate import --verbose --everything --include='*.[mM][dD]'

  assert_pointer "refs/heads/main" "a.md" "$md_main_oid" "140"
  assert_pointer "refs/tags/abc" "a.md" "$md_main_oid" "140"
  assert_pointer "refs/tags/def" "a.md" "$md_main_oid" "140"
  assert_pointer "refs/heads/my-feature" "a.md" "$md_feature_oid" "30"

  git tag --points-at refs/tags/abc | grep -q def
  git tag --points-at refs/tags/def | grep -q abc && exit 1

  assert_local_object "$md_main_oid" "140"
  assert_local_object "$md_feature_oid" "30"
  refute_local_object "$txt_main_oid"
  refute_local_object "$txt_feature_oid"
)
end_test

begin_test "migrate import (nested sub-trees and --include with wildcard)"
(
  set -e

  setup_single_local_branch_deep_trees

  oid="$(calc_oid "$(git cat-file -p :foo/bar/baz/a.txt)")"
  size="$(git cat-file -p :foo/bar/baz/a.txt | wc -c | awk '{ print $1 }')"

  git lfs migrate import --include="**/*ar/**"

  assert_pointer "refs/heads/main" "foo/bar/baz/a.txt" "$oid" "$size"
  assert_local_object "$oid" "$size"
)
end_test

begin_test "migrate import (handle copies of files)"
(
  set -e

  setup_single_local_branch_deep_trees

  # add the object from the sub-tree to the root directory
  cp foo/bar/baz/a.txt a.txt
  git add a.txt
  git commit -m "duplicated file"

  oid_root="$(calc_oid "$(git cat-file -p :a.txt)")"
  oid_tree="$(calc_oid "$(git cat-file -p :foo/bar/baz/a.txt)")"
  size="$(git cat-file -p :foo/bar/baz/a.txt | wc -c | awk '{ print $1 }')"

  # only import objects under "foo"
  git lfs migrate import --include="foo/**"

  assert_pointer "refs/heads/main" "foo/bar/baz/a.txt" "$oid_tree" "$size"
  assert_local_object "$oid_tree" "$size"

  # "a.txt" is not under "foo" and therefore should not be in LFS
  oid_root_after_migration="$(calc_oid "$(git cat-file -p :a.txt)")"
  [ "$oid_root" = "$oid_root_after_migration" ]
)
end_test

begin_test "migrate import (filter matches files only)"
(
  set -e

  setup_single_local_branch_same_file_tree_ext

  txt_root_oid="$(calc_oid "$(git cat-file -p :a.txt)")"
  txt_foo_oid="$(calc_oid "$(git cat-file -p :foo/a.txt)")"
  md_bar_oid="$(calc_oid "$(git cat-file -p :bar.txt/b.md)")"
  txt_bar_oid="$(calc_oid "$(git cat-file -p :bar.txt/b.txt)")"

  git lfs migrate import --include="*.txt"

  assert_local_object "$txt_root_oid" "120"
  assert_local_object "$txt_foo_oid" "120"
  assert_local_object "$txt_bar_oid" "120"
  refute_local_object "$md_bar_oid"
)
end_test

begin_test "migrate import (--object-map)"
(
  set -e

  setup_multiple_local_branches

  output_dir="$GIT_LFS_TEST_DIR/import-object-map-$(lfstest-genrandom --base64url 32)"
  mkdir -p "$output_dir"

  git log --all --pretty='format:%H' > "${output_dir}/old_sha.txt"
  git lfs migrate import --everything --object-map "${output_dir}/object-map.txt"
  git log --all --pretty='format:%H' > "${output_dir}/new_sha.txt"
  paste -d',' "${output_dir}/old_sha.txt" "${output_dir}/new_sha.txt" > "${output_dir}/expected-map.txt"

  diff -u <(sort "${output_dir}/expected-map.txt") <(sort "${output_dir}/object-map.txt")
)
end_test

begin_test "migrate import (--include with space)"
(
  set -e

  setup_local_branch_with_space

  oid="$(calc_oid "$(git cat-file -p :"a file.txt")")"

  git lfs migrate import --include "a file.txt"

  assert_pointer "refs/heads/main" "a file.txt" "$oid" 50
  cat .gitattributes
  if [ 1 -ne "$(grep -c "a\[\[:space:\]\]file.txt" .gitattributes)" ]; then
    echo >&2 "fatal: expected \"a[[:space:]]file.txt\" to appear in .gitattributes"
    echo >&2 "fatal: got"
    sed -e 's/^/  /g' < .gitattributes >&2
    exit 1
  fi
)
end_test

begin_test "migrate import (handle symbolic link)"
(
  set -e

  setup_local_branch_with_symlink

  txt_oid="$(calc_oid "$(git cat-file -p :a.txt)")"
  link_oid="$(calc_oid "$(git cat-file -p :link.txt)")"

  git lfs migrate import --include="*.txt"

  assert_pointer "refs/heads/main" "a.txt" "$txt_oid" "120"

  assert_local_object "$txt_oid" "120"
  # "link.txt" is a symbolic link so it should be not in LFS
  refute_local_object "$link_oid" "5"
)
end_test

begin_test "migrate import (commit --allow-empty)"
(
  set -e

  reponame="migrate---allow-empty"
  git init "$reponame"
  cd "$reponame"

  git commit --allow-empty -m "initial commit"

  original_head="$(git rev-parse HEAD)"
  git lfs migrate import --everything
  migrated_head="$(git rev-parse HEAD)"

  assert_ref_unmoved "HEAD" "$original_head" "$migrated_head"
)
end_test

begin_test "migrate import (multiple remotes)"
(
  set -e

  setup_multiple_remotes

  original_main="$(git rev-parse main)"

  git lfs migrate import

  migrated_main="$(git rev-parse main)"

  assert_ref_unmoved "main" "$original_main" "$migrated_main"
)
end_test

begin_test "migrate import (dirty copy, default negative answer)"
(
  set -e

  setup_local_branch_with_dirty_copy

  original_main="$(git rev-parse main)"

  echo | git lfs migrate import --everything 2>&1 | tee migrate.log
  grep "migrate: working copy must not be dirty" migrate.log

  migrated_main="$(git rev-parse main)"

  assert_ref_unmoved "main" "$original_main" "$migrated_main"
)
end_test

begin_test "migrate import (dirty copy, negative answer)"
(
  set -e

  setup_local_branch_with_dirty_copy

  original_main="$(git rev-parse main)"

  echo "n" | git lfs migrate import --everything 2>&1 | tee migrate.log
  grep "migrate: working copy must not be dirty" migrate.log

  migrated_main="$(git rev-parse main)"

  assert_ref_unmoved "main" "$original_main" "$migrated_main"
)
end_test

begin_test "migrate import (dirty copy, unknown then negative answer)"
(
  set -e

  setup_local_branch_with_dirty_copy

  original_main="$(git rev-parse main)"

  echo "x\nn" | git lfs migrate import --everything 2>&1 | tee migrate.log

  cat migrate.log

  [ "2" -eq "$(grep -o "override changes in your working copy" migrate.log \
    | wc -l | awk '{ print $1 }')" ]
  grep "migrate: working copy must not be dirty" migrate.log

  migrated_main="$(git rev-parse main)"

  assert_ref_unmoved "main" "$original_main" "$migrated_main"
)
end_test

begin_test "migrate import (dirty copy, positive answer)"
(
  set -e

  setup_local_branch_with_dirty_copy

  oid="$(calc_oid "$(git cat-file -p :a.txt)")"

  echo "y" | git lfs migrate import --everything 2>&1 | tee migrate.log
  grep "migrate: changes in your working copy will be overridden ..." \
    migrate.log

  assert_pointer "refs/heads/main" "a.txt" "$oid" "5"
  assert_local_object "$oid" "5"
)
end_test

begin_test "migrate import (non-standard refs)"
(
  set -e

  setup_multiple_local_branches_non_standard

  md_oid="$(calc_oid "$(git cat-file -p :a.md)")"
  txt_oid="$(calc_oid "$(git cat-file -p :a.txt)")"
  md_feature_oid="$(calc_oid "$(git cat-file -p my-feature:a.md)")"

  git lfs migrate import --everything

  assert_pointer "refs/heads/main" "a.md" "$md_oid" "140"
  assert_pointer "refs/heads/main" "a.txt" "$txt_oid" "120"
  assert_pointer "refs/pull/1/base" "a.md" "$md_oid" "140"
  assert_pointer "refs/pull/1/base" "a.txt" "$txt_oid" "120"

  assert_pointer "refs/heads/my-feature" "a.txt" "$txt_oid" "120"
  assert_pointer "refs/pull/1/head" "a.txt" "$txt_oid" "120"

  assert_local_object "$md_oid" "140"
  assert_local_object "$txt_oid" "120"
  assert_local_object "$md_feature_oid" "30"
)
end_test

begin_test "migrate import (copied file)"
(
  set -e

  setup_local_branch_with_copied_file

  git lfs migrate import --above=1b

  # Expect attributes for "/dir/a" and "/a"
  if ! grep -q "^/dir/a.txt" ./.gitattributes || ! grep -q "^/a.txt" ./.gitattributes; then
    exit 1
  fi
)
end_test

begin_test "migrate import (copied file with only a single path)"
(
  set -e

  setup_local_branch_with_copied_file

  oid="$(calc_oid "$(git cat-file -p :a.txt)")"

  # Prevent MSYS from rewriting /a.txt into a Windows path.
  MSYS_NO_PATHCONV=1 git lfs migrate import --include="/a.txt" --everything

  # Expect attribute for only "/a.txt".
  if grep -q "^/dir/a.txt" ./.gitattributes || ! grep -q "^/a.txt" ./.gitattributes; then
    exit 1
  fi
  refute_pointer "refs/heads/main" "dir/a.txt" "$oid" 5
)
end_test

begin_test "migrate import (filename special characters)"
(
  set -e
  setup_local_branch_with_special_character_files
  git lfs migrate import --above=1b
  # Windows does not allow creation of files with '*', so expect 2 files, not 3
  if [ "$IS_WINDOWS" -eq "1" ] ; then
    test "$(git check-attr filter -- *.bin |grep lfs | wc -l)" -eq 2 || exit 1
  else
    test "$(git check-attr filter -- *.bin |grep lfs | wc -l)" -eq 3 || exit 1
  fi
)
end_test