]> git.ipfire.org Git - thirdparty/git.git/blob - t/t1401-symbolic-ref.sh
parse-options: simplify positivation handling
[thirdparty/git.git] / t / t1401-symbolic-ref.sh
1 #!/bin/sh
2
3 test_description='basic symbolic-ref tests'
4
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 # If the tests munging HEAD fail, they can break detection of
9 # the git repo, meaning that further tests will operate on
10 # the surrounding git repo instead of the trash directory.
11 reset_to_sane() {
12 rm -rf .git &&
13 "$TAR" xf .git.tar
14 }
15
16 test_expect_success 'setup' '
17 git symbolic-ref HEAD refs/heads/foo &&
18 test_commit file &&
19 "$TAR" cf .git.tar .git/
20 '
21
22 test_expect_success 'symbolic-ref read/write roundtrip' '
23 git symbolic-ref HEAD refs/heads/read-write-roundtrip &&
24 echo refs/heads/read-write-roundtrip >expect &&
25 git symbolic-ref HEAD >actual &&
26 test_cmp expect actual
27 '
28
29 test_expect_success 'symbolic-ref refuses non-ref for HEAD' '
30 test_must_fail git symbolic-ref HEAD foo
31 '
32
33 reset_to_sane
34
35 test_expect_success 'symbolic-ref refuses bare sha1' '
36 rev=$(git rev-parse HEAD) &&
37 test_must_fail git symbolic-ref HEAD "$rev"
38 '
39
40 reset_to_sane
41
42 test_expect_success 'HEAD cannot be removed' '
43 test_must_fail git symbolic-ref -d HEAD
44 '
45
46 reset_to_sane
47
48 test_expect_success 'symbolic-ref can be deleted' '
49 git symbolic-ref NOTHEAD refs/heads/foo &&
50 git symbolic-ref -d NOTHEAD &&
51 git rev-parse refs/heads/foo &&
52 test_must_fail git symbolic-ref NOTHEAD
53 '
54 reset_to_sane
55
56 test_expect_success 'symbolic-ref can delete dangling symref' '
57 git symbolic-ref NOTHEAD refs/heads/missing &&
58 git symbolic-ref -d NOTHEAD &&
59 test_must_fail git rev-parse refs/heads/missing &&
60 test_must_fail git symbolic-ref NOTHEAD
61 '
62 reset_to_sane
63
64 test_expect_success 'symbolic-ref fails to delete missing FOO' '
65 echo "fatal: Cannot delete FOO, not a symbolic ref" >expect &&
66 test_must_fail git symbolic-ref -d FOO >actual 2>&1 &&
67 test_cmp expect actual
68 '
69 reset_to_sane
70
71 test_expect_success 'symbolic-ref fails to delete real ref' '
72 echo "fatal: Cannot delete refs/heads/foo, not a symbolic ref" >expect &&
73 test_must_fail git symbolic-ref -d refs/heads/foo >actual 2>&1 &&
74 git rev-parse --verify refs/heads/foo &&
75 test_cmp expect actual
76 '
77 reset_to_sane
78
79 test_expect_success 'create large ref name' '
80 # make 256+ character ref; some systems may not handle that,
81 # so be gentle
82 long=0123456789abcdef &&
83 long=$long/$long/$long/$long &&
84 long=$long/$long/$long/$long &&
85 long_ref=refs/heads/$long &&
86 tree=$(git write-tree) &&
87 commit=$(echo foo | git commit-tree $tree) &&
88 if git update-ref $long_ref $commit; then
89 test_set_prereq LONG_REF
90 else
91 echo >&2 "long refs not supported"
92 fi
93 '
94
95 test_expect_success LONG_REF 'symbolic-ref can point to large ref name' '
96 git symbolic-ref HEAD $long_ref &&
97 echo $long_ref >expect &&
98 git symbolic-ref HEAD >actual &&
99 test_cmp expect actual
100 '
101
102 test_expect_success LONG_REF 'we can parse long symbolic ref' '
103 echo $commit >expect &&
104 git rev-parse --verify HEAD >actual &&
105 test_cmp expect actual
106 '
107
108 test_expect_success 'symbolic-ref reports failure in exit code' '
109 test_when_finished "rm -f .git/HEAD.lock" &&
110 >.git/HEAD.lock &&
111 test_must_fail git symbolic-ref HEAD refs/heads/whatever
112 '
113
114 test_expect_success 'symbolic-ref writes reflog entry' '
115 git checkout -b log1 &&
116 test_commit one &&
117 git checkout -b log2 &&
118 test_commit two &&
119 git checkout --orphan orphan &&
120 git symbolic-ref -m create HEAD refs/heads/log1 &&
121 git symbolic-ref -m update HEAD refs/heads/log2 &&
122 cat >expect <<-\EOF &&
123 update
124 create
125 EOF
126 git log --format=%gs -g -2 >actual &&
127 test_cmp expect actual
128 '
129
130 test_expect_success 'symbolic-ref does not create ref d/f conflicts' '
131 git checkout -b df &&
132 test_commit df &&
133 test_must_fail git symbolic-ref refs/heads/df/conflict refs/heads/df &&
134 git pack-refs --all --prune &&
135 test_must_fail git symbolic-ref refs/heads/df/conflict refs/heads/df
136 '
137
138 test_expect_success 'symbolic-ref can overwrite pointer to invalid name' '
139 test_when_finished reset_to_sane &&
140 head=$(git rev-parse HEAD) &&
141 git symbolic-ref HEAD refs/heads/outer &&
142 test_when_finished "git update-ref -d refs/heads/outer/inner" &&
143 git update-ref refs/heads/outer/inner $head &&
144 git symbolic-ref HEAD refs/heads/unrelated
145 '
146
147 test_expect_success 'symbolic-ref can resolve d/f name (EISDIR)' '
148 test_when_finished reset_to_sane &&
149 head=$(git rev-parse HEAD) &&
150 git symbolic-ref HEAD refs/heads/outer/inner &&
151 test_when_finished "git update-ref -d refs/heads/outer" &&
152 git update-ref refs/heads/outer $head &&
153 echo refs/heads/outer/inner >expect &&
154 git symbolic-ref HEAD >actual &&
155 test_cmp expect actual
156 '
157
158 test_expect_success 'symbolic-ref can resolve d/f name (ENOTDIR)' '
159 test_when_finished reset_to_sane &&
160 head=$(git rev-parse HEAD) &&
161 git symbolic-ref HEAD refs/heads/outer &&
162 test_when_finished "git update-ref -d refs/heads/outer/inner" &&
163 git update-ref refs/heads/outer/inner $head &&
164 echo refs/heads/outer >expect &&
165 git symbolic-ref HEAD >actual &&
166 test_cmp expect actual
167 '
168
169 test_expect_success 'symbolic-ref refuses invalid target for non-HEAD' '
170 test_must_fail git symbolic-ref refs/heads/invalid foo..bar
171 '
172
173 test_expect_success 'symbolic-ref allows top-level target for non-HEAD' '
174 git symbolic-ref refs/heads/top-level FETCH_HEAD &&
175 git update-ref FETCH_HEAD HEAD &&
176 test_cmp_rev top-level HEAD
177 '
178
179 test_expect_success 'symbolic-ref pointing at another' '
180 git update-ref refs/heads/maint-2.37 HEAD &&
181 git symbolic-ref refs/heads/maint refs/heads/maint-2.37 &&
182 git checkout maint &&
183
184 git symbolic-ref HEAD >actual &&
185 echo refs/heads/maint-2.37 >expect &&
186 test_cmp expect actual &&
187
188 git symbolic-ref --no-recurse HEAD >actual &&
189 echo refs/heads/maint >expect &&
190 test_cmp expect actual
191 '
192
193 test_expect_success 'symbolic-ref --short handles complex utf8 case' '
194 name="测试-加-增加-加-增加" &&
195 git symbolic-ref TEST_SYMREF "refs/heads/$name" &&
196 # In the real world, we saw problems with this case only
197 # when the locale includes UTF-8. Set it here to try to make things as
198 # hard as possible for us to pass, but in practice we should do the
199 # right thing regardless (and of course some platforms may not even
200 # have this locale).
201 LC_ALL=en_US.UTF-8 git symbolic-ref --short TEST_SYMREF >actual &&
202 echo "$name" >expect &&
203 test_cmp expect actual
204 '
205
206 test_expect_success 'symbolic-ref --short handles name with suffix' '
207 git symbolic-ref TEST_SYMREF "refs/remotes/origin/HEAD" &&
208 git symbolic-ref --short TEST_SYMREF >actual &&
209 echo "origin" >expect &&
210 test_cmp expect actual
211 '
212
213 test_expect_success 'symbolic-ref --short handles almost-matching name' '
214 git symbolic-ref TEST_SYMREF "refs/headsXfoo" &&
215 git symbolic-ref --short TEST_SYMREF >actual &&
216 echo "headsXfoo" >expect &&
217 test_cmp expect actual
218 '
219
220 test_expect_success 'symbolic-ref --short handles name with percent' '
221 git symbolic-ref TEST_SYMREF "refs/heads/%foo" &&
222 git symbolic-ref --short TEST_SYMREF >actual &&
223 echo "%foo" >expect &&
224 test_cmp expect actual
225 '
226
227 test_done