summaryrefslogtreecommitdiff
path: root/t/t2018-checkout-branch.sh
blob: b66db2b8818b7b084889ea1b2c7687fc59c12127 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/sh
 
test_description='checkout '
 
. ./test-lib.sh
 
# Arguments: <branch> <sha> [<checkout options>]
#
# Runs "git checkout" to switch to <branch>, testing that
#
#   1) we are on the specified branch, <branch>;
#   2) HEAD is <sha>; if <sha> is not specified, the old HEAD is used.
#
# If <checkout options> is not specified, "git checkout" is run with -b.
do_checkout() {
	exp_branch=$1 &&
	exp_ref="refs/heads/$exp_branch" &&
 
	# if <sha> is not specified, use HEAD.
	exp_sha=${2:-$(git rev-parse --verify HEAD)} &&
 
	# default options for git checkout: -b
	if [ -z "$3" ]; then
		opts="-b"
	else
		opts="$3"
	fi
 
	git checkout $opts $exp_branch $exp_sha &&
 
	test $exp_ref = $(git rev-parse --symbolic-full-name HEAD) &&
	test $exp_sha = $(git rev-parse --verify HEAD)
}
 
test_dirty_unmergeable() {
	! git diff --exit-code >/dev/null
}
 
setup_dirty_unmergeable() {
	echo >>file1 change2
}
 
test_dirty_mergeable() {
	! git diff --cached --exit-code >/dev/null
}
 
setup_dirty_mergeable() {
	echo >file2 file2 &&
	git add file2
}
 
test_expect_success 'setup' '
	test_commit initial file1 &&
	HEAD1=$(git rev-parse --verify HEAD) &&
 
	test_commit change1 file1 &&
	HEAD2=$(git rev-parse --verify HEAD) &&
 
	git branch -m branch1
'
 
test_expect_success 'checkout -b to a new branch, set to HEAD' '
	do_checkout branch2
'
 
test_expect_success 'checkout -b to a new branch, set to an explicit ref' '
	git checkout branch1 &&
	git branch -D branch2 &&
 
	do_checkout branch2 $HEAD1
'
 
test_expect_success 'checkout -b to a new branch with unmergeable changes fails' '
	git checkout branch1 &&
 
	# clean up from previous test
	git branch -D branch2 &&
 
	setup_dirty_unmergeable &&
	test_must_fail do_checkout branch2 $HEAD1 &&
	test_dirty_unmergeable
'
 
test_expect_success 'checkout -f -b to a new branch with unmergeable changes discards changes' '
	# still dirty and on branch1
	do_checkout branch2 $HEAD1 "-f -b" &&
	test_must_fail test_dirty_unmergeable
'
 
test_expect_success 'checkout -b to a new branch preserves mergeable changes' '
	git checkout branch1 &&
 
	# clean up from previous test
	git branch -D branch2 &&
 
	setup_dirty_mergeable &&
	do_checkout branch2 $HEAD1 &&
	test_dirty_mergeable
'
 
test_expect_success 'checkout -f -b to a new branch with mergeable changes discards changes' '
	# clean up from previous test
	git reset --hard &&
 
	git checkout branch1 &&
 
	# clean up from previous test
	git branch -D branch2 &&
 
	setup_dirty_mergeable &&
	do_checkout branch2 $HEAD1 "-f -b" &&
	test_must_fail test_dirty_mergeable
'
 
test_expect_success 'checkout -b to an existing branch fails' '
	git reset --hard HEAD &&
 
	test_must_fail do_checkout branch2 $HEAD2
'
 
test_expect_success 'checkout -B to an existing branch resets branch to HEAD' '
	git checkout branch1 &&
 
	do_checkout branch2 "" -B
'
 
test_expect_success 'checkout -B to an existing branch from detached HEAD resets branch to HEAD' '
	git checkout $(git rev-parse --verify HEAD) &&
 
	do_checkout branch2 "" -B
'
 
test_expect_success 'checkout -B to an existing branch with an explicit ref resets branch to that ref' '
	git checkout branch1 &&
 
	do_checkout branch2 $HEAD1 -B
'
 
test_expect_success 'checkout -B to an existing branch with unmergeable changes fails' '
	git checkout branch1 &&
 
	setup_dirty_unmergeable &&
	test_must_fail do_checkout branch2 $HEAD1 -B &&
	test_dirty_unmergeable
'
 
test_expect_success 'checkout -f -B to an existing branch with unmergeable changes discards changes' '
	# still dirty and on branch1
	do_checkout branch2 $HEAD1 "-f -B" &&
	test_must_fail test_dirty_unmergeable
'
 
test_expect_success 'checkout -B to an existing branch preserves mergeable changes' '
	git checkout branch1 &&
 
	setup_dirty_mergeable &&
	do_checkout branch2 $HEAD1 -B &&
	test_dirty_mergeable
'
 
test_expect_success 'checkout -f -B to an existing branch with mergeable changes discards changes' '
	# clean up from previous test
	git reset --hard &&
 
	git checkout branch1 &&
 
	setup_dirty_mergeable &&
	do_checkout branch2 $HEAD1 "-f -B" &&
	test_must_fail test_dirty_mergeable
'
 
test_expect_success 'checkout -b <describe>' '
	git tag -f -m "First commit" initial initial &&
	git checkout -f change1 &&
	name=$(git describe) &&
	git checkout -b $name &&
	git diff --exit-code change1 &&
	echo "refs/heads/$name" >expect &&
	git symbolic-ref HEAD >actual &&
	test_cmp expect actual
'
 
test_expect_success 'checkout -B to the current branch fails before merging' '
	git checkout branch1 &&
	setup_dirty_mergeable &&
	git commit -mfooble &&
	test_must_fail git checkout -B branch1 initial &&
	test_must_fail test_dirty_mergeable
'
 
test_done