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 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
25 if [ "$ret" != "0" ]; then
26 echo "got branch command failed unexpectedly"
27 test_done "$testroot" "$ret"
31 # Ensure that Git recognizes the branch Got has created
32 (cd $testroot/repo && git checkout -q newbranch)
34 if [ "$ret" != "0" ]; then
35 echo "git checkout command failed unexpectedly"
36 test_done "$testroot" "$ret"
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
44 if [ "$ret" != "0" ]; then
45 echo "got checkout command failed unexpectedly"
46 test_done "$testroot" "$ret"
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
54 if [ "$ret" != "0" ]; then
55 diff -u $testroot/content.expected $testroot/content
56 test_done "$testroot" "$ret"
60 # Create a branch based on the work tree's branch
61 (cd $testroot/wt && got branch anotherbranch)
63 if [ "$ret" != "0" ]; then
64 test_done "$testroot" "$ret"
68 (cd $testroot/repo && git checkout -q anotherbranch)
70 if [ "$ret" != "0" ]; then
71 echo "git checkout command failed unexpectedly"
72 test_done "$testroot" "$ret"
76 # Create a branch based on another specific branch
77 (cd $testroot/wt && got branch yetanotherbranch master)
79 if [ "$ret" != "0" ]; then
80 test_done "$testroot" "$ret"
84 (cd $testroot/repo && git checkout -q yetanotherbranch)
86 if [ "$ret" != "0" ]; then
87 echo "git checkout command failed unexpectedly"
88 test_done "$testroot" "$ret"
92 # Create a branch based on a specific commit
93 local commit_id=`git_show_head $testroot/repo`
94 got branch -r $testroot/repo commitbranch $commit_id
96 if [ "$ret" != "0" ]; then
97 echo "got branch command failed unexpectedly"
98 test_done "$testroot" "$ret"
102 (cd $testroot/repo && git checkout -q commitbranch)
104 if [ "$ret" != "0" ]; then
105 echo "git checkout command failed unexpectedly"
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
117 if [ "$ret" != "0" ]; then
118 echo "got branch command failed unexpectedly"
119 test_done "$testroot" "$ret"
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
131 if [ "$ret" != "0" ]; then
132 diff -u $testroot/stdout.expected $testroot/stdout
133 test_done "$testroot" "$ret"
137 got checkout $testroot/repo $testroot/wt >/dev/null
139 if [ "$ret" != "0" ]; then
140 echo "got checkout command failed unexpectedly"
141 test_done "$testroot" "$ret"
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
152 if [ "$ret" != "0" ]; then
153 diff -u $testroot/stdout.expected $testroot/stdout
154 test_done "$testroot" "$ret"
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
169 if [ "$ret" != "0" ]; then
170 diff -u $testroot/stdout.expected $testroot/stdout
171 test_done "$testroot" "$ret"
175 (cd $testroot/wt && got update > /dev/null)
177 if [ "$ret" != "0" ]; then
178 echo "got update command failed unexpectedly"
179 test_done "$testroot" "$ret"
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
190 if [ "$ret" != "0" ]; then
191 diff -u $testroot/stdout.expected $testroot/stdout
192 test_done "$testroot" "$ret"
196 (cd $testroot/wt && got update -b branch1 > /dev/null)
198 if [ "$ret" != "0" ]; then
199 echo "got update command failed unexpectedly"
200 test_done "$testroot" "$ret"
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
211 if [ "$ret" != "0" ]; then
212 diff -u $testroot/stdout.expected $testroot/stdout
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
224 if [ "$ret" != "0" ]; then
225 echo "got branch command failed unexpectedly"
226 test_done "$testroot" "$ret"
231 got branch -d branch2 -r $testroot/repo > $testroot/stdout
233 if [ "$ret" != "0" ]; then
234 echo "got update command failed unexpectedly"
235 test_done "$testroot" "$ret"
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
245 if [ "$ret" != "0" ]; then
246 diff -u $testroot/stdout.expected $testroot/stdout
247 test_done "$testroot" "$ret"
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
258 if [ "$ret" != "0" ]; then
259 diff -u $testroot/stdout.expected $testroot/stdout
260 test_done "$testroot" "$ret"
264 got branch -d bogus_branch_name -r $testroot/repo \
265 > $testroot/stdout 2> $testroot/stderr
267 if [ "$ret" == "0" ]; then
268 echo "got update succeeded unexpectedly"
269 test_done "$testroot" "$ret"
273 echo "got: reference refs/heads/bogus_branch_name not found" \
274 > $testroot/stderr.expected
275 cmp -s $testroot/stderr $testroot/stderr.expected
277 if [ "$ret" != "0" ]; then
278 diff -u $testroot/stderr.expected $testroot/stderr
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
289 if [ "$ret" != "0" ]; then
290 echo "got checkout command failed unexpectedly"
291 test_done "$testroot" "$ret"
295 (cd $testroot/wt && got branch -d master > $testroot/stdout \
298 echo "got: will not delete this work tree's current branch" \
299 > $testroot/stderr.expected
300 cmp -s $testroot/stderr $testroot/stderr.expected
302 if [ "$ret" != "0" ]; then
303 diff -u $testroot/stderr.expected $testroot/stderr
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
315 if [ "$ret" != "0" ]; then
316 echo "got branch command failed unexpectedly"
317 test_done "$testroot" "$ret"
322 (cd $testroot/repo && git pack-refs --all)
324 got branch -d branch2 -r $testroot/repo > $testroot/stdout
326 if [ "$ret" != "0" ]; then
327 echo "got update command failed unexpectedly"
328 test_done "$testroot" "$ret"
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
338 if [ "$ret" != "0" ]; then
339 diff -u $testroot/stdout.expected $testroot/stdout
340 test_done "$testroot" "$ret"
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
351 if [ "$ret" != "0" ]; then
352 diff -u $testroot/stdout.expected $testroot/stdout
353 test_done "$testroot" "$ret"
357 got branch -d bogus_branch_name -r $testroot/repo \
358 > $testroot/stdout 2> $testroot/stderr
360 if [ "$ret" == "0" ]; then
361 echo "got update succeeded unexpectedly"
362 test_done "$testroot" "$ret"
366 echo "got: reference refs/heads/bogus_branch_name not found" \
367 > $testroot/stderr.expected
368 cmp -s $testroot/stderr $testroot/stderr.expected
370 if [ "$ret" != "0" ]; then
371 diff -u $testroot/stderr.expected $testroot/stderr
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
383 if [ "$ret" != "0" ]; then
384 echo "got branch command failed unexpectedly"
385 test_done "$testroot" "$ret"
390 got checkout $testroot/repo $testroot/wt >/dev/null
392 if [ "$ret" != "0" ]; then
393 echo "got checkout command failed unexpectedly"
394 test_done "$testroot" "$ret"
398 (cd $testroot/wt && got branch > $testroot/stdout)
399 echo "master" > $testroot/stdout.expected
400 cmp -s $testroot/stdout $testroot/stdout.expected
402 if [ "$ret" != "0" ]; then
403 diff -u $testroot/stdout.expected $testroot/stdout
404 test_done "$testroot" "$ret"
408 (cd $testroot/wt && got update -b branch1 > /dev/null)
410 if [ "$ret" != "0" ]; then
411 echo "got update command failed unexpectedly"
412 test_done "$testroot" "$ret"
416 (cd $testroot/wt && got branch > $testroot/stdout)
417 echo "branch1" > $testroot/stdout.expected
418 cmp -s $testroot/stdout $testroot/stdout.expected
420 if [ "$ret" != "0" ]; then
421 diff -u $testroot/stdout.expected $testroot/stdout
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