Git Mailing List Archive mirror
 help / color / mirror / Atom feed
From: "Tao Klerks via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Tao Klerks <tao@klerks.biz>, Tao Klerks <tao@klerks.biz>
Subject: [PATCH] rev-parse: respect push.autosetupremote when evaluating @{push}
Date: Sun, 10 Jul 2022 19:16:33 +0000	[thread overview]
Message-ID: <pull.1279.git.1657480594123.gitgitgadget@gmail.com> (raw)

From: Tao Klerks <tao@klerks.biz>

In a previous release, the push.autosetupremote config was introduced to
ease new branch management in "simple" single-remote workflows. This makes
"git push" work on new branches (without configured upstream) with
push.default set to "simple" or "upstream" and it implies
"--set-upstream" regardless of whether the same-name remote branch exists
or not.

The "@{push}" suffix logic was not adjusted to account for this new option,
however, and sometimes returns an error when "git push" would successfully
push to an existing remote branch.

This is an edge-case, as the main context where push.autosetupremote will
apply is for *new* branches, with no corresponding remote branch yet, and
so even if the defaulting is handled correctly, the rev-parse will still
fail with "unknown revision or path not in the working tree".

Fix this edge-case so "git rev-parse @{push}" works, if there is no
upstream tracking relationship set up but the remote tracking branch that
will be defaulted to does exist and can be resolved.

Also add corresponding test cases.

Signed-off-by: Tao Klerks <tao@klerks.biz>
---
    rev-parse: respect push.autosetupremote when evaluating @{push}
    
    Minor consistency fix for previously-introduced "push.autosetupremote"
    option.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1279%2FTaoK%2Ftao-rev-parse-autosetupremote-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1279/TaoK/tao-rev-parse-autosetupremote-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1279

 remote.c                  | 30 ++++++++++++++++++++++++++++--
 t/t1514-rev-parse-push.sh | 20 ++++++++++++++++++++
 2 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/remote.c b/remote.c
index b19e3a2f015..59f5bf5b5f5 100644
--- a/remote.c
+++ b/remote.c
@@ -1919,6 +1919,23 @@ static const char *tracking_for_push_dest(struct remote *remote,
 	return ret;
 }
 
+static const char *default_missing_upstream(struct remote *remote,
+				    struct branch *branch,
+				    struct strbuf *err)
+{
+	int autosetupremote = 0;
+
+	if (branch && (!branch->merge || !branch->merge[0])) {
+		repo_config_get_bool(the_repository,
+				     "push.autosetupremote",
+				     &autosetupremote);
+		if (autosetupremote)
+			return tracking_for_push_dest(remote, branch->refname, err);
+	}
+
+	return NULL;
+}
+
 static const char *branch_get_push_1(struct remote_state *remote_state,
 				     struct branch *branch, struct strbuf *err)
 {
@@ -1959,13 +1976,22 @@ static const char *branch_get_push_1(struct remote_state *remote_state,
 		return tracking_for_push_dest(remote, branch->refname, err);
 
 	case PUSH_DEFAULT_UPSTREAM:
-		return branch_get_upstream(branch, err);
-
+		{
+			const char *up;
+			up = default_missing_upstream(remote, branch, err);
+			if (up)
+				return up;
+			return branch_get_upstream(branch, err);
+		}
 	case PUSH_DEFAULT_UNSPECIFIED:
 	case PUSH_DEFAULT_SIMPLE:
 		{
 			const char *up, *cur;
 
+			up = default_missing_upstream(remote, branch, err);
+			if (up)
+				return up;
+
 			up = branch_get_upstream(branch, err);
 			if (!up)
 				return NULL;
diff --git a/t/t1514-rev-parse-push.sh b/t/t1514-rev-parse-push.sh
index d868a081105..ffa0db14585 100755
--- a/t/t1514-rev-parse-push.sh
+++ b/t/t1514-rev-parse-push.sh
@@ -21,7 +21,10 @@ test_expect_success 'setup' '
 	git push origin HEAD &&
 	git branch --set-upstream-to=origin/main main &&
 	git branch --track topic origin/main &&
+	git branch --no-track indie_topic origin/main &&
+	git branch --no-track new_topic origin/main &&
 	git push origin topic &&
+	git push origin indie_topic &&
 	git push other topic
 '
 
@@ -73,4 +76,21 @@ test_expect_success 'resolving @{push} fails with a detached HEAD' '
 	test_must_fail git rev-parse @{push}
 '
 
+test_expect_success '@{push} with default=simple without tracking' '
+	test_config push.default simple &&
+	test_must_fail git rev-parse indie_topic@{push}
+'
+
+test_expect_success '@{push} with default=simple with autosetupremote' '
+	test_config push.default simple &&
+	test_config push.autosetupremote true &&
+	resolve indie_topic@{push} refs/remotes/origin/indie_topic
+'
+
+test_expect_success '@{push} with default=simple with autosetupremote, new branch' '
+	test_config push.default simple &&
+	test_config push.autosetupremote true &&
+	test_must_fail git rev-parse new_topic@{push}
+'
+
 test_done

base-commit: 39c15e485575089eb77c769f6da02f98a55905e0
-- 
gitgitgadget

             reply	other threads:[~2022-07-10 19:16 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-10 19:16 Tao Klerks via GitGitGadget [this message]
2022-07-10 20:42 ` [PATCH] rev-parse: respect push.autosetupremote when evaluating @{push} Junio C Hamano
2022-07-16 17:40   ` Tao Klerks
2023-05-28  9:58 ` [PATCH v2] " Tao Klerks via GitGitGadget

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=pull.1279.git.1657480594123.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).