Git 1.7.3 rc1
[git/mingw/4msysgit/kblees.git] / t / t2018-checkout-branch.sh
blobfa69016381b0196c49472af51e36948ec7c5a2a9
1 #!/bin/sh
3 test_description='checkout '
5 . ./test-lib.sh
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.
15 do_checkout() {
16 exp_branch=$1 &&
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
23 if [ -z "$3" ]; then
24 opts="-b"
25 else
26 opts="$3"
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() {
40 echo >>file1 change2
43 test_dirty_mergeable() {
44 ! git diff --cached --exit-code >/dev/null
47 setup_dirty_mergeable() {
48 echo >file2 file2 &&
49 git add file2
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) &&
59 git branch -m branch1
62 test_expect_success 'checkout -b to a new branch, set to HEAD' '
63 do_checkout branch2
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 &&
98 test_dirty_mergeable
101 test_expect_success 'checkout -f -b to a new branch with mergeable changes discards changes' '
102 # clean up from previous test
103 git reset --hard &&
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
121 test_expect_success 'checkout -B to an existing branch resets branch to HEAD' '
122 git checkout branch1 &&
124 do_checkout branch2 "" -B
127 test_expect_success 'checkout -B to an existing branch from detached HEAD resets branch to HEAD' '
128 git checkout $(git rev-parse --verify HEAD) &&
130 do_checkout branch2 "" -B
133 test_expect_success 'checkout -B to an existing branch with an explicit ref resets branch to that ref' '
134 git checkout branch1 &&
136 do_checkout branch2 $HEAD1 -B
139 test_expect_success 'checkout -B to an existing branch with unmergeable changes fails' '
140 git checkout branch1 &&
142 setup_dirty_unmergeable &&
143 test_must_fail do_checkout branch2 $HEAD1 -B &&
144 test_dirty_unmergeable
147 test_expect_success 'checkout -f -B to an existing branch with unmergeable changes discards changes' '
148 # still dirty and on branch1
149 do_checkout branch2 $HEAD1 "-f -B" &&
150 test_must_fail test_dirty_unmergeable
153 test_expect_success 'checkout -B to an existing branch preserves mergeable changes' '
154 git checkout branch1 &&
156 setup_dirty_mergeable &&
157 do_checkout branch2 $HEAD1 -B &&
158 test_dirty_mergeable
161 test_expect_success 'checkout -f -B to an existing branch with mergeable changes discards changes' '
162 # clean up from previous test
163 git reset --hard &&
165 git checkout branch1 &&
167 setup_dirty_mergeable &&
168 do_checkout branch2 $HEAD1 "-f -B" &&
169 test_must_fail test_dirty_mergeable
172 test_done