@@ -185,29 +185,37 @@ static const char message_detached_head_die[] =
185185 "\n"
186186 " git push %s HEAD:<name-of-remote-branch>\n" );
187187
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 )
190189{
191- if (!branch )
192- die (_ (message_detached_head_die ), remote -> name );
193190 if (!branch -> merge_nr || !branch -> merge || !branch -> remote_name )
194191 die (_ ("The current branch %s has no upstream branch.\n"
195192 "To push the current branch and set the remote as upstream, use\n"
196193 "\n"
197194 " git push --set-upstream %s %s\n" ),
198195 branch -> name ,
199- remote -> name ,
196+ remote_name ,
200197 branch -> name );
201198 if (branch -> merge_nr != 1 )
202199 die (_ ("The current branch %s has multiple upstream branches, "
203200 "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 );
204212 if (!same_remote )
205213 die (_ ("You are pushing to remote '%s', which is not the upstream of\n"
206214 "your current branch '%s', without telling me what to push\n"
207215 "to update which remote branch." ),
208216 remote -> name , branch -> name );
209217
210- refspec_appendf (& rs , "%s:%s" , branch -> refname , branch -> merge [ 0 ] -> src );
218+ refspec_appendf (& rs , "%s:%s" , branch -> refname , upstream_ref );
211219}
212220
213221static 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
223231 die (_ (message_detached_head_die ), remote -> name );
224232
225233 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 );
237237
238238 /* Additional safety */
239- if (strcmp (branch -> refname , branch -> merge [ 0 ] -> src ))
239+ if (strcmp (branch -> refname , upstream_ref ))
240240 die_push_simple (branch , remote );
241241 }
242242 refspec_appendf (& rs , "%s:%s" , branch -> refname , branch -> refname );
0 commit comments