{"thread":{"id":"46439","subject":"[PATCH v2] refs: use skip_prefix() in ref_is_hidden()","startedAt":"2017-07-22T04:39:29Z","lastAt":"2017-07-24T16:10:03Z","messageCount":2,"participants":["Christian Couder","Jeff King"],"isPatch":true,"patchVersion":2,"patchTotal":null},"messages":[{"id":"324893","messageId":"20170722043912.32118-1-chriscool@tuxfamily.org","threadId":"46439","inReplyTo":null,"subject":"[PATCH v2] refs: use skip_prefix() in ref_is_hidden()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2017-07-22T04:39:12Z","receivedAt":"2017-07-22T04:39:29Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"This is shorter, makes the logic a bit easier to follow, and is\nperhaps a bit faster too.\n\nThe logic is to make the final decision only when \"subject\" is there,\nits early part matches \"match\", and the match is at the slash\nboundary (or the whole thing).\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n\nThe change with the previous version was suggested by Junio in\n\nhttps://public-inbox.org/git/xmqqk231hgpz.fsf@gitster.mtv.corp.google.com/\n\n refs.c | 9 ++++-----\n 1 file changed, 4 insertions(+), 5 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex ba22f4acef..ea2b9f84f8 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1160,7 +1160,7 @@ int ref_is_hidden(const char *refname, const char *refname_full)\n \t\tconst char *match = hide_refs->items[i].string;\n \t\tconst char *subject;\n \t\tint neg = 0;\n-\t\tint len;\n+\t\tconst char *p;\n \n \t\tif (*match == '!') {\n \t\t\tneg = 1;\n@@ -1175,10 +1175,9 @@ int ref_is_hidden(const char *refname, const char *refname_full)\n \t\t}\n \n \t\t/* refname can be NULL when namespaces are used. */\n-\t\tif (!subject || !starts_with(subject, match))\n-\t\t\tcontinue;\n-\t\tlen = strlen(match);\n-\t\tif (!subject[len] || subject[len] == '/')\n+\t\tif (subject &&\n+\t\t    skip_prefix(subject, match, &p) &&\n+\t\t    (!*p || *p == '/'))\n \t\t\treturn !neg;\n \t}\n \treturn 0;\n-- \n2.14.0.rc0.26.g981adb928e.dirty\n\n"},{"id":"324951","messageId":"20170724160938.bpdqf64p2ja2vvbd@sigill.intra.peff.net","threadId":"46439","inReplyTo":"20170722043912.32118-1-chriscool@tuxfamily.org","subject":"Re: [PATCH v2] refs: use skip_prefix() in ref_is_hidden()","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-07-24T16:09:39Z","receivedAt":"2017-07-24T16:10:03Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Sat, Jul 22, 2017 at 06:39:12AM +0200, Christian Couder wrote:\n\n> @@ -1175,10 +1175,9 @@ int ref_is_hidden(const char *refname, const char *refname_full)\n>  \t\t}\n>  \n>  \t\t/* refname can be NULL when namespaces are used. */\n> -\t\tif (!subject || !starts_with(subject, match))\n> -\t\t\tcontinue;\n> -\t\tlen = strlen(match);\n> -\t\tif (!subject[len] || subject[len] == '/')\n> +\t\tif (subject &&\n> +\t\t    skip_prefix(subject, match, &p) &&\n> +\t\t    (!*p || *p == '/'))\n>  \t\t\treturn !neg;\n\nThis looks good to me.\n\nI seem to recall running across a similar pattern elsewhere, where a\ncaller wanted either an exact match, or a match ending with a particular\ncharacter. We could add one more helper like:\n\n  if (subject && skip_prefix_with_sep(subject, match, '/', &p))\n\treturn !neg;\n\nBut I don't offhand recall where that other place was, and it's not like\nit's saving a huge amount of complexity in the caller. So I'll file it\naway in the back of my mind and see if it comes up a third time. ;)\n\n-Peff\n"}]}