@@ -185,98 +185,73 @@ static const char message_detached_head_die[] =
185
185
"\n"
186
186
" git push %s HEAD:<name-of-remote-branch>\n" );
187
187
188
- static void setup_push_upstream (struct remote * remote , struct branch * branch ,
189
- int same_remote )
188
+ static const char * get_upstream_ref (struct branch * branch , const char * remote_name )
190
189
{
191
- if (!branch )
192
- die (_ (message_detached_head_die ), remote -> name );
193
190
if (!branch -> merge_nr || !branch -> merge || !branch -> remote_name )
194
191
die (_ ("The current branch %s has no upstream branch.\n"
195
192
"To push the current branch and set the remote as upstream, use\n"
196
193
"\n"
197
194
" git push --set-upstream %s %s\n" ),
198
195
branch -> name ,
199
- remote -> name ,
196
+ remote_name ,
200
197
branch -> name );
201
198
if (branch -> merge_nr != 1 )
202
199
die (_ ("The current branch %s has multiple upstream branches, "
203
200
"refusing to push." ), branch -> name );
204
- if (!same_remote )
205
- die (_ ("You are pushing to remote '%s', which is not the upstream of\n"
206
- "your current branch '%s', without telling me what to push\n"
207
- "to update which remote branch." ),
208
- remote -> name , branch -> name );
209
201
210
- refspec_appendf ( & rs , "%s:%s" , branch -> refname , branch -> merge [0 ]-> src ) ;
202
+ return branch -> merge [0 ]-> src ;
211
203
}
212
204
213
- static void setup_push_current (struct remote * remote , struct branch * branch )
205
+ static void setup_default_push_refspecs (struct remote * remote )
214
206
{
215
- if (!branch )
216
- die (_ (message_detached_head_die ), remote -> name );
217
- refspec_appendf (& rs , "%s:%s" , branch -> refname , branch -> refname );
218
- }
207
+ struct branch * branch ;
208
+ const char * dst ;
209
+ int same_remote ;
219
210
220
- static void setup_push_simple ( struct remote * remote , struct branch * branch , int same_remote )
221
- {
222
- if (! branch )
223
- die ( _ ( message_detached_head_die ), remote -> name ) ;
211
+ switch ( push_default ) {
212
+ case PUSH_DEFAULT_MATCHING :
213
+ refspec_append ( & rs , ":" );
214
+ return ;
224
215
225
- if (same_remote ) {
226
- if (!branch -> merge_nr || !branch -> merge || !branch -> remote_name )
227
- die (_ ("The current branch %s has no upstream branch.\n"
228
- "To push the current branch and set the remote as upstream, use\n"
229
- "\n"
230
- " git push --set-upstream %s %s\n" ),
231
- branch -> name ,
232
- remote -> name ,
233
- branch -> name );
234
- if (branch -> merge_nr != 1 )
235
- die (_ ("The current branch %s has multiple upstream branches, "
236
- "refusing to push." ), branch -> name );
237
-
238
- /* Additional safety */
239
- if (strcmp (branch -> refname , branch -> merge [0 ]-> src ))
240
- die_push_simple (branch , remote );
216
+ case PUSH_DEFAULT_NOTHING :
217
+ die (_ ("You didn't specify any refspecs to push, and "
218
+ "push.default is \"nothing\"." ));
219
+ return ;
220
+ default :
221
+ break ;
241
222
}
242
- refspec_appendf (& rs , "%s:%s" , branch -> refname , branch -> refname );
243
- }
244
223
245
- static int is_same_remote (struct remote * remote )
246
- {
247
- struct remote * fetch_remote = remote_get (NULL );
248
- return (!fetch_remote || fetch_remote == remote );
249
- }
224
+ branch = branch_get (NULL );
225
+ if (!branch )
226
+ die (_ (message_detached_head_die ), remote -> name );
250
227
251
- static void setup_default_push_refspecs (struct remote * remote )
252
- {
253
- struct branch * branch = branch_get (NULL );
254
- int same_remote = is_same_remote (remote );
228
+ dst = branch -> refname ;
229
+ same_remote = !strcmp (remote -> name , remote_for_branch (branch , NULL ));
255
230
256
231
switch (push_default ) {
257
232
default :
258
- case PUSH_DEFAULT_MATCHING :
259
- refspec_append (& rs , ":" );
260
- break ;
261
-
262
233
case PUSH_DEFAULT_UNSPECIFIED :
263
234
case PUSH_DEFAULT_SIMPLE :
264
- setup_push_simple (remote , branch , same_remote );
235
+ if (!same_remote )
236
+ break ;
237
+ if (strcmp (branch -> refname , get_upstream_ref (branch , remote -> name )))
238
+ die_push_simple (branch , remote );
265
239
break ;
266
240
267
241
case PUSH_DEFAULT_UPSTREAM :
268
- setup_push_upstream (remote , branch , same_remote );
242
+ if (!same_remote )
243
+ die (_ ("You are pushing to remote '%s', which is not the upstream of\n"
244
+ "your current branch '%s', without telling me what to push\n"
245
+ "to update which remote branch." ),
246
+ remote -> name , branch -> name );
247
+ dst = get_upstream_ref (branch , remote -> name );
269
248
break ;
270
249
271
250
case PUSH_DEFAULT_CURRENT :
272
- setup_push_current (remote , branch );
273
- break ;
274
-
275
- case PUSH_DEFAULT_NOTHING :
276
- die (_ ("You didn't specify any refspecs to push, and "
277
- "push.default is \"nothing\"." ));
278
251
break ;
279
252
}
253
+
254
+ refspec_appendf (& rs , "%s:%s" , branch -> refname , dst );
280
255
}
281
256
282
257
static const char message_advice_pull_before_push [] =
0 commit comments