push: create new get_upstream_ref() helper
This code is duplicated among multiple functions. No functional changes. Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Felipe Contreras committed
May 31, 2021 at 14:51 UTC
533e0325abcf51d6b3ff689dfacf8fa95e3b6da1
1 file changed
+18
-18
builtin/push.c
+18
-18
@@ -185,29 +185,37 @@ static const char message_detached_head_die[] =
185
"\n"
186
" git push %s HEAD:<name-of-remote-branch>\n");
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)
189
{
191
- if (!branch)
192
- die(_(message_detached_head_die), remote->name);
190
if (!branch->merge_nr || !branch->merge || !branch->remote_name)
191
die(_("The current branch %s has no upstream branch.\n"
192
"To push the current branch and set the remote as upstream, use\n"
193
"\n"
194
" git push --set-upstream %s %s\n"),
195
branch->name,
199
- remote->name,
196
+ remote_name,
197
branch->name);
198
if (branch->merge_nr != 1)
199
die(_("The current branch %s has multiple upstream branches, "
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);
212
if (!same_remote)
213
die(_("You are pushing to remote '%s', which is not the upstream of\n"
214
"your current branch '%s', without telling me what to push\n"
215
"to update which remote branch."),
216
remote->name, branch->name);
217
210
- refspec_appendf(&rs, "%s:%s", branch->refname, branch->merge[0]->src);
218
+ refspec_appendf(&rs, "%s:%s", branch->refname, upstream_ref);
219
}
220
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
231
die(_(message_detached_head_die), remote->name);
232
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
238
/* Additional safety */
239
- if (strcmp(branch->refname, branch->merge[0]->src))
239
+ if (strcmp(branch->refname, upstream_ref))
240
die_push_simple(branch, remote);
241
}
242
refspec_appendf(&rs, "%s:%s", branch->refname, branch->refname);