From 050f76b9af75bf69110d542e0cee569d0112f320 Mon Sep 17 00:00:00 2001 From: Felipe Contreras Date: Mon, 31 May 2021 14:32:31 -0500 Subject: [PATCH 1/7] push: rename !triangular to same_remote The typical case is what git was designed for: distributed remotes. It's only the atypical case--fetching and pushing to the same remote--that we need to keep an eye on. No functional changes. Liked-by: Junio C Hamano Signed-off-by: Felipe Contreras Signed-off-by: Junio C Hamano --- builtin/push.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/builtin/push.c b/builtin/push.c index 194967ed79..06406353ce 100644 --- a/builtin/push.c +++ b/builtin/push.c @@ -186,7 +186,7 @@ static const char message_detached_head_die[] = " git push %s HEAD:\n"); static void setup_push_upstream(struct remote *remote, struct branch *branch, - int triangular, int simple) + int same_remote, int simple) { if (!branch) die(_(message_detached_head_die), remote->name); @@ -201,7 +201,7 @@ static void setup_push_upstream(struct remote *remote, struct branch *branch, if (branch->merge_nr != 1) die(_("The current branch %s has multiple upstream branches, " "refusing to push."), branch->name); - if (triangular) + if (!same_remote) die(_("You are pushing to remote '%s', which is not the upstream of\n" "your current branch '%s', without telling me what to push\n" "to update which remote branch."), @@ -223,16 +223,16 @@ static void setup_push_current(struct remote *remote, struct branch *branch) refspec_appendf(&rs, "%s:%s", branch->refname, branch->refname); } -static int is_workflow_triangular(struct remote *remote) +static int is_same_remote(struct remote *remote) { struct remote *fetch_remote = remote_get(NULL); - return (fetch_remote && fetch_remote != remote); + return (!fetch_remote || fetch_remote == remote); } static void setup_default_push_refspecs(struct remote *remote) { struct branch *branch = branch_get(NULL); - int triangular = is_workflow_triangular(remote); + int same_remote = is_same_remote(remote); switch (push_default) { default: @@ -242,14 +242,14 @@ static void setup_default_push_refspecs(struct remote *remote) case PUSH_DEFAULT_UNSPECIFIED: case PUSH_DEFAULT_SIMPLE: - if (triangular) + if (!same_remote) setup_push_current(remote, branch); else - setup_push_upstream(remote, branch, triangular, 1); + setup_push_upstream(remote, branch, same_remote, 1); break; case PUSH_DEFAULT_UPSTREAM: - setup_push_upstream(remote, branch, triangular, 0); + setup_push_upstream(remote, branch, same_remote, 0); break; case PUSH_DEFAULT_CURRENT: From 3b9fd8361f1ed98f06df24d406b9a618c7f39df1 Mon Sep 17 00:00:00 2001 From: Felipe Contreras Date: Mon, 31 May 2021 14:32:32 -0500 Subject: [PATCH 2/7] push: hedge code of default=simple `simple` is the most important mode so move the relevant code to its own function to make it easier to see what it's doing. Reviewed-by: Elijah Newren Signed-off-by: Felipe Contreras Signed-off-by: Junio C Hamano --- builtin/push.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/builtin/push.c b/builtin/push.c index 06406353ce..48c38fe25a 100644 --- a/builtin/push.c +++ b/builtin/push.c @@ -223,6 +223,14 @@ static void setup_push_current(struct remote *remote, struct branch *branch) refspec_appendf(&rs, "%s:%s", branch->refname, branch->refname); } +static void setup_push_simple(struct remote *remote, struct branch *branch, int same_remote) +{ + if (!same_remote) + setup_push_current(remote, branch); + else + setup_push_upstream(remote, branch, same_remote, 1); +} + static int is_same_remote(struct remote *remote) { struct remote *fetch_remote = remote_get(NULL); @@ -242,10 +250,7 @@ static void setup_default_push_refspecs(struct remote *remote) case PUSH_DEFAULT_UNSPECIFIED: case PUSH_DEFAULT_SIMPLE: - if (!same_remote) - setup_push_current(remote, branch); - else - setup_push_upstream(remote, branch, same_remote, 1); + setup_push_simple(remote, branch, same_remote); break; case PUSH_DEFAULT_UPSTREAM: From d099b9c9c723e8ca974e5ece027165e2824a1b89 Mon Sep 17 00:00:00 2001 From: Felipe Contreras Date: Mon, 31 May 2021 14:32:33 -0500 Subject: [PATCH 3/7] push: copy code to setup_push_simple() In order to avoid doing unnecessary things and simplify it in further patches. In particular moving the additional name safety out of setup_push_upstream() and into setup_push_simple() and thus making both more straightforward. The code is copied exactly as-is; no functional changes. Reviewed-by: Elijah Newren Signed-off-by: Felipe Contreras Signed-off-by: Junio C Hamano --- builtin/push.c | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/builtin/push.c b/builtin/push.c index 48c38fe25a..6a620a90e3 100644 --- a/builtin/push.c +++ b/builtin/push.c @@ -225,10 +225,38 @@ static void setup_push_current(struct remote *remote, struct branch *branch) static void setup_push_simple(struct remote *remote, struct branch *branch, int same_remote) { - if (!same_remote) - setup_push_current(remote, branch); - else - setup_push_upstream(remote, branch, same_remote, 1); + if (!same_remote) { + if (!branch) + die(_(message_detached_head_die), remote->name); + refspec_appendf(&rs, "%s:%s", branch->refname, branch->refname); + } else { + if (!branch) + die(_(message_detached_head_die), remote->name); + if (!branch->merge_nr || !branch->merge || !branch->remote_name) + die(_("The current branch %s has no upstream branch.\n" + "To push the current branch and set the remote as upstream, use\n" + "\n" + " git push --set-upstream %s %s\n"), + branch->name, + remote->name, + branch->name); + if (branch->merge_nr != 1) + die(_("The current branch %s has multiple upstream branches, " + "refusing to push."), branch->name); + if (!same_remote) + die(_("You are pushing to remote '%s', which is not the upstream of\n" + "your current branch '%s', without telling me what to push\n" + "to update which remote branch."), + remote->name, branch->name); + + if (1) { + /* Additional safety */ + if (strcmp(branch->refname, branch->merge[0]->src)) + die_push_simple(branch, remote); + } + + refspec_appendf(&rs, "%s:%s", branch->refname, branch->merge[0]->src); + } } static int is_same_remote(struct remote *remote) From 6b010c80a2596c75d562afa5ab098ffff21bab06 Mon Sep 17 00:00:00 2001 From: Felipe Contreras Date: Mon, 31 May 2021 14:32:34 -0500 Subject: [PATCH 4/7] push: reorganize setup_push_simple() Simply move the code around and remove dead code. In particular the '!same_remote' conditional is a no-op since that part of the code is the same_remote leg of the conditional beforehand. No functional changes. Suggestions-by: Elijah Newren Signed-off-by: Felipe Contreras Signed-off-by: Junio C Hamano --- builtin/push.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/builtin/push.c b/builtin/push.c index 6a620a90e3..972d8e1cfd 100644 --- a/builtin/push.c +++ b/builtin/push.c @@ -225,13 +225,14 @@ static void setup_push_current(struct remote *remote, struct branch *branch) static void setup_push_simple(struct remote *remote, struct branch *branch, int same_remote) { + const char *dst; + + if (!branch) + die(_(message_detached_head_die), remote->name); + if (!same_remote) { - if (!branch) - die(_(message_detached_head_die), remote->name); - refspec_appendf(&rs, "%s:%s", branch->refname, branch->refname); + dst = branch->refname; } else { - if (!branch) - die(_(message_detached_head_die), remote->name); if (!branch->merge_nr || !branch->merge || !branch->remote_name) die(_("The current branch %s has no upstream branch.\n" "To push the current branch and set the remote as upstream, use\n" @@ -243,20 +244,14 @@ static void setup_push_simple(struct remote *remote, struct branch *branch, int if (branch->merge_nr != 1) die(_("The current branch %s has multiple upstream branches, " "refusing to push."), branch->name); - if (!same_remote) - die(_("You are pushing to remote '%s', which is not the upstream of\n" - "your current branch '%s', without telling me what to push\n" - "to update which remote branch."), - remote->name, branch->name); - if (1) { - /* Additional safety */ - if (strcmp(branch->refname, branch->merge[0]->src)) - die_push_simple(branch, remote); - } + /* Additional safety */ + if (strcmp(branch->refname, branch->merge[0]->src)) + die_push_simple(branch, remote); - refspec_appendf(&rs, "%s:%s", branch->refname, branch->merge[0]->src); + dst = branch->merge[0]->src; } + refspec_appendf(&rs, "%s:%s", branch->refname, dst); } static int is_same_remote(struct remote *remote) From b8e8b98647f02e721fc3c1b5db6936ba7c1c1167 Mon Sep 17 00:00:00 2001 From: Felipe Contreras Date: Mon, 31 May 2021 14:32:35 -0500 Subject: [PATCH 5/7] push: simplify setup_push_simple() There's a safety check to make sure branch->refname isn't different from branch->merge[0]->src, otherwise we die(). Therefore we always push to branch->refname. Suggestions-by: Elijah Newren Signed-off-by: Felipe Contreras Signed-off-by: Junio C Hamano --- builtin/push.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/builtin/push.c b/builtin/push.c index 972d8e1cfd..e37c751268 100644 --- a/builtin/push.c +++ b/builtin/push.c @@ -225,14 +225,10 @@ static void setup_push_current(struct remote *remote, struct branch *branch) static void setup_push_simple(struct remote *remote, struct branch *branch, int same_remote) { - const char *dst; - if (!branch) die(_(message_detached_head_die), remote->name); - if (!same_remote) { - dst = branch->refname; - } else { + if (same_remote) { if (!branch->merge_nr || !branch->merge || !branch->remote_name) die(_("The current branch %s has no upstream branch.\n" "To push the current branch and set the remote as upstream, use\n" @@ -248,10 +244,8 @@ static void setup_push_simple(struct remote *remote, struct branch *branch, int /* Additional safety */ if (strcmp(branch->refname, branch->merge[0]->src)) die_push_simple(branch, remote); - - dst = branch->merge[0]->src; } - refspec_appendf(&rs, "%s:%s", branch->refname, dst); + refspec_appendf(&rs, "%s:%s", branch->refname, branch->refname); } static int is_same_remote(struct remote *remote) From 7e6d72bb111b080ef37e26fbdfe080672521c570 Mon Sep 17 00:00:00 2001 From: Felipe Contreras Date: Mon, 31 May 2021 14:32:36 -0500 Subject: [PATCH 6/7] push: remove unused code in setup_push_upstream() Now it's not used for the simple mode. Signed-off-by: Felipe Contreras Signed-off-by: Junio C Hamano --- builtin/push.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/builtin/push.c b/builtin/push.c index e37c751268..29fea70ff1 100644 --- a/builtin/push.c +++ b/builtin/push.c @@ -186,7 +186,7 @@ static const char message_detached_head_die[] = " git push %s HEAD:\n"); static void setup_push_upstream(struct remote *remote, struct branch *branch, - int same_remote, int simple) + int same_remote) { if (!branch) die(_(message_detached_head_die), remote->name); @@ -207,12 +207,6 @@ static void setup_push_upstream(struct remote *remote, struct branch *branch, "to update which remote branch."), remote->name, branch->name); - if (simple) { - /* Additional safety */ - if (strcmp(branch->refname, branch->merge[0]->src)) - die_push_simple(branch, remote); - } - refspec_appendf(&rs, "%s:%s", branch->refname, branch->merge[0]->src); } @@ -271,7 +265,7 @@ static void setup_default_push_refspecs(struct remote *remote) break; case PUSH_DEFAULT_UPSTREAM: - setup_push_upstream(remote, branch, same_remote, 0); + setup_push_upstream(remote, branch, same_remote); break; case PUSH_DEFAULT_CURRENT: From 90cfb2666b5913e0be4ffb84630866287dde4f9a Mon Sep 17 00:00:00 2001 From: Felipe Contreras Date: Mon, 31 May 2021 14:32:37 -0500 Subject: [PATCH 7/7] doc: push: explain default=simple correctly Now that the code has been simplified and it's clear what it's actually doing, update the documentation to reflect that. Namely; the simple mode only barfs when working on a centralized workflow, and there's no configured upstream branch with the same name. Cc: Elijah Newren Signed-off-by: Felipe Contreras Signed-off-by: Junio C Hamano --- Documentation/config/push.txt | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Documentation/config/push.txt b/Documentation/config/push.txt index 21b256e0a4..5dbf343bd0 100644 --- a/Documentation/config/push.txt +++ b/Documentation/config/push.txt @@ -24,15 +24,14 @@ push.default:: * `tracking` - This is a deprecated synonym for `upstream`. -* `simple` - in centralized workflow, work like `upstream` with an - added safety to refuse to push if the upstream branch's name is - different from the local one. +* `simple` - pushes the current branch with the same name on the remote. + -When pushing to a remote that is different from the remote you normally -pull from, work as `current`. This is the safest option and is suited -for beginners. +If you are working on a centralized workflow (pushing to the same repository you +pull from, which is typically `origin`), then you need to configure an upstream +branch with the same name. + -This mode has become the default in Git 2.0. +This mode is the default since Git 2.0, and is the safest option suited for +beginners. * `matching` - push all branches having the same name on both ends. This makes the repository you are pushing to remember the set of