git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v1 1/2] refs: extract function to normalize partial refs

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 4, 2017, 02:27 UTC
Message-ID
<xmqqo9oiok10.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<20171104004144.5975-2-rafa.almas@gmail.com>
Rafael Ascensão <rafa.almas@gmail.com> writes:
> `for_each_glob_ref_in` has some code built into it that converts
> partial refs like 'heads/master' to their full qualified form
s/full/&y/ (read: that "full" needs "y" at the end).
> 'refs/heads/master'. It also assume a trailing '/*' if no glob
s/assume/&s/
Show 8 quoted lines
> characters are present in the pattern.
>
> Extract that logic to its own function which can be reused elsewhere
> where the same behaviour is needed, and add an ENSURE_GLOB flag
> to toggle if a trailing '/*' is to be appended to the result.
>
> Signed-off-by: Kevin Daudt <me@ikke.info>
> Signed-off-by: Rafael Ascensão <rafa.almas@gmail.com>

Could you explain Kevin's sign-off we see above? It is a bit unusual (I am not yet saying it is wrong---I cannot judge until I find out why it is there) to see a patch from person X with sign off from person Y and then person X in that order. It is normal for a patch authored by person X to have sign-off by X and then Y if X wrote it, signed it off and passed to Y, and then Y resent it after signing it off (while preserving the authorship of X by adding an in-body From: header), but I do not think that is what we have here.

It could be that you did pretty much all the work on this patch and Kevin helped you to polish this patch off-list, in which case the usual thing to do is to use "Helped-by: Kevin" instead.

Show 17 quoted lines
> ---
>  refs.c | 34 ++++++++++++++++++++--------------
>  refs.h | 16 ++++++++++++++++
>  2 files changed, 36 insertions(+), 14 deletions(-)
>
> diff --git a/refs.c b/refs.c
> index c590a992f..1e74b48e6 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -369,32 +369,38 @@ int head_ref_namespaced(each_ref_fn fn, void *cb_data)
>  	return ret;
>  }
>  
> -int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
> -	const char *prefix, void *cb_data)
> +void normalize_glob_ref(struct strbuf *normalized_pattern, const char *prefix,
> +		const char *pattern, int flags)

It is better to use "unsigned" for a single word "flags" used as a collection of bits. In older parts of the codebase, we have codepaths that pass signed int as a flags word, simply because we didn't know better, but we do not have to spread that practice to new code.

Show 24 quoted lines
>  {
> -	struct strbuf real_pattern = STRBUF_INIT;
> -	struct ref_filter filter;
> -	int ret;
> -
>  	if (!prefix && !starts_with(pattern, "refs/"))
> -		strbuf_addstr(&real_pattern, "refs/");
> +		strbuf_addstr(normalized_pattern, "refs/");
>  	else if (prefix)
> -		strbuf_addstr(&real_pattern, prefix);
> -	strbuf_addstr(&real_pattern, pattern);
> +		strbuf_addstr(normalized_pattern, prefix);
> +	strbuf_addstr(normalized_pattern, pattern);
>  
> -	if (!has_glob_specials(pattern)) {
> +	if (!has_glob_specials(pattern) && (flags & ENSURE_GLOB)) {
>  		/* Append implied '/' '*' if not present. */
> -		strbuf_complete(&real_pattern, '/');
> +		strbuf_complete(normalized_pattern, '/');
>  		/* No need to check for '*', there is none. */
> -		strbuf_addch(&real_pattern, '*');
> +		strbuf_addch(normalized_pattern, '*');
>  	}
> +}

The above looks like a pure and regression-free code movement (plus a small new feature) that is faithful to the original, which is good.

I however notice that addition of /* to the tail is trying to be careful by using strbuf_complete('/'), but prefixing with "refs/" does not and we would end up with a double-slash if pattern begins with a slash. The contract between the caller of this function (or its original, which is for_each_glob_ref_in()) and the callee is that prefix must not begin with '/', so it may be OK, but we might want to add "if (*pattern == '/') BUG(...)" at the beginning.

I dunno. In any case, that is totally outside the scope of this two patch series.

Show 10 quoted lines
> diff --git a/refs.h b/refs.h
> index a02b628c8..9f9a8bb27 100644
> --- a/refs.h
> +++ b/refs.h
> @@ -312,6 +312,22 @@ int for_each_namespaced_ref(each_ref_fn fn, void *cb_data);
>  int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data);
>  int for_each_rawref(each_ref_fn fn, void *cb_data);
>  
> +/*
> + * Normalizes partial refs to their full qualified form.
s/full/&y/;
> + * If prefix is NULL, will prepend 'refs/' to the pattern if it doesn't start
> + * with 'refs/'. Results in refs/<pattern>
> + *
> + * If prefix is not NULL will result in <prefix>/<pattern>
s/NULL/&,/;
Show 13 quoted lines
> + *
> + * If ENSURE_GLOB is set and no glob characters are found in the
> + * pattern, a trailing </><*> will be appended to the result.
> + * (<> characters to avoid breaking C comment syntax)
> + */
> +
> +#define ENSURE_GLOB 1
> +void normalize_glob_ref (struct strbuf *normalized_pattern, const char *prefix,
> +				const char *pattern, int flags);
> +
>  static inline const char *has_glob_specials(const char *pattern)
>  {
>  	return strpbrk(pattern, "?*[");
Thanks.  Other than the above minor points, looks good to me.
Previous: Rafael AscensãoNext: Rafael Ascensão
Message 3 of 24 in “Add option to git log to choose which refs receive decoration”
  1. 0/2 Add option to git log to choose which refs receive decorationRafael Ascensão, Nov 4, 2017
  2. 1/2 refs: extract function to normalize partial refsRafael Ascensão, Nov 4, 2017
  3. Junio C HamanoNov 4, 2017
  4. Rafael AscensãoNov 4, 2017
  5. Kevin DaudtNov 4, 2017
  6. Michael HaggertyNov 5, 2017
  7. Michael HaggertyNov 5, 2017
  8. Junio C HamanoNov 6, 2017
  9. Rafael AscensãoNov 6, 2017
  10. Michael HaggertyNov 6, 2017
  11. 2/2 log: add option to choose which refs to decorateRafael Ascensão, Nov 4, 2017
  12. Junio C HamanoNov 4, 2017
  13. Rafael AscensãoNov 4, 2017
  14. Junio C HamanoNov 5, 2017
  15. Junio C HamanoNov 5, 2017
  16. Rafael AscensãoNov 6, 2017
  17. Junio C HamanoNov 6, 2017
  18. Michael HaggertyNov 6, 2017
  19. Jacob KellerNov 6, 2017
  20. Junio C HamanoNov 7, 2017
  21. Rafael AscensãoNov 10, 2017
  22. Junio C HamanoNov 10, 2017
  23. log: add option to choose which refs to decorateRafael Ascensão, Nov 21, 2017
  24. Junio C HamanoNov 22, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.