gitignore.txt: use backticks instead of double quotes
[git/debian.git] / t / t1503-rev-parse-verify.sh
blobbc136833c1098d1c4e43d4b05cfadc7bef50b24c
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_PASSES_SANITIZE_LEAK=true
13 . ./test-lib.sh
15 add_line_into_file()
17 _line=$1
18 _file=$2
20 if [ -f "$_file" ]; then
21 echo "$_line" >> $_file || return $?
22 MSG="Add <$_line> into <$_file>."
23 else
24 echo "$_line" > $_file || return $?
25 git add $_file || return $?
26 MSG="Create file <$_file> with <$_line> inside."
29 test_tick
30 git commit --quiet -m "$MSG" $_file
33 HASH1=
34 HASH2=
35 HASH3=
36 HASH4=
38 test_expect_success 'set up basic repo with 1 file (hello) and 4 commits' '
39 add_line_into_file "1: Hello World" hello &&
40 HASH1=$(git rev-parse --verify HEAD) &&
41 add_line_into_file "2: A new day for git" hello &&
42 HASH2=$(git rev-parse --verify HEAD) &&
43 add_line_into_file "3: Another new day for git" hello &&
44 HASH3=$(git rev-parse --verify HEAD) &&
45 add_line_into_file "4: Ciao for now" hello &&
46 HASH4=$(git rev-parse --verify HEAD)
49 test_expect_success 'works with one good rev' '
50 rev_hash1=$(git rev-parse --verify $HASH1) &&
51 test "$rev_hash1" = "$HASH1" &&
52 rev_hash2=$(git rev-parse --verify $HASH2) &&
53 test "$rev_hash2" = "$HASH2" &&
54 rev_hash3=$(git rev-parse --verify $HASH3) &&
55 test "$rev_hash3" = "$HASH3" &&
56 rev_hash4=$(git rev-parse --verify $HASH4) &&
57 test "$rev_hash4" = "$HASH4" &&
58 rev_main=$(git rev-parse --verify main) &&
59 test "$rev_main" = "$HASH4" &&
60 rev_head=$(git rev-parse --verify HEAD) &&
61 test "$rev_head" = "$HASH4"
64 test_expect_success 'fails with any bad rev or many good revs' '
65 test_must_fail git rev-parse --verify 2>error &&
66 grep "single revision" error &&
67 test_must_fail git rev-parse --verify foo 2>error &&
68 grep "single revision" error &&
69 test_must_fail git rev-parse --verify HEAD bar 2>error &&
70 grep "single revision" error &&
71 test_must_fail git rev-parse --verify baz HEAD 2>error &&
72 grep "single revision" error &&
73 test_must_fail git rev-parse --verify $HASH2 HEAD 2>error &&
74 grep "single revision" error
77 test_expect_success 'fails silently when using -q' '
78 test_must_fail git rev-parse --verify --quiet 2>error &&
79 test_must_be_empty error &&
80 test_must_fail git rev-parse -q --verify foo 2>error &&
81 test_must_be_empty error &&
82 test_must_fail git rev-parse --verify -q HEAD bar 2>error &&
83 test_must_be_empty error &&
84 test_must_fail git rev-parse --quiet --verify baz HEAD 2>error &&
85 test_must_be_empty error &&
86 test_must_fail git rev-parse -q --verify $HASH2 HEAD 2>error &&
87 test_must_be_empty error
90 test_expect_success 'fails silently when using -q with deleted reflogs' '
91 ref=$(git rev-parse HEAD) &&
92 git update-ref --create-reflog -m "message for refs/test" refs/test "$ref" &&
93 git reflog delete --updateref --rewrite refs/test@{1} &&
94 test_must_fail git rev-parse -q --verify refs/test@{1} >error 2>&1 &&
95 test_must_be_empty error
98 test_expect_success 'fails silently when using -q with not enough reflogs' '
99 ref=$(git rev-parse HEAD) &&
100 git update-ref --create-reflog -m "message for refs/test2" refs/test2 "$ref" &&
101 test_must_fail git rev-parse -q --verify refs/test2@{999} >error 2>&1 &&
102 test_must_be_empty error
105 test_expect_success 'succeeds silently with -q and reflogs that do not go far back enough in time' '
106 ref=$(git rev-parse HEAD) &&
107 git update-ref --create-reflog -m "message for refs/test3" refs/test3 "$ref" &&
108 git rev-parse -q --verify refs/test3@{1.year.ago} >actual 2>error &&
109 test_must_be_empty error &&
110 echo "$ref" >expect &&
111 test_cmp expect actual
114 test_expect_success 'no stdout output on error' '
115 test -z "$(git rev-parse --verify)" &&
116 test -z "$(git rev-parse --verify foo)" &&
117 test -z "$(git rev-parse --verify baz HEAD)" &&
118 test -z "$(git rev-parse --verify HEAD bar)" &&
119 test -z "$(git rev-parse --verify $HASH2 HEAD)"
122 test_expect_success 'use --default' '
123 git rev-parse --verify --default main &&
124 git rev-parse --verify --default main HEAD &&
125 git rev-parse --default main --verify &&
126 git rev-parse --default main --verify HEAD &&
127 git rev-parse --verify HEAD --default main &&
128 test_must_fail git rev-parse --verify foo --default main &&
129 test_must_fail git rev-parse --default HEAD --verify bar &&
130 test_must_fail git rev-parse --verify --default HEAD baz &&
131 test_must_fail git rev-parse --default foo --verify &&
132 test_must_fail git rev-parse --verify --default bar
135 test_expect_success 'main@{n} for various n' '
136 git reflog >out &&
137 N=$(wc -l <out) &&
138 Nm1=$(($N-1)) &&
139 Np1=$(($N+1)) &&
140 git rev-parse --verify main@{0} &&
141 git rev-parse --verify main@{1} &&
142 git rev-parse --verify main@{$Nm1} &&
143 test_must_fail git rev-parse --verify main@{$N} &&
144 test_must_fail git rev-parse --verify main@{$Np1}
147 test_expect_success SYMLINKS,REFFILES 'ref resolution not confused by broken symlinks' '
148 ln -s does-not-exist .git/refs/heads/broken &&
149 test_must_fail git rev-parse --verify broken
152 test_expect_success 'options can appear after --verify' '
153 git rev-parse --verify HEAD >expect &&
154 git rev-parse --verify -q HEAD >actual &&
155 test_cmp expect actual
158 test_expect_success 'verify respects --end-of-options' '
159 git update-ref refs/heads/-tricky HEAD &&
160 git rev-parse --verify HEAD >expect &&
161 git rev-parse --verify --end-of-options -tricky >actual &&
162 test_cmp expect actual
165 test_done