1#!/bin/sh
2
3test_description='switch basic functionality'
4
5GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
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' '
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
33'
34
35test_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
40'
41
42test_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
49'
50
51test_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
60'
61
62test_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
72'
73
74test_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
80'
81
82test_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
89'
90
91test_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
101'
102
103test_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
110test_done
111