Git fork
1#!/bin/sh
2
3test_description='test <branch>@{upstream} syntax'
4
5GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8. ./test-lib.sh
9
10
11test_expect_success 'setup' '
12
13 test_commit 1 &&
14 git checkout -b side &&
15 test_commit 2 &&
16 git checkout main &&
17 git clone . clone &&
18 test_commit 3 &&
19 (cd clone &&
20 test_commit 4 &&
21 git branch --track my-side origin/side &&
22 git branch --track local-main main &&
23 git branch --track fun@ny origin/side &&
24 git branch --track @funny origin/side &&
25 git branch --track funny@ origin/side &&
26 git remote add -t main main-only .. &&
27 git fetch main-only &&
28 git branch bad-upstream &&
29 git config branch.bad-upstream.remote main-only &&
30 git config branch.bad-upstream.merge refs/heads/side
31 )
32'
33
34commit_subject () {
35 (cd clone &&
36 git show -s --pretty=tformat:%s "$@")
37}
38
39error_message () {
40 (cd clone &&
41 test_must_fail git rev-parse --verify "$@" 2>../error)
42}
43
44test_expect_success '@{upstream} resolves to correct full name' '
45 echo refs/remotes/origin/main >expect &&
46 git -C clone rev-parse --symbolic-full-name @{upstream} >actual &&
47 test_cmp expect actual &&
48 git -C clone rev-parse --symbolic-full-name @{UPSTREAM} >actual &&
49 test_cmp expect actual &&
50 git -C clone rev-parse --symbolic-full-name @{UpSTReam} >actual &&
51 test_cmp expect actual
52'
53
54test_expect_success '@{u} resolves to correct full name' '
55 echo refs/remotes/origin/main >expect &&
56 git -C clone rev-parse --symbolic-full-name @{u} >actual &&
57 test_cmp expect actual &&
58 git -C clone rev-parse --symbolic-full-name @{U} >actual &&
59 test_cmp expect actual
60'
61
62test_expect_success 'my-side@{upstream} resolves to correct full name' '
63 echo refs/remotes/origin/side >expect &&
64 git -C clone rev-parse --symbolic-full-name my-side@{u} >actual &&
65 test_cmp expect actual
66'
67
68test_expect_success 'upstream of branch with @ in middle' '
69 git -C clone rev-parse --symbolic-full-name fun@ny@{u} >actual &&
70 echo refs/remotes/origin/side >expect &&
71 test_cmp expect actual &&
72 git -C clone rev-parse --symbolic-full-name fun@ny@{U} >actual &&
73 test_cmp expect actual
74'
75
76test_expect_success 'upstream of branch with @ at start' '
77 git -C clone rev-parse --symbolic-full-name @funny@{u} >actual &&
78 echo refs/remotes/origin/side >expect &&
79 test_cmp expect actual
80'
81
82test_expect_success 'upstream of branch with @ at end' '
83 git -C clone rev-parse --symbolic-full-name funny@@{u} >actual &&
84 echo refs/remotes/origin/side >expect &&
85 test_cmp expect actual
86'
87
88test_expect_success 'refs/heads/my-side@{upstream} does not resolve to my-side{upstream}' '
89 test_must_fail git -C clone rev-parse --symbolic-full-name refs/heads/my-side@{upstream}
90'
91
92test_expect_success 'my-side@{u} resolves to correct commit' '
93 git checkout side &&
94 test_commit 5 &&
95 (cd clone && git fetch) &&
96 echo 2 >expect &&
97 commit_subject my-side >actual &&
98 test_cmp expect actual &&
99 echo 5 >expect &&
100 commit_subject my-side@{u} >actual &&
101 test_cmp expect actual
102'
103
104test_expect_success 'not-tracking@{u} fails' '
105 test_must_fail git -C clone rev-parse --symbolic-full-name non-tracking@{u} &&
106 (cd clone && git checkout --no-track -b non-tracking) &&
107 test_must_fail git -C clone rev-parse --symbolic-full-name non-tracking@{u}
108'
109
110test_expect_success '<branch>@{u}@{1} resolves correctly' '
111 test_commit 6 &&
112 (cd clone && git fetch) &&
113 echo 5 >expect &&
114 commit_subject my-side@{u}@{1} >actual &&
115 test_cmp expect actual &&
116 commit_subject my-side@{U}@{1} >actual &&
117 test_cmp expect actual
118'
119
120test_expect_success '@{u} without specifying branch fails on a detached HEAD' '
121 git checkout HEAD^0 &&
122 test_must_fail git rev-parse @{u} &&
123 test_must_fail git rev-parse @{U}
124'
125
126test_expect_success 'checkout -b new my-side@{u} forks from the same' '
127(
128 cd clone &&
129 git checkout -b new my-side@{u} &&
130 git rev-parse --symbolic-full-name my-side@{u} >expect &&
131 git rev-parse --symbolic-full-name new@{u} >actual &&
132 test_cmp expect actual
133)
134'
135
136test_expect_success 'merge my-side@{u} records the correct name' '
137(
138 cd clone &&
139 git checkout main &&
140 test_might_fail git branch -D new &&
141 git branch -t new my-side@{u} &&
142 git merge -s ours new@{u} &&
143 git show -s --pretty=tformat:%s >actual &&
144 echo "Merge remote-tracking branch ${SQ}origin/side${SQ}" >expect &&
145 test_cmp expect actual
146)
147'
148
149test_expect_success 'branch -d other@{u}' '
150 git checkout -t -b other main &&
151 git branch -d @{u} &&
152 git for-each-ref refs/heads/main >actual &&
153 test_must_be_empty actual
154'
155
156test_expect_success 'checkout other@{u}' '
157 git branch -f main HEAD &&
158 git checkout -t -b another main &&
159 git checkout @{u} &&
160 git symbolic-ref HEAD >actual &&
161 echo refs/heads/main >expect &&
162 test_cmp expect actual
163'
164
165test_expect_success 'branch@{u} works when tracking a local branch' '
166 echo refs/heads/main >expect &&
167 git -C clone rev-parse --symbolic-full-name local-main@{u} >actual &&
168 test_cmp expect actual
169'
170
171test_expect_success 'branch@{u} error message when no upstream' '
172 cat >expect <<-EOF &&
173 fatal: no upstream configured for branch ${SQ}non-tracking${SQ}
174 EOF
175 error_message non-tracking@{u} &&
176 test_cmp expect error
177'
178
179test_expect_success '@{u} error message when no upstream' '
180 cat >expect <<-EOF &&
181 fatal: no upstream configured for branch ${SQ}main${SQ}
182 EOF
183 test_must_fail git rev-parse --verify @{u} 2>actual &&
184 test_cmp expect actual
185'
186
187test_expect_success '@{u} silent error when no upstream' '
188 test_must_fail git rev-parse --verify --quiet @{u} 2>actual &&
189 test_must_be_empty actual
190'
191
192test_expect_success 'branch@{u} error message with misspelt branch' '
193 cat >expect <<-EOF &&
194 fatal: no such branch: ${SQ}no-such-branch${SQ}
195 EOF
196 error_message no-such-branch@{u} &&
197 test_cmp expect error
198'
199
200test_expect_success '@{u} error message when not on a branch' '
201 cat >expect <<-EOF &&
202 fatal: HEAD does not point to a branch
203 EOF
204 git checkout HEAD^0 &&
205 test_must_fail git rev-parse --verify @{u} 2>actual &&
206 test_cmp expect actual
207'
208
209test_expect_success 'branch@{u} error message if upstream branch not fetched' '
210 cat >expect <<-EOF &&
211 fatal: upstream branch ${SQ}refs/heads/side${SQ} not stored as a remote-tracking branch
212 EOF
213 error_message bad-upstream@{u} &&
214 test_cmp expect error
215'
216
217test_expect_success 'pull works when tracking a local branch' '
218(
219 cd clone &&
220 git checkout local-main &&
221 git pull
222)
223'
224
225# makes sense if the previous one succeeded
226test_expect_success '@{u} works when tracking a local branch' '
227 echo refs/heads/main >expect &&
228 git -C clone rev-parse --symbolic-full-name @{u} >actual &&
229 test_cmp expect actual
230'
231
232test_expect_success 'log -g other@{u}' '
233 commit=$(git rev-parse HEAD) &&
234 cat >expect <<-EOF &&
235 commit $commit
236 Reflog: main@{0} (C O Mitter <committer@example.com>)
237 Reflog message: branch: Created from HEAD
238 Author: A U Thor <author@example.com>
239 Date: Thu Apr 7 15:15:13 2005 -0700
240
241 3
242 EOF
243 git log -1 -g other@{u} >actual &&
244 test_cmp expect actual
245'
246
247test_expect_success 'log -g other@{u}@{now}' '
248 commit=$(git rev-parse HEAD) &&
249 cat >expect <<-EOF &&
250 commit $commit
251 Reflog: main@{Thu Apr 7 15:17:13 2005 -0700} (C O Mitter <committer@example.com>)
252 Reflog message: branch: Created from HEAD
253 Author: A U Thor <author@example.com>
254 Date: Thu Apr 7 15:15:13 2005 -0700
255
256 3
257 EOF
258 git log -1 -g other@{u}@{now} >actual &&
259 test_cmp expect actual
260'
261
262test_expect_success '@{reflog}-parsing does not look beyond colon' '
263 echo content >@{yesterday} &&
264 git add @{yesterday} &&
265 git commit -m "funny reflog file" &&
266 git hash-object @{yesterday} >expect &&
267 git rev-parse HEAD:@{yesterday} >actual &&
268 test_cmp expect actual
269'
270
271test_expect_success '@{upstream}-parsing does not look beyond colon' '
272 echo content >@{upstream} &&
273 git add @{upstream} &&
274 git commit -m "funny upstream file" &&
275 git hash-object @{upstream} >expect &&
276 git rev-parse HEAD:@{upstream} >actual &&
277 test_cmp expect actual
278'
279
280test_done