Blob


1 #!/bin/sh
2 #
3 # Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
4 #
5 # Permission to use, copy, modify, and distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8 #
9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 . ./common.sh
19 function test_branch_create {
20 local testroot=`test_init branch_create`
22 # Create a branch based on repository's HEAD reference
23 got branch -r $testroot/repo newbranch
24 ret="$?"
25 if [ "$ret" != "0" ]; then
26 echo "got branch command failed unexpectedly"
27 test_done "$testroot" "$ret"
28 return 1
29 fi
31 # Ensure that Git recognizes the branch Got has created
32 (cd $testroot/repo && git checkout -q newbranch)
33 ret="$?"
34 if [ "$ret" != "0" ]; then
35 echo "git checkout command failed unexpectedly"
36 test_done "$testroot" "$ret"
37 return 1
38 fi
39 echo "modified delta on branch" > $testroot/repo/gamma/delta
40 git_commit $testroot/repo -m "committing to delta on newbranch"
42 got checkout -b newbranch $testroot/repo $testroot/wt >/dev/null
43 ret="$?"
44 if [ "$ret" != "0" ]; then
45 echo "got checkout command failed unexpectedly"
46 test_done "$testroot" "$ret"
47 return 1
48 fi
50 echo "modified delta on branch" > $testroot/content.expected
51 cat $testroot/wt/gamma/delta > $testroot/content
52 cmp -s $testroot/content.expected $testroot/content
53 ret="$?"
54 if [ "$ret" != "0" ]; then
55 diff -u $testroot/content.expected $testroot/content
56 test_done "$testroot" "$ret"
57 return 1
58 fi
60 # Create a branch based on the work tree's branch
61 (cd $testroot/wt && got branch anotherbranch)
62 ret="$?"
63 if [ "$ret" != "0" ]; then
64 test_done "$testroot" "$ret"
65 return 1
66 fi
68 (cd $testroot/repo && git checkout -q anotherbranch)
69 ret="$?"
70 if [ "$ret" != "0" ]; then
71 echo "git checkout command failed unexpectedly"
72 test_done "$testroot" "$ret"
73 return 1
74 fi
76 # Create a branch based on another specific branch
77 (cd $testroot/wt && got branch -c master yetanotherbranch)
78 ret="$?"
79 if [ "$ret" != "0" ]; then
80 test_done "$testroot" "$ret"
81 return 1
82 fi
84 (cd $testroot/repo && git checkout -q yetanotherbranch)
85 ret="$?"
86 if [ "$ret" != "0" ]; then
87 echo "git checkout command failed unexpectedly"
88 test_done "$testroot" "$ret"
89 return 1
90 fi
92 # Create a branch based on a specific commit
93 local commit_id=`git_show_head $testroot/repo`
94 got branch -r $testroot/repo -c $commit_id commitbranch
95 ret="$?"
96 if [ "$ret" != "0" ]; then
97 echo "got branch command failed unexpectedly"
98 test_done "$testroot" "$ret"
99 return 1
100 fi
102 (cd $testroot/repo && git checkout -q commitbranch)
103 ret="$?"
104 if [ "$ret" != "0" ]; then
105 echo "git checkout command failed unexpectedly"
106 fi
107 test_done "$testroot" "$ret"
110 function test_branch_list {
111 local testroot=`test_init branch_list`
112 local commit_id=`git_show_head $testroot/repo`
114 for b in branch1 branch2 branch3; do
115 got branch -r $testroot/repo $b
116 ret="$?"
117 if [ "$ret" != "0" ]; then
118 echo "got branch command failed unexpectedly"
119 test_done "$testroot" "$ret"
120 return 1
121 fi
122 done
124 got branch -l -r $testroot/repo > $testroot/stdout
125 echo " branch1: $commit_id" > $testroot/stdout.expected
126 echo " branch2: $commit_id" >> $testroot/stdout.expected
127 echo " branch3: $commit_id" >> $testroot/stdout.expected
128 echo " master: $commit_id" >> $testroot/stdout.expected
129 cmp -s $testroot/stdout $testroot/stdout.expected
130 ret="$?"
131 if [ "$ret" != "0" ]; then
132 diff -u $testroot/stdout.expected $testroot/stdout
133 test_done "$testroot" "$ret"
134 return 1
135 fi
137 got checkout $testroot/repo $testroot/wt >/dev/null
138 ret="$?"
139 if [ "$ret" != "0" ]; then
140 echo "got checkout command failed unexpectedly"
141 test_done "$testroot" "$ret"
142 return 1
143 fi
145 (cd $testroot/wt && got branch -l > $testroot/stdout)
146 echo " branch1: $commit_id" > $testroot/stdout.expected
147 echo " branch2: $commit_id" >> $testroot/stdout.expected
148 echo " branch3: $commit_id" >> $testroot/stdout.expected
149 echo "* master: $commit_id" >> $testroot/stdout.expected
150 cmp -s $testroot/stdout $testroot/stdout.expected
151 ret="$?"
152 if [ "$ret" != "0" ]; then
153 diff -u $testroot/stdout.expected $testroot/stdout
154 test_done "$testroot" "$ret"
155 return 1
156 fi
158 echo "modified delta" > $testroot/repo/gamma/delta
159 git_commit $testroot/repo -m "committing to delta"
160 local commit_id2=`git_show_head $testroot/repo`
162 (cd $testroot/wt && got branch -l > $testroot/stdout)
163 echo " branch1: $commit_id" > $testroot/stdout.expected
164 echo " branch2: $commit_id" >> $testroot/stdout.expected
165 echo " branch3: $commit_id" >> $testroot/stdout.expected
166 echo "~ master: $commit_id2" >> $testroot/stdout.expected
167 cmp -s $testroot/stdout $testroot/stdout.expected
168 ret="$?"
169 if [ "$ret" != "0" ]; then
170 diff -u $testroot/stdout.expected $testroot/stdout
171 test_done "$testroot" "$ret"
172 return 1
173 fi
175 (cd $testroot/wt && got update > /dev/null)
176 ret="$?"
177 if [ "$ret" != "0" ]; then
178 echo "got update command failed unexpectedly"
179 test_done "$testroot" "$ret"
180 return 1
181 fi
183 (cd $testroot/wt && got branch -l > $testroot/stdout)
184 echo " branch1: $commit_id" > $testroot/stdout.expected
185 echo " branch2: $commit_id" >> $testroot/stdout.expected
186 echo " branch3: $commit_id" >> $testroot/stdout.expected
187 echo "* master: $commit_id2" >> $testroot/stdout.expected
188 cmp -s $testroot/stdout $testroot/stdout.expected
189 ret="$?"
190 if [ "$ret" != "0" ]; then
191 diff -u $testroot/stdout.expected $testroot/stdout
192 test_done "$testroot" "$ret"
193 return 1
194 fi
196 (cd $testroot/wt && got update -b branch1 > /dev/null)
197 ret="$?"
198 if [ "$ret" != "0" ]; then
199 echo "got update command failed unexpectedly"
200 test_done "$testroot" "$ret"
201 return 1
202 fi
204 (cd $testroot/wt && got branch -l > $testroot/stdout)
205 echo "* branch1: $commit_id" > $testroot/stdout.expected
206 echo " branch2: $commit_id" >> $testroot/stdout.expected
207 echo " branch3: $commit_id" >> $testroot/stdout.expected
208 echo " master: $commit_id2" >> $testroot/stdout.expected
209 cmp -s $testroot/stdout $testroot/stdout.expected
210 ret="$?"
211 if [ "$ret" != "0" ]; then
212 diff -u $testroot/stdout.expected $testroot/stdout
213 fi
214 test_done "$testroot" "$ret"
217 function test_branch_delete {
218 local testroot=`test_init branch_delete`
219 local commit_id=`git_show_head $testroot/repo`
221 for b in branch1 branch2 branch3; do
222 got branch -r $testroot/repo $b
223 ret="$?"
224 if [ "$ret" != "0" ]; then
225 echo "got branch command failed unexpectedly"
226 test_done "$testroot" "$ret"
227 return 1
228 fi
229 done
231 got branch -d branch2 -r $testroot/repo > $testroot/stdout
232 ret="$?"
233 if [ "$ret" != "0" ]; then
234 echo "got update command failed unexpectedly"
235 test_done "$testroot" "$ret"
236 return 1
237 fi
239 got branch -l -r $testroot/repo > $testroot/stdout
240 echo " branch1: $commit_id" > $testroot/stdout.expected
241 echo " branch3: $commit_id" >> $testroot/stdout.expected
242 echo " master: $commit_id" >> $testroot/stdout.expected
243 cmp -s $testroot/stdout $testroot/stdout.expected
244 ret="$?"
245 if [ "$ret" != "0" ]; then
246 diff -u $testroot/stdout.expected $testroot/stdout
247 test_done "$testroot" "$ret"
248 return 1
249 fi
251 got ref -l -r $testroot/repo > $testroot/stdout
252 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
253 echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
254 echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
255 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
256 cmp -s $testroot/stdout $testroot/stdout.expected
257 ret="$?"
258 if [ "$ret" != "0" ]; then
259 diff -u $testroot/stdout.expected $testroot/stdout
260 test_done "$testroot" "$ret"
261 return 1
262 fi
264 got branch -d bogus_branch_name -r $testroot/repo \
265 > $testroot/stdout 2> $testroot/stderr
266 ret="$?"
267 if [ "$ret" == "0" ]; then
268 echo "got update succeeded unexpectedly"
269 test_done "$testroot" "$ret"
270 return 1
271 fi
273 echo "got: reference refs/heads/bogus_branch_name not found" \
274 > $testroot/stderr.expected
275 cmp -s $testroot/stderr $testroot/stderr.expected
276 ret="$?"
277 if [ "$ret" != "0" ]; then
278 diff -u $testroot/stderr.expected $testroot/stderr
279 fi
280 test_done "$testroot" "$ret"
283 function test_branch_delete_current_branch {
284 local testroot=`test_init branch_delete_current_branch`
285 local commit_id=`git_show_head $testroot/repo`
287 got checkout $testroot/repo $testroot/wt >/dev/null
288 ret="$?"
289 if [ "$ret" != "0" ]; then
290 echo "got checkout command failed unexpectedly"
291 test_done "$testroot" "$ret"
292 return 1
293 fi
295 (cd $testroot/wt && got branch -d master > $testroot/stdout \
296 2> $testroot/stderr)
298 echo "got: will not delete this work tree's current branch" \
299 > $testroot/stderr.expected
300 cmp -s $testroot/stderr $testroot/stderr.expected
301 ret="$?"
302 if [ "$ret" != "0" ]; then
303 diff -u $testroot/stderr.expected $testroot/stderr
304 fi
305 test_done "$testroot" "$ret"
308 function test_branch_delete_packed {
309 local testroot=`test_init branch_delete_packed`
310 local commit_id=`git_show_head $testroot/repo`
312 for b in branch1 branch2 branch3; do
313 got branch -r $testroot/repo $b
314 ret="$?"
315 if [ "$ret" != "0" ]; then
316 echo "got branch command failed unexpectedly"
317 test_done "$testroot" "$ret"
318 return 1
319 fi
320 done
322 (cd $testroot/repo && git pack-refs --all)
324 got branch -d branch2 -r $testroot/repo > $testroot/stdout
325 ret="$?"
326 if [ "$ret" != "0" ]; then
327 echo "got update command failed unexpectedly"
328 test_done "$testroot" "$ret"
329 return 1
330 fi
332 got branch -l -r $testroot/repo > $testroot/stdout
333 echo " branch1: $commit_id" > $testroot/stdout.expected
334 echo " branch3: $commit_id" >> $testroot/stdout.expected
335 echo " master: $commit_id" >> $testroot/stdout.expected
336 cmp -s $testroot/stdout $testroot/stdout.expected
337 ret="$?"
338 if [ "$ret" != "0" ]; then
339 diff -u $testroot/stdout.expected $testroot/stdout
340 test_done "$testroot" "$ret"
341 return 1
342 fi
344 got ref -l -r $testroot/repo > $testroot/stdout
345 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
346 echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
347 echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
348 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
349 cmp -s $testroot/stdout $testroot/stdout.expected
350 ret="$?"
351 if [ "$ret" != "0" ]; then
352 diff -u $testroot/stdout.expected $testroot/stdout
353 test_done "$testroot" "$ret"
354 return 1
355 fi
357 got branch -d bogus_branch_name -r $testroot/repo \
358 > $testroot/stdout 2> $testroot/stderr
359 ret="$?"
360 if [ "$ret" == "0" ]; then
361 echo "got update succeeded unexpectedly"
362 test_done "$testroot" "$ret"
363 return 1
364 fi
366 echo "got: reference refs/heads/bogus_branch_name not found" \
367 > $testroot/stderr.expected
368 cmp -s $testroot/stderr $testroot/stderr.expected
369 ret="$?"
370 if [ "$ret" != "0" ]; then
371 diff -u $testroot/stderr.expected $testroot/stderr
372 fi
373 test_done "$testroot" "$ret"
376 function test_branch_show {
377 local testroot=`test_init branch_show`
378 local commit_id=`git_show_head $testroot/repo`
380 for b in branch1 branch2 branch3; do
381 got branch -r $testroot/repo $b
382 ret="$?"
383 if [ "$ret" != "0" ]; then
384 echo "got branch command failed unexpectedly"
385 test_done "$testroot" "$ret"
386 return 1
387 fi
388 done
390 got checkout $testroot/repo $testroot/wt >/dev/null
391 ret="$?"
392 if [ "$ret" != "0" ]; then
393 echo "got checkout command failed unexpectedly"
394 test_done "$testroot" "$ret"
395 return 1
396 fi
398 (cd $testroot/wt && got branch > $testroot/stdout)
399 echo "master" > $testroot/stdout.expected
400 cmp -s $testroot/stdout $testroot/stdout.expected
401 ret="$?"
402 if [ "$ret" != "0" ]; then
403 diff -u $testroot/stdout.expected $testroot/stdout
404 test_done "$testroot" "$ret"
405 return 1
406 fi
408 (cd $testroot/wt && got update -b branch1 > /dev/null)
409 ret="$?"
410 if [ "$ret" != "0" ]; then
411 echo "got update command failed unexpectedly"
412 test_done "$testroot" "$ret"
413 return 1
414 fi
416 (cd $testroot/wt && got branch > $testroot/stdout)
417 echo "branch1" > $testroot/stdout.expected
418 cmp -s $testroot/stdout $testroot/stdout.expected
419 ret="$?"
420 if [ "$ret" != "0" ]; then
421 diff -u $testroot/stdout.expected $testroot/stdout
422 fi
423 test_done "$testroot" "$ret"
427 run_test test_branch_create
428 run_test test_branch_list
429 run_test test_branch_delete
430 run_test test_branch_delete_current_branch
431 run_test test_branch_delete_packed
432 run_test test_branch_show