3 test_description
='checkout '
7 # Arguments: <branch> <sha> [<checkout options>]
9 # Runs "git checkout" to switch to <branch>, testing that
11 # 1) we are on the specified branch, <branch>;
12 # 2) HEAD is <sha>; if <sha> is not specified, the old HEAD is used.
14 # If <checkout options> is not specified, "git checkout" is run with -b.
17 exp_ref
="refs/heads/$exp_branch" &&
19 # if <sha> is not specified, use HEAD.
20 exp_sha
=${2:-$(git rev-parse --verify HEAD)} &&
22 # default options for git checkout: -b
29 git checkout
$opts $exp_branch $exp_sha &&
31 test $exp_ref = $
(git rev-parse
--symbolic-full-name HEAD
) &&
32 test $exp_sha = $
(git rev-parse
--verify HEAD
)
35 test_dirty_unmergeable
() {
36 ! git
diff --exit-code >/dev
/null
39 setup_dirty_unmergeable
() {
43 test_dirty_mergeable
() {
44 ! git
diff --cached --exit-code >/dev
/null
47 setup_dirty_mergeable
() {
52 test_expect_success
'setup' '
53 test_commit initial file1 &&
54 HEAD1=$(git rev-parse --verify HEAD) &&
56 test_commit change1 file1 &&
57 HEAD2=$(git rev-parse --verify HEAD) &&
62 test_expect_success
'checkout -b to a new branch, set to HEAD' '
66 test_expect_success
'checkout -b to a new branch, set to an explicit ref' '
67 git checkout branch1 &&
68 git branch -D branch2 &&
70 do_checkout branch2 $HEAD1
73 test_expect_success
'checkout -b to a new branch with unmergeable changes fails' '
74 git checkout branch1 &&
76 # clean up from previous test
77 git branch -D branch2 &&
79 setup_dirty_unmergeable &&
80 test_must_fail do_checkout branch2 $HEAD1 &&
81 test_dirty_unmergeable
84 test_expect_success
'checkout -f -b to a new branch with unmergeable changes discards changes' '
85 # still dirty and on branch1
86 do_checkout branch2 $HEAD1 "-f -b" &&
87 test_must_fail test_dirty_unmergeable
90 test_expect_success
'checkout -b to a new branch preserves mergeable changes' '
91 git checkout branch1 &&
93 # clean up from previous test
94 git branch -D branch2 &&
96 setup_dirty_mergeable &&
97 do_checkout branch2 $HEAD1 &&
101 test_expect_success
'checkout -f -b to a new branch with mergeable changes discards changes' '
102 # clean up from previous test
105 git checkout branch1 &&
107 # clean up from previous test
108 git branch -D branch2 &&
110 setup_dirty_mergeable &&
111 do_checkout branch2 $HEAD1 "-f -b" &&
112 test_must_fail test_dirty_mergeable
115 test_expect_success
'checkout -b to an existing branch fails' '
116 git reset --hard HEAD &&
118 test_must_fail do_checkout branch2 $HEAD2