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`
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 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 function 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 function 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 update 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 update 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 fi
299 test_done "$testroot" "$ret"
302 function 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
307 ret="$?"
308 if [ "$ret" != "0" ]; then
309 echo "got checkout command failed unexpectedly"
310 test_done "$testroot" "$ret"
311 return 1
312 fi
314 (cd $testroot/wt && got branch -d master > $testroot/stdout \
315 2> $testroot/stderr)
317 echo "got: will not delete this work tree's current branch" \
318 > $testroot/stderr.expected
319 cmp -s $testroot/stderr $testroot/stderr.expected
320 ret="$?"
321 if [ "$ret" != "0" ]; then
322 diff -u $testroot/stderr.expected $testroot/stderr
323 fi
324 test_done "$testroot" "$ret"
327 function 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
333 ret="$?"
334 if [ "$ret" != "0" ]; then
335 echo "got branch command failed unexpectedly"
336 test_done "$testroot" "$ret"
337 return 1
338 fi
339 done
341 (cd $testroot/repo && git pack-refs --all)
343 got branch -d branch2 -r $testroot/repo > $testroot/stdout
344 ret="$?"
345 if [ "$ret" != "0" ]; then
346 echo "got update command failed unexpectedly"
347 test_done "$testroot" "$ret"
348 return 1
349 fi
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
356 ret="$?"
357 if [ "$ret" != "0" ]; then
358 diff -u $testroot/stdout.expected $testroot/stdout
359 test_done "$testroot" "$ret"
360 return 1
361 fi
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
369 ret="$?"
370 if [ "$ret" != "0" ]; then
371 diff -u $testroot/stdout.expected $testroot/stdout
372 test_done "$testroot" "$ret"
373 return 1
374 fi
376 got branch -d bogus_branch_name -r $testroot/repo \
377 > $testroot/stdout 2> $testroot/stderr
378 ret="$?"
379 if [ "$ret" == "0" ]; then
380 echo "got update succeeded unexpectedly"
381 test_done "$testroot" "$ret"
382 return 1
383 fi
385 echo "got: reference refs/heads/bogus_branch_name not found" \
386 > $testroot/stderr.expected
387 cmp -s $testroot/stderr $testroot/stderr.expected
388 ret="$?"
389 if [ "$ret" != "0" ]; then
390 diff -u $testroot/stderr.expected $testroot/stderr
391 fi
392 test_done "$testroot" "$ret"
395 function test_branch_show {
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
401 ret="$?"
402 if [ "$ret" != "0" ]; then
403 echo "got branch command failed unexpectedly"
404 test_done "$testroot" "$ret"
405 return 1
406 fi
407 done
409 got checkout $testroot/repo $testroot/wt >/dev/null
410 ret="$?"
411 if [ "$ret" != "0" ]; then
412 echo "got checkout command failed unexpectedly"
413 test_done "$testroot" "$ret"
414 return 1
415 fi
417 (cd $testroot/wt && got branch > $testroot/stdout)
418 echo "master" > $testroot/stdout.expected
419 cmp -s $testroot/stdout $testroot/stdout.expected
420 ret="$?"
421 if [ "$ret" != "0" ]; then
422 diff -u $testroot/stdout.expected $testroot/stdout
423 test_done "$testroot" "$ret"
424 return 1
425 fi
427 (cd $testroot/wt && got update -b branch1 > /dev/null)
428 ret="$?"
429 if [ "$ret" != "0" ]; then
430 echo "got update command failed unexpectedly"
431 test_done "$testroot" "$ret"
432 return 1
433 fi
435 (cd $testroot/wt && got branch > $testroot/stdout)
436 echo "branch1" > $testroot/stdout.expected
437 cmp -s $testroot/stdout $testroot/stdout.expected
438 ret="$?"
439 if [ "$ret" != "0" ]; then
440 diff -u $testroot/stdout.expected $testroot/stdout
441 fi
442 test_done "$testroot" "$ret"
446 run_test test_branch_create
447 run_test test_branch_list
448 run_test test_branch_delete
449 run_test test_branch_delete_current_branch
450 run_test test_branch_delete_packed
451 run_test test_branch_show