@@ -185,29 +185,37 @@ 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 );
201
+
202
+ return branch -> merge [0 ]-> src ;
203
+ }
204
+
205
+ static void setup_push_upstream (struct remote * remote , struct branch * branch ,
206
+ int same_remote )
207
+ {
208
+ const char * upstream_ref ;
209
+ if (!branch )
210
+ die (_ (message_detached_head_die ), remote -> name );
211
+ upstream_ref = get_upstream_ref (branch , remote -> name );
204
212
if (!same_remote )
205
213
die (_ ("You are pushing to remote '%s', which is not the upstream of\n"
206
214
"your current branch '%s', without telling me what to push\n"
207
215
"to update which remote branch." ),
208
216
remote -> name , branch -> name );
209
217
210
- refspec_appendf (& rs , "%s:%s" , branch -> refname , branch -> merge [ 0 ] -> src );
218
+ refspec_appendf (& rs , "%s:%s" , branch -> refname , upstream_ref );
211
219
}
212
220
213
221
static void setup_push_current (struct remote * remote , struct branch * branch )
@@ -223,20 +231,12 @@ static void setup_push_simple(struct remote *remote, struct branch *branch, int
223
231
die (_ (message_detached_head_die ), remote -> name );
224
232
225
233
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 );
234
+ const char * upstream_ref ;
235
+
236
+ upstream_ref = get_upstream_ref (branch , remote -> name );
237
237
238
238
/* Additional safety */
239
- if (strcmp (branch -> refname , branch -> merge [ 0 ] -> src ))
239
+ if (strcmp (branch -> refname , upstream_ref ))
240
240
die_push_simple (branch , remote );
241
241
}
242
242
refspec_appendf (& rs , "%s:%s" , branch -> refname , branch -> refname );
0 commit comments