Sync with 2.38.4
[git.git] / t / t2012-checkout-last.sh
blob1f6c4ed0428a5c966c76359c1f4fa2e536f37fcf
1 #!/bin/sh
3 test_description='checkout can switch to last branch and merge base'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 . ./test-lib.sh
10 test_expect_success 'setup' '
11 test_commit initial world hello &&
12 git branch other &&
13 test_commit --append second world "hello again"
16 test_expect_success '"checkout -" does not work initially' '
17 test_must_fail git checkout -
20 test_expect_success 'first branch switch' '
21 git checkout other
24 test_cmp_symbolic_HEAD_ref () {
25 echo refs/heads/"$1" >expect &&
26 git symbolic-ref HEAD >actual &&
27 test_cmp expect actual
30 test_expect_success '"checkout -" switches back' '
31 git checkout - &&
32 test_cmp_symbolic_HEAD_ref main
35 test_expect_success '"checkout -" switches forth' '
36 git checkout - &&
37 test_cmp_symbolic_HEAD_ref other
40 test_expect_success 'detach HEAD' '
41 git checkout $(git rev-parse HEAD)
44 test_expect_success '"checkout -" attaches again' '
45 git checkout - &&
46 test_cmp_symbolic_HEAD_ref other
49 test_expect_success '"checkout -" detaches again' '
50 git checkout - &&
52 git rev-parse other >expect &&
53 git rev-parse HEAD >actual &&
54 test_cmp expect actual &&
56 test_must_fail git symbolic-ref HEAD
59 test_expect_success 'more switches' '
60 for i in 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
62 git checkout -b branch$i || return 1
63 done
66 more_switches () {
67 for i in 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
69 git checkout branch$i || return 1
70 done
73 test_expect_success 'switch to the last' '
74 more_switches &&
75 git checkout @{-1} &&
76 test_cmp_symbolic_HEAD_ref branch2
79 test_expect_success 'switch to second from the last' '
80 more_switches &&
81 git checkout @{-2} &&
82 test_cmp_symbolic_HEAD_ref branch3
85 test_expect_success 'switch to third from the last' '
86 more_switches &&
87 git checkout @{-3} &&
88 test_cmp_symbolic_HEAD_ref branch4
91 test_expect_success 'switch to fourth from the last' '
92 more_switches &&
93 git checkout @{-4} &&
94 test_cmp_symbolic_HEAD_ref branch5
97 test_expect_success 'switch to twelfth from the last' '
98 more_switches &&
99 git checkout @{-12} &&
100 test_cmp_symbolic_HEAD_ref branch13
103 test_expect_success 'merge base test setup' '
104 git checkout -b another other &&
105 test_commit --append third world "hello again"
108 test_expect_success 'another...main' '
109 git checkout another &&
110 git checkout another...main &&
112 git rev-parse --verify main^ >expect &&
113 git rev-parse --verify HEAD >actual &&
114 test_cmp expect actual
117 test_expect_success '...main' '
118 git checkout another &&
119 git checkout ...main &&
121 git rev-parse --verify main^ >expect &&
122 git rev-parse --verify HEAD >actual &&
123 test_cmp expect actual
126 test_expect_success 'main...' '
127 git checkout another &&
128 git checkout main... &&
130 git rev-parse --verify main^ >expect &&
131 git rev-parse --verify HEAD >actual &&
132 test_cmp expect actual
135 test_expect_success '"checkout -" works after a rebase A' '
136 git checkout main &&
137 git checkout other &&
138 git rebase main &&
139 git checkout - &&
140 test_cmp_symbolic_HEAD_ref main
143 test_expect_success '"checkout -" works after a rebase A B' '
144 git branch moodle main~1 &&
145 git checkout main &&
146 git checkout other &&
147 git rebase main moodle &&
148 git checkout - &&
149 test_cmp_symbolic_HEAD_ref main
152 test_expect_success '"checkout -" works after a rebase -i A' '
153 git checkout main &&
154 git checkout other &&
155 git rebase -i main &&
156 git checkout - &&
157 test_cmp_symbolic_HEAD_ref main
160 test_expect_success '"checkout -" works after a rebase -i A B' '
161 git branch foodle main~1 &&
162 git checkout main &&
163 git checkout other &&
164 git rebase main foodle &&
165 git checkout - &&
166 test_cmp_symbolic_HEAD_ref main
169 test_done