Skip to content

Commit 07e230d

Browse files
committed
Merge branch 'fc/push-simple-updates'
Some code and doc clarification around "git push". * fc/push-simple-updates: doc: push: explain default=simple correctly push: remove unused code in setup_push_upstream() push: simplify setup_push_simple() push: reorganize setup_push_simple() push: copy code to setup_push_simple() push: hedge code of default=simple push: rename !triangular to same_remote
2 parents 5d96bcb + 90cfb26 commit 07e230d

File tree

2 files changed

+38
-23
lines changed

2 files changed

+38
-23
lines changed

Documentation/config/push.txt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@ push.default::
2424

2525
* `tracking` - This is a deprecated synonym for `upstream`.
2626

27-
* `simple` - in centralized workflow, work like `upstream` with an
28-
added safety to refuse to push if the upstream branch's name is
29-
different from the local one.
27+
* `simple` - pushes the current branch with the same name on the remote.
3028
+
31-
When pushing to a remote that is different from the remote you normally
32-
pull from, work as `current`. This is the safest option and is suited
33-
for beginners.
29+
If you are working on a centralized workflow (pushing to the same repository you
30+
pull from, which is typically `origin`), then you need to configure an upstream
31+
branch with the same name.
3432
+
35-
This mode has become the default in Git 2.0.
33+
This mode is the default since Git 2.0, and is the safest option suited for
34+
beginners.
3635

3736
* `matching` - push all branches having the same name on both ends.
3837
This makes the repository you are pushing to remember the set of

builtin/push.c

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ static const char message_detached_head_die[] =
186186
" git push %s HEAD:<name-of-remote-branch>\n");
187187

188188
static void setup_push_upstream(struct remote *remote, struct branch *branch,
189-
int triangular, int simple)
189+
int same_remote)
190190
{
191191
if (!branch)
192192
die(_(message_detached_head_die), remote->name);
@@ -201,18 +201,12 @@ static void setup_push_upstream(struct remote *remote, struct branch *branch,
201201
if (branch->merge_nr != 1)
202202
die(_("The current branch %s has multiple upstream branches, "
203203
"refusing to push."), branch->name);
204-
if (triangular)
204+
if (!same_remote)
205205
die(_("You are pushing to remote '%s', which is not the upstream of\n"
206206
"your current branch '%s', without telling me what to push\n"
207207
"to update which remote branch."),
208208
remote->name, branch->name);
209209

210-
if (simple) {
211-
/* Additional safety */
212-
if (strcmp(branch->refname, branch->merge[0]->src))
213-
die_push_simple(branch, remote);
214-
}
215-
216210
refspec_appendf(&rs, "%s:%s", branch->refname, branch->merge[0]->src);
217211
}
218212

@@ -223,16 +217,41 @@ static void setup_push_current(struct remote *remote, struct branch *branch)
223217
refspec_appendf(&rs, "%s:%s", branch->refname, branch->refname);
224218
}
225219

226-
static int is_workflow_triangular(struct remote *remote)
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);
224+
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);
241+
}
242+
refspec_appendf(&rs, "%s:%s", branch->refname, branch->refname);
243+
}
244+
245+
static int is_same_remote(struct remote *remote)
227246
{
228247
struct remote *fetch_remote = remote_get(NULL);
229-
return (fetch_remote && fetch_remote != remote);
248+
return (!fetch_remote || fetch_remote == remote);
230249
}
231250

232251
static void setup_default_push_refspecs(struct remote *remote)
233252
{
234253
struct branch *branch = branch_get(NULL);
235-
int triangular = is_workflow_triangular(remote);
254+
int same_remote = is_same_remote(remote);
236255

237256
switch (push_default) {
238257
default:
@@ -242,14 +261,11 @@ static void setup_default_push_refspecs(struct remote *remote)
242261

243262
case PUSH_DEFAULT_UNSPECIFIED:
244263
case PUSH_DEFAULT_SIMPLE:
245-
if (triangular)
246-
setup_push_current(remote, branch);
247-
else
248-
setup_push_upstream(remote, branch, triangular, 1);
264+
setup_push_simple(remote, branch, same_remote);
249265
break;
250266

251267
case PUSH_DEFAULT_UPSTREAM:
252-
setup_push_upstream(remote, branch, triangular, 0);
268+
setup_push_upstream(remote, branch, same_remote);
253269
break;
254270

255271
case PUSH_DEFAULT_CURRENT:

0 commit comments

Comments
 (0)