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

Re: [PATCH 1/2] pathspec: allow querying for attributes

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 13, 2017, 02:43 UTC
Message-ID
<xmqqo9x5uc9u.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<20170309210756.105566-2-bmwill@google.com>
Brandon Williams <bmwill@google.com> writes:
Show 16 quoted lines
> +struct attr_check *attr_check_dup(const struct attr_check *check)
> +{
> +	struct attr_check *ret;
> +
> +	if (!check)
> +		return NULL;
> +
> +	ret = attr_check_alloc();
> +
> +	ret->nr = check->nr;
> +	ret->alloc = check->alloc;
> +	ALLOC_ARRAY(ret->items, ret->nr);
> +	COPY_ARRAY(ret->items, check->items, ret->nr);
> +
> +	return ret;
> +}

Because an attr_check instance cannot be shared and used by multiple threads, we expect that the callers that go multi-thread to copy pathspec to each worker, and preload_index(), which is an existing example of such a caller, already does so with copy_pathspec().

Makes sense.
Show 26 quoted lines
> @@ -565,26 +653,47 @@ void parse_pathspec(struct pathspec *pathspec,
>  
>  void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
>  {
> -	int i;
> +	int i, j;
>  
>  	*dst = *src;
>  	ALLOC_ARRAY(dst->items, dst->nr);
>  	COPY_ARRAY(dst->items, src->items, dst->nr);
>  
>  	for (i = 0; i < dst->nr; i++) {
> -		dst->items[i].match = xstrdup(src->items[i].match);
> -		dst->items[i].original = xstrdup(src->items[i].original);
> +		struct pathspec_item *d = &dst->items[i];
> +		struct pathspec_item *s = &src->items[i];
> +
> +		d->match = xstrdup(s->match);
> +		d->original = xstrdup(s->original);
> +
> +		ALLOC_ARRAY(d->attr_match, d->attr_match_nr);
> +		COPY_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);
> +		for (j = 0; j < d->attr_match_nr; j++) {
> +			const char *value = s->attr_match[j].value;
> +			if (value)
> +				d->attr_match[j].value = xstrdup(value);
We have xstrdup_or_null(), which may help here.
Show 26 quoted lines
> +		}
> +
> +		d->attr_check = attr_check_dup(s->attr_check);
>  	}
>  }
>  
>  void clear_pathspec(struct pathspec *pathspec)
>  {
> -	int i;
> +	int i, j;
>  
>  	for (i = 0; i < pathspec->nr; i++) {
>  		free(pathspec->items[i].match);
>  		free(pathspec->items[i].original);
> +
> +		for (j = 0; j < pathspec->items[j].attr_match_nr; j++)
> +			free(pathspec->items[i].attr_match[j].value);
> +		free(pathspec->items[i].attr_match);
> +
> +		if (pathspec->items[i].attr_check)
> +			attr_check_free(pathspec->items[i].attr_check);
>  	}
> +
>  	free(pathspec->items);
>  	pathspec->items = NULL;
>  	pathspec->nr = 0;
OK, makes sense.
Show 13 quoted lines
> diff --git a/t/t6135-pathspec-with-attrs.sh b/t/t6135-pathspec-with-attrs.sh
> new file mode 100755
> index 000000000..b5e5a0607
> --- /dev/null
> +++ b/t/t6135-pathspec-with-attrs.sh
> @@ -0,0 +1,181 @@
> +#!/bin/sh
> +
> +test_description='test labels in pathspecs'
> +. ./test-lib.sh
> +
> +test_expect_success 'setup a tree' '
> +	cat <<-EOF >expect &&

Minor style nit. Quote the 'EOF' marker and you signal to readers that what they'll see are literally the values, and they do not have to worry about $variable_interpolation. I.e.

	cat <<-\EOF >expect &&
Show 6 quoted lines
> +test_expect_success 'fail if attr magic is used places not implemented' '
> +	# The main purpose of this test is to check that we actually fail
> +	# when you attempt to use attr magic in commands that do not implement
> +	# attr magic. This test does not advocate git-add to stay that way,
> +	# though, but git-add is convenient as it has its own internal pathspec
> +	# parsing.

That's thought-provoking ;-) Would it help to add a test-pathspec helper, similar to test-config helper, that serves as a vehicle to test this?

> +	test_must_fail git add ":(attr:labelB)" 2>actual &&
> +	test_i18ngrep "unsupported magic" actual
> +'
Thanks.
Previous: Brandon WilliamsNext: Stefan Beller
Message 5 of 23 in “bringing attributes to pathspecs”
  1. 0/2 bringing attributes to pathspecsBrandon Williams, Mar 9, 2017
  2. 1/2 pathspec: allow querying for attributesBrandon Williams, Mar 9, 2017
  3. Jonathan TanMar 9, 2017
  4. Brandon WilliamsMar 10, 2017
  5. Junio C HamanoMar 13, 2017
  6. Stefan BellerMar 13, 2017
  7. 2/2 pathspec: allow escaped query valuesBrandon Williams, Mar 9, 2017
  8. Jonathan TanMar 9, 2017
  9. Brandon WilliamsMar 10, 2017
  10. Stefan BellerMar 9, 2017
  11. 0/2 bringing attributes to pathspecsBrandon Williams, Mar 10, 2017
  12. 1/2 pathspec: allow querying for attributesBrandon Williams, Mar 10, 2017
  13. Jonathan TanMar 10, 2017
  14. Brandon WilliamsMar 11, 2017
  15. 2/2 pathspec: allow escaped query valuesBrandon Williams, Mar 10, 2017
  16. 0/2 bringing attributes to pathspecsBrandon Williams, Mar 13, 2017
  17. 1/2 pathspec: allow querying for attributesBrandon Williams, Mar 13, 2017
  18. 2/2 pathspec: allow escaped query valuesBrandon Williams, Mar 13, 2017
  19. Junio C HamanoMar 13, 2017
  20. Brandon WilliamsMar 13, 2017
  21. Duy NguyenMar 21, 2017
  22. Junio C HamanoMar 21, 2017
  23. Brandon WilliamsMar 21, 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.