branch: add flags and config to inherit tracking
[git/debian.git] / t / t2060-switch.sh
blobebb961be293ef935d88c3ad3b8056725d248684b
1 #!/bin/sh
3 test_description='switch basic functionality'
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 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
19 test_expect_success 'switch branch no arguments' '
20 test_must_fail git switch
23 test_expect_success 'switch branch' '
24 git switch first-branch &&
25 test_path_is_missing second.t
28 test_expect_success 'switch and detach' '
29 test_when_finished git switch main &&
30 test_must_fail git switch main^{commit} &&
31 git switch --detach main^{commit} &&
32 test_must_fail git symbolic-ref HEAD
35 test_expect_success 'switch and detach current branch' '
36 test_when_finished git switch main &&
37 git switch main &&
38 git switch --detach &&
39 test_must_fail git symbolic-ref HEAD
42 test_expect_success 'switch and create branch' '
43 test_when_finished git switch main &&
44 git switch -c temp main^ &&
45 test_cmp_rev main^ refs/heads/temp &&
46 echo refs/heads/temp >expected-branch &&
47 git symbolic-ref HEAD >actual-branch &&
48 test_cmp expected-branch actual-branch
51 test_expect_success 'force create branch from HEAD' '
52 test_when_finished git switch main &&
53 git switch --detach main &&
54 test_must_fail git switch -c temp &&
55 git switch -C temp &&
56 test_cmp_rev main refs/heads/temp &&
57 echo refs/heads/temp >expected-branch &&
58 git symbolic-ref HEAD >actual-branch &&
59 test_cmp expected-branch actual-branch
62 test_expect_success 'new orphan branch from empty' '
63 test_when_finished git switch main &&
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
74 test_expect_success 'orphan branch works with --discard-changes' '
75 test_when_finished git switch main &&
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
82 test_expect_success 'switching ignores file of same branch name' '
83 test_when_finished git switch main &&
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
91 test_expect_success 'guess and create branch' '
92 test_when_finished git switch main &&
93 test_must_fail git switch --no-guess foo &&
94 test_config checkout.guess false &&
95 test_must_fail git switch foo &&
96 test_config checkout.guess true &&
97 git switch foo &&
98 echo refs/heads/foo >expected &&
99 git symbolic-ref HEAD >actual &&
100 test_cmp expected actual
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 @^
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
138 test_done