]>
Commit | Line | Data |
---|---|---|
c479ea36 NTND |
1 | #!/bin/sh |
2 | ||
3 | test_description='switch basic functionality' | |
4 | ||
883b98ef | 5 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
334afbc7 JS |
6 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
7 | ||
c479ea36 NTND |
8 | . ./test-lib.sh |
9 | ||
10 | test_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 | ||
19 | test_expect_success 'switch branch no arguments' ' | |
20 | test_must_fail git switch | |
21 | ' | |
22 | ||
23 | test_expect_success 'switch branch' ' | |
24 | git switch first-branch && | |
25 | test_path_is_missing second.t | |
26 | ' | |
27 | ||
28 | test_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 | ||
35 | test_expect_success 'switch and detach current branch' ' | |
883b98ef JS |
36 | test_when_finished git switch main && |
37 | git switch main && | |
c479ea36 NTND |
38 | git switch --detach && |
39 | test_must_fail git symbolic-ref HEAD | |
40 | ' | |
41 | ||
42 | test_expect_success 'switch and create branch' ' | |
883b98ef JS |
43 | test_when_finished git switch main && |
44 | git switch -c temp main^ && | |
45 | test_cmp_rev main^ refs/heads/temp && | |
c479ea36 NTND |
46 | echo refs/heads/temp >expected-branch && |
47 | git symbolic-ref HEAD >actual-branch && | |
48 | test_cmp expected-branch actual-branch | |
49 | ' | |
50 | ||
51 | test_expect_success 'force create branch from HEAD' ' | |
883b98ef JS |
52 | test_when_finished git switch main && |
53 | git switch --detach main && | |
c479ea36 NTND |
54 | test_must_fail git switch -c temp && |
55 | git switch -C temp && | |
883b98ef | 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 | ||
62 | test_expect_success 'new orphan branch from empty' ' | |
883b98ef | 63 | test_when_finished git switch main && |
c479ea36 NTND |
64 | test_must_fail git switch --orphan new-orphan HEAD && |
65 | git switch --orphan new-orphan && | |
66 | test_commit orphan && | |
67 | git cat-file commit refs/heads/new-orphan >commit && | |
68 | ! grep ^parent commit && | |
69 | git ls-files >tracked-files && | |
70 | echo orphan.t >expected && | |
71 | test_cmp expected tracked-files | |
72 | ' | |
73 | ||
8d3e33da | 74 | test_expect_success 'orphan branch works with --discard-changes' ' |
883b98ef | 75 | test_when_finished git switch main && |
8d3e33da | 76 | echo foo >foo.txt && |
77 | git switch --discard-changes --orphan new-orphan2 && | |
78 | git ls-files >tracked-files && | |
79 | test_must_be_empty tracked-files | |
80 | ' | |
81 | ||
c479ea36 | 82 | test_expect_success 'switching ignores file of same branch name' ' |
883b98ef | 83 | test_when_finished git switch main && |
c479ea36 NTND |
84 | : >first-branch && |
85 | git switch first-branch && | |
86 | echo refs/heads/first-branch >expected && | |
87 | git symbolic-ref HEAD >actual && | |
88 | test_cmp expected actual | |
89 | ' | |
90 | ||
64f1f58f | 91 | test_expect_success 'guess and create branch' ' |
883b98ef | 92 | test_when_finished git switch main && |
c479ea36 | 93 | test_must_fail git switch --no-guess foo && |
64f1f58f DL |
94 | test_config checkout.guess false && |
95 | test_must_fail git switch foo && | |
96 | test_config checkout.guess true && | |
c479ea36 NTND |
97 | git switch foo && |
98 | echo refs/heads/foo >expected && | |
99 | git symbolic-ref HEAD >actual && | |
100 | test_cmp expected actual | |
101 | ' | |
102 | ||
103 | test_expect_success 'not switching when something is in progress' ' | |
104 | test_when_finished rm -f .git/MERGE_HEAD && | |
105 | # fake a merge-in-progress | |
106 | cp .git/HEAD .git/MERGE_HEAD && | |
107 | test_must_fail git switch -d @^ | |
108 | ' | |
109 | ||
d3115660 JS |
110 | test_expect_success 'tracking info copied with autoSetupMerge=inherit' ' |
111 | # default config does not copy tracking info | |
112 | git switch -c foo-no-inherit foo && | |
113 | test_cmp_config "" --default "" branch.foo-no-inherit.remote && | |
114 | test_cmp_config "" --default "" branch.foo-no-inherit.merge && | |
115 | # with --track=inherit, we copy tracking info from foo | |
116 | git switch --track=inherit -c foo2 foo && | |
117 | test_cmp_config origin branch.foo2.remote && | |
118 | test_cmp_config refs/heads/foo branch.foo2.merge && | |
119 | # with autoSetupMerge=inherit, we do the same | |
120 | test_config branch.autoSetupMerge inherit && | |
121 | git switch -c foo3 foo && | |
122 | test_cmp_config origin branch.foo3.remote && | |
123 | test_cmp_config refs/heads/foo branch.foo3.merge && | |
124 | # with --track, we override autoSetupMerge | |
125 | git switch --track -c foo4 foo && | |
126 | test_cmp_config . branch.foo4.remote && | |
127 | test_cmp_config refs/heads/foo branch.foo4.merge && | |
128 | # and --track=direct does as well | |
129 | git switch --track=direct -c foo5 foo && | |
130 | test_cmp_config . branch.foo5.remote && | |
131 | test_cmp_config refs/heads/foo branch.foo5.merge && | |
132 | # no tracking info to inherit from main | |
133 | git switch -c main2 main && | |
134 | test_cmp_config "" --default "" branch.main2.remote && | |
135 | test_cmp_config "" --default "" branch.main2.merge | |
136 | ' | |
137 | ||
c479ea36 | 138 | test_done |