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

Re: [PATCH] Check order when reading index

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 21, 2014, 18:51 UTC
Message-ID
<xmqq38cpsmli.fsf@gitster.dls.corp.google.com>
In-Reply-To
<1408628606-12975-1-git-send-email-jsorianopastor@gmail.com>
Jaime Soriano Pastor <jsorianopastor@gmail.com> writes:
Show 14 quoted lines
> Signed-off-by: Jaime Soriano Pastor <jsorianopastor@gmail.com>
> ---
>  read-cache.c | 18 ++++++++++++++++++
>  1 file changed, 18 insertions(+)
>
> diff --git a/read-cache.c b/read-cache.c
> index 7f5645e..e117d3a 100644
> --- a/read-cache.c
> +++ b/read-cache.c
> @@ -1438,6 +1438,21 @@ static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk,
>  	return ce;
>  }
>  
> +void check_next_ce(struct cache_entry *ce, struct cache_entry *next_ce) {
Have opening brace for the function on its own line, i.e.
	void check_next_ce(struct cache_entry *ce, struct cache_entry *next_ce)
	{
The function might be misnamed (see below), though.
> +	if (!ce || !next_ce)
> +		return;

Hmph, would it be either a programming error or a corrupt index input to see a NULL in either of these variables?

> +	if (cache_name_compare(ce->name, ce_namelen(ce),
> +						   next_ce->name, ce_namelen(next_ce)) > 1)
An odd indentation that is overly deep to make it hard to read.
	if (cache_name_compare(ce->name, ce_namelen(ce),
			       next_ce->name, ce_namelen(next_ce)) > 1)

should be sufficient (replacing 7-SP before next_ce with a HT is OK if the existing code nearby does so).

What is the significance of the return value from cache_name_compare() that is strictly greater than 1 (as opposed to merely "is it positive?")?

Perhaps you want something that is modeled after ce_same_name() that ignores the stage, i.e.

	int ce_name_compare(const struct cache_entry *a, const struct cache_entry *b)
	{
		return strcmp(a->ce_name, b->ce_name);
	}
without reimplementing the cache-name-compare() as
	int bad_ce_same_name(const struct cache_entry *a, const struct cache_entry *b)
	{
        	return !ce_same_name(a, b);
	}

to keep the "two names with different length could never be the same" optimization.

	- if (0 <= ce_name_compare(ce, next)) then the names are not sorted
        - if (!stage(ce) && !name_compare(ce, next)) then the merged
          entry 'ce' is not the only entry for the path
Show 5 quoted lines
> +		die("Unordered stage entries in index");
> +	if (ce_same_name(ce, next_ce)) {
> +		if (!ce_stage(ce))
> +			die("Multiple stage entries for merged file '%s'",
> +				ce->name);
Show 14 quoted lines
> +		if (ce_stage(ce) >= ce_stage(next_ce))
> +			die("Unordered stage entries for '%s'", ce->name);
> +	}
> +}
> +
>  /* remember to discard_cache() before reading a different cache! */
>  int read_index_from(struct index_state *istate, const char *path)
>  {
> @@ -1499,6 +1514,9 @@ int read_index_from(struct index_state *istate, const char *path)
>  		ce = create_from_disk(disk_ce, &consumed, previous_name);
>  		set_index_entry(istate, i, ce);
>  
> +		if (i > 0)
> +			check_next_ce(istate->cache[i-1], ce);
Have a SP each on both sides of binary operator "-".

Judging from the way this helper function is used, it looks like check_with_previous_ce() is a more appropriate name. After all, you are not checking the next ce which you haven't even created yet ;-)

Thanks.
Previous: Duy NguyenNext: Jaime Soriano Pastor
Message 26 of 51 in “read-cache.c: Ensure unmerged entries are removed”
  1. read-cache.c: Ensure unmerged entries are removedJaime Soriano Pastor, Aug 12, 2014
  2. read-cache.c: Ensure unmerged entries are removedJaime Soriano Pastor, Aug 12, 2014
  3. Junio C HamanoAug 12, 2014
  4. Jaime Soriano PastorAug 13, 2014
  5. Junio C HamanoAug 13, 2014
  6. Jaime Soriano PastorAug 15, 2014
  7. Junio C HamanoAug 15, 2014
  8. Jaime Soriano PastorAug 16, 2014
  9. Junio C HamanoAug 18, 2014
  10. Jaime Soriano PastorAug 18, 2014
  11. 0/4 Handling unmerged files with merged entriesJaime Soriano Pastor, Aug 20, 2014
  12. 1/4 read_index_unmerged doesn't loop forever if merged stage exists for unmerged fileJaime Soriano Pastor, Aug 20, 2014
  13. 2/4 Error out when adding a file with merged and unmerged entriesJaime Soriano Pastor, Aug 20, 2014
  14. 3/4 Added tests for the case of merged and unmerged entries for the same fileJaime Soriano Pastor, Aug 20, 2014
  15. Junio C HamanoAug 20, 2014
  16. Jaime Soriano PastorAug 21, 2014
  17. Junio C HamanoAug 21, 2014
  18. 4/4 git update-index --cacheinfo can be used to select a stage when there are merged and unmerged entriesJaime Soriano Pastor, Aug 20, 2014
  19. Junio C HamanoAug 20, 2014
  20. Jaime Soriano PastorAug 21, 2014
  21. Junio C HamanoAug 20, 2014
  22. Jaime Soriano PastorAug 21, 2014
  23. Check order when reading indexJaime Soriano Pastor, Aug 21, 2014
  24. Jaime Soriano PastorAug 21, 2014
  25. Duy NguyenAug 21, 2014
  26. Junio C HamanoAug 21, 2014
  27. 1/2 Check order when reading indexJaime Soriano Pastor, Aug 24, 2014
  28. 2/2 Loop index increases monotonically when reading unmerged entriesJaime Soriano Pastor, Aug 24, 2014
  29. Jaime Soriano PastorAug 24, 2014
  30. Junio C HamanoAug 25, 2014
  31. Junio C HamanoAug 25, 2014
  32. Jeff KingAug 25, 2014
  33. Junio C HamanoAug 25, 2014
  34. Jaime Soriano PastorAug 26, 2014
  35. Jeff KingAug 26, 2014
  36. Junio C HamanoAug 26, 2014
  37. Jaime Soriano PastorAug 26, 2014
  38. Junio C HamanoAug 26, 2014
  39. 1/2 read_index_from(): catch out of order entries when reading an index fileJaime Soriano Pastor, Aug 27, 2014
  40. 2/2 read_index_unmerged(): remove unnecessary loop index adjustmentJaime Soriano Pastor, Aug 27, 2014
  41. Eric SunshineAug 29, 2014
  42. read_index_from(): catch out of order entries when reading an index fileJaime Soriano Pastor, Aug 29, 2014
  43. Junio C HamanoAug 29, 2014
  44. Jaime Soriano PastorAug 27, 2014
  45. Junio C HamanoAug 27, 2014
  46. Jaime Soriano PastorAug 27, 2014
  47. Jaime Soriano PastorAug 25, 2014
  48. Johannes SixtAug 21, 2014
  49. Junio C HamanoAug 21, 2014
  50. Junio C HamanoAug 12, 2014
  51. Jaime Soriano PastorAug 13, 2014

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.