fetch: prefer suffix substitution in compact fetch.output

I have a remote named "jch" and it has a branch with the same name. And fetch.output is set to "compact". Fetching this remote looks like this From https://github.com/gitster/git + eb7fd39f6b...835363af2f jch -> */jch (forced update) 6f11fd5edb..59b12ae96a nd/config-move-to -> jch/* * [new branch] nd/diff-parseopt -> jch/* * [new branch] nd/the-index-final -> jch/* Notice that the local side of branch jch starts with "*" instead of ending with it like the rest. It's not exactly wrong. It just looks weird. This patch changes the find-and-replace code a bit to try finding prefix first before falling back to strstr() which finds a substring from left to right. Now we have something less OCD From https://github.com/gitster/git + eb7fd39f6b...835363af2f jch -> jch/* (forced update) 6f11fd5edb..59b12ae96a nd/config-move-to -> jch/* * [new branch] nd/diff-parseopt -> jch/* * [new branch] nd/the-index-final -> jch/* Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Jan 25, 2019 at 16:51 UTC dc40b24df47af35f486e2c5139121b6cddc2dcb9
1 file changed +6 -2
builtin/fetch.c
+6 -2
@@ -629,9 +629,14 @@ static int find_and_replace(struct strbuf *haystack,
629 const char *needle,
630 const char *placeholder)
631 {
632 - const char *p = strstr(haystack->buf, needle);
632 + const char *p = NULL;
633 int plen, nlen;
634
635 + nlen = strlen(needle);
636 + if (ends_with(haystack->buf, needle))
637 + p = haystack->buf + haystack->len - nlen;
638 + else
639 + p = strstr(haystack->buf, needle);
640 if (!p)
641 return 0;
642
@@ -639,7 +644,6 @@ static int find_and_replace(struct strbuf *haystack,
644 return 0;
645
646 plen = strlen(p);
642 - nlen = strlen(needle);
647 if (plen > nlen && p[nlen] != '/')
648 return 0;
649