All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Tao Klerks via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: "Tao Klerks" <tao@klerks.biz>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>,
	"Eric Sunshine" <sunshine@sunshineco.com>,
	"Josh Steadmon" <steadmon@google.com>,
	"Tao Klerks" <tao@klerks.biz>, "Tao Klerks" <tao@klerks.biz>
Subject: [PATCH v5 3/3] push: new config option "push.autoSetupRemote" supports "simple" push
Date: Fri, 29 Apr 2022 09:56:46 +0000	[thread overview]
Message-ID: <41c88e51ac6baf3ddaf08f2335015b4fa69fadf6.1651226207.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1161.v5.git.1651226206.gitgitgadget@gmail.com>

From: Tao Klerks <tao@klerks.biz>

In some "simple" centralized workflows, users expect remote tracking
branch names to match local branch names. "git push" pushes to the
remote version/instance of the branch, and "git pull" pulls any changes
to the remote branch (changes made by the same user in another place, or
by other users).

This expectation is supported by the push.default default option "simple"
which refuses a default push for a mismatching tracking branch name, and
by the new branch.autosetupmerge option, "simple", which only sets up
remote tracking for same-name remote branches.

When a new branch has been created by the user and has not yet been
pushed (and push.default is not set to "current"), the user is prompted
with a "The current branch %s has no upstream branch" error, and
instructions on how to push and add tracking.

This error is helpful in that following the advice once per branch
"resolves" the issue for that branch forever, but inconvenient in that
for the "simple" centralized workflow, this is always the right thing to
do, so it would be better to just do it.

Support this workflow with a new config setting, push.autoSetupRemote,
which will cause a default push, when there is no remote tracking branch
configured, to push to the same-name on the remote and --set-upstream.

Also add a hint offering this new option when the "The current branch %s
has no upstream branch" error is encountered, and add corresponding tests.

Signed-off-by: Tao Klerks <tao@klerks.biz>
---
 Documentation/config/push.txt | 11 +++++++++
 builtin/push.c                | 44 ++++++++++++++++++++++++++++-------
 t/t5528-push-default.sh       | 14 +++++++++++
 transport.h                   |  1 +
 4 files changed, 62 insertions(+), 8 deletions(-)

diff --git a/Documentation/config/push.txt b/Documentation/config/push.txt
index 632033638c4..e32801e6c91 100644
--- a/Documentation/config/push.txt
+++ b/Documentation/config/push.txt
@@ -1,3 +1,14 @@
+push.autoSetupRemote::
+	If set to "true" assume `--set-upstream` on default push when no
+	upstream tracking exists for the current branch; this option
+	takes effect with push.default options 'simple', 'upstream',
+	and 'current'. It is useful if by default you want new branches
+	to be pushed to the default remote (like the behavior of
+	'push.default=current') and you also want the upstream tracking
+	to be set. Workflows most likely to benefit from this option are
+	'simple' central workflows where all branches are expected to
+	have the same name on the remote.
+
 push.default::
 	Defines the action `git push` should take if no refspec is
 	given (whether from the command-line, config, or elsewhere).
diff --git a/builtin/push.c b/builtin/push.c
index 447f91f5b47..86b44f8aa71 100644
--- a/builtin/push.c
+++ b/builtin/push.c
@@ -195,16 +195,32 @@ static const char message_detached_head_die[] =
 	   "\n"
 	   "    git push %s HEAD:<name-of-remote-branch>\n");
 
-static const char *get_upstream_ref(struct branch *branch, const char *remote_name)
+static const char *get_upstream_ref(int flags, struct branch *branch, const char *remote_name)
 {
-	if (!branch->merge_nr || !branch->merge || !branch->remote_name)
+	if (branch->merge_nr == 0 && (flags & TRANSPORT_PUSH_AUTO_UPSTREAM)) {
+		/* if missing, assume same; set_upstream will be defined later */
+		return branch->refname;
+	}
+
+	if (!branch->merge_nr || !branch->merge || !branch->remote_name) {
+		const char *advice_autosetup_maybe = "";
+		if (!(flags & TRANSPORT_PUSH_AUTO_UPSTREAM)) {
+			advice_autosetup_maybe = _("\n"
+					   "To have this happen automatically for "
+					   "branches without a tracking\n"
+					   "upstream, see 'push.autoSetupRemote' "
+					   "in 'git help config'.\n");
+		}
 		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"),
+		    "    git push --set-upstream %s %s\n"
+		    "%s"),
 		    branch->name,
 		    remote_name,
-		    branch->name);
+		    branch->name,
+		    advice_autosetup_maybe);
+	}
 	if (branch->merge_nr != 1)
 		die(_("The current branch %s has multiple upstream branches, "
 		    "refusing to push."), branch->name);
