All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Jaime Soriano Pastor <jsorianopastor@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 0/4] Handling unmerged files with merged entries
Date: Wed, 20 Aug 2014 15:19:03 -0700	[thread overview]
Message-ID: <xmqqr40ast2g.fsf@gitster.dls.corp.google.com> (raw)
In-Reply-To: <cover.1408533065.git.jsorianopastor@gmail.com> (Jaime Soriano Pastor's message of "Wed, 20 Aug 2014 13:25:59 +0200")

Jaime Soriano Pastor <jsorianopastor@gmail.com> writes:

> New approach for the case of finding unmerged files with merged entries
> in the index.
> After some discussion the solution tries to:
> - Avoid the problems with infinite loops in this case.
> - Provide better information to the user in the commands affected.
> - Make sure there are ways to clean the index.
> - Provide also a way to specifically recover each one of the files with
>   this problem.
>
> With these patches the behaviour of these commands (for this case) change:
> - git reset is able to finish, cleaning the index, but warning out the
>   information about the removed stages.
> - git merge is able to finish, reporting that there is a merge in progress as
>   usual, it also warns about the unmerged files with merged entries.
> - git add fails when this case happens, telling the user to check the state
>   with 'git ls-files -s', before, it did nothing. The same with git commit -a.
> - git update-index --cacheinfo can be used to select an specific staged
>   version to resolve the conflict, without the need of reseting the working
>   copy. It did nothing before.
>
> Tests added for these cases. Rest of the tests remain unchanged and pass too.

Thanks.

After looking at what you did in 1/4, I started to wonder if we can
solve this in add_index_entry_with_check() in a less intrusive way.
When we call the function with a stage #0 entry, we are telling the
index that any entry in higher stage for the same path must
disappear.  Since the current implementation of the function assumes
that the index is not corrupt in this particular way to have both
merged and unmerged entries for the same path, it fails to remove
the higher stage entries.  If we fix the function, wouldn't it make
your 1/4 unnecessary?  Read-only operations such as "ls-files -s"
would not call add_index_entry() so diagnostic tools would not be
affected even with such a fix.

... which may look something like the one attached at the end.

But then it made me wonder even more.

There are other ways a piece of software can leave a corrupt index
for us to read from.  Your fix, or the simpler one I suggested for
that matter, would still assume that the index entries are in the
sorted order, and a corrupt index that does not sort its entries
correctly will cause us to behave in an undefined way.  At some
point we should draw a line and say "Your index is hopelessly
corrupt.", send it back to whatever broken software that originally
wrote such a mess and have the user use that software to fix the
corrupt index up before talking to us.

For that, we need to catch an index whose entries are not sorted and
error out, perhaps when read_index_from() iterates over the mmapped
index entries.  We can even draw that "hopelessly corrupt" line
above the breakage you are addressing and add a check to make sure
no path has both merged and unmerged entries to the same check to
make it error out.

I suspect that such a "detect and error out" may be sufficient and
also may be more robust than the approach that assumes that a
breakage is only to have both merged and unmerged entries for the
same path, the entries are still correctly sorted.


 read-cache.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/read-cache.c b/read-cache.c
index 7f5645e..56006a3 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -943,9 +943,16 @@ static int add_index_entry_with_check(struct index_state *istate, struct cache_e
 	if (pos >= 0) {
 		if (!new_only)
 			replace_index_entry(istate, pos, ce);
-		return 0;
+		/*
+		 * ... but protect ourselves from a corrupt index
+		 * that has an unmerged entry for the same path.
+		 */
+		if (istate->cache_nr <= pos + 1 ||
+		    !ce_same_name(ce, istate->cache[pos + 1]))
+			return 0;
+	} else {
+		pos = -pos-1;
 	}
-	pos = -pos-1;
 
 	/*
 	 * Inserting a merged entry ("stage 0") into the index





[Footnote]

  parent reply	other threads:[~2014-08-20 22:19 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-12 15:31 [PATCH] read-cache.c: Ensure unmerged entries are removed Jaime Soriano Pastor
2014-08-12 15:31 ` Jaime Soriano Pastor
2014-08-12 18:39   ` Junio C Hamano
2014-08-13 22:10     ` Jaime Soriano Pastor
2014-08-13 23:04       ` Junio C Hamano
2014-08-15 19:50         ` Jaime Soriano Pastor
2014-08-15 21:45           ` Junio C Hamano
2014-08-16 14:51             ` Jaime Soriano Pastor
2014-08-18 16:34               ` Junio C Hamano
2014-08-18 17:23                 ` Jaime Soriano Pastor
2014-08-20 11:25                   ` [PATCH 0/4] Handling unmerged files with merged entries Jaime Soriano Pastor
2014-08-20 11:26                     ` [PATCH 1/4] read_index_unmerged doesn't loop forever if merged stage exists for unmerged file Jaime Soriano Pastor
2014-08-20 11:26                     ` [PATCH 2/4] Error out when adding a file with merged and unmerged entries Jaime Soriano Pastor
2014-08-20 11:26                     ` [PATCH 3/4] Added tests for the case of merged and unmerged entries for the same file Jaime Soriano Pastor
2014-08-20 21:00                       ` Junio C Hamano
2014-08-21 13:51                         ` Jaime Soriano Pastor
2014-08-21 22:21                           ` Junio C Hamano
2014-08-20 11:26                     ` [PATCH 4/4] git update-index --cacheinfo can be used to select a stage when there are merged and unmerged entries Jaime Soriano Pastor
2014-08-20 21:08                       ` Junio C Hamano
2014-08-21 13:57                         ` Jaime Soriano Pastor
2014-08-20 22:19                     ` Junio C Hamano [this message]
2014-08-21 13:42                       ` [PATCH 0/4] Handling unmerged files with merged entries Jaime Soriano Pastor
2014-08-21 13:43                         ` [PATCH] Check order when reading index Jaime Soriano Pastor
2014-08-21 13:49                           ` Jaime Soriano Pastor
2014-08-21 13:59                           ` Duy Nguyen
2014-08-21 18:51                           ` Junio C Hamano
2014-08-24 17:57                             ` [PATCH 1/2] " Jaime Soriano Pastor
2014-08-24 17:57                               ` [PATCH 2/2] Loop index increases monotonically when reading unmerged entries Jaime Soriano Pastor
2014-08-24 18:04                                 ` Jaime Soriano Pastor
2014-08-25 17:09                                   ` Junio C Hamano
2014-08-25 17:21                               ` [PATCH 1/2] Check order when reading index Junio C Hamano
2014-08-25 19:44                                 ` Jeff King
2014-08-25 20:52                                   ` Junio C Hamano
2014-08-26 12:08                                     ` Jaime Soriano Pastor
2014-08-26 12:20                                       ` Jeff King
2014-08-26 16:53                                         ` Junio C Hamano
2014-08-26 17:22                                           ` Jaime Soriano Pastor
2014-08-26 17:43                                             ` Junio C Hamano
2014-08-27 19:48                                               ` [PATCH 1/2] read_index_from(): catch out of order entries when reading an index file Jaime Soriano Pastor
2014-08-27 19:48                                                 ` [PATCH 2/2] read_index_unmerged(): remove unnecessary loop index adjustment Jaime Soriano Pastor
2014-08-29  2:16                                                 ` [PATCH 1/2] read_index_from(): catch out of order entries when reading an index file Eric Sunshine
2014-08-29  8:54                                                   ` [PATCH] " Jaime Soriano Pastor
2014-08-29 17:06                                                     ` Junio C Hamano
2014-08-27 19:52                                               ` [PATCH 1/2] Check order when reading index Jaime Soriano Pastor
2014-08-27 21:11                                                 ` Junio C Hamano
2014-08-27 22:13                                                   ` Jaime Soriano Pastor
2014-08-25 20:26                                 ` Jaime Soriano Pastor
2014-08-21 18:40                       ` [PATCH 0/4] Handling unmerged files with merged entries Johannes Sixt
2014-08-21 22:18                         ` Junio C Hamano
2014-08-12 18:31 ` [PATCH] read-cache.c: Ensure unmerged entries are removed Junio C Hamano
2014-08-13 22:10   ` Jaime Soriano Pastor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqr40ast2g.fsf@gitster.dls.corp.google.com \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=jsorianopastor@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.