git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: Ulrich Windl <Ulrich.Windl@rz.uni-regensburg.de>
Cc: git@vger.kernel.org
Subject: [PATCH 2/2] help_unknown_ref(): check for refname ambiguity
Date: Tue, 14 May 2019 08:05:05 -0400	[thread overview]
Message-ID: <20190514120505.GB27229@sigill.intra.peff.net> (raw)
In-Reply-To: <20190514120220.GA7714@sigill.intra.peff.net>

When the user asks to merge "foo" and we suggest "origin/foo" instead,
we do so by simply chopping off "refs/remotes/" from the front of the
suggested ref. This is usually fine, but it's possible that the
resulting name is ambiguous (e.g., you have "refs/heads/origin/foo",
too).

Let's use shorten_unambiguous_ref() to do this the right way, which
should usually yield the same "origin/foo", but "remotes/origin/foo" if
necessary.

Note that in this situation there may be other options (e.g., we could
suggest "heads/origin/foo" as well). I'll leave that up for debate; the
focus here is just to avoid giving advice that does not actually do what
we expect.

Signed-off-by: Jeff King <peff@peff.net>
---
 help.c           | 6 +++---
 t/t7600-merge.sh | 6 ++++++
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/help.c b/help.c
index d3b3f64e3c..5261d83ecf 100644
--- a/help.c
+++ b/help.c
@@ -754,12 +754,12 @@ static int append_similar_ref(const char *refname, const struct object_id *oid,
 {
 	struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
 	char *branch = strrchr(refname, '/') + 1;
-	const char *remote;
 
 	/* A remote branch of the same name is deemed similar */
-	if (skip_prefix(refname, "refs/remotes/", &remote) &&
+	if (starts_with(refname, "refs/remotes/") &&
 	    !strcmp(branch, cb->base_ref))
-		string_list_append(cb->similar_refs, remote);
+		string_list_append_nodup(cb->similar_refs,
+					 shorten_unambiguous_ref(refname, 1));
 	return 0;
 }
 
diff --git a/t/t7600-merge.sh b/t/t7600-merge.sh
index 7551ae3488..2286b0799d 100755
--- a/t/t7600-merge.sh
+++ b/t/t7600-merge.sh
@@ -881,4 +881,10 @@ test_expect_success 'merge suggests matching remote refname' '
 	grep origin/not-local stderr
 '
 
+test_expect_success 'suggested names are not ambiguous' '
+	git update-ref refs/heads/origin/not-local HEAD &&
+	test_must_fail git merge not-local 2>stderr &&
+	grep remotes/origin/not-local stderr
+'
+
 test_done
-- 
2.21.0.1388.g2b1efd806f

      parent reply	other threads:[~2019-05-14 12:05 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-14  9:54 some git confusion (where git's advice didn't help) Ulrich Windl
2019-05-14 11:21 ` Jeff King
2019-05-14 11:29   ` Duy Nguyen
2019-05-14 12:16     ` Jeff King
2019-05-14 12:02   ` [PATCH 0/2] some fixes for help_unknown_ref() Jeff King
2019-05-14 12:04     ` [PATCH 1/2] help_unknown_ref(): duplicate collected refnames Jeff King
2019-05-14 12:05     ` Jeff King [this message]

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=20190514120505.GB27229@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=Ulrich.Windl@rz.uni-regensburg.de \
    --cc=git@vger.kernel.org \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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).