Skip to content

Commit 0659866

Browse files
committed
Merge branch 'fc/push-simple-updates-cleanup'
Some more code and doc clarification around "git push". * fc/push-simple-updates-cleanup: push: don't get a full remote object push: only check same_remote when needed push: remove trivial function push: remove redundant check push: factor out the typical case push: get rid of all the setup_push_* functions push: trivial simplifications push: make setup_push_* return the dst push: only get the branch when needed push: factor out null branch check push: split switch cases push: return immediately in trivial switch case push: create new get_upstream_ref() helper
2 parents 07e230d + 7088ce7 commit 0659866

File tree

1 file changed

+34
-59
lines changed

1 file changed

+34
-59
lines changed

builtin/push.c

Lines changed: 34 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -185,98 +185,73 @@ 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);
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);
209201

210-
refspec_appendf(&rs, "%s:%s", branch->refname, branch->merge[0]->src);
202+
return branch->merge[0]->src;
211203
}
212204

213-
static void setup_push_current(struct remote *remote, struct branch *branch)
205+
static void setup_default_push_refspecs(struct remote *remote)
214206
{
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;
219210

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;
224215

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;
241222
}
242-
refspec_appendf(&rs, "%s:%s", branch->refname, branch->refname);
243-
}
244223

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);
250227

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));
255230

256231
switch (push_default) {
257232
default:
258-
case PUSH_DEFAULT_MATCHING:
259-
refspec_append(&rs, ":");
260-
break;
261-
262233
case PUSH_DEFAULT_UNSPECIFIED:
263234
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);
265239
break;
266240

267241
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);
269248
break;
270249

271250
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\"."));
278251
break;
279252
}
253+
254+
refspec_appendf(&rs, "%s:%s", branch->refname, dst);
280255
}
281256

282257
static const char message_advice_pull_before_push[] =

0 commit comments

Comments
 (0)