Skip to content

Commit 533e032

Browse files
felipecgitster
authored andcommitted
push: create new get_upstream_ref() helper
This code is duplicated among multiple functions. No functional changes. Signed-off-by: Felipe Contreras <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 90cfb26 commit 533e032

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

builtin/push.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

213221
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
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

Comments
 (0)