The eighteenth batch
[git.git] / t / t5528-push-default.sh
blobbc2bada34c62984c37d73e94b2b76f6959ca2c75
1 #!/bin/sh
3 test_description='check various push.default settings'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7 TEST_PASSES_SANITIZE_LEAK=true
8 . ./test-lib.sh
10 test_expect_success 'setup bare remotes' '
11 git init --bare repo1 &&
12 git remote add parent1 repo1 &&
13 git init --bare repo2 &&
14 git remote add parent2 repo2 &&
15 test_commit one &&
16 git push parent1 HEAD &&
17 git push parent2 HEAD
20 # $1 = local revision
21 # $2 = remote revision (tested to be equal to the local one)
22 # $3 = [optional] repo to check for actual output (repo1 by default)
23 check_pushed_commit () {
24 git log -1 --format='%h %s' "$1" >expect &&
25 git --git-dir="${3:-repo1}" log -1 --format='%h %s' "$2" >actual &&
26 test_cmp expect actual
29 # $1 = push.default value
30 # $2 = expected target branch for the push
31 # $3 = [optional] repo to check for actual output (repo1 by default)
32 test_push_success () {
33 git ${1:+-c} ${1:+push.default="$1"} push &&
34 check_pushed_commit HEAD "$2" "$3"
37 # $1 = push.default value
38 # check that push fails and does not modify any remote branch
39 test_push_failure () {
40 git --git-dir=repo1 log --no-walk --format='%h %s' --all >expect &&
41 test_must_fail git ${1:+-c} ${1:+push.default="$1"} push &&
42 git --git-dir=repo1 log --no-walk --format='%h %s' --all >actual &&
43 test_cmp expect actual
46 # $1 = success or failure
47 # $2 = push.default value
48 # $3 = branch to check for actual output (main or foo)
49 # $4 = [optional] switch to triangular workflow
50 test_pushdefault_workflow () {
51 workflow=central
52 pushdefault=parent1
53 if test -n "${4-}"; then
54 workflow=triangular
55 pushdefault=parent2
57 test_expect_success "push.default = $2 $1 in $workflow workflows" "
58 test_config branch.main.remote parent1 &&
59 test_config branch.main.merge refs/heads/foo &&
60 test_config remote.pushdefault $pushdefault &&
61 test_commit commit-for-$2${4+-triangular} &&
62 test_push_$1 $2 $3 ${4+repo2}
66 test_expect_success '"upstream" pushes to configured upstream' '
67 git checkout main &&
68 test_config branch.main.remote parent1 &&
69 test_config branch.main.merge refs/heads/foo &&
70 test_commit two &&
71 test_push_success upstream foo
74 test_expect_success '"upstream" does not push on unconfigured remote' '
75 git checkout main &&
76 test_unconfig branch.main.remote &&
77 test_commit three &&
78 test_push_failure upstream
81 test_expect_success '"upstream" does not push on unconfigured branch' '
82 git checkout main &&
83 test_config branch.main.remote parent1 &&
84 test_unconfig branch.main.merge &&
85 test_commit four &&
86 test_push_failure upstream
89 test_expect_success '"upstream" does not push when remotes do not match' '
90 git checkout main &&
91 test_config branch.main.remote parent1 &&
92 test_config branch.main.merge refs/heads/foo &&
93 test_config push.default upstream &&
94 test_commit five &&
95 test_must_fail git push parent2
98 test_expect_success '"current" does not push when multiple remotes and none origin' '
99 git checkout main &&
100 test_config push.default current &&
101 test_commit current-multi &&
102 test_must_fail git push
105 test_expect_success '"current" pushes when remote explicitly specified' '
106 git checkout main &&
107 test_config push.default current &&
108 test_commit current-specified &&
109 git push parent1
112 test_expect_success '"current" pushes to origin when no remote specified among multiple' '
113 git checkout main &&
114 test_config remote.origin.url repo1 &&
115 test_config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" &&
116 test_commit current-origin &&
117 test_push_success current main
120 test_expect_success '"current" pushes to single remote even when not specified' '
121 git checkout main &&
122 test_when_finished git remote add parent1 repo1 &&
123 git remote remove parent1 &&
124 test_commit current-implied &&
125 test_push_success current main repo2
128 test_expect_success 'push from/to new branch with non-defaulted remote fails with upstream, matching, current and simple ' '
129 git checkout -b new-branch &&
130 test_push_failure simple &&
131 test_push_failure matching &&
132 test_push_failure upstream &&
133 test_push_failure current
136 test_expect_success 'push from/to new branch fails with upstream and simple ' '
137 git checkout -b new-branch-1 &&
138 test_config branch.new-branch-1.remote parent1 &&
139 test_push_failure simple &&
140 test_push_failure upstream
143 # The behavior here is surprising but not entirely wrong:
144 # - the current branch is used to determine the target remote
145 # - the "matching" push default pushes matching branches, *ignoring* the
146 # current new branch as it does not have upstream tracking
147 # - the default push succeeds
149 # A previous test expected this to fail, but for the wrong reasons:
150 # it expected a fail becaause the branch is new and cannot be pushed, but
151 # in fact it was failing because of an ambiguous remote
153 test_expect_failure 'push from/to new branch fails with matching ' '
154 git checkout -b new-branch-2 &&
155 test_config branch.new-branch-2.remote parent1 &&
156 test_push_failure matching
159 test_expect_success 'push from/to branch with tracking fails with nothing ' '
160 git checkout -b tracked-branch &&
161 test_config branch.tracked-branch.remote parent1 &&
162 test_config branch.tracked-branch.merge refs/heads/tracked-branch &&
163 test_push_failure nothing
166 test_expect_success 'push from/to new branch succeeds with upstream if push.autoSetupRemote' '
167 git checkout -b new-branch-a &&
168 test_config push.autoSetupRemote true &&
169 test_config branch.new-branch-a.remote parent1 &&
170 test_push_success upstream new-branch-a
173 test_expect_success 'push from/to new branch succeeds with simple if push.autoSetupRemote' '
174 git checkout -b new-branch-c &&
175 test_config push.autoSetupRemote true &&
176 test_config branch.new-branch-c.remote parent1 &&
177 test_push_success simple new-branch-c
180 test_expect_success '"matching" fails if none match' '
181 git init --bare empty &&
182 test_must_fail git push empty : 2>actual &&
183 test_grep "Perhaps you should specify a branch" actual
186 test_expect_success 'push ambiguously named branch with upstream, matching and simple' '
187 git checkout -b ambiguous &&
188 test_config branch.ambiguous.remote parent1 &&
189 test_config branch.ambiguous.merge refs/heads/ambiguous &&
190 git tag ambiguous &&
191 test_push_success simple ambiguous &&
192 test_push_success matching ambiguous &&
193 test_push_success upstream ambiguous
196 test_expect_success 'push from/to new branch with current creates remote branch' '
197 test_config branch.new-branch.remote repo1 &&
198 git checkout new-branch &&
199 test_push_success current new-branch
202 test_expect_success 'push to existing branch, with no upstream configured' '
203 test_config branch.main.remote repo1 &&
204 git checkout main &&
205 test_push_failure simple &&
206 test_push_failure upstream
209 test_expect_success 'push to existing branch, upstream configured with same name' '
210 test_config branch.main.remote repo1 &&
211 test_config branch.main.merge refs/heads/main &&
212 git checkout main &&
213 test_commit six &&
214 test_push_success upstream main &&
215 test_commit seven &&
216 test_push_success simple main
219 test_expect_success 'push to existing branch, upstream configured with different name' '
220 test_config branch.main.remote repo1 &&
221 test_config branch.main.merge refs/heads/other-name &&
222 git checkout main &&
223 test_commit eight &&
224 test_push_success upstream other-name &&
225 test_commit nine &&
226 test_push_failure simple &&
227 git --git-dir=repo1 log -1 --format="%h %s" "other-name" >expect-other-name &&
228 test_push_success current main &&
229 git --git-dir=repo1 log -1 --format="%h %s" "other-name" >actual-other-name &&
230 test_cmp expect-other-name actual-other-name
233 # We are on 'main', which integrates with 'foo' from parent1
234 # remote (set in test_pushdefault_workflow helper). Push to
235 # parent1 in centralized, and push to parent2 in triangular workflow.
236 # The parent1 repository has 'main' and 'foo' branches, while
237 # the parent2 repository has only 'main' branch.
239 # test_pushdefault_workflow() arguments:
240 # $1 = success or failure
241 # $2 = push.default value
242 # $3 = branch to check for actual output (main or foo)
243 # $4 = [optional] switch to triangular workflow
245 # update parent1's main (which is not our upstream)
246 test_pushdefault_workflow success current main
248 # update parent1's foo (which is our upstream)
249 test_pushdefault_workflow success upstream foo
251 # upstream is foo which is not the name of the current branch
252 test_pushdefault_workflow failure simple main
254 # main and foo are updated
255 test_pushdefault_workflow success matching main
257 # main is updated
258 test_pushdefault_workflow success current main triangular
260 # upstream mode cannot be used in triangular
261 test_pushdefault_workflow failure upstream foo triangular
263 # in triangular, 'simple' works as 'current' and update the branch
264 # with the same name.
265 test_pushdefault_workflow success simple main triangular
267 # main is updated (parent2 does not have foo)
268 test_pushdefault_workflow success matching main triangular
270 # default tests, when no push-default is specified. This
271 # should behave the same as "simple" in non-triangular
272 # settings, and as "current" otherwise.
274 test_expect_success 'default behavior allows "simple" push' '
275 test_config branch.main.remote parent1 &&
276 test_config branch.main.merge refs/heads/main &&
277 test_config remote.pushdefault parent1 &&
278 test_commit default-main-main &&
279 test_push_success "" main
282 test_expect_success 'default behavior rejects non-simple push' '
283 test_config branch.main.remote parent1 &&
284 test_config branch.main.merge refs/heads/foo &&
285 test_config remote.pushdefault parent1 &&
286 test_commit default-main-foo &&
287 test_push_failure ""
290 test_expect_success 'default triangular behavior acts like "current"' '
291 test_config branch.main.remote parent1 &&
292 test_config branch.main.merge refs/heads/foo &&
293 test_config remote.pushdefault parent2 &&
294 test_commit default-triangular &&
295 test_push_success "" main repo2
298 test_done