t8*: adjust the references to the default branch name "main"
[git] / t / t3204-branch-name-interpretation.sh
1 #!/bin/sh
2
3 test_description='interpreting exotic branch name arguments
4
5 Branch name arguments are usually names which are taken to be inside of
6 refs/heads/, but we interpret some magic syntax like @{-1}, @{upstream}, etc.
7 This script aims to check the behavior of those corner cases.
8 '
9 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
10 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
11
12 . ./test-lib.sh
13
14 expect_branch() {
15         git log -1 --format=%s "$1" >actual &&
16         echo "$2" >expect &&
17         test_cmp expect actual
18 }
19
20 expect_deleted() {
21         test_must_fail git rev-parse --verify "$1"
22 }
23
24 test_expect_success 'set up repo' '
25         test_commit one &&
26         test_commit two &&
27         git remote add origin foo.git
28 '
29
30 test_expect_success 'update branch via @{-1}' '
31         git branch previous one &&
32
33         git checkout previous &&
34         git checkout main &&
35
36         git branch -f @{-1} two &&
37         expect_branch previous two
38 '
39
40 test_expect_success 'update branch via local @{upstream}' '
41         git branch local one &&
42         git branch --set-upstream-to=local &&
43
44         git branch -f @{upstream} two &&
45         expect_branch local two
46 '
47
48 test_expect_success 'disallow updating branch via remote @{upstream}' '
49         git update-ref refs/remotes/origin/remote one &&
50         git branch --set-upstream-to=origin/remote &&
51
52         test_must_fail git branch -f @{upstream} two
53 '
54
55 test_expect_success 'create branch with pseudo-qualified name' '
56         git branch refs/heads/qualified two &&
57         expect_branch refs/heads/refs/heads/qualified two
58 '
59
60 test_expect_success 'delete branch via @{-1}' '
61         git branch previous-del &&
62
63         git checkout previous-del &&
64         git checkout main &&
65
66         git branch -D @{-1} &&
67         expect_deleted previous-del
68 '
69
70 test_expect_success 'delete branch via local @{upstream}' '
71         git branch local-del &&
72         git branch --set-upstream-to=local-del &&
73
74         git branch -D @{upstream} &&
75         expect_deleted local-del
76 '
77
78 test_expect_success 'delete branch via remote @{upstream}' '
79         git update-ref refs/remotes/origin/remote-del two &&
80         git branch --set-upstream-to=origin/remote-del &&
81
82         git branch -r -D @{upstream} &&
83         expect_deleted origin/remote-del
84 '
85
86 # Note that we create two oddly named local branches here. We want to make
87 # sure that we do not accidentally delete either of them, even if
88 # shorten_unambiguous_ref() tweaks the name to avoid ambiguity.
89 test_expect_success 'delete @{upstream} expansion matches -r option' '
90         git update-ref refs/remotes/origin/remote-del two &&
91         git branch --set-upstream-to=origin/remote-del &&
92         git update-ref refs/heads/origin/remote-del two &&
93         git update-ref refs/heads/remotes/origin/remote-del two &&
94
95         test_must_fail git branch -D @{upstream} &&
96         expect_branch refs/heads/origin/remote-del two &&
97         expect_branch refs/heads/remotes/origin/remote-del two
98 '
99
100 test_expect_success 'disallow deleting remote branch via @{-1}' '
101         git update-ref refs/remotes/origin/previous one &&
102
103         git checkout -b origin/previous two &&
104         git checkout main &&
105
106         test_must_fail git branch -r -D @{-1} &&
107         expect_branch refs/remotes/origin/previous one &&
108         expect_branch refs/heads/origin/previous two
109 '
110
111 # The thing we are testing here is that "@" is the real branch refs/heads/@,
112 # and not refs/heads/HEAD. These tests should not imply that refs/heads/@ is a
113 # sane thing, but it _is_ technically allowed for now. If we disallow it, these
114 # can be switched to test_must_fail.
115 test_expect_success 'create branch named "@"' '
116         git branch -f @ one &&
117         expect_branch refs/heads/@ one
118 '
119
120 test_expect_success 'delete branch named "@"' '
121         git update-ref refs/heads/@ two &&
122         git branch -D @ &&
123         expect_deleted refs/heads/@
124 '
125
126 test_expect_success 'checkout does not treat remote @{upstream} as a branch' '
127         git update-ref refs/remotes/origin/checkout one &&
128         git branch --set-upstream-to=origin/checkout &&
129         git update-ref refs/heads/origin/checkout two &&
130         git update-ref refs/heads/remotes/origin/checkout two &&
131
132         git checkout @{upstream} &&
133         expect_branch HEAD one
134 '
135
136 test_done