threads / patch / 34172

patchname-rev: Allow to omit refs/tags/ part in --refs option when --tags used

Subject: [PATCH] name-rev: Allow to omit refs/tags/ part in --refs option when --tags used

## tl;dr

5 messages between Jun 17, 2013 and Jun 18, 2013. Diffs are folded; open one to read it.

replies: 4people: 2as markdown or json

Namhyung Kim· Jun 17, 2013, 07:53 UTC · lore

In its current form, when an user wants to filter specific ref using --refs option, she needs to give something like --refs=refs/tags/v1.*.

This is not intuitive as users might think it's enough to give just actual tag name part like --refs=v1.*. It applies to refs other than just tags too. Change it for users to be able to use --refs=sth or --refs=remotes/sth.

Also remove the leading 'tags/' part in the output when --tags option was given since the option restricts to work with tags only. This is what we have if --name-only option was given also.

Signed-off-by: Namhyung Kim <namhyung.kim@lge.com>
---
 builtin/name-rev.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
Show changes to builtin/name-rev.c +6 −2
diff --git a/builtin/name-rev.c b/builtin/name-rev.c
index 6238247..446743b 100644
--- a/builtin/name-rev.c
+++ b/builtin/name-rev.c
@@ -97,7 +97,8 @@ static int name_ref(const char *path, const unsigned char *sha1, int flags, void
 	if (data->tags_only && prefixcmp(path, "refs/tags/"))
 		return 0;
 
-	if (data->ref_filter && fnmatch(data->ref_filter, path, 0))
+	if (data->ref_filter && !prefixcmp(data->ref_filter, "refs/")
+	    && fnmatch(data->ref_filter, path, 0))
 		return 0;
 
 	while (o && o->type == OBJ_TAG) {
@@ -113,12 +114,15 @@ static int name_ref(const char *path, const unsigned char *sha1, int flags, void
 		if (!prefixcmp(path, "refs/heads/"))
 			path = path + 11;
 		else if (data->tags_only
-		    && data->name_only
 		    && !prefixcmp(path, "refs/tags/"))
 			path = path + 10;
 		else if (!prefixcmp(path, "refs/"))
 			path = path + 5;
 
+		if (data->ref_filter && prefixcmp(data->ref_filter, "refs/")
+		    && fnmatch(data->ref_filter, path, 0))
+			return 0;
+
 		name_rev(commit, xstrdup(path), 0, 0, deref);
 	}
 	return 0;
-- 
1.7.11.7
Junio C Hamano· Jun 17, 2013, 15:27 UTC · re: Namhyung Kim · lore

Re: [PATCH] name-rev: Allow to omit refs/tags/ part in --refs option when --tags used

Namhyung Kim <namhyung.kim@lge.com> writes:
Show 5 quoted lines
> In its current form, when an user wants to filter specific ref using
> --refs option, she needs to give something like --refs=refs/tags/v1.*.
>
> This is not intuitive as users might think it's enough to give just
> actual tag name part like --refs=v1.*.

I do not think "Users might think" is not particularly a good justification, but I agree that it would be useful to allow --refs=v1.\* to match refs/heads/v1.4-maint and refs/tags/v1.4.0; it is easy for the users to disambiguate with longer prefix if they wanted to.

Show 6 quoted lines
> It applies to refs other than
> just tags too.  Change it for users to be able to use --refs=sth or
> --refs=remotes/sth.
>
> Also remove the leading 'tags/' part in the output when --tags option
> was given since the option restricts to work with tags only.

This part is questionable, as it changes the output people's scripts have been reading from the command since eternity ago.

If the pattern asks to match with v1.* (not tags/v1.* or refs/tags/v1.*) and you find refs/tags/v1.*, it might be acceptable to strip "refs/tags/" part. Existing users are _expected_ to feed a pattern with full refname starting with refs/, so they will not be negatively affected by such a usability enhancement on the output side.

Show 12 quoted lines
> diff --git a/builtin/name-rev.c b/builtin/name-rev.c
> index 6238247..446743b 100644
> --- a/builtin/name-rev.c
> +++ b/builtin/name-rev.c
> @@ -97,7 +97,8 @@ static int name_ref(const char *path, const unsigned char *sha1, int flags, void
>  	if (data->tags_only && prefixcmp(path, "refs/tags/"))
>  		return 0;
>  
> -	if (data->ref_filter && fnmatch(data->ref_filter, path, 0))
> +	if (data->ref_filter && !prefixcmp(data->ref_filter, "refs/")
> +	    && fnmatch(data->ref_filter, path, 0))
>  		return 0;

What does this mean? "When --refs is specified, if it begins with refs/ then do not show unmatching path, but let any path be subject to the following if --refs does not begin with refs/" sounds like a broken logic, unless you add another fnmatch() later in the codepath to compensate. And you indeed do so, but then at that point, do we still need this "if(...) return 0" at all?

I think it can and should be improved here, and then the one in the main logic you added can be removed.

Wouldn't it make more sense to see if the given pattern matches a tail substring of the ref, instead of using the hardcoded "strip refs/heads/, refs/tags or refs/, and then match once" logic? That way, --refs=origin/* can find refs/remotes/origin/master by running fnmatch of origin/* against its substrings, i.e.

	refs/remotes/origin/master
        remotes/origin/master
        origin/master
and find that the pattern matches it.
Perhaps it is just the matter of adding something like:
	static int subpath_matches(const char *path, const char	*filter)
	{        
		const char *subpath = path;
		while (subpath) {
                	if (!fnmatch(data->ref_filter, subpath, 0))
				return subpath - path;
			subpath = strchr(path, '/');
                        if (subpath)
	                        subpath++;
		}
		return -1;
	}
and then at the beginning of name_ref() do this:
	int can_abbreviate_output = data->name_only;
	if (data->tags_only && prefixcmp(path, "refs/tags/"))
		return 0;
	if (data->ref_filter) {
        	switch (subpath_matches(path, data->ref_filter)) {
		case -1: /* did not match */
			return 0;
		default: /* matched subpath */
			can_abbreviate_output = 1;
			break;
		case 0: /* matched fully */
                	break;
		}
	}

The logic before calling name_rev() will be kept as "only decide how the output looks like", without mixing the unrelated "decide if we want to use it" logic in.

Show 17 quoted lines
>  	while (o && o->type == OBJ_TAG) {
> @@ -113,12 +114,15 @@ static int name_ref(const char *path, const unsigned char *sha1, int flags, void
>  		if (!prefixcmp(path, "refs/heads/"))
>  			path = path + 11;
>  		else if (data->tags_only
> -		    && data->name_only
>  		    && !prefixcmp(path, "refs/tags/"))
>  			path = path + 10;
>  		else if (!prefixcmp(path, "refs/"))
>  			path = path + 5;
>  
> +		if (data->ref_filter && prefixcmp(data->ref_filter, "refs/")
> +		    && fnmatch(data->ref_filter, path, 0))
> +			return 0;
>  		name_rev(commit, xstrdup(path), 0, 0, deref);
>  	}
>  	return 0;
Junio C Hamano· Jun 17, 2013, 15:53 UTC · re: Junio C Hamano · lore

Re: [PATCH] name-rev: Allow to omit refs/tags/ part in --refs option when --tags used

Junio C Hamano <gitster@pobox.com> writes:
Show 35 quoted lines
> Wouldn't it make more sense to see if the given pattern matches a
> tail substring of the ref, instead of using the hardcoded "strip
> refs/heads/, refs/tags or refs/, and then match once" logic?  That
> way, --refs=origin/* can find refs/remotes/origin/master by running
> fnmatch of origin/* against its substrings, i.e.
>
> 	refs/remotes/origin/master
>         remotes/origin/master
>         origin/master
>
> and find that the pattern matches it.
>
> Perhaps it is just the matter of adding something like:
> ...
> and then at the beginning of name_ref() do this:
>
> 	int can_abbreviate_output = data->name_only;
>
> 	if (data->tags_only && prefixcmp(path, "refs/tags/"))
> 		return 0;
> 	if (data->ref_filter) {
>         	switch (subpath_matches(path, data->ref_filter)) {
> 		case -1: /* did not match */
> 			return 0;
> 		default: /* matched subpath */
> 			can_abbreviate_output = 1;
> 			break;
> 		case 0: /* matched fully */
>                 	break;
> 		}
> 	}
>
> The logic before calling name_rev() will be kept as "only decide how
> the output looks like", without mixing the unrelated "decide if we
> want to use it" logic in.

... which may make the "call name_rev with this abbreviated path" logic look something like this:

	if (o && o->type == OBJ_COMMIT) {
        	if (can_abbreviate_output)
			path = shorten_unambiguous_ref(path, 0);
		else if (!prefixcmp(path, "refs/heads/"))
			path = path + 11;
		else if (data->tags_only
		    && data->name_only
		    && !prefixcmp(path, "refs/tags/"))
			path = path + 10;
		else if (!prefixcmp(path, "refs/"))
			path = path + 5;
		name_rev((struct commit *) o, xstrdup(path), 0, 0, deref);
	}
Namhyung Kim· Jun 18, 2013, 12:24 UTC · re: Junio C Hamano · lore

Re: [PATCH] name-rev: Allow to omit refs/tags/ part in --refs option when --tags used

2013-06-18 AM 12:53, Junio C Hamano wrote:
Show 57 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
>
>> Wouldn't it make more sense to see if the given pattern matches a
>> tail substring of the ref, instead of using the hardcoded "strip
>> refs/heads/, refs/tags or refs/, and then match once" logic?  That
>> way, --refs=origin/* can find refs/remotes/origin/master by running
>> fnmatch of origin/* against its substrings, i.e.
>>
>> 	refs/remotes/origin/master
>>          remotes/origin/master
>>          origin/master
>>
>> and find that the pattern matches it.
>>
>> Perhaps it is just the matter of adding something like:
>> ...
>> and then at the beginning of name_ref() do this:
>>
>> 	int can_abbreviate_output = data->name_only;
>>
>> 	if (data->tags_only && prefixcmp(path, "refs/tags/"))
>> 		return 0;
>> 	if (data->ref_filter) {
>>          	switch (subpath_matches(path, data->ref_filter)) {
>> 		case -1: /* did not match */
>> 			return 0;
>> 		default: /* matched subpath */
>> 			can_abbreviate_output = 1;
>> 			break;
>> 		case 0: /* matched fully */
>>                  	break;
>> 		}
>> 	}
>>
>> The logic before calling name_rev() will be kept as "only decide how
>> the output looks like", without mixing the unrelated "decide if we
>> want to use it" logic in.
>
> ... which may make the "call name_rev with this abbreviated path"
> logic look something like this:
>
> 	if (o && o->type == OBJ_COMMIT) {
>          	if (can_abbreviate_output)
> 			path = shorten_unambiguous_ref(path, 0);
> 		else if (!prefixcmp(path, "refs/heads/"))
> 			path = path + 11;
> 		else if (data->tags_only
> 		    && data->name_only
> 		    && !prefixcmp(path, "refs/tags/"))
> 			path = path + 10;
> 		else if (!prefixcmp(path, "refs/"))
> 			path = path + 5;
>
> 		name_rev((struct commit *) o, xstrdup(path), 0, 0, deref);
> 	}
>
>
Hmm.. I thought about it twice.

