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

Re: [PATCH v3 1/2] cache-tree: invalidate i-t-a paths after generating trees

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 13, 2012, 02:04 UTC
Message-ID
<7v4njq1ze7.fsf@alter.siamese.dyndns.org>
In-Reply-To
<1355362747-13474-1-git-send-email-pclouds@gmail.com>
Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
Show 38 quoted lines
> Intent-to-add entries used to forbid writing trees so it was not a
> problem. After commit 3f6d56d (commit: ignore intent-to-add entries
> instead of refusing - 2012-02-07), we can generate trees from an index
> with i-t-a entries.
>
> However, the commit forgets to invalidate all paths leading to i-t-a
> entries. With fully valid cache-tree (e.g. after commit or
> write-tree), diff operations may prefer cache-tree to index and not
> see i-t-a entries in the index, because cache-tree does not have them.
>
> Reported-by: Jonathon Mah <me@JonathonMah.com>
> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
> ---
>  This version ensures that entry_count can only be >= -1 after
>  update_one returns. Not ideal but good enough.
>
>  cache-tree.c          | 40 ++++++++++++++++++++++++++++++++++++----
>  t/t2203-add-intent.sh | 20 ++++++++++++++++++++
>  2 files changed, 56 insertions(+), 4 deletions(-)
>
> diff --git a/cache-tree.c b/cache-tree.c
> index 28ed657..1fbc81a 100644
> --- a/cache-tree.c
> +++ b/cache-tree.c
> @@ -248,6 +248,7 @@ static int update_one(struct cache_tree *it,
>  	int missing_ok = flags & WRITE_TREE_MISSING_OK;
>  	int dryrun = flags & WRITE_TREE_DRY_RUN;
>  	int i;
> +	int to_invalidate = 0;
>  
>  	if (0 <= it->entry_count && has_sha1_file(it->sha1))
>  		return it->entry_count;
> @@ -324,7 +325,14 @@ static int update_one(struct cache_tree *it,
>  			if (!sub)
>  				die("cache-tree.c: '%.*s' in '%s' not found",
>  				    entlen, path + baselen, path);
> -			i += sub->cache_tree->entry_count - 1;
> +			i--; /* this entry is already counted in "sub" */
Huh?

The "-1" in the original is the bog-standard compensation for the for(;;i++) loop.

Show 7 quoted lines
> +			if (sub->cache_tree->entry_count < 0) {
> +				i -= sub->cache_tree->entry_count;
> +				sub->cache_tree->entry_count = -1;
> +				to_invalidate = 1;
> +			}
> +			else
> +				i += sub->cache_tree->entry_count;

While the rewritten version is not *wrong* per-se, I have a feeling that it may be much easier to read if written like this:

	if (sub->cache_tree_entry_count < 0) {
		to_invalidate = 1;
                to_skip = 0 - sub->cache_tree_entry_count;
		sub->cache_tree_entry_count = -1;
	} else {
		to_skip = sub->cache_tree_entry_count;
	}
        i += to_skip - 1;
Show 22 quoted lines
> @@ -360,7 +383,7 @@ static int update_one(struct cache_tree *it,
>  	}
>  
>  	strbuf_release(&buffer);
> -	it->entry_count = i;
> +	it->entry_count = to_invalidate ? -i : i;
>  #if DEBUG
>  	fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
>  		it->entry_count, it->subtree_nr,
> @@ -381,6 +404,15 @@ int cache_tree_update(struct cache_tree *it,
>  	i = update_one(it, cache, entries, "", 0, flags);
>  	if (i < 0)
>  		return i;
> +	/*
> +	 * update_one() uses negative entry_count as a way to mark an
> +	 * entry invalid _and_ pass the number of entries back to
> +	 * itself at the parent level. This is for internal use and
> +	 * should not be leaked out after the top-level update_one
> +	 * exits.
> +	 */
> +	if (it->entry_count < 0)
> +		it->entry_count = -1;

Nice. I think what entry_count means immediately after update_one() returned should be commented near the beginning of that function, too, though.

Thanks.
Previous: Junio C HamanoNext: Nguyen Thai Ngoc Duy
Message 15 of 16 in “Bug: write-tree corrupts intent-to-add index state”
  1. Jonathon MahNov 6, 2012
  2. Nguyen Thai Ngoc DuyNov 6, 2012
  3. cache-tree: invalidate i-t-a paths after writing treesNguyễn Thái Ngọc Duy, Nov 9, 2012
  4. Junio C HamanoNov 9, 2012
  5. Nguyen Thai Ngoc DuyNov 10, 2012
  6. Junio C HamanoNov 30, 2012
  7. Nguyen Thai Ngoc DuyNov 30, 2012
  8. cache-tree: invalidate i-t-a paths after generating treesNguyễn Thái Ngọc Duy, Dec 8, 2012
  9. Junio C HamanoDec 10, 2012
  10. Nguyen Thai Ngoc DuyDec 10, 2012
  11. Junio C HamanoDec 10, 2012
  12. 1/2 cache-tree: invalidate i-t-a paths after generating treesNguyễn Thái Ngọc Duy, Dec 13, 2012
  13. 2/2 cache-tree: remove dead i-t-a code in verify_cache()Nguyễn Thái Ngọc Duy, Dec 13, 2012
  14. Junio C HamanoDec 13, 2012
  15. Junio C HamanoDec 13, 2012
  16. Nguyen Thai Ngoc DuyDec 15, 2012

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.