3 # Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
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
26 if [ "$ret" != "0" ]; then
27 echo "got branch command failed unexpectedly"
28 test_done "$testroot" "$ret"
32 # Ensure that Git recognizes the branch Got has created
33 (cd $testroot/repo && git checkout -q newbranch)
35 if [ "$ret" != "0" ]; then
36 echo "git checkout command failed unexpectedly"
37 test_done "$testroot" "$ret"
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
45 if [ "$ret" != "0" ]; then
46 echo "got checkout command failed unexpectedly"
47 test_done "$testroot" "$ret"
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
55 if [ "$ret" != "0" ]; then
56 diff -u $testroot/content.expected $testroot/content
57 test_done "$testroot" "$ret"
61 # Create a branch based on the work tree's branch
62 (cd $testroot/wt && got branch -n refs/heads/anotherbranch)
64 if [ "$ret" != "0" ]; then
65 test_done "$testroot" "$ret"
69 (cd $testroot/repo && git checkout -q anotherbranch)
71 if [ "$ret" != "0" ]; then
72 echo "git checkout command failed unexpectedly"
73 test_done "$testroot" "$ret"
77 # Create a branch based on another specific branch
78 (cd $testroot/wt && got branch -n -c master yetanotherbranch)
80 if [ "$ret" != "0" ]; then
81 test_done "$testroot" "$ret"
85 (cd $testroot/repo && git checkout -q yetanotherbranch)
87 if [ "$ret" != "0" ]; then
88 echo "git checkout command failed unexpectedly"
89 test_done "$testroot" "$ret"
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
97 if [ "$ret" != "0" ]; then
98 echo "got branch command failed unexpectedly"
99 test_done "$testroot" "$ret"
103 (cd $testroot/repo && git checkout -q commitbranch)
105 if [ "$ret" != "0" ]; then
106 echo "git checkout command failed unexpectedly"
107 test_done "$testroot" "$ret"
111 # Create a branch and let the work tree be updated to it
112 (cd $testroot/wt && got branch -c $commit_id0 updatebranch \
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 refs/heads/updatebranch: $commit_id0" \
120 >> $testroot/stdout.expected
122 cmp -s $testroot/stdout.expected $testroot/stdout
124 if [ "$ret" != "0" ]; then
125 diff -u $testroot/stdout.expected $testroot/stdout
127 test_done "$testroot" "$ret"
131 local testroot=`test_init branch_list`
132 local commit_id=`git_show_head $testroot/repo`
134 for b in branch1 branch2 branch3; do
135 got branch -r $testroot/repo $b
137 if [ "$ret" != "0" ]; then
138 echo "got branch command failed unexpectedly"
139 test_done "$testroot" "$ret"
144 got branch -l -r $testroot/repo > $testroot/stdout
145 echo " branch1: $commit_id" > $testroot/stdout.expected
146 echo " branch2: $commit_id" >> $testroot/stdout.expected
147 echo " branch3: $commit_id" >> $testroot/stdout.expected
148 echo " master: $commit_id" >> $testroot/stdout.expected
149 cmp -s $testroot/stdout $testroot/stdout.expected
151 if [ "$ret" != "0" ]; then
152 diff -u $testroot/stdout.expected $testroot/stdout
153 test_done "$testroot" "$ret"
157 got checkout $testroot/repo $testroot/wt >/dev/null
159 if [ "$ret" != "0" ]; then
160 echo "got checkout command failed unexpectedly"
161 test_done "$testroot" "$ret"
165 (cd $testroot/wt && got branch -l > $testroot/stdout)
166 echo " branch1: $commit_id" > $testroot/stdout.expected
167 echo " branch2: $commit_id" >> $testroot/stdout.expected
168 echo " branch3: $commit_id" >> $testroot/stdout.expected
169 echo "* master: $commit_id" >> $testroot/stdout.expected
170 cmp -s $testroot/stdout $testroot/stdout.expected
172 if [ "$ret" != "0" ]; then
173 diff -u $testroot/stdout.expected $testroot/stdout
174 test_done "$testroot" "$ret"
178 echo "modified delta" > $testroot/repo/gamma/delta
179 git_commit $testroot/repo -m "committing to delta"
180 local commit_id2=`git_show_head $testroot/repo`
182 (cd $testroot/wt && got branch -l > $testroot/stdout)
183 echo " branch1: $commit_id" > $testroot/stdout.expected
184 echo " branch2: $commit_id" >> $testroot/stdout.expected
185 echo " branch3: $commit_id" >> $testroot/stdout.expected
186 echo "~ master: $commit_id2" >> $testroot/stdout.expected
187 cmp -s $testroot/stdout $testroot/stdout.expected
189 if [ "$ret" != "0" ]; then
190 diff -u $testroot/stdout.expected $testroot/stdout
191 test_done "$testroot" "$ret"
195 (cd $testroot/wt && got update > /dev/null)
197 if [ "$ret" != "0" ]; then
198 echo "got update command failed unexpectedly"
199 test_done "$testroot" "$ret"
203 (cd $testroot/wt && got branch -l > $testroot/stdout)
204 echo " branch1: $commit_id" > $testroot/stdout.expected
205 echo " branch2: $commit_id" >> $testroot/stdout.expected
206 echo " branch3: $commit_id" >> $testroot/stdout.expected
207 echo "* master: $commit_id2" >> $testroot/stdout.expected
208 cmp -s $testroot/stdout $testroot/stdout.expected
210 if [ "$ret" != "0" ]; then
211 diff -u $testroot/stdout.expected $testroot/stdout
212 test_done "$testroot" "$ret"
216 (cd $testroot/wt && got update -b branch1 > /dev/null)
218 if [ "$ret" != "0" ]; then
219 echo "got update command failed unexpectedly"
220 test_done "$testroot" "$ret"
224 (cd $testroot/wt && got branch -l > $testroot/stdout)
225 echo "* branch1: $commit_id" > $testroot/stdout.expected
226 echo " branch2: $commit_id" >> $testroot/stdout.expected
227 echo " branch3: $commit_id" >> $testroot/stdout.expected
228 echo " master: $commit_id2" >> $testroot/stdout.expected
229 cmp -s $testroot/stdout $testroot/stdout.expected
231 if [ "$ret" != "0" ]; then
232 diff -u $testroot/stdout.expected $testroot/stdout
234 test_done "$testroot" "$ret"
237 test_branch_delete() {
238 local testroot=`test_init branch_delete`
239 local commit_id=`git_show_head $testroot/repo`
241 for b in branch1 branch2 branch3; do
242 got branch -r $testroot/repo $b
244 if [ "$ret" != "0" ]; then
245 echo "got branch command failed unexpectedly"
246 test_done "$testroot" "$ret"
251 got branch -d branch2 -r $testroot/repo > $testroot/stdout
253 if [ "$ret" != "0" ]; then
254 echo "got branch command failed unexpectedly"
255 test_done "$testroot" "$ret"
259 got branch -l -r $testroot/repo > $testroot/stdout
260 echo " branch1: $commit_id" > $testroot/stdout.expected
261 echo " branch3: $commit_id" >> $testroot/stdout.expected
262 echo " master: $commit_id" >> $testroot/stdout.expected
263 cmp -s $testroot/stdout $testroot/stdout.expected
265 if [ "$ret" != "0" ]; then
266 diff -u $testroot/stdout.expected $testroot/stdout
267 test_done "$testroot" "$ret"
271 got ref -l -r $testroot/repo > $testroot/stdout
272 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
273 echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
274 echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
275 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
276 cmp -s $testroot/stdout $testroot/stdout.expected
278 if [ "$ret" != "0" ]; then
279 diff -u $testroot/stdout.expected $testroot/stdout
280 test_done "$testroot" "$ret"
284 got branch -d bogus_branch_name -r $testroot/repo \
285 > $testroot/stdout 2> $testroot/stderr
287 if [ "$ret" = "0" ]; then
288 echo "got branch succeeded unexpectedly"
289 test_done "$testroot" "$ret"
293 echo "got: reference refs/heads/bogus_branch_name not found" \
294 > $testroot/stderr.expected
295 cmp -s $testroot/stderr $testroot/stderr.expected
297 if [ "$ret" != "0" ]; then
298 diff -u $testroot/stderr.expected $testroot/stderr
299 test_done "$testroot" "$ret"
303 got ref -r $testroot/repo -c master refs/remotes/origin/master
304 got ref -r $testroot/repo -c branch1 refs/remotes/origin/branch1
305 got ref -r $testroot/repo -c branch3 refs/remotes/origin/branch3
307 got ref -l -r $testroot/repo > $testroot/stdout
308 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
309 echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
310 echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
311 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
312 echo "refs/remotes/origin/branch1: $commit_id" \
313 >> $testroot/stdout.expected
314 echo "refs/remotes/origin/branch3: $commit_id" \
315 >> $testroot/stdout.expected
316 echo "refs/remotes/origin/master: $commit_id" \
317 >> $testroot/stdout.expected
318 cmp -s $testroot/stdout $testroot/stdout.expected
320 if [ "$ret" != "0" ]; then
321 diff -u $testroot/stdout.expected $testroot/stdout
322 test_done "$testroot" "$ret"
326 got branch -d origin/branch1 -r $testroot/repo > $testroot/stdout
328 if [ "$ret" != "0" ]; then
329 echo "got branch command failed unexpectedly"
330 test_done "$testroot" "$ret"
334 got branch -d refs/remotes/origin/branch3 -r $testroot/repo \
337 if [ "$ret" != "0" ]; then
338 echo "got branch command failed unexpectedly"
339 test_done "$testroot" "$ret"
343 got ref -l -r $testroot/repo > $testroot/stdout
344 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
345 echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
346 echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
347 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
348 echo "refs/remotes/origin/master: $commit_id" \
349 >> $testroot/stdout.expected
350 cmp -s $testroot/stdout $testroot/stdout.expected
352 if [ "$ret" != "0" ]; then
353 diff -u $testroot/stdout.expected $testroot/stdout
355 test_done "$testroot" "$ret"
358 test_branch_delete_current_branch() {
359 local testroot=`test_init branch_delete_current_branch`
360 local commit_id=`git_show_head $testroot/repo`
362 got checkout $testroot/repo $testroot/wt >/dev/null
364 if [ "$ret" != "0" ]; then
365 echo "got checkout command failed unexpectedly"
366 test_done "$testroot" "$ret"
370 (cd $testroot/wt && got branch -d master > $testroot/stdout \
373 echo "got: will not delete this work tree's current branch" \
374 > $testroot/stderr.expected
375 cmp -s $testroot/stderr $testroot/stderr.expected
377 if [ "$ret" != "0" ]; then
378 diff -u $testroot/stderr.expected $testroot/stderr
380 test_done "$testroot" "$ret"
383 test_branch_delete_packed() {
384 local testroot=`test_init branch_delete_packed`
385 local commit_id=`git_show_head $testroot/repo`
387 for b in branch1 branch2 branch3; do
388 got branch -r $testroot/repo $b
390 if [ "$ret" != "0" ]; then
391 echo "got branch command failed unexpectedly"
392 test_done "$testroot" "$ret"
397 (cd $testroot/repo && git pack-refs --all)
399 got branch -d refs/heads/branch2 -r $testroot/repo > $testroot/stdout
401 if [ "$ret" != "0" ]; then
402 echo "got update command failed unexpectedly"
403 test_done "$testroot" "$ret"
407 got branch -l -r $testroot/repo > $testroot/stdout
408 echo " branch1: $commit_id" > $testroot/stdout.expected
409 echo " branch3: $commit_id" >> $testroot/stdout.expected
410 echo " master: $commit_id" >> $testroot/stdout.expected
411 cmp -s $testroot/stdout $testroot/stdout.expected
413 if [ "$ret" != "0" ]; then
414 diff -u $testroot/stdout.expected $testroot/stdout
415 test_done "$testroot" "$ret"
419 got ref -l -r $testroot/repo > $testroot/stdout
420 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
421 echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
422 echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
423 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
424 cmp -s $testroot/stdout $testroot/stdout.expected
426 if [ "$ret" != "0" ]; then
427 diff -u $testroot/stdout.expected $testroot/stdout
428 test_done "$testroot" "$ret"
432 got branch -d bogus_branch_name -r $testroot/repo \
433 > $testroot/stdout 2> $testroot/stderr
435 if [ "$ret" = "0" ]; then
436 echo "got update succeeded unexpectedly"
437 test_done "$testroot" "$ret"
441 echo "got: reference refs/heads/bogus_branch_name not found" \
442 > $testroot/stderr.expected
443 cmp -s $testroot/stderr $testroot/stderr.expected
445 if [ "$ret" != "0" ]; then
446 diff -u $testroot/stderr.expected $testroot/stderr
448 test_done "$testroot" "$ret"
452 local testroot=`test_init branch_show`
453 local commit_id=`git_show_head $testroot/repo`
455 for b in branch1 branch2 branch3; do
456 got branch -r $testroot/repo $b
458 if [ "$ret" != "0" ]; then
459 echo "got branch command failed unexpectedly"
460 test_done "$testroot" "$ret"
465 got checkout $testroot/repo $testroot/wt >/dev/null
467 if [ "$ret" != "0" ]; then
468 echo "got checkout command failed unexpectedly"
469 test_done "$testroot" "$ret"
473 (cd $testroot/wt && got branch > $testroot/stdout)
474 echo "master" > $testroot/stdout.expected
475 cmp -s $testroot/stdout $testroot/stdout.expected
477 if [ "$ret" != "0" ]; then
478 diff -u $testroot/stdout.expected $testroot/stdout
479 test_done "$testroot" "$ret"
483 (cd $testroot/wt && got update -b branch1 > /dev/null)
485 if [ "$ret" != "0" ]; then
486 echo "got update command failed unexpectedly"
487 test_done "$testroot" "$ret"
491 (cd $testroot/wt && got branch > $testroot/stdout)
492 echo "branch1" > $testroot/stdout.expected
493 cmp -s $testroot/stdout $testroot/stdout.expected
495 if [ "$ret" != "0" ]; then
496 diff -u $testroot/stdout.expected $testroot/stdout
498 test_done "$testroot" "$ret"
503 run_test test_branch_create
504 run_test test_branch_list
505 run_test test_branch_delete
506 run_test test_branch_delete_current_branch
507 run_test test_branch_delete_packed
508 run_test test_branch_show