help_unknown_ref(): check for refname ambiguity
authorJeff King <peff@peff.net>
Tue, 14 May 2019 12:05:05 +0000 (08:05 -0400)
committerJunio C Hamano <gitster@pobox.com>
Wed, 15 May 2019 01:58:02 +0000 (10:58 +0900)
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
help.c
t/t7600-merge.sh
diff --git a/help.c b/help.c
index 5583f6cded869fdc3bd4d4c640856f0fd77a7387..602d9fa20153ca7265314edf69e8416a90085c82 100644 (file)
--- a/help.c
+++ b/help.c
@@ -761,12 +761,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;
 }
 
index 592850cef7a106e2f0df8b9902e4549c6561bb10..36b2c8c91ffdf57d192281857a075e12b5eb801f 100755 (executable)
@@ -836,4 +836,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