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 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 commit $commit_id0" >> $testroot/stdout.expected
121 cmp -s $testroot/stdout.expected $testroot/stdout
123 if [ "$ret" != "0" ]; then
124 diff -u $testroot/stdout.expected $testroot/stdout
126 test_done "$testroot" "$ret"
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
136 if [ "$ret" != "0" ]; then
137 echo "got branch command failed unexpectedly"
138 test_done "$testroot" "$ret"
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
150 if [ "$ret" != "0" ]; then
151 diff -u $testroot/stdout.expected $testroot/stdout
152 test_done "$testroot" "$ret"
156 got checkout $testroot/repo $testroot/wt >/dev/null
158 if [ "$ret" != "0" ]; then
159 echo "got checkout command failed unexpectedly"
160 test_done "$testroot" "$ret"
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
171 if [ "$ret" != "0" ]; then
172 diff -u $testroot/stdout.expected $testroot/stdout
173 test_done "$testroot" "$ret"
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
188 if [ "$ret" != "0" ]; then
189 diff -u $testroot/stdout.expected $testroot/stdout
190 test_done "$testroot" "$ret"
194 (cd $testroot/wt && got update > /dev/null)
196 if [ "$ret" != "0" ]; then
197 echo "got update command failed unexpectedly"
198 test_done "$testroot" "$ret"
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
209 if [ "$ret" != "0" ]; then
210 diff -u $testroot/stdout.expected $testroot/stdout
211 test_done "$testroot" "$ret"
215 (cd $testroot/wt && got update -b branch1 > /dev/null)
217 if [ "$ret" != "0" ]; then
218 echo "got update command failed unexpectedly"
219 test_done "$testroot" "$ret"
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
230 if [ "$ret" != "0" ]; then
231 diff -u $testroot/stdout.expected $testroot/stdout
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
243 if [ "$ret" != "0" ]; then
244 echo "got branch command failed unexpectedly"
245 test_done "$testroot" "$ret"
250 got branch -d branch2 -r $testroot/repo > $testroot/stdout
252 if [ "$ret" != "0" ]; then
253 echo "got update command failed unexpectedly"
254 test_done "$testroot" "$ret"
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
264 if [ "$ret" != "0" ]; then
265 diff -u $testroot/stdout.expected $testroot/stdout
266 test_done "$testroot" "$ret"
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
277 if [ "$ret" != "0" ]; then
278 diff -u $testroot/stdout.expected $testroot/stdout
279 test_done "$testroot" "$ret"
283 got branch -d bogus_branch_name -r $testroot/repo \
284 > $testroot/stdout 2> $testroot/stderr
286 if [ "$ret" == "0" ]; then
287 echo "got update succeeded unexpectedly"
288 test_done "$testroot" "$ret"
292 echo "got: reference refs/heads/bogus_branch_name not found" \
293 > $testroot/stderr.expected
294 cmp -s $testroot/stderr $testroot/stderr.expected
296 if [ "$ret" != "0" ]; then
297 diff -u $testroot/stderr.expected $testroot/stderr
299 test_done "$testroot" "$ret"
302 test_branch_delete_current_branch() {
303 local testroot=`test_init branch_delete_current_branch`
304 local commit_id=`git_show_head $testroot/repo`
306 got checkout $testroot/repo $testroot/wt >/dev/null
308 if [ "$ret" != "0" ]; then
309 echo "got checkout command failed unexpectedly"
310 test_done "$testroot" "$ret"
314 (cd $testroot/wt && got branch -d master > $testroot/stdout \
317 echo "got: will not delete this work tree's current branch" \
318 > $testroot/stderr.expected
319 cmp -s $testroot/stderr $testroot/stderr.expected
321 if [ "$ret" != "0" ]; then
322 diff -u $testroot/stderr.expected $testroot/stderr
324 test_done "$testroot" "$ret"
327 test_branch_delete_packed() {
328 local testroot=`test_init branch_delete_packed`
329 local commit_id=`git_show_head $testroot/repo`
331 for b in branch1 branch2 branch3; do
332 got branch -r $testroot/repo $b
334 if [ "$ret" != "0" ]; then
335 echo "got branch command failed unexpectedly"
336 test_done "$testroot" "$ret"
341 (cd $testroot/repo && git pack-refs --all)
343 got branch -d branch2 -r $testroot/repo > $testroot/stdout
345 if [ "$ret" != "0" ]; then
346 echo "got update command failed unexpectedly"
347 test_done "$testroot" "$ret"
351 got branch -l -r $testroot/repo > $testroot/stdout
352 echo " branch1: $commit_id" > $testroot/stdout.expected
353 echo " branch3: $commit_id" >> $testroot/stdout.expected
354 echo " master: $commit_id" >> $testroot/stdout.expected
355 cmp -s $testroot/stdout $testroot/stdout.expected
357 if [ "$ret" != "0" ]; then
358 diff -u $testroot/stdout.expected $testroot/stdout
359 test_done "$testroot" "$ret"
363 got ref -l -r $testroot/repo > $testroot/stdout
364 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
365 echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
366 echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
367 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
368 cmp -s $testroot/stdout $testroot/stdout.expected
370 if [ "$ret" != "0" ]; then
371 diff -u $testroot/stdout.expected $testroot/stdout
372 test_done "$testroot" "$ret"
376 got branch -d bogus_branch_name -r $testroot/repo \
377 > $testroot/stdout 2> $testroot/stderr
379 if [ "$ret" == "0" ]; then
380 echo "got update succeeded unexpectedly"
381 test_done "$testroot" "$ret"
385 echo "got: reference refs/heads/bogus_branch_name not found" \
386 > $testroot/stderr.expected
387 cmp -s $testroot/stderr $testroot/stderr.expected
389 if [ "$ret" != "0" ]; then
390 diff -u $testroot/stderr.expected $testroot/stderr
392 test_done "$testroot" "$ret"
396 local testroot=`test_init branch_show`
397 local commit_id=`git_show_head $testroot/repo`
399 for b in branch1 branch2 branch3; do
400 got branch -r $testroot/repo $b
402 if [ "$ret" != "0" ]; then
403 echo "got branch command failed unexpectedly"
404 test_done "$testroot" "$ret"
409 got checkout $testroot/repo $testroot/wt >/dev/null
411 if [ "$ret" != "0" ]; then
412 echo "got checkout command failed unexpectedly"
413 test_done "$testroot" "$ret"
417 (cd $testroot/wt && got branch > $testroot/stdout)
418 echo "master" > $testroot/stdout.expected
419 cmp -s $testroot/stdout $testroot/stdout.expected
421 if [ "$ret" != "0" ]; then
422 diff -u $testroot/stdout.expected $testroot/stdout
423 test_done "$testroot" "$ret"
427 (cd $testroot/wt && got update -b branch1 > /dev/null)
429 if [ "$ret" != "0" ]; then
430 echo "got update command failed unexpectedly"
431 test_done "$testroot" "$ret"
435 (cd $testroot/wt && got branch > $testroot/stdout)
436 echo "branch1" > $testroot/stdout.expected
437 cmp -s $testroot/stdout $testroot/stdout.expected
439 if [ "$ret" != "0" ]; then
440 diff -u $testroot/stdout.expected $testroot/stdout
442 test_done "$testroot" "$ret"
447 run_test test_branch_create
448 run_test test_branch_list
449 run_test test_branch_delete
450 run_test test_branch_delete_current_branch
451 run_test test_branch_delete_packed
452 run_test test_branch_show