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 test_branch_create() {
20 local testroot=`test_init branch_create`
21 local commit_id0=`git_show_head $testroot/repo`
23 # Create a branch based on repository's HEAD reference
24 got branch -r $testroot/repo newbranch
25 ret="$?"
26 if [ "$ret" != "0" ]; then
27 echo "got branch command failed unexpectedly"
28 test_done "$testroot" "$ret"
29 return 1
30 fi
32 # Ensure that Git recognizes the branch Got has created
33 (cd $testroot/repo && git checkout -q newbranch)
34 ret="$?"
35 if [ "$ret" != "0" ]; then
36 echo "git checkout command failed unexpectedly"
37 test_done "$testroot" "$ret"
38 return 1
39 fi
40 echo "modified delta on branch" > $testroot/repo/gamma/delta
41 git_commit $testroot/repo -m "committing to delta on newbranch"
43 got checkout -b newbranch $testroot/repo $testroot/wt >/dev/null
44 ret="$?"
45 if [ "$ret" != "0" ]; then
46 echo "got checkout command failed unexpectedly"
47 test_done "$testroot" "$ret"
48 return 1
49 fi
51 echo "modified delta on branch" > $testroot/content.expected
52 cat $testroot/wt/gamma/delta > $testroot/content
53 cmp -s $testroot/content.expected $testroot/content
54 ret="$?"
55 if [ "$ret" != "0" ]; then
56 diff -u $testroot/content.expected $testroot/content
57 test_done "$testroot" "$ret"
58 return 1
59 fi
61 # Create a branch based on the work tree's branch
62 (cd $testroot/wt && got branch -n refs/heads/anotherbranch)
63 ret="$?"
64 if [ "$ret" != "0" ]; then
65 test_done "$testroot" "$ret"
66 return 1
67 fi
69 (cd $testroot/repo && git checkout -q anotherbranch)
70 ret="$?"
71 if [ "$ret" != "0" ]; then
72 echo "git checkout command failed unexpectedly"
73 test_done "$testroot" "$ret"
74 return 1
75 fi
77 # Create a branch based on another specific branch
78 (cd $testroot/wt && got branch -n -c master yetanotherbranch)
79 ret="$?"
80 if [ "$ret" != "0" ]; then
81 test_done "$testroot" "$ret"
82 return 1
83 fi
85 (cd $testroot/repo && git checkout -q yetanotherbranch)
86 ret="$?"
87 if [ "$ret" != "0" ]; then
88 echo "git checkout command failed unexpectedly"
89 test_done "$testroot" "$ret"
90 return 1
91 fi
93 # Create a branch based on a specific commit
94 local commit_id=`git_show_head $testroot/repo`
95 got branch -r $testroot/repo -c $commit_id commitbranch
96 ret="$?"
97 if [ "$ret" != "0" ]; then
98 echo "got branch command failed unexpectedly"
99 test_done "$testroot" "$ret"
100 return 1
101 fi
103 (cd $testroot/repo && git checkout -q commitbranch)
104 ret="$?"
105 if [ "$ret" != "0" ]; then
106 echo "git checkout command failed unexpectedly"
107 test_done "$testroot" "$ret"
108 return 1
109 fi
111 # Create a branch and let the work tree be updated to it
112 (cd $testroot/wt && got branch -c $commit_id0 updatebranch \
113 > $testroot/stdout)
115 echo -n "Switching work tree from refs/heads/newbranch to " \
116 > $testroot/stdout.expected
117 echo "refs/heads/updatebranch" >> $testroot/stdout.expected
118 echo "U gamma/delta" >> $testroot/stdout.expected
119 echo "Updated to commit $commit_id0" >> $testroot/stdout.expected
121 cmp -s $testroot/stdout.expected $testroot/stdout
122 ret="$?"
123 if [ "$ret" != "0" ]; then
124 diff -u $testroot/stdout.expected $testroot/stdout
125 fi
126 test_done "$testroot" "$ret"
129 test_branch_list() {
130 local testroot=`test_init branch_list`
131 local commit_id=`git_show_head $testroot/repo`
133 for b in branch1 branch2 branch3; do
134 got branch -r $testroot/repo $b
135 ret="$?"
136 if [ "$ret" != "0" ]; then
137 echo "got branch command failed unexpectedly"
138 test_done "$testroot" "$ret"
139 return 1
140 fi
141 done
143 got branch -l -r $testroot/repo > $testroot/stdout
144 echo " branch1: $commit_id" > $testroot/stdout.expected
145 echo " branch2: $commit_id" >> $testroot/stdout.expected
146 echo " branch3: $commit_id" >> $testroot/stdout.expected
147 echo " master: $commit_id" >> $testroot/stdout.expected
148 cmp -s $testroot/stdout $testroot/stdout.expected
149 ret="$?"
150 if [ "$ret" != "0" ]; then
151 diff -u $testroot/stdout.expected $testroot/stdout
152 test_done "$testroot" "$ret"
153 return 1
154 fi
156 got checkout $testroot/repo $testroot/wt >/dev/null
157 ret="$?"
158 if [ "$ret" != "0" ]; then
159 echo "got checkout command failed unexpectedly"
160 test_done "$testroot" "$ret"
161 return 1
162 fi
164 (cd $testroot/wt && got branch -l > $testroot/stdout)
165 echo " branch1: $commit_id" > $testroot/stdout.expected
166 echo " branch2: $commit_id" >> $testroot/stdout.expected
167 echo " branch3: $commit_id" >> $testroot/stdout.expected
168 echo "* master: $commit_id" >> $testroot/stdout.expected
169 cmp -s $testroot/stdout $testroot/stdout.expected
170 ret="$?"
171 if [ "$ret" != "0" ]; then
172 diff -u $testroot/stdout.expected $testroot/stdout
173 test_done "$testroot" "$ret"
174 return 1
175 fi
177 echo "modified delta" > $testroot/repo/gamma/delta
178 git_commit $testroot/repo -m "committing to delta"
179 local commit_id2=`git_show_head $testroot/repo`
181 (cd $testroot/wt && got branch -l > $testroot/stdout)
182 echo " branch1: $commit_id" > $testroot/stdout.expected
183 echo " branch2: $commit_id" >> $testroot/stdout.expected
184 echo " branch3: $commit_id" >> $testroot/stdout.expected
185 echo "~ master: $commit_id2" >> $testroot/stdout.expected
186 cmp -s $testroot/stdout $testroot/stdout.expected
187 ret="$?"
188 if [ "$ret" != "0" ]; then
189 diff -u $testroot/stdout.expected $testroot/stdout
190 test_done "$testroot" "$ret"
191 return 1
192 fi
194 (cd $testroot/wt && got update > /dev/null)
195 ret="$?"
196 if [ "$ret" != "0" ]; then
197 echo "got update command failed unexpectedly"
198 test_done "$testroot" "$ret"
199 return 1
200 fi
202 (cd $testroot/wt && got branch -l > $testroot/stdout)
203 echo " branch1: $commit_id" > $testroot/stdout.expected
204 echo " branch2: $commit_id" >> $testroot/stdout.expected
205 echo " branch3: $commit_id" >> $testroot/stdout.expected
206 echo "* master: $commit_id2" >> $testroot/stdout.expected
207 cmp -s $testroot/stdout $testroot/stdout.expected
208 ret="$?"
209 if [ "$ret" != "0" ]; then
210 diff -u $testroot/stdout.expected $testroot/stdout
211 test_done "$testroot" "$ret"
212 return 1
213 fi
215 (cd $testroot/wt && got update -b branch1 > /dev/null)
216 ret="$?"
217 if [ "$ret" != "0" ]; then
218 echo "got update command failed unexpectedly"
219 test_done "$testroot" "$ret"
220 return 1
221 fi
223 (cd $testroot/wt && got branch -l > $testroot/stdout)
224 echo "* branch1: $commit_id" > $testroot/stdout.expected
225 echo " branch2: $commit_id" >> $testroot/stdout.expected
226 echo " branch3: $commit_id" >> $testroot/stdout.expected
227 echo " master: $commit_id2" >> $testroot/stdout.expected
228 cmp -s $testroot/stdout $testroot/stdout.expected
229 ret="$?"
230 if [ "$ret" != "0" ]; then
231 diff -u $testroot/stdout.expected $testroot/stdout
232 fi
233 test_done "$testroot" "$ret"
236 test_branch_delete() {
237 local testroot=`test_init branch_delete`
238 local commit_id=`git_show_head $testroot/repo`
240 for b in branch1 branch2 branch3; do
241 got branch -r $testroot/repo $b
242 ret="$?"
243 if [ "$ret" != "0" ]; then
244 echo "got branch command failed unexpectedly"
245 test_done "$testroot" "$ret"
246 return 1
247 fi
248 done
250 got branch -d branch2 -r $testroot/repo > $testroot/stdout
251 ret="$?"
252 if [ "$ret" != "0" ]; then
253 echo "got branch command failed unexpectedly"
254 test_done "$testroot" "$ret"
255 return 1
256 fi
258 got branch -l -r $testroot/repo > $testroot/stdout
259 echo " branch1: $commit_id" > $testroot/stdout.expected
260 echo " branch3: $commit_id" >> $testroot/stdout.expected
261 echo " master: $commit_id" >> $testroot/stdout.expected
262 cmp -s $testroot/stdout $testroot/stdout.expected
263 ret="$?"
264 if [ "$ret" != "0" ]; then
265 diff -u $testroot/stdout.expected $testroot/stdout
266 test_done "$testroot" "$ret"
267 return 1
268 fi
270 got ref -l -r $testroot/repo > $testroot/stdout
271 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
272 echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
273 echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
274 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
275 cmp -s $testroot/stdout $testroot/stdout.expected
276 ret="$?"
277 if [ "$ret" != "0" ]; then
278 diff -u $testroot/stdout.expected $testroot/stdout
279 test_done "$testroot" "$ret"
280 return 1
281 fi
283 got branch -d bogus_branch_name -r $testroot/repo \
284 > $testroot/stdout 2> $testroot/stderr
285 ret="$?"
286 if [ "$ret" = "0" ]; then
287 echo "got branch succeeded unexpectedly"
288 test_done "$testroot" "$ret"
289 return 1
290 fi
292 echo "got: reference refs/heads/bogus_branch_name not found" \
293 > $testroot/stderr.expected
294 cmp -s $testroot/stderr $testroot/stderr.expected
295 ret="$?"
296 if [ "$ret" != "0" ]; then
297 diff -u $testroot/stderr.expected $testroot/stderr
298 test_done "$testroot" "$ret"
299 return 1
300 fi
302 got ref -r $testroot/repo -c master refs/remotes/origin/master
303 got ref -r $testroot/repo -c branch1 refs/remotes/origin/branch1
304 got ref -r $testroot/repo -c branch3 refs/remotes/origin/branch3
306 got ref -l -r $testroot/repo > $testroot/stdout
307 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
308 echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
309 echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
310 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
311 echo "refs/remotes/origin/branch1: $commit_id" \
312 >> $testroot/stdout.expected
313 echo "refs/remotes/origin/branch3: $commit_id" \
314 >> $testroot/stdout.expected
315 echo "refs/remotes/origin/master: $commit_id" \
316 >> $testroot/stdout.expected
317 cmp -s $testroot/stdout $testroot/stdout.expected
318 ret="$?"
319 if [ "$ret" != "0" ]; then
320 diff -u $testroot/stdout.expected $testroot/stdout
321 test_done "$testroot" "$ret"
322 return 1
323 fi
325 got branch -d origin/branch1 -r $testroot/repo > $testroot/stdout
326 ret="$?"
327 if [ "$ret" != "0" ]; then
328 echo "got branch command failed unexpectedly"
329 test_done "$testroot" "$ret"
330 return 1
331 fi
333 got branch -d refs/remotes/origin/branch3 -r $testroot/repo \
334 > $testroot/stdout
335 ret="$?"
336 if [ "$ret" != "0" ]; then
337 echo "got branch command failed unexpectedly"
338 test_done "$testroot" "$ret"
339 return 1
340 fi
342 got ref -l -r $testroot/repo > $testroot/stdout
343 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
344 echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
345 echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
346 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
347 echo "refs/remotes/origin/master: $commit_id" \
348 >> $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 fi
354 test_done "$testroot" "$ret"
357 test_branch_delete_current_branch() {
358 local testroot=`test_init branch_delete_current_branch`
359 local commit_id=`git_show_head $testroot/repo`
361 got checkout $testroot/repo $testroot/wt >/dev/null
362 ret="$?"
363 if [ "$ret" != "0" ]; then
364 echo "got checkout command failed unexpectedly"
365 test_done "$testroot" "$ret"
366 return 1
367 fi
369 (cd $testroot/wt && got branch -d master > $testroot/stdout \
370 2> $testroot/stderr)
372 echo "got: will not delete this work tree's current branch" \
373 > $testroot/stderr.expected
374 cmp -s $testroot/stderr $testroot/stderr.expected
375 ret="$?"
376 if [ "$ret" != "0" ]; then
377 diff -u $testroot/stderr.expected $testroot/stderr
378 fi
379 test_done "$testroot" "$ret"
382 test_branch_delete_packed() {
383 local testroot=`test_init branch_delete_packed`
384 local commit_id=`git_show_head $testroot/repo`
386 for b in branch1 branch2 branch3; do
387 got branch -r $testroot/repo $b
388 ret="$?"
389 if [ "$ret" != "0" ]; then
390 echo "got branch command failed unexpectedly"
391 test_done "$testroot" "$ret"
392 return 1
393 fi
394 done
396 (cd $testroot/repo && git pack-refs --all)
398 got branch -d refs/heads/branch2 -r $testroot/repo > $testroot/stdout
399 ret="$?"
400 if [ "$ret" != "0" ]; then
401 echo "got update command failed unexpectedly"
402 test_done "$testroot" "$ret"
403 return 1
404 fi
406 got branch -l -r $testroot/repo > $testroot/stdout
407 echo " branch1: $commit_id" > $testroot/stdout.expected
408 echo " branch3: $commit_id" >> $testroot/stdout.expected
409 echo " master: $commit_id" >> $testroot/stdout.expected
410 cmp -s $testroot/stdout $testroot/stdout.expected
411 ret="$?"
412 if [ "$ret" != "0" ]; then
413 diff -u $testroot/stdout.expected $testroot/stdout
414 test_done "$testroot" "$ret"
415 return 1
416 fi
418 got ref -l -r $testroot/repo > $testroot/stdout
419 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
420 echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
421 echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
422 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
423 cmp -s $testroot/stdout $testroot/stdout.expected
424 ret="$?"
425 if [ "$ret" != "0" ]; then
426 diff -u $testroot/stdout.expected $testroot/stdout
427 test_done "$testroot" "$ret"
428 return 1
429 fi
431 got branch -d bogus_branch_name -r $testroot/repo \
432 > $testroot/stdout 2> $testroot/stderr
433 ret="$?"
434 if [ "$ret" = "0" ]; then
435 echo "got update succeeded unexpectedly"
436 test_done "$testroot" "$ret"
437 return 1
438 fi
440 echo "got: reference refs/heads/bogus_branch_name not found" \
441 > $testroot/stderr.expected
442 cmp -s $testroot/stderr $testroot/stderr.expected
443 ret="$?"
444 if [ "$ret" != "0" ]; then
445 diff -u $testroot/stderr.expected $testroot/stderr
446 fi
447 test_done "$testroot" "$ret"
450 test_branch_show() {
451 local testroot=`test_init branch_show`
452 local commit_id=`git_show_head $testroot/repo`
454 for b in branch1 branch2 branch3; do
455 got branch -r $testroot/repo $b
456 ret="$?"
457 if [ "$ret" != "0" ]; then
458 echo "got branch command failed unexpectedly"
459 test_done "$testroot" "$ret"
460 return 1
461 fi
462 done
464 got checkout $testroot/repo $testroot/wt >/dev/null
465 ret="$?"
466 if [ "$ret" != "0" ]; then
467 echo "got checkout command failed unexpectedly"
468 test_done "$testroot" "$ret"
469 return 1
470 fi
472 (cd $testroot/wt && got branch > $testroot/stdout)
473 echo "master" > $testroot/stdout.expected
474 cmp -s $testroot/stdout $testroot/stdout.expected
475 ret="$?"
476 if [ "$ret" != "0" ]; then
477 diff -u $testroot/stdout.expected $testroot/stdout
478 test_done "$testroot" "$ret"
479 return 1
480 fi
482 (cd $testroot/wt && got update -b branch1 > /dev/null)
483 ret="$?"
484 if [ "$ret" != "0" ]; then
485 echo "got update command failed unexpectedly"
486 test_done "$testroot" "$ret"
487 return 1
488 fi
490 (cd $testroot/wt && got branch > $testroot/stdout)
491 echo "branch1" > $testroot/stdout.expected
492 cmp -s $testroot/stdout $testroot/stdout.expected
493 ret="$?"
494 if [ "$ret" != "0" ]; then
495 diff -u $testroot/stdout.expected $testroot/stdout
496 fi
497 test_done "$testroot" "$ret"
501 test_parseargs "$@"
502 run_test test_branch_create
503 run_test test_branch_list
504 run_test test_branch_delete
505 run_test test_branch_delete_current_branch
506 run_test test_branch_delete_packed
507 run_test test_branch_show