Merge branch 'ar/submodule-add-config'
[git/debian.git] / t / t1503-rev-parse-verify.sh
blob40958615ebb9c16af55ab81c08584ef784e0574c
1 #!/bin/sh
3 # Copyright (c) 2008 Christian Couder
5 test_description='test git rev-parse --verify'
7 exec </dev/null
9 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
10 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
12 . ./test-lib.sh
14 add_line_into_file()
16 _line=$1
17 _file=$2
19 if [ -f "$_file" ]; then
20 echo "$_line" >> $_file || return $?
21 MSG="Add <$_line> into <$_file>."
22 else
23 echo "$_line" > $_file || return $?
24 git add $_file || return $?
25 MSG="Create file <$_file> with <$_line> inside."
28 test_tick
29 git commit --quiet -m "$MSG" $_file
32 HASH1=
33 HASH2=
34 HASH3=
35 HASH4=
37 test_expect_success 'set up basic repo with 1 file (hello) and 4 commits' '
38 add_line_into_file "1: Hello World" hello &&
39 HASH1=$(git rev-parse --verify HEAD) &&
40 add_line_into_file "2: A new day for git" hello &&
41 HASH2=$(git rev-parse --verify HEAD) &&
42 add_line_into_file "3: Another new day for git" hello &&
43 HASH3=$(git rev-parse --verify HEAD) &&
44 add_line_into_file "4: Ciao for now" hello &&
45 HASH4=$(git rev-parse --verify HEAD)
48 test_expect_success 'works with one good rev' '
49 rev_hash1=$(git rev-parse --verify $HASH1) &&
50 test "$rev_hash1" = "$HASH1" &&
51 rev_hash2=$(git rev-parse --verify $HASH2) &&
52 test "$rev_hash2" = "$HASH2" &&
53 rev_hash3=$(git rev-parse --verify $HASH3) &&
54 test "$rev_hash3" = "$HASH3" &&
55 rev_hash4=$(git rev-parse --verify $HASH4) &&
56 test "$rev_hash4" = "$HASH4" &&
57 rev_main=$(git rev-parse --verify main) &&
58 test "$rev_main" = "$HASH4" &&
59 rev_head=$(git rev-parse --verify HEAD) &&
60 test "$rev_head" = "$HASH4"
63 test_expect_success 'fails with any bad rev or many good revs' '
64 test_must_fail git rev-parse --verify 2>error &&
65 grep "single revision" error &&
66 test_must_fail git rev-parse --verify foo 2>error &&
67 grep "single revision" error &&
68 test_must_fail git rev-parse --verify HEAD bar 2>error &&
69 grep "single revision" error &&
70 test_must_fail git rev-parse --verify baz HEAD 2>error &&
71 grep "single revision" error &&
72 test_must_fail git rev-parse --verify $HASH2 HEAD 2>error &&
73 grep "single revision" error
76 test_expect_success 'fails silently when using -q' '
77 test_must_fail git rev-parse --verify --quiet 2>error &&
78 test_must_be_empty error &&
79 test_must_fail git rev-parse -q --verify foo 2>error &&
80 test_must_be_empty error &&
81 test_must_fail git rev-parse --verify -q HEAD bar 2>error &&
82 test_must_be_empty error &&
83 test_must_fail git rev-parse --quiet --verify baz HEAD 2>error &&
84 test_must_be_empty error &&
85 test_must_fail git rev-parse -q --verify $HASH2 HEAD 2>error &&
86 test_must_be_empty error
89 test_expect_success 'fails silently when using -q with deleted reflogs' '
90 ref=$(git rev-parse HEAD) &&
91 git update-ref --create-reflog -m "message for refs/test" refs/test "$ref" &&
92 git reflog delete --updateref --rewrite refs/test@{1} &&
93 test_must_fail git rev-parse -q --verify refs/test@{1} >error 2>&1 &&
94 test_must_be_empty error
97 test_expect_success 'fails silently when using -q with not enough reflogs' '
98 ref=$(git rev-parse HEAD) &&
99 git update-ref --create-reflog -m "message for refs/test2" refs/test2 "$ref" &&
100 test_must_fail git rev-parse -q --verify refs/test2@{999} >error 2>&1 &&
101 test_must_be_empty error
104 test_expect_success 'succeeds silently with -q and reflogs that do not go far back enough in time' '
105 ref=$(git rev-parse HEAD) &&
106 git update-ref --create-reflog -m "message for refs/test3" refs/test3 "$ref" &&
107 git rev-parse -q --verify refs/test3@{1.year.ago} >actual 2>error &&
108 test_must_be_empty error &&
109 echo "$ref" >expect &&
110 test_cmp expect actual
113 test_expect_success 'no stdout output on error' '
114 test -z "$(git rev-parse --verify)" &&
115 test -z "$(git rev-parse --verify foo)" &&
116 test -z "$(git rev-parse --verify baz HEAD)" &&
117 test -z "$(git rev-parse --verify HEAD bar)" &&
118 test -z "$(git rev-parse --verify $HASH2 HEAD)"
121 test_expect_success 'use --default' '
122 git rev-parse --verify --default main &&
123 git rev-parse --verify --default main HEAD &&
124 git rev-parse --default main --verify &&
125 git rev-parse --default main --verify HEAD &&
126 git rev-parse --verify HEAD --default main &&
127 test_must_fail git rev-parse --verify foo --default main &&
128 test_must_fail git rev-parse --default HEAD --verify bar &&
129 test_must_fail git rev-parse --verify --default HEAD baz &&
130 test_must_fail git rev-parse --default foo --verify &&
131 test_must_fail git rev-parse --verify --default bar
134 test_expect_success 'main@{n} for various n' '
135 N=$(git reflog | wc -l) &&
136 Nm1=$(($N-1)) &&
137 Np1=$(($N+1)) &&
138 git rev-parse --verify main@{0} &&
139 git rev-parse --verify main@{1} &&
140 git rev-parse --verify main@{$Nm1} &&
141 test_must_fail git rev-parse --verify main@{$N} &&
142 test_must_fail git rev-parse --verify main@{$Np1}
145 test_expect_success SYMLINKS,REFFILES 'ref resolution not confused by broken symlinks' '
146 ln -s does-not-exist .git/refs/heads/broken &&
147 test_must_fail git rev-parse --verify broken
150 test_expect_success 'options can appear after --verify' '
151 git rev-parse --verify HEAD >expect &&
152 git rev-parse --verify -q HEAD >actual &&
153 test_cmp expect actual
156 test_expect_success 'verify respects --end-of-options' '
157 git update-ref refs/heads/-tricky HEAD &&
158 git rev-parse --verify HEAD >expect &&
159 git rev-parse --verify --end-of-options -tricky >actual &&
160 test_cmp expect actual
163 test_done