Skip to content

Commit 2ed2e19

Browse files
peffgitster
authored andcommitted
help_unknown_ref(): check for refname ambiguity
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8ed51b0 commit 2ed2e19

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

help.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -761,12 +761,12 @@ static int append_similar_ref(const char *refname, const struct object_id *oid,
761761
{
762762
struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
763763
char *branch = strrchr(refname, '/') + 1;
764-
const char *remote;
765764

766765
/* A remote branch of the same name is deemed similar */
767-
if (skip_prefix(refname, "refs/remotes/", &remote) &&
766+
if (starts_with(refname, "refs/remotes/") &&
768767
!strcmp(branch, cb->base_ref))
769-
string_list_append(cb->similar_refs, remote);
768+
string_list_append_nodup(cb->similar_refs,
769+
shorten_unambiguous_ref(refname, 1));
770770
return 0;
771771
}
772772

t/t7600-merge.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,4 +836,10 @@ test_expect_success 'merge suggests matching remote refname' '
836836
grep origin/not-local stderr
837837
'
838838

839+
test_expect_success 'suggested names are not ambiguous' '
840+
git update-ref refs/heads/origin/not-local HEAD &&
841+
test_must_fail git merge not-local 2>stderr &&
842+
grep remotes/origin/not-local stderr
843+
'
844+
839845
test_done

0 commit comments

Comments
 (0)