]> git.ipfire.org Git - thirdparty/git.git/blame - t/t2060-switch.sh
The third batch
[thirdparty/git.git] / t / t2060-switch.sh
CommitLineData
c479ea36
NTND
1#!/bin/sh
2
3test_description='switch basic functionality'
4
883b98ef 5GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
6export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
c479ea36
NTND
8. ./test-lib.sh
9
10test_expect_success 'setup' '
11 test_commit first &&
12 git branch first-branch &&
13 test_commit second &&
14 test_commit third &&
15 git remote add origin nohost:/nopath &&
16 git update-ref refs/remotes/origin/foo first-branch
17'
18
19test_expect_success 'switch branch no arguments' '
20 test_must_fail git switch
21'
22
23test_expect_success 'switch branch' '
24 git switch first-branch &&
25 test_path_is_missing second.t
26'
27
28test_expect_success 'switch and detach' '
883b98ef
JS
29 test_when_finished git switch main &&
30 test_must_fail git switch main^{commit} &&
31 git switch --detach main^{commit} &&
c479ea36
NTND
32 test_must_fail git symbolic-ref HEAD
33'
34
808213ba
AH
35test_expect_success 'suggestion to detach' '
36 test_must_fail git switch main^{commit} 2>stderr &&
37 grep "try again with the --detach option" stderr
38'
39
40test_expect_success 'suggestion to detach is suppressed with advice.suggestDetachingHead=false' '
41 test_config advice.suggestDetachingHead false &&
42 test_must_fail git switch main^{commit} 2>stderr &&
43 ! grep "try again with the --detach option" stderr
44'
45
c479ea36 46test_expect_success 'switch and detach current branch' '
883b98ef
JS
47 test_when_finished git switch main &&
48 git switch main &&
c479ea36
NTND
49 git switch --detach &&
50 test_must_fail git symbolic-ref HEAD
51'
52
53test_expect_success 'switch and create branch' '
883b98ef
JS
54 test_when_finished git switch main &&
55 git switch -c temp main^ &&
56 test_cmp_rev main^ refs/heads/temp &&
c479ea36
NTND
57 echo refs/heads/temp >expected-branch &&
58 git symbolic-ref HEAD >actual-branch &&
59 test_cmp expected-branch actual-branch
60'
61
62test_expect_success 'force create branch from HEAD' '
883b98ef
JS
63 test_when_finished git switch main &&
64 git switch --detach main &&
c479ea36
NTND
65 test_must_fail git switch -c temp &&
66 git switch -C temp &&
883b98ef 67 test_cmp_rev main refs/heads/temp &&
c479ea36
NTND
68 echo refs/heads/temp >expected-branch &&
69 git symbolic-ref HEAD >actual-branch &&
70 test_cmp expected-branch actual-branch
71'
72
73test_expect_success 'new orphan branch from empty' '
883b98ef 74 test_when_finished git switch main &&
c479ea36
NTND
75 test_must_fail git switch --orphan new-orphan HEAD &&
76 git switch --orphan new-orphan &&
77 test_commit orphan &&
78 git cat-file commit refs/heads/new-orphan >commit &&
79 ! grep ^parent commit &&
80 git ls-files >tracked-files &&
81 echo orphan.t >expected &&
82 test_cmp expected tracked-files
83'
84
8d3e33da 85test_expect_success 'orphan branch works with --discard-changes' '
883b98ef 86 test_when_finished git switch main &&
8d3e33da 87 echo foo >foo.txt &&
88 git switch --discard-changes --orphan new-orphan2 &&
89 git ls-files >tracked-files &&
90 test_must_be_empty tracked-files
91'
92
c479ea36 93test_expect_success 'switching ignores file of same branch name' '
883b98ef 94 test_when_finished git switch main &&
c479ea36
NTND
95 : >first-branch &&
96 git switch first-branch &&
97 echo refs/heads/first-branch >expected &&
98 git symbolic-ref HEAD >actual &&
99 test_cmp expected actual
100'
101
64f1f58f 102test_expect_success 'guess and create branch' '
883b98ef 103 test_when_finished git switch main &&
c479ea36 104 test_must_fail git switch --no-guess foo &&
64f1f58f
DL
105 test_config checkout.guess false &&
106 test_must_fail git switch foo &&
107 test_config checkout.guess true &&
c479ea36
NTND
108 git switch foo &&
109 echo refs/heads/foo >expected &&
110 git symbolic-ref HEAD >actual &&
111 test_cmp expected actual
112'
113
114test_expect_success 'not switching when something is in progress' '
115 test_when_finished rm -f .git/MERGE_HEAD &&
116 # fake a merge-in-progress
117 cp .git/HEAD .git/MERGE_HEAD &&
118 test_must_fail git switch -d @^
119'
120
d3115660
JS
121test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
122 # default config does not copy tracking info
123 git switch -c foo-no-inherit foo &&
124 test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
125 test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
126 # with --track=inherit, we copy tracking info from foo
127 git switch --track=inherit -c foo2 foo &&
128 test_cmp_config origin branch.foo2.remote &&
129 test_cmp_config refs/heads/foo branch.foo2.merge &&
130 # with autoSetupMerge=inherit, we do the same
131 test_config branch.autoSetupMerge inherit &&
132 git switch -c foo3 foo &&
133 test_cmp_config origin branch.foo3.remote &&
134 test_cmp_config refs/heads/foo branch.foo3.merge &&
135 # with --track, we override autoSetupMerge
136 git switch --track -c foo4 foo &&
137 test_cmp_config . branch.foo4.remote &&
138 test_cmp_config refs/heads/foo branch.foo4.merge &&
139 # and --track=direct does as well
140 git switch --track=direct -c foo5 foo &&
141 test_cmp_config . branch.foo5.remote &&
142 test_cmp_config refs/heads/foo branch.foo5.merge &&
143 # no tracking info to inherit from main
144 git switch -c main2 main &&
145 test_cmp_config "" --default "" branch.main2.remote &&
146 test_cmp_config "" --default "" branch.main2.merge
147'
148
894ea945
RJ
149test_expect_success 'switch back when temporarily detached and checked out elsewhere ' '
150 test_when_finished "
151 git worktree remove wt1 ||:
152 git worktree remove wt2 ||:
153 git checkout - ||:
154 git branch -D shared ||:
155 " &&
156 git checkout -b shared &&
157 test_commit shared-first &&
158 HASH1=$(git rev-parse --verify HEAD) &&
159 test_commit shared-second &&
160 test_commit shared-third &&
161 HASH2=$(git rev-parse --verify HEAD) &&
162 git worktree add wt1 -f shared &&
163 git -C wt1 bisect start &&
164 git -C wt1 bisect good $HASH1 &&
165 git -C wt1 bisect bad $HASH2 &&
166 git worktree add wt2 -f shared &&
167 git -C wt2 bisect start &&
168 git -C wt2 bisect good $HASH1 &&
169 git -C wt2 bisect bad $HASH2 &&
170 # we test in both worktrees to ensure that works
171 # as expected with "first" and "next" worktrees
172 test_must_fail git -C wt1 switch shared &&
b23285a9 173 test_must_fail git -C wt1 switch -C shared &&
894ea945
RJ
174 git -C wt1 switch --ignore-other-worktrees shared &&
175 test_must_fail git -C wt2 switch shared &&
b23285a9 176 test_must_fail git -C wt2 switch -C shared &&
894ea945
RJ
177 git -C wt2 switch --ignore-other-worktrees shared
178'
179
c479ea36 180test_done