This will affects the output of `--name-only --refs=refs/remotes/origin/*` case. (AFAIK it only affected to tags so far) As the name_only always sets can_abbreviate_output, it'll shorten the name of remote ref even if it's fully matched.

  $ ./git name-rev --refs=refs/remotes/origin/* a2055c2
  a2055c2 remotes/origin/maint~642
  $ ./git name-rev --refs=refs/remotes/origin/* --name-only a2055c2
  origin/maint~642

I think it should be 'data->tags_only && data->name_only' for compatibility reason.

I also see that the 3rd condition of 'tags_only && name_only' turned out to be useless for the similar reason. When name_only set, it'll take the first case so 3rd case cannot be reached. When it's not set it cannot take the third case too obviously. So I'll just remove it.

Thanks, Namhyung

Namhyung Kim· Jun 18, 2013, 11:55 UTC · re: Junio C Hamano · lore

Re: [PATCH] name-rev: Allow to omit refs/tags/ part in --refs option when --tags used

Hi Junio,
2013-06-18 AM 12:27, Junio C Hamano wrote:
Show 13 quoted lines
> Namhyung Kim <namhyung.kim@lge.com> writes:
>
>> In its current form, when an user wants to filter specific ref using
>> --refs option, she needs to give something like --refs=refs/tags/v1.*.
>>
>> This is not intuitive as users might think it's enough to give just
>> actual tag name part like --refs=v1.*.
>
> I do not think "Users might think" is not particularly a good
> justification, but I agree that it would be useful to allow
> --refs=v1.\* to match refs/heads/v1.4-maint and refs/tags/v1.4.0; it
> is easy for the users to disambiguate with longer prefix if they
> wanted to.
Right.  I just failed to find right words. :)
Show 10 quoted lines
>
>> It applies to refs other than
>> just tags too.  Change it for users to be able to use --refs=sth or
>> --refs=remotes/sth.
>>
>> Also remove the leading 'tags/' part in the output when --tags option
>> was given since the option restricts to work with tags only.
>
> This part is questionable, as it changes the output people's scripts
> have been reading from the command since eternity ago.
True.
Show 7 quoted lines
>
> If the pattern asks to match with v1.* (not tags/v1.* or
> refs/tags/v1.*) and you find refs/tags/v1.*, it might be acceptable
> to strip "refs/tags/" part.  Existing users are _expected_ to feed a
> pattern with full refname starting with refs/, so they will not be
> negatively affected by such a usability enhancement on the output
> side.
This is what I wanted to do exactly. :)
Show 45 quoted lines
>
>> diff --git a/builtin/name-rev.c b/builtin/name-rev.c
>> index 6238247..446743b 100644
>> --- a/builtin/name-rev.c
>> +++ b/builtin/name-rev.c
>> @@ -97,7 +97,8 @@ static int name_ref(const char *path, const unsigned char *sha1, int flags, void
>>   	if (data->tags_only && prefixcmp(path, "refs/tags/"))
>>   		return 0;
>>
>> -	if (data->ref_filter && fnmatch(data->ref_filter, path, 0))
>> +	if (data->ref_filter && !prefixcmp(data->ref_filter, "refs/")
>> +	    && fnmatch(data->ref_filter, path, 0))
>>   		return 0;
>
> What does this mean?  "When --refs is specified, if it begins with
> refs/ then do not show unmatching path, but let any path be subject
> to the following if --refs does not begin with refs/" sounds like a
> broken logic, unless you add another fnmatch() later in the codepath
> to compensate.  And you indeed do so, but then at that point, do we
> still need this "if(...) return 0" at all?
>
> I think it can and should be improved here, and then the one in the
> main logic you added can be removed.
>
> Wouldn't it make more sense to see if the given pattern matches a
> tail substring of the ref, instead of using the hardcoded "strip
> refs/heads/, refs/tags or refs/, and then match once" logic?  That
> way, --refs=origin/* can find refs/remotes/origin/master by running
> fnmatch of origin/* against its substrings, i.e.
>
> 	refs/remotes/origin/master
>          remotes/origin/master
>          origin/master
>
> and find that the pattern matches it.
>
> Perhaps it is just the matter of adding something like:
>
> 	static int subpath_matches(const char *path, const char	*filter)
> 	{
> 		const char *subpath = path;
> 		while (subpath) {
>                  	if (!fnmatch(data->ref_filter, subpath, 0))
> 				return subpath - path;
> 			subpath = strchr(path, '/');
   					 subpath
Show 27 quoted lines
>                          if (subpath)
> 	                        subpath++;
> 		}
> 		return -1;
> 	}
>
> and then at the beginning of name_ref() do this:
>
> 	int can_abbreviate_output = data->name_only;
>
> 	if (data->tags_only && prefixcmp(path, "refs/tags/"))
> 		return 0;
> 	if (data->ref_filter) {
>          	switch (subpath_matches(path, data->ref_filter)) {
> 		case -1: /* did not match */
> 			return 0;
> 		default: /* matched subpath */
> 			can_abbreviate_output = 1;
> 			break;
> 		case 0: /* matched fully */
>                  	break;
> 		}
> 	}
>
> The logic before calling name_rev() will be kept as "only decide how
> the output looks like", without mixing the unrelated "decide if we
> want to use it" logic in.
Looks good to me with the little change above!
I'll resend v2 with changes in this and your other reply.

Thanks, Namhyung

← back to recent threads