@@ -212,7 +228,7 @@ static const char *get_upstream_ref(struct branch *branch, const char *remote_na
 	return branch->merge[0]->src;
 }
 
-static void setup_default_push_refspecs(struct remote *remote)
+static void setup_default_push_refspecs(int *flags, struct remote *remote)
 {
 	struct branch *branch;
 	const char *dst;
@@ -244,7 +260,7 @@ static void setup_default_push_refspecs(struct remote *remote)
 	case PUSH_DEFAULT_SIMPLE:
 		if (!same_remote)
 			break;
-		if (strcmp(branch->refname, get_upstream_ref(branch, remote->name)))
+		if (strcmp(branch->refname, get_upstream_ref(*flags, branch, remote->name)))
 			die_push_simple(branch, remote);
 		break;
 
@@ -254,13 +270,21 @@ static void setup_default_push_refspecs(struct remote *remote)
 			      "your current branch '%s', without telling me what to push\n"
 			      "to update which remote branch."),
 			    remote->name, branch->name);
-		dst = get_upstream_ref(branch, remote->name);
+		dst = get_upstream_ref(*flags, branch, remote->name);
 		break;
 
 	case PUSH_DEFAULT_CURRENT:
 		break;
 	}
 
+	/*
+	 * this is a default push - if auto-upstream is enabled and there is
+	 * no upstream defined, then set it (with options 'simple', 'upstream',
+	 * and 'current').
+	 */
+	if ((*flags & TRANSPORT_PUSH_AUTO_UPSTREAM) && branch->merge_nr == 0)
+		*flags |= TRANSPORT_PUSH_SET_UPSTREAM;
+
 	refspec_appendf(&rs, "%s:%s", branch->refname, dst);
 }
 
@@ -411,7 +435,7 @@ static int do_push(int flags,
 		if (remote->push.nr) {
 			push_refspec = &remote->push;
 		} else if (!(flags & TRANSPORT_PUSH_MIRROR))
-			setup_default_push_refspecs(remote);
+			setup_default_push_refspecs(&flags, remote);
 	}
 	errs = 0;
 	url_nr = push_url_of_remote(remote, &url);
@@ -482,6 +506,10 @@ static int git_push_config(const char *k, const char *v, void *cb)
 		else
 			*flags &= ~TRANSPORT_PUSH_FOLLOW_TAGS;
 		return 0;
