11#! /bin/sh
22
3- test_description=' checkout '
3+ test_description=' checkout'
44
55. ./test-lib.sh
66
7- # Arguments: <branch> <sha > [<checkout options>]
7+ # Arguments: [!] <branch> <oid > [<checkout options>]
88#
99# Runs "git checkout" to switch to <branch>, testing that
1010#
1111# 1) we are on the specified branch, <branch>;
12- # 2) HEAD is <sha >; if <sha > is not specified, the old HEAD is used.
12+ # 2) HEAD is <oid >; if <oid > is not specified, the old HEAD is used.
1313#
1414# If <checkout options> is not specified, "git checkout" is run with -b.
15- do_checkout () {
15+ #
16+ # If the first argument is `!`, "git checkout" is expected to fail when
17+ # it is run.
18+ do_checkout () {
19+ should_fail= &&
20+ if test " x$1 " = " x!"
21+ then
22+ should_fail=yes &&
23+ shift
24+ fi &&
1625 exp_branch=$1 &&
1726 exp_ref=" refs/heads/$exp_branch " &&
1827
19- # if <sha > is not specified, use HEAD.
20- exp_sha =${2:- $(git rev-parse --verify HEAD)} &&
28+ # if <oid > is not specified, use HEAD.
29+ exp_oid =${2:- $(git rev-parse --verify HEAD)} &&
2130
2231 # default options for git checkout: -b
23- if [ -z " $3 " ]; then
32+ if test -z " $3 "
33+ then
2434 opts=" -b"
2535 else
2636 opts=" $3 "
2737 fi
2838
29- git checkout $opts $exp_branch $exp_sha &&
39+ if test -n " $should_fail "
40+ then
41+ test_must_fail git checkout $opts $exp_branch $exp_oid
42+ else
43+ git checkout $opts $exp_branch $exp_oid &&
44+ echo " $exp_ref " > ref.expect &&
45+ git rev-parse --symbolic-full-name HEAD > ref.actual &&
46+ test_cmp ref.expect ref.actual &&
47+ echo " $exp_oid " > oid.expect &&
48+ git rev-parse --verify HEAD > oid.actual &&
49+ test_cmp oid.expect oid.actual
50+ fi
51+ }
3052
31- test $exp_ref = $( git rev-parse --symbolic-full-name HEAD ) &&
32- test $exp_sha = $( git rev-parse --verify HEAD )
53+ test_dirty_unmergeable () {
54+ test_expect_code 1 git diff --exit-code
3355}
3456
35- test_dirty_unmergeable () {
36- ! git diff --exit-code > /dev/null
57+ test_dirty_unmergeable_discards_changes () {
58+ git diff --exit-code
3759}
3860
39- setup_dirty_unmergeable () {
61+ setup_dirty_unmergeable () {
4062 echo >> file1 change2
4163}
4264
43- test_dirty_mergeable () {
44- ! git diff --cached --exit-code > /dev/null
65+ test_dirty_mergeable () {
66+ test_expect_code 1 git diff --cached --exit-code
67+ }
68+
69+ test_dirty_mergeable_discards_changes () {
70+ git diff --cached --exit-code
4571}
4672
47- setup_dirty_mergeable () {
73+ setup_dirty_mergeable () {
4874 echo > file2 file2 &&
4975 git add file2
5076}
@@ -82,7 +108,7 @@ test_expect_success 'checkout -b to a new branch, set to an explicit ref' '
82108
83109test_expect_success ' checkout -b to a new branch with unmergeable changes fails' '
84110 setup_dirty_unmergeable &&
85- test_must_fail do_checkout branch2 $HEAD1 &&
111+ do_checkout ! branch2 $HEAD1 &&
86112 test_dirty_unmergeable
87113'
88114
@@ -93,7 +119,7 @@ test_expect_success 'checkout -f -b to a new branch with unmergeable changes dis
93119
94120 # still dirty and on branch1
95121 do_checkout branch2 $HEAD1 "-f -b" &&
96- test_must_fail test_dirty_unmergeable
122+ test_dirty_unmergeable_discards_changes
97123'
98124
99125test_expect_success ' checkout -b to a new branch preserves mergeable changes' '
@@ -111,12 +137,12 @@ test_expect_success 'checkout -f -b to a new branch with mergeable changes disca
111137 test_when_finished git reset --hard HEAD &&
112138 setup_dirty_mergeable &&
113139 do_checkout branch2 $HEAD1 "-f -b" &&
114- test_must_fail test_dirty_mergeable
140+ test_dirty_mergeable_discards_changes
115141'
116142
117143test_expect_success ' checkout -b to an existing branch fails' '
118144 test_when_finished git reset --hard HEAD &&
119- test_must_fail do_checkout branch2 $HEAD2
145+ do_checkout ! branch2 $HEAD2
120146'
121147
122148test_expect_success ' checkout -b to @{-1} fails with the right branch name' '
@@ -140,7 +166,8 @@ test_expect_success 'checkout -B to a merge base' '
140166'
141167
142168test_expect_success ' checkout -B to an existing branch from detached HEAD resets branch to HEAD' '
143- git checkout $(git rev-parse --verify HEAD) &&
169+ head=$(git rev-parse --verify HEAD) &&
170+ git checkout "$head" &&
144171
145172 do_checkout branch2 "" -B
146173'
@@ -155,14 +182,14 @@ test_expect_success 'checkout -B to an existing branch with unmergeable changes
155182 git checkout branch1 &&
156183
157184 setup_dirty_unmergeable &&
158- test_must_fail do_checkout branch2 $HEAD1 -B &&
185+ do_checkout ! branch2 $HEAD1 -B &&
159186 test_dirty_unmergeable
160187'
161188
162189test_expect_success ' checkout -f -B to an existing branch with unmergeable changes discards changes' '
163190 # still dirty and on branch1
164191 do_checkout branch2 $HEAD1 "-f -B" &&
165- test_must_fail test_dirty_unmergeable
192+ test_dirty_unmergeable_discards_changes
166193'
167194
168195test_expect_success ' checkout -B to an existing branch preserves mergeable changes' '
@@ -179,7 +206,7 @@ test_expect_success 'checkout -f -B to an existing branch with mergeable changes
179206
180207 setup_dirty_mergeable &&
181208 do_checkout branch2 $HEAD1 "-f -B" &&
182- test_must_fail test_dirty_mergeable
209+ test_dirty_mergeable_discards_changes
183210'
184211
185212test_expect_success ' checkout -b <describe>' '
0 commit comments