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

Re: [PATCH] pathspec: adjust prefixlen after striping trailing slash

From
Jens Lehmann <jens.lehmann@web.de>
Date
Apr 19, 2015, 12:53 UTC
Message-ID
<5533A567.7070301@web.de>
In-Reply-To
<1429319946-19890-1-git-send-email-pclouds@gmail.com>
Am 18.04.2015 um 03:19 schrieb Nguyễn Thái Ngọc Duy:
Show 32 quoted lines
> A path(spec) from git perspective consists of two parts, the prefix,
> and the rest. The prefix covers the part of `pwd` after expanding ".."
> components. The split is to support case-insensitive match in a sane
> way (see 93d9353, especially the big comment block added in dir.c).
>
> Normally the prefix is found after prefix_path_gently() and that will
> be it. Unfortunately the *STRIP_SUBMODULE* flags can strip the
> trailing slash (see 2ce53f9 for the reason) and cut into this prefix
> part. In the test, the prefix is "submodule/", but the final path is
> just "submodule". We need to readjust prefix info when this happens.
>
> The assert() that catches this is turned to die() to make sure it's
> always active. prefix_pathspec() is not in any critical path to be a
> performance concern because of this change.
>
> 93d9353 (parse_pathspec: accept :(icase)path syntax - 2013-07-14)
> 2ce53f9 (git add: do not add files from a submodule - 2009-01-02)
>
> Noticed-by: djanos_ (via irc)
> Helped-by: Dennis Kaarsemaker <dennis@kaarsemaker.net>
> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
> ---
>   On Fri, Apr 17, 2015 at 2:27 AM, Jens Lehmann <Jens.Lehmann@web.de> wrote:
>   > The problem seems to be that "subrepo" is still registered as a
>   > submodule in the index. But we should see a proper error message
>   > instead of an assert in that case ... CCed Duy who knows much
>   > more about pathspec.c than me.
>
>   This deals with the bug in pathspec code. I leave it to you to decide
>   how git-add should do when a submodule is registered in index, but
>   it's no longer a submodule in worktree. Yeah maybe it should error
>   out.

Thanks for answering with a patch! I tested it and it silences the assert and doesn't add anything below the gitlink to the index.

And me thinks it should error out. We already do that when trying to add a specific file from above the gitlink:

    $ git add subrepo/a
    fatal: Pathspec 'subrepo/a' is in submodule 'subrepo'
And from below the gitlink:
    $ cd subrepo
    $ git add a
    fatal: Pathspec 'a' is in submodule 'subrepo'

I think we should do the same when using "git add ." below the gitlink.

Show 72 quoted lines
>   pathspec.c                    | 18 +++++++++++++++---
>   t/t3703-add-magic-pathspec.sh |  8 ++++++++
>   2 files changed, 23 insertions(+), 3 deletions(-)
>
> diff --git a/pathspec.c b/pathspec.c
> index 9304ee3..aa5e2c7 100644
> --- a/pathspec.c
> +++ b/pathspec.c
> @@ -262,7 +262,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item,
>   	} else
>   		item->original = elt;
>   	item->len = strlen(item->match);
> -	item->prefix = prefixlen;
>
>   	if ((flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP) &&
>   	    (item->len >= 1 && item->match[item->len - 1] == '/') &&
> @@ -292,6 +291,15 @@ static unsigned prefix_pathspec(struct pathspec_item *item,
>   				     elt, ce_len, ce->name);
>   		}
>
> +	/*
> +	 * Adjust prefixlen if the above trailing slash stripping cuts
> +	 * into the prefix part
> +	 */
> +	if ((flags & (PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP |
> +		      PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE)) &&
> +	    prefixlen > item->len)
> +		prefixlen = item->len;
> +
>   	if (magic & PATHSPEC_LITERAL)
>   		item->nowildcard_len = item->len;
>   	else {
> @@ -299,6 +307,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item,
>   		if (item->nowildcard_len < prefixlen)
>   			item->nowildcard_len = prefixlen;
>   	}
> +	item->prefix = prefixlen;
>   	item->flags = 0;
>   	if (magic & PATHSPEC_GLOB) {
>   		/*
> @@ -313,8 +322,11 @@ static unsigned prefix_pathspec(struct pathspec_item *item,
>   	}
>
>   	/* sanity checks, pathspec matchers assume these are sane */
> -	assert(item->nowildcard_len <= item->len &&
> -	       item->prefix         <= item->len);
> +	if (!(item->nowildcard_len <= item->len &&
> +	      item->prefix         <= item->len))
> +		die("BUG: item->nowildcard_len (%d) or item->prefix (%d)"
> +		    " is longer than item->len (%d)",
> +		    item->nowildcard_len, item->prefix, item->len);
>   	return magic;
>   }
>
> diff --git a/t/t3703-add-magic-pathspec.sh b/t/t3703-add-magic-pathspec.sh
> index 5115de7..cced8c4 100755
> --- a/t/t3703-add-magic-pathspec.sh
> +++ b/t/t3703-add-magic-pathspec.sh
> @@ -55,4 +55,12 @@ test_expect_success COLON_DIR 'a file with the same (short) magic name exists' '
>   	git add -n "./:/bar"
>   '
>
> +test_expect_success 'prefix is updated after trailing slash is stripped' '
> +	git init submodule &&
> +	( cd submodule && test_commit test ) &&
> +	git add submodule &&
> +	mv submodule/.git submodule/dotgit &&
> +	( cd submodule && git add . )
> +'
> +
>   test_done
>
Previous: Nguyễn Thái Ngọc DuyNext: Duy Nguyen
Message 5 of 15 in “assert failed in submodule edge case”
  1. Dennis KaarsemakerApr 13, 2015
  2. Dennis KaarsemakerApr 13, 2015
  3. Jens LehmannApr 16, 2015
  4. pathspec: adjust prefixlen after striping trailing slashNguyễn Thái Ngọc Duy, Apr 18, 2015
  5. Jens LehmannApr 19, 2015
  6. Duy NguyenApr 20, 2015
  7. Junio C HamanoApr 20, 2015
  8. Duy NguyenApr 20, 2015
  9. Junio C HamanoApr 21, 2015
  10. Jens LehmannApr 22, 2015
  11. Junio C HamanoApr 22, 2015
  12. Jens LehmannApr 22, 2015
  13. Junio C HamanoApr 23, 2015
  14. Duy NguyenJun 14, 2015
  15. Junio C HamanoJun 14, 2015

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.