]>
Commit | Line | Data |
---|---|---|
a356e8e2 JK |
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 | ' | |
d6c6b108 | 9 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
334afbc7 JS |
10 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
11 | ||
80d32e84 | 12 | TEST_PASSES_SANITIZE_LEAK=true |
a356e8e2 JK |
13 | . ./test-lib.sh |
14 | ||
15 | expect_branch() { | |
16 | git log -1 --format=%s "$1" >actual && | |
17 | echo "$2" >expect && | |
18 | test_cmp expect actual | |
19 | } | |
20 | ||
21 | expect_deleted() { | |
22 | test_must_fail git rev-parse --verify "$1" | |
23 | } | |
24 | ||
25 | test_expect_success 'set up repo' ' | |
26 | test_commit one && | |
27 | test_commit two && | |
28 | git remote add origin foo.git | |
29 | ' | |
30 | ||
31 | test_expect_success 'update branch via @{-1}' ' | |
32 | git branch previous one && | |
33 | ||
34 | git checkout previous && | |
d6c6b108 | 35 | git checkout main && |
a356e8e2 JK |
36 | |
37 | git branch -f @{-1} two && | |
38 | expect_branch previous two | |
39 | ' | |
40 | ||
41 | test_expect_success 'update branch via local @{upstream}' ' | |
42 | git branch local one && | |
43 | git branch --set-upstream-to=local && | |
44 | ||
45 | git branch -f @{upstream} two && | |
46 | expect_branch local two | |
47 | ' | |
48 | ||
7d5c960b | 49 | test_expect_success 'disallow updating branch via remote @{upstream}' ' |
a356e8e2 JK |
50 | git update-ref refs/remotes/origin/remote one && |
51 | git branch --set-upstream-to=origin/remote && | |
52 | ||
53 | test_must_fail git branch -f @{upstream} two | |
54 | ' | |
55 | ||
56 | test_expect_success 'create branch with pseudo-qualified name' ' | |
57 | git branch refs/heads/qualified two && | |
58 | expect_branch refs/heads/refs/heads/qualified two | |
59 | ' | |
60 | ||
cfbd173c RJ |
61 | test_expect_success 'force-copy a branch to itself via @{-1} is no-op' ' |
62 | git branch -t copiable main && | |
63 | git checkout copiable && | |
64 | git checkout - && | |
65 | git branch -C @{-1} copiable && | |
66 | git config --get-all branch.copiable.merge >actual && | |
67 | echo refs/heads/main >expect && | |
68 | test_cmp expect actual | |
69 | ' | |
70 | ||
a356e8e2 JK |
71 | test_expect_success 'delete branch via @{-1}' ' |
72 | git branch previous-del && | |
73 | ||
74 | git checkout previous-del && | |
d6c6b108 | 75 | git checkout main && |
a356e8e2 JK |
76 | |
77 | git branch -D @{-1} && | |
78 | expect_deleted previous-del | |
79 | ' | |
80 | ||
81 | test_expect_success 'delete branch via local @{upstream}' ' | |
82 | git branch local-del && | |
83 | git branch --set-upstream-to=local-del && | |
84 | ||
85 | git branch -D @{upstream} && | |
86 | expect_deleted local-del | |
87 | ' | |
88 | ||
89 | test_expect_success 'delete branch via remote @{upstream}' ' | |
90 | git update-ref refs/remotes/origin/remote-del two && | |
91 | git branch --set-upstream-to=origin/remote-del && | |
92 | ||
93 | git branch -r -D @{upstream} && | |
94 | expect_deleted origin/remote-del | |
95 | ' | |
96 | ||
97 | # Note that we create two oddly named local branches here. We want to make | |
98 | # sure that we do not accidentally delete either of them, even if | |
99 | # shorten_unambiguous_ref() tweaks the name to avoid ambiguity. | |
6b145e01 | 100 | test_expect_success 'delete @{upstream} expansion matches -r option' ' |
a356e8e2 JK |
101 | git update-ref refs/remotes/origin/remote-del two && |
102 | git branch --set-upstream-to=origin/remote-del && | |
103 | git update-ref refs/heads/origin/remote-del two && | |
104 | git update-ref refs/heads/remotes/origin/remote-del two && | |
105 | ||
106 | test_must_fail git branch -D @{upstream} && | |
107 | expect_branch refs/heads/origin/remote-del two && | |
108 | expect_branch refs/heads/remotes/origin/remote-del two | |
109 | ' | |
110 | ||
6b145e01 | 111 | test_expect_success 'disallow deleting remote branch via @{-1}' ' |
a356e8e2 JK |
112 | git update-ref refs/remotes/origin/previous one && |
113 | ||
114 | git checkout -b origin/previous two && | |
d6c6b108 | 115 | git checkout main && |
a356e8e2 JK |
116 | |
117 | test_must_fail git branch -r -D @{-1} && | |
118 | expect_branch refs/remotes/origin/previous one && | |
119 | expect_branch refs/heads/origin/previous two | |
120 | ' | |
121 | ||
122 | # The thing we are testing here is that "@" is the real branch refs/heads/@, | |
123 | # and not refs/heads/HEAD. These tests should not imply that refs/heads/@ is a | |
124 | # sane thing, but it _is_ technically allowed for now. If we disallow it, these | |
125 | # can be switched to test_must_fail. | |
7d5c960b | 126 | test_expect_success 'create branch named "@"' ' |
a356e8e2 JK |
127 | git branch -f @ one && |
128 | expect_branch refs/heads/@ one | |
129 | ' | |
130 | ||
6b145e01 | 131 | test_expect_success 'delete branch named "@"' ' |
a356e8e2 JK |
132 | git update-ref refs/heads/@ two && |
133 | git branch -D @ && | |
134 | expect_deleted refs/heads/@ | |
135 | ' | |
136 | ||
fd4692ff JK |
137 | test_expect_success 'checkout does not treat remote @{upstream} as a branch' ' |
138 | git update-ref refs/remotes/origin/checkout one && | |
139 | git branch --set-upstream-to=origin/checkout && | |
140 | git update-ref refs/heads/origin/checkout two && | |
141 | git update-ref refs/heads/remotes/origin/checkout two && | |
142 | ||
143 | git checkout @{upstream} && | |
144 | expect_branch HEAD one | |
145 | ' | |
146 | ||
0dc4e5c5 RJ |
147 | test_expect_success 'edit-description via @{-1}' ' |
148 | git checkout -b desc-branch && | |
149 | git checkout -b non-desc-branch && | |
150 | write_script editor <<-\EOF && | |
151 | echo "Branch description" >"$1" | |
152 | EOF | |
153 | EDITOR=./editor git branch --edit-description @{-1} && | |
154 | test_must_fail git config branch.non-desc-branch.description && | |
155 | git config branch.desc-branch.description >actual && | |
156 | printf "Branch description\n\n" >expect && | |
157 | test_cmp expect actual | |
158 | ' | |
159 | ||
160 | test_expect_success 'modify branch upstream via "@{-1}" and "@{-1}@{upstream}"' ' | |
161 | git checkout -b upstream-branch && | |
162 | git checkout -b upstream-other -t upstream-branch && | |
163 | git branch --set-upstream-to upstream-other @{-1} && | |
164 | git config branch.upstream-branch.merge >actual && | |
165 | echo "refs/heads/upstream-other" >expect && | |
166 | test_cmp expect actual && | |
167 | git branch --unset-upstream @{-1}@{upstream} && | |
168 | test_must_fail git config branch.upstream-other.merge | |
169 | ' | |
170 | ||
a356e8e2 | 171 | test_done |