+	} else if (!strcmp(k, "push.autosetupremote")) {
+		if (git_config_bool(k, v))
+			*flags |= TRANSPORT_PUSH_AUTO_UPSTREAM;
+		return 0;
 	} else if (!strcmp(k, "push.gpgsign")) {
 		const char *value;
 		if (!git_config_get_value("push.gpgsign", &value)) {
diff --git a/t/t5528-push-default.sh b/t/t5528-push-default.sh
index 0d6c9869ed3..284e20fefda 100755
--- a/t/t5528-push-default.sh
+++ b/t/t5528-push-default.sh
@@ -162,6 +162,20 @@ test_expect_success 'push from/to branch with tracking fails with nothing ' '
 	test_push_failure nothing
 '
 
+test_expect_success 'push from/to new branch succeeds with upstream if push.autoSetupRemote' '
+	git checkout -b new-branch-a &&
+	test_config push.autoSetupRemote true &&
+	test_config branch.new-branch-a.remote parent1 &&
+	test_push_success upstream new-branch-a
+'
+
+test_expect_success 'push from/to new branch succeeds with simple if push.autoSetupRemote' '
+	git checkout -b new-branch-c &&
+	test_config push.autoSetupRemote true &&
+	test_config branch.new-branch-c.remote parent1 &&
+	test_push_success simple new-branch-c
+'
+
 test_expect_success '"matching" fails if none match' '
 	git init --bare empty &&
 	test_must_fail git push empty : 2>actual &&
diff --git a/transport.h b/transport.h
index 12bc08fc339..b5bf7b3e704 100644
--- a/transport.h
+++ b/transport.h
@@ -145,6 +145,7 @@ struct transport {
 #define TRANSPORT_PUSH_OPTIONS			(1<<14)
 #define TRANSPORT_RECURSE_SUBMODULES_ONLY	(1<<15)
 #define TRANSPORT_PUSH_FORCE_IF_INCLUDES	(1<<16)
+#define TRANSPORT_PUSH_AUTO_UPSTREAM		(1<<17)
 
 int transport_summary_width(const struct ref *refs);
 
-- 
gitgitgadget

  parent reply	other threads:[~2022-04-29  9:57 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-24  9:45 [PATCH 0/3] adding new branch.autosetupmerge option "simple" Tao Klerks via GitGitGadget
2022-02-24  9:45 ` [PATCH 1/3] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget
2022-02-24 19:20   ` Junio C Hamano
2022-02-24  9:45 ` [PATCH 2/3] t3200: tests for new branch.autosetupmerge option "simple" Tao Klerks via GitGitGadget
2022-02-24  9:45 ` [PATCH 3/3] branch documentation: new autosetupmerge " Tao Klerks via GitGitGadget
2022-02-24 19:38   ` Junio C Hamano
2022-02-25 18:52 ` [PATCH v2 0/2] adding new branch.autosetupmerge " Tao Klerks via GitGitGadget
2022-02-25 18:52   ` [PATCH v2 1/2] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget
2022-02-25 20:15     ` Junio C Hamano
2022-02-27 23:59       ` Tao Klerks
2022-02-25 18:52   ` [PATCH v2 2/2] t3200: tests for new branch.autosetupmerge option "simple" Tao Klerks via GitGitGadget
2022-02-28  7:14   ` [PATCH v3 0/2] adding " Tao Klerks via GitGitGadget
2022-02-28  7:14     ` [PATCH v3 1/2] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget
2022-02-28 10:39       ` Ævar Arnfjörð Bjarmason
2022-03-02  9:35         ` Tao Klerks
2022-03-20 17:00           ` Tao Klerks
2022-02-28  7:14     ` [PATCH v3 2/2] t3200: tests for new branch.autosetupmerge option "simple" Tao Klerks via GitGitGadget
2022-02-28  9:34       ` Ævar Arnfjörð Bjarmason
2022-03-01  2:58         ` Eric Sunshine
2022-03-01  9:59           ` Tao Klerks
2022-03-01  9:59         ` Tao Klerks
2022-03-21  6:17     ` [PATCH v4] merge: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget
2022-04-18 18:15       ` Josh Steadmon
2022-04-20  5:12         ` Tao Klerks
2022-04-20 17:19           ` Josh Steadmon
2022-04-20 17:43           ` Junio C Hamano
2022-04-20 21:31             ` Tao Klerks
2022-04-21  1:53               ` Junio C Hamano
2022-04-21 10:04                 ` Tao Klerks
2022-04-22  2:27                   ` Junio C Hamano
2022-04-22  9:24                     ` Tao Klerks
2022-04-22 13:27                       ` Tao Klerks
2022-04-23  4:44                       ` Junio C Hamano
2022-04-24 11:57                         ` Tao Klerks
2022-04-29  7:31                           ` Tao Klerks
2022-04-29  9:56       ` [PATCH v5 0/3] New options to support "simple" centralized workflow Tao Klerks via GitGitGadget
2022-04-29  9:56         ` [PATCH v5 1/3] branch: new autosetupmerge option 'simple' for matching branches Tao Klerks via GitGitGadget
2022-04-29  9:56         ` [PATCH v5 2/3] push: default to single remote even when not named origin Tao Klerks via GitGitGadget
2022-04-29  9:56         ` Tao Klerks via GitGitGadget [this message]
2022-04-29 18:50         ` [PATCH v5 0/3] New options to support "simple" centralized workflow Junio C Hamano
2022-04-30 15:48           ` Tao Klerks

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=41c88e51ac6baf3ddaf08f2335015b4fa69fadf6.1651226207.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=steadmon@google.com \
    --cc=sunshine@sunshineco.com \
    --cc=tao@klerks.biz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.