-
Notifications
You must be signed in to change notification settings - Fork 418
/
test_git.sh
executable file
·989 lines (856 loc) · 28.3 KB
/
test_git.sh
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
#!/bin/bash
#
# Copyright 2023 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -o errexit
set -o nounset
set -o pipefail
function fail() {
echo "FAIL:" "$@" >&3
return 42
}
function pass() {
echo "PASS"
}
function assert_file_exists() {
if ! [[ -f "$1" ]]; then
fail "$1 does not exist"
fi
}
function assert_file_absent() {
if [[ -f "$1" ]]; then
fail "$1 exists but should not"
fi
}
function assert_eq() {
if [[ "$1" == "$2" ]]; then
return
fi
fail "'$1' does not equal '$2'"
}
function assert_substr() {
if [[ "$1" == *"$2"* ]]; then
return
fi
fail "'$1' does not contain '$2'"
}
# DIR is the directory in which all this test's state lives.
RUNID="${RANDOM}${RANDOM}"
DIR="/tmp/git-sync-git.$RUNID"
mkdir "$DIR"
# WORKDIR is where test cases run
WORKDIR="$DIR/work"
function clean_workdir() {
rm -rf "$WORKDIR"
mkdir -p "$WORKDIR"
}
#
# After all the test functions are defined, we can iterate over them and run
# them all automatically. See the end of this file.
#
##############################################
# Test `git init` on an existing repo
##############################################
function git::reinit_existing_repo() {
git init -b main
date > file
git add file
git commit -qam 'commit_1'
TREE1="$(git ls-tree HEAD | cut -d' ' -f3 | cut -f1)"
git init
TREE2="$(git ls-tree HEAD | cut -d' ' -f3 | cut -f1)"
assert_eq "$TREE1" "$TREE2"
}
##############################################
# Test `git fetch` of a branch
##############################################
function git::fetch_upstream_branch() {
mkdir upstream
pushd upstream >/dev/null
git init -b main
# A commit on branch 1
git checkout -b upstream_branch_1
date > file_1
git add file_1
git commit -qam 'commit_1'
SHA1="$(git rev-parse HEAD)"
# A commit on branch 2
git checkout -b upstream_branch_2
date > file_2
git add file_2
git commit -qam 'commit_2'
SHA2="$(git rev-parse HEAD)"
popd >/dev/null
mkdir clone
cd clone
git init -b clone_branch
assert_substr "$(git cat-file -t "$SHA1" 2>&1 || true)" "could not get object info"
assert_substr "$(git cat-file -t "$SHA2" 2>&1 || true)" "could not get object info"
git fetch "file://$WORKDIR/upstream" upstream_branch_1
assert_eq "$(git cat-file -t "$SHA1")" "commit"
assert_substr "$(git cat-file -t "$SHA2" 2>&1 || true)" "could not get object info"
git checkout "$SHA1"
assert_file_exists file_1
assert_file_absent file_2
git fetch "file://$WORKDIR/upstream" upstream_branch_2
assert_eq "$(git cat-file -t "$SHA1")" "commit"
assert_eq "$(git cat-file -t "$SHA2")" "commit"
git checkout "$SHA2"
assert_file_exists file_1
assert_file_exists file_2
}
##############################################
# Test `git fetch` of a tag
##############################################
function git::fetch_upstream_tag() {
mkdir upstream
pushd upstream >/dev/null
git init -b main
# A tag on branch 1 (not at HEAD)
git checkout -b upstream_branch_1
date > file_1
git add file_1
git commit -qam 'commit_1'
SHA1="$(git rev-parse HEAD)"
git tag upstream_tag_1
# Another tag on branch 1 (at HEAD)
date > file_2
git add file_2
git commit -qam 'commit_2'
SHA2="$(git rev-parse HEAD)"
git tag upstream_tag_2
# A tag on branch 2 (not at HEAD)
git checkout -b upstream_branch_2
date > file_3
git add file_3
git commit -qam 'commit_3'
SHA3="$(git rev-parse HEAD)"
git tag upstream_tag_3
# Another tag on branch 2 (at HEAD)
date > file_4
git add file_4
git commit -qam 'commit_4'
SHA4="$(git rev-parse HEAD)"
git tag upstream_tag_4
popd >/dev/null
mkdir clone
pushd clone >/dev/null
git init -b clone_branch
assert_substr "$(git cat-file -t "$SHA1" 2>&1 || true)" "could not get object info"
assert_substr "$(git cat-file -t "$SHA2" 2>&1 || true)" "could not get object info"
assert_substr "$(git cat-file -t "$SHA3" 2>&1 || true)" "could not get object info"
assert_substr "$(git cat-file -t "$SHA4" 2>&1 || true)" "could not get object info"
git fetch "file://$WORKDIR/upstream" upstream_tag_1
assert_eq "$(git cat-file -t "$SHA1")" "commit"
assert_substr "$(git cat-file -t "$SHA2" 2>&1 || true)" "could not get object info"
assert_substr "$(git cat-file -t "$SHA3" 2>&1 || true)" "could not get object info"
assert_substr "$(git cat-file -t "$SHA4" 2>&1 || true)" "could not get object info"
git checkout "$SHA1"
assert_file_exists file_1
assert_file_absent file_2
assert_file_absent file_3
assert_file_absent file_4
git fetch "file://$WORKDIR/upstream" upstream_tag_2
assert_eq "$(git cat-file -t "$SHA1")" "commit"
assert_eq "$(git cat-file -t "$SHA2")" "commit"
assert_substr "$(git cat-file -t "$SHA3" 2>&1 || true)" "could not get object info"
assert_substr "$(git cat-file -t "$SHA4" 2>&1 || true)" "could not get object info"
git checkout "$SHA2"
assert_file_exists file_1
assert_file_exists file_2
assert_file_absent file_3
assert_file_absent file_4
git fetch "file://$WORKDIR/upstream" upstream_tag_3
assert_eq "$(git cat-file -t "$SHA1")" "commit"
assert_eq "$(git cat-file -t "$SHA2")" "commit"
assert_eq "$(git cat-file -t "$SHA3")" "commit"
assert_substr "$(git cat-file -t "$SHA4" 2>&1 || true)" "could not get object info"
git checkout "$SHA3"
assert_file_exists file_1
assert_file_exists file_2
assert_file_exists file_3
assert_file_absent file_4
git fetch "file://$WORKDIR/upstream" upstream_tag_4
assert_eq "$(git cat-file -t "$SHA1")" "commit"
assert_eq "$(git cat-file -t "$SHA2")" "commit"
assert_eq "$(git cat-file -t "$SHA3")" "commit"
assert_eq "$(git cat-file -t "$SHA4")" "commit"
git checkout "$SHA4"
assert_file_exists file_1
assert_file_exists file_2
assert_file_exists file_3
assert_file_exists file_4
}
##############################################
# Test `git fetch` of an annotated tag
##############################################
function git::fetch_upstream_tag_annotated() {
mkdir upstream
pushd upstream >/dev/null
git init -b main
# A tag on branch 1 (not at HEAD)
git checkout -b upstream_branch_1
date > file_1
git add file_1
git commit -qam 'commit_1'
SHA1="$(git rev-parse HEAD)"
git tag -am "anntag_1" upstream_anntag_1
# Another tag on branch 1 (at HEAD)
date > file_2
git add file_2
git commit -qam 'commit_2'
SHA2="$(git rev-parse HEAD)"
git tag -am "anntag_2" upstream_anntag_2
# A tag on branch 2 (not at HEAD)
git checkout -b upstream_branch_2
date > file_3
git add file_3
git commit -qam 'commit_3'
SHA3="$(git rev-parse HEAD)"
git tag -am "anntag_3" upstream_anntag_3
# Another tag on branch 2 (at HEAD)
date > file_4
git add file_4
git commit -qam 'commit_4'
SHA4="$(git rev-parse HEAD)"
git tag -am "anntag_4" upstream_anntag_4
popd >/dev/null
mkdir clone
pushd clone >/dev/null
git init -b clone_branch
assert_substr "$(git cat-file -t "$SHA1" 2>&1 || true)" "could not get object info"
assert_substr "$(git cat-file -t "$SHA2" 2>&1 || true)" "could not get object info"
assert_substr "$(git cat-file -t "$SHA3" 2>&1 || true)" "could not get object info"
assert_substr "$(git cat-file -t "$SHA4" 2>&1 || true)" "could not get object info"
git fetch "file://$WORKDIR/upstream" upstream_anntag_1
assert_eq "$(git cat-file -t "$SHA1")" "commit"
assert_substr "$(git cat-file -t "$SHA2" 2>&1 || true)" "could not get object info"
assert_substr "$(git cat-file -t "$SHA3" 2>&1 || true)" "could not get object info"
assert_substr "$(git cat-file -t "$SHA4" 2>&1 || true)" "could not get object info"
git checkout "$SHA1"
assert_file_exists file_1
assert_file_absent file_2
assert_file_absent file_3
assert_file_absent file_4
git fetch "file://$WORKDIR/upstream" upstream_anntag_2
assert_eq "$(git cat-file -t "$SHA1")" "commit"
assert_eq "$(git cat-file -t "$SHA2")" "commit"
assert_substr "$(git cat-file -t "$SHA3" 2>&1 || true)" "could not get object info"
assert_substr "$(git cat-file -t "$SHA4" 2>&1 || true)" "could not get object info"
git checkout "$SHA2"
assert_file_exists file_1
assert_file_exists file_2
assert_file_absent file_3
assert_file_absent file_4
git fetch "file://$WORKDIR/upstream" upstream_anntag_3
assert_eq "$(git cat-file -t "$SHA1")" "commit"
assert_eq "$(git cat-file -t "$SHA2")" "commit"
assert_eq "$(git cat-file -t "$SHA3")" "commit"
assert_substr "$(git cat-file -t "$SHA4" 2>&1 || true)" "could not get object info"
git checkout "$SHA3"
assert_file_exists file_1
assert_file_exists file_2
assert_file_exists file_3
assert_file_absent file_4
git fetch "file://$WORKDIR/upstream" upstream_anntag_4
assert_eq "$(git cat-file -t "$SHA1")" "commit"
assert_eq "$(git cat-file -t "$SHA2")" "commit"
assert_eq "$(git cat-file -t "$SHA3")" "commit"
assert_eq "$(git cat-file -t "$SHA4")" "commit"
git checkout "$SHA4"
assert_file_exists file_1
assert_file_exists file_2
assert_file_exists file_3
assert_file_exists file_4
}
##############################################
# Test `git fetch` of a SHA
##############################################
function git::fetch_upstream_sha() {
mkdir upstream
pushd upstream >/dev/null
git init -b main
# A commit on branch 1 (not at HEAD)
git checkout -b upstream_branch_1
date > file_1
git add file_1
git commit -qam 'commit_1'
SHA1="$(git rev-parse HEAD)"
# Another commit on branch 1 (at HEAD)
date > file_2
git add file_2
git commit -qam 'commit_2'
SHA2="$(git rev-parse HEAD)"
# A commit on branch 2 (not at HEAD)
git checkout -b upstream_branch_2
date > file_3
git add file_3
git commit -qam 'commit_3'
SHA3="$(git rev-parse HEAD)"
# Another commit on branch 2 (at HEAD)
date > file_4
git add file_4
git commit -qam 'commit_4'
SHA4="$(git rev-parse HEAD)"
popd >/dev/null
mkdir clone
pushd clone >/dev/null
git init -b clone_branch
assert_substr "$(git cat-file -t "$SHA1" 2>&1 || true)" "could not get object info"
assert_substr "$(git cat-file -t "$SHA2" 2>&1 || true)" "could not get object info"
assert_substr "$(git cat-file -t "$SHA3" 2>&1 || true)" "could not get object info"
assert_substr "$(git cat-file -t "$SHA4" 2>&1 || true)" "could not get object info"
git fetch "file://$WORKDIR/upstream" "$SHA1"
assert_eq "$(git cat-file -t "$SHA1")" "commit"
assert_substr "$(git cat-file -t "$SHA2" 2>&1 || true)" "could not get object info"
assert_substr "$(git cat-file -t "$SHA3" 2>&1 || true)" "could not get object info"
assert_substr "$(git cat-file -t "$SHA4" 2>&1 || true)" "could not get object info"
git checkout "$SHA1"
assert_file_exists file_1
assert_file_absent file_2
assert_file_absent file_3
assert_file_absent file_4
git fetch "file://$WORKDIR/upstream" "$SHA2"
assert_eq "$(git cat-file -t "$SHA1")" "commit"
assert_eq "$(git cat-file -t "$SHA2")" "commit"
assert_substr "$(git cat-file -t "$SHA3" 2>&1 || true)" "could not get object info"
assert_substr "$(git cat-file -t "$SHA4" 2>&1 || true)" "could not get object info"
git checkout "$SHA2"
assert_file_exists file_1
assert_file_exists file_2
assert_file_absent file_3
assert_file_absent file_4
git fetch "file://$WORKDIR/upstream" "$SHA3"
assert_eq "$(git cat-file -t "$SHA1")" "commit"
assert_eq "$(git cat-file -t "$SHA2")" "commit"
assert_eq "$(git cat-file -t "$SHA3")" "commit"
assert_substr "$(git cat-file -t "$SHA4" 2>&1 || true)" "could not get object info"
git checkout "$SHA3"
assert_file_exists file_1
assert_file_exists file_2
assert_file_exists file_3
assert_file_absent file_4
git fetch "file://$WORKDIR/upstream" "$SHA4"
assert_eq "$(git cat-file -t "$SHA1")" "commit"
assert_eq "$(git cat-file -t "$SHA2")" "commit"
assert_eq "$(git cat-file -t "$SHA3")" "commit"
assert_eq "$(git cat-file -t "$SHA4")" "commit"
git checkout "$SHA4"
assert_file_exists file_1
assert_file_exists file_2
assert_file_exists file_3
assert_file_exists file_4
}
##############################################
# Test git shallow fetch from a branch
##############################################
function git::shallow_fetch_branch() {
mkdir upstream
pushd upstream >/dev/null
git init -b upstream_branch
# Some commits
date > file_1
git add file_1
git commit -qam 'commit_1'
date > file_2
git add file_2
git commit -qam 'commit_2'
SHA="$(git rev-parse HEAD)"
popd >/dev/null
mkdir clone
cd clone
git init -b clone_branch
git fetch "file://$WORKDIR/upstream" upstream_branch
git checkout "$SHA"
assert_file_exists file_1
assert_file_exists file_2
assert_eq "$(git rev-parse --is-shallow-repository)" "false"
git fetch "file://$WORKDIR/upstream" upstream_branch --depth 1
git checkout "$SHA"
assert_file_exists file_1
assert_file_exists file_2
assert_eq "$(git rev-parse --is-shallow-repository)" "true"
git fetch "file://$WORKDIR/upstream" upstream_branch --unshallow
git checkout "$SHA"
assert_file_exists file_1
assert_file_exists file_2
assert_eq "$(git rev-parse --is-shallow-repository)" "false"
}
##############################################
# Test git shallow fetch from a tag
##############################################
function git::shallow_fetch_tag() {
mkdir upstream
pushd upstream >/dev/null
git init -b upstream_branch
# Some commits
date > file_1
git add file_1
git commit -qam 'commit_1'
date > file_2
git add file_2
git commit -qam 'commit_2'
SHA="$(git rev-parse HEAD)"
git tag upstream_tag
popd >/dev/null
mkdir clone
cd clone
git init -b clone_branch
git fetch "file://$WORKDIR/upstream" upstream_tag
git checkout "$SHA"
assert_file_exists file_1
assert_file_exists file_2
assert_eq "$(git rev-parse --is-shallow-repository)" "false"
git fetch "file://$WORKDIR/upstream" upstream_tag --depth 1
git checkout "$SHA"
assert_file_exists file_1
assert_file_exists file_2
assert_eq "$(git rev-parse --is-shallow-repository)" "true"
git fetch "file://$WORKDIR/upstream" upstream_tag --unshallow
git checkout "$SHA"
assert_file_exists file_1
assert_file_exists file_2
assert_eq "$(git rev-parse --is-shallow-repository)" "false"
}
##############################################
# Test git shallow fetch from an annotated tag
##############################################
function git::shallow_fetch_tag_annotated() {
mkdir upstream
pushd upstream >/dev/null
git init -b upstream_branch
# Some commits
date > file_1
git add file_1
git commit -qam 'commit_1'
date > file_2
git add file_2
git commit -qam 'commit_2'
SHA="$(git rev-parse HEAD)"
git tag -am "upstream_anntag" upstream_anntag
popd >/dev/null
mkdir clone
cd clone
git init -b clone_branch
git fetch "file://$WORKDIR/upstream" upstream_anntag
git checkout "$SHA"
assert_file_exists file_1
assert_file_exists file_2
assert_eq "$(git rev-parse --is-shallow-repository)" "false"
git fetch "file://$WORKDIR/upstream" upstream_anntag --depth 1
git checkout "$SHA"
assert_file_exists file_1
assert_file_exists file_2
assert_eq "$(git rev-parse --is-shallow-repository)" "true"
git fetch "file://$WORKDIR/upstream" upstream_anntag --unshallow
git checkout "$SHA"
assert_file_exists file_1
assert_file_exists file_2
assert_eq "$(git rev-parse --is-shallow-repository)" "false"
}
##############################################
# Test git shallow fetch from a SHA
##############################################
function git::shallow_fetch_sha() {
mkdir upstream
pushd upstream >/dev/null
git init -b upstream_branch
# Some commits
date > file_1
git add file_1
git commit -qam 'commit_1'
date > file_2
git add file_2
git commit -qam 'commit_2'
SHA="$(git rev-parse HEAD)"
popd >/dev/null
mkdir clone
cd clone
git init -b clone_branch
git fetch "file://$WORKDIR/upstream" "$SHA"
git checkout "$SHA"
assert_file_exists file_1
assert_file_exists file_2
assert_eq "$(git rev-parse --is-shallow-repository)" "false"
git fetch "file://$WORKDIR/upstream" "$SHA" --depth 1
git checkout "$SHA"
assert_file_exists file_1
assert_file_exists file_2
assert_eq "$(git rev-parse --is-shallow-repository)" "true"
git fetch "file://$WORKDIR/upstream" "$SHA" --unshallow
git checkout "$SHA"
assert_file_exists file_1
assert_file_exists file_2
assert_eq "$(git rev-parse --is-shallow-repository)" "false"
}
##############################################
# Test git fetch with depth too large
##############################################
function git::fetch_too_large_depth() {
mkdir upstream
pushd upstream >/dev/null
git init -b upstream_branch
# Some commits
date > file_1
git add file_1
git commit -qam 'commit_1'
date > file_2
git add file_2
git commit -qam 'commit_2'
SHA="$(git rev-parse HEAD)"
popd >/dev/null
mkdir clone
cd clone
git init -b clone_branch
git fetch "file://$WORKDIR/upstream" upstream_branch --depth 1000
git checkout "$SHA"
assert_file_exists file_1
assert_file_exists file_2
assert_eq "$(git rev-parse --is-shallow-repository)" "false"
}
##############################################
# Test git rev-parse with a branch
##############################################
function git::rev_parse_branch() {
mkdir repo
pushd repo >/dev/null
git init -b main
# A commit on branch 1
git checkout -b branch_1
date > file_1
git add file_1
git commit -qam 'commit_1'
SHA1="$(git rev-parse HEAD)"
# A commit on branch 2
git checkout -b branch_2
date > file_2
git add file_2
git commit -qam 'commit_2'
SHA2="$(git rev-parse HEAD)"
assert_eq "$(git rev-parse branch_1)" "$SHA1"
assert_eq "$(git rev-parse 'branch_1^{commit}')" "$SHA1"
assert_eq "$(git rev-parse branch_2)" "$SHA2"
assert_eq "$(git rev-parse 'branch_2^{commit}')" "$SHA2"
}
##############################################
# Test git rev-parse with a tag
##############################################
function git::rev_parse_tag() {
mkdir repo
pushd repo >/dev/null
git init -b main
# A tag on branch 1 (not at HEAD)
git checkout -b branch_1
date > file_1
git add file_1
git commit -qam 'commit_1'
SHA1="$(git rev-parse HEAD)"
git tag tag_1
# Another tag on branch 1 (at HEAD)
date > file_2
git add file_2
git commit -qam 'commit_2'
SHA2="$(git rev-parse HEAD)"
git tag tag_2
# A tag on branch 2 (not at HEAD)
git checkout -b branch_2
date > file_3
git add file_3
git commit -qam 'commit_3'
SHA3="$(git rev-parse HEAD)"
git tag tag_3
# Another tag on branch 2 (at HEAD)
date > file_4
git add file_4
git commit -qam 'commit_4'
SHA4="$(git rev-parse HEAD)"
git tag tag_4
assert_eq "$(git rev-parse tag_1)" "$SHA1"
assert_eq "$(git rev-parse 'tag_1^{commit}')" "$SHA1"
assert_eq "$(git rev-parse tag_2)" "$SHA2"
assert_eq "$(git rev-parse 'tag_2^{commit}')" "$SHA2"
assert_eq "$(git rev-parse tag_3)" "$SHA3"
assert_eq "$(git rev-parse 'tag_3^{commit}')" "$SHA3"
assert_eq "$(git rev-parse tag_4)" "$SHA4"
assert_eq "$(git rev-parse 'tag_4^{commit}')" "$SHA4"
}
##############################################
# Test git rev-parse with an annotated tag
##############################################
function git::rev_parse_tag_annotated() {
mkdir repo
pushd repo >/dev/null
git init -b main
# A tag on branch 1 (not at HEAD)
git checkout -b branch_1
date > file_1
git add file_1
git commit -qam 'commit_1'
SHA1="$(git rev-parse HEAD)"
git tag -am "anntag_1" anntag_1
# Another tag on branch 1 (at HEAD)
date > file_2
git add file_2
git commit -qam 'commit_2'
SHA2="$(git rev-parse HEAD)"
git tag -am "anntag_2" anntag_2
# A tag on branch 2 (not at HEAD)
git checkout -b branch_2
date > file_3
git add file_3
git commit -qam 'commit_3'
SHA3="$(git rev-parse HEAD)"
git tag -am "anntag_3" anntag_3
# Another tag on branch 2 (at HEAD)
date > file_4
git add file_4
git commit -qam 'commit_4'
SHA4="$(git rev-parse HEAD)"
git tag -am "anntag_4" anntag_4
# Annotated tags have their own SHA, which can be found with rev-parse, but
# it doesn't make sense to test rev-parse against itself.
assert_eq "$(git rev-parse 'anntag_1^{commit}')" "$SHA1"
assert_eq "$(git rev-parse 'anntag_2^{commit}')" "$SHA2"
assert_eq "$(git rev-parse 'anntag_3^{commit}')" "$SHA3"
assert_eq "$(git rev-parse 'anntag_4^{commit}')" "$SHA4"
}
##############################################
# Test git rev-parse with a SHA
##############################################
function git::rev_parse_sha() {
mkdir repo
pushd repo >/dev/null
git init -b main
# A commit on branch 1 (not at HEAD)
git checkout -b branch_1
date > file_1
git add file_1
git commit -qam 'commit_1'
SHA1="$(git rev-parse HEAD)"
SHORT1="${SHA1%????????}"
# Another commit on branch 1 (at HEAD)
date > file_2
git add file_2
git commit -qam 'commit_2'
SHA2="$(git rev-parse HEAD)"
SHORT2="${SHA2%????????}"
# A commit on branch 2 (not at HEAD)
git checkout -b branch_2
date > file_3
git add file_3
git commit -qam 'commit_3'
SHA3="$(git rev-parse HEAD)"
SHORT3="${SHA3%????????}"
# Another commit on branch 2 (at HEAD)
date > file_4
git add file_4
git commit -qam 'commit_4'
SHA4="$(git rev-parse HEAD)"
SHORT4="${SHA4%????????}"
assert_eq "$(git rev-parse "$SHA1")" "$SHA1"
assert_eq "$(git rev-parse "$SHA1^{commit}")" "$SHA1"
assert_eq "$(git rev-parse "$SHORT1")" "$SHA1"
assert_eq "$(git rev-parse "$SHA2")" "$SHA2"
assert_eq "$(git rev-parse "$SHA2^{commit}")" "$SHA2"
assert_eq "$(git rev-parse "$SHORT2")" "$SHA2"
assert_eq "$(git rev-parse "$SHA3")" "$SHA3"
assert_eq "$(git rev-parse "$SHA3^{commit}")" "$SHA3"
assert_eq "$(git rev-parse "$SHORT3")" "$SHA3"
assert_eq "$(git rev-parse "$SHA4")" "$SHA4"
assert_eq "$(git rev-parse "$SHA4^{commit}")" "$SHA4"
assert_eq "$(git rev-parse "$SHORT4")" "$SHA4"
}
##############################################
# Test git rev-parse with a non-existent ref
##############################################
function git::rev_parse_non_existent_name() {
mkdir repo
pushd repo >/dev/null
git init -b main
assert_substr "$(git rev-parse does-not-exist 2>&1 || true)" "unknown revision"
}
##############################################
# Test git rev-parse with a non-existent sha
##############################################
function git::rev_parse_non_existent_sha() {
mkdir repo
pushd repo >/dev/null
git init -b main
# As long as it tastes like a SHA, rev-parse is happy, but there is no
# commit for it.
assert_eq "$(git rev-parse 0123456789abcdef0123456789abcdef01234567)" "0123456789abcdef0123456789abcdef01234567"
assert_substr "$(git rev-parse '0123456789abcdef0123456789abcdef01234567^{commit}' 2>&1 || true)" "unknown revision"
# Less-than-full SHAs do not work.
assert_substr "$(git rev-parse 0123456789abcdef 2>&1 || true)" "unknown revision"
assert_substr "$(git rev-parse '0123456789abcdef^{commit}' 2>&1 || true)" "unknown revision"
}
#
# main
#
function list_tests() {
(
shopt -s extdebug
declare -F \
| cut -f3 -d' ' \
| grep "^git::" \
| while read -r X; do declare -F "$X"; done \
| sort -n -k2 \
| cut -f1 -d' ' \
| sed 's/^git:://'
)
}
# Figure out which, if any, tests to run.
mapfile -t all_tests < <(list_tests)
tests_to_run=()
function print_tests() {
echo "available tests:"
for t in "${all_tests[@]}"; do
echo " $t"
done
}
# Validate and accumulate tests to run if args are specified.
for arg; do
# Use -? to list known tests.
if [[ "${arg}" == "-?" ]]; then
print_tests
exit 0
fi
if [[ "${arg}" =~ ^- ]]; then
echo "ERROR: unknown flag '${arg}'"
exit 1
fi
# Make sure each non-flag arg matches at least one test.
nmatches=0
for t in "${all_tests[@]}"; do
if [[ "${t}" =~ ${arg} ]]; then
nmatches=$((nmatches+1))
# Don't run tests twice, just keep the first match.
if [[ " ${tests_to_run[*]} " == *" ${t} "* ]]; then
continue
fi
tests_to_run+=("${t}")
continue
fi
done
if [[ ${nmatches} == 0 ]]; then
echo "ERROR: no tests match pattern '${arg}'"
echo
print_tests
exit 1
fi
tests_to_run+=("${matches[@]}")
done
set -- "${tests_to_run[@]}"
# If no tests were specified, run them all.
if [[ "$#" == 0 ]]; then
set -- "${all_tests[@]}"
fi
function finish() {
r=$?
trap "" INT EXIT ERR
if [[ $r != 0 ]]; then
echo
echo "the directory $DIR was not removed as it contains"\
"log files useful for debugging"
fi
exit $r
}
trap finish INT EXIT ERR
echo
echo "test root is $DIR"
echo
# Run a test function and return its error code. This is needed because POSIX
# dictates that `errexit` does not apply inside a function called in an `if`
# context. But if we don't call it with `if`, then it terminates the whole
# test run as soon as one test fails. So this jumps through hoops to let the
# individual test functions run outside of `if` and return a code in a
# variable.
#
# Args:
# $1: the name of a variable to populate with the return code
# $2+: the test function to run and optional args
function run_test() {
retvar=$1
shift
declare -g "$retvar"
local restore_opts
restore_opts=$(set +o)
set +o errexit
set +o nounset
set +o pipefail
(
set -o errexit
set -o nounset
set -o pipefail
"$@"
)
eval "$retvar=$?"
eval "$restore_opts"
}
# Override local configs for predictability in this test.
export GIT_CONFIG_GLOBAL=/dev/null
export GIT_CONFIG_SYSTEM=/dev/null
# TODO: add a flag to run all the tests inside the git-sync container image
# Iterate over the chosen tests and run them.
FAILS=()
FINAL_RET=0
RUNS="${RUNS:-1}"
for t; do
TEST_RET=0
RUN=0
while (( "${RUN}" < "${RUNS}" )); do
clean_workdir
pushd "$WORKDIR" >/dev/null
sfx=""
if (( "${RUNS}" > 1 )); then
sfx=" ($((RUN+1))/${RUNS})"
fi
echo -n "testcase ${t}${sfx}: "
# Set &3 for our own output, let testcases use &2 and &1.
exec 3>&1
# See comments on run_test for details.
RUN_RET=0
LOG="${DIR}/log.$t"
run_test RUN_RET "git::${t}" >"${LOG}.${RUN}" 2>&1
if [[ "$RUN_RET" == 0 ]]; then
pass
else
TEST_RET=1
if [[ "$RUN_RET" != 42 ]]; then
echo "FAIL: unknown error"
fi
fi
RUN=$((RUN+1))
popd >/dev/null
done
if [[ "$TEST_RET" != 0 ]]; then
FINAL_RET=1
FAILS+=("$t (log: ${LOG}.*)")
fi
done
if [[ "$FINAL_RET" != 0 ]]; then
echo
echo "the following tests failed:"
for f in "${FAILS[@]}"; do
echo " $f"
done
exit 1
fi
# Finally...
echo
if [[ "${CLEANUP:-}" == 0 ]]; then
echo "leaving logs in $DIR"
else
rm -rf "$DIR"
fi