From: Nguyễn Thái Ngọc Duy Date: Fri, 25 Jan 2019 09:51:22 +0000 (+0700) Subject: fetch: prefer suffix substitution in compact fetch.output X-Git-Tag: v2.21.0-rc0~32^2 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/dc40b24df47af35f486e2c5139121b6cddc2dcb9?ds=inline;hp=--cc 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 Signed-off-by: Junio C Hamano --- dc40b24df47af35f486e2c5139121b6cddc2dcb9 diff --git a/builtin/fetch.c b/builtin/fetch.c index e0140327aa..e0173f8a33 100644 --- a/builtin/fetch.c +++ b/builtin/fetch.c @@ -629,9 +629,14 @@ static int find_and_replace(struct strbuf *haystack, const char *needle, const char *placeholder) { - const char *p = strstr(haystack->buf, needle); + const char *p = NULL; int plen, nlen; + nlen = strlen(needle); + if (ends_with(haystack->buf, needle)) + p = haystack->buf + haystack->len - nlen; + else + p = strstr(haystack->buf, needle); if (!p) return 0; @@ -639,7 +644,6 @@ static int find_and_replace(struct strbuf *haystack, return 0; plen = strlen(p); - nlen = strlen(needle); if (plen > nlen && p[nlen] != '/') return 0;