git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH v2] refs: use skip_prefix() in ref_is_hidden()
@ 2017-07-22  4:39 Christian Couder
  2017-07-24 16:09 ` Jeff King
  0 siblings, 1 reply; 2+ messages in thread
From: Christian Couder @ 2017-07-22  4:39 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Lukas Fleischer, Jeff King, Christian Couder

This is shorter, makes the logic a bit easier to follow, and is
perhaps a bit faster too.

The logic is to make the final decision only when "subject" is there,
its early part matches "match", and the match is at the slash
boundary (or the whole thing).

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---

The change with the previous version was suggested by Junio in

https://public-inbox.org/git/xmqqk231hgpz.fsf@gitster.mtv.corp.google.com/

 refs.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/refs.c b/refs.c
index ba22f4acef..ea2b9f84f8 100644
--- a/refs.c
+++ b/refs.c
@@ -1160,7 +1160,7 @@ int ref_is_hidden(const char *refname, const char *refname_full)
 		const char *match = hide_refs->items[i].string;
 		const char *subject;
 		int neg = 0;
-		int len;
+		const char *p;
 
 		if (*match == '!') {
 			neg = 1;
@@ -1175,10 +1175,9 @@ int ref_is_hidden(const char *refname, const char *refname_full)
 		}
 
 		/* refname can be NULL when namespaces are used. */
-		if (!subject || !starts_with(subject, match))
-			continue;
-		len = strlen(match);
-		if (!subject[len] || subject[len] == '/')
+		if (subject &&
+		    skip_prefix(subject, match, &p) &&
+		    (!*p || *p == '/'))
 			return !neg;
 	}
 	return 0;
-- 
2.14.0.rc0.26.g981adb928e.dirty


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] refs: use skip_prefix() in ref_is_hidden()
  2017-07-22  4:39 [PATCH v2] refs: use skip_prefix() in ref_is_hidden() Christian Couder
@ 2017-07-24 16:09 ` Jeff King
  0 siblings, 0 replies; 2+ messages in thread
From: Jeff King @ 2017-07-24 16:09 UTC (permalink / raw)
  To: Christian Couder; +Cc: git, Junio C Hamano, Lukas Fleischer, Christian Couder

On Sat, Jul 22, 2017 at 06:39:12AM +0200, Christian Couder wrote:

> @@ -1175,10 +1175,9 @@ int ref_is_hidden(const char *refname, const char *refname_full)
>  		}
>  
>  		/* refname can be NULL when namespaces are used. */
> -		if (!subject || !starts_with(subject, match))
> -			continue;
> -		len = strlen(match);
> -		if (!subject[len] || subject[len] == '/')
> +		if (subject &&
> +		    skip_prefix(subject, match, &p) &&
> +		    (!*p || *p == '/'))
>  			return !neg;

This looks good to me.

I seem to recall running across a similar pattern elsewhere, where a
caller wanted either an exact match, or a match ending with a particular
character. We could add one more helper like:

  if (subject && skip_prefix_with_sep(subject, match, '/', &p))
	return !neg;

But I don't offhand recall where that other place was, and it's not like
it's saving a huge amount of complexity in the caller. So I'll file it
away in the back of my mind and see if it comes up a third time. ;)

-Peff

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-07-24 16:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-22  4:39 [PATCH v2] refs: use skip_prefix() in ref_is_hidden() Christian Couder
2017-07-24 16:09 ` Jeff King

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).