Skip to content

Commit 8d32d25

Browse files
committed
Merge branch 'jk/help-unknown-ref-fix'
Improve the code to show args with potential typo that cannot be interpreted as a commit-ish. * jk/help-unknown-ref-fix: help_unknown_ref(): check for refname ambiguity help_unknown_ref(): duplicate collected refnames
2 parents e91f65d + 2ed2e19 commit 8d32d25

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

help.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -754,19 +754,19 @@ static int append_similar_ref(const char *refname, const struct object_id *oid,
754754
{
755755
struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
756756
char *branch = strrchr(refname, '/') + 1;
757-
const char *remote;
758757

759758
/* A remote branch of the same name is deemed similar */
760-
if (skip_prefix(refname, "refs/remotes/", &remote) &&
759+
if (starts_with(refname, "refs/remotes/") &&
761760
!strcmp(branch, cb->base_ref))
762-
string_list_append(cb->similar_refs, remote);
761+
string_list_append_nodup(cb->similar_refs,
762+
shorten_unambiguous_ref(refname, 1));
763763
return 0;
764764
}
765765

766766
static struct string_list guess_refs(const char *ref)
767767
{
768768
struct similar_ref_cb ref_cb;
769-
struct string_list similar_refs = STRING_LIST_INIT_NODUP;
769+
struct string_list similar_refs = STRING_LIST_INIT_DUP;
770770

771771
ref_cb.base_ref = ref;
772772
ref_cb.similar_refs = &similar_refs;

t/t7600-merge.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,4 +893,24 @@ test_expect_success 'merge --quit' '
893893
)
894894
'
895895

896+
test_expect_success 'merge suggests matching remote refname' '
897+
git commit --allow-empty -m not-local &&
898+
git update-ref refs/remotes/origin/not-local HEAD &&
899+
git reset --hard HEAD^ &&
900+
901+
# This is white-box testing hackery; we happen to know
902+
# that reading packed refs is more picky about the memory
903+
# ownership of strings we pass to for_each_ref() callbacks.
904+
git pack-refs --all --prune &&
905+
906+
test_must_fail git merge not-local 2>stderr &&
907+
grep origin/not-local stderr
908+
'
909+
910+
test_expect_success 'suggested names are not ambiguous' '
911+
git update-ref refs/heads/origin/not-local HEAD &&
912+
test_must_fail git merge not-local 2>stderr &&
913+
grep remotes/origin/not-local stderr
914+
'
915+
896916
test_done

0 commit comments

Comments
 (0)