Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <junkio@cox.net>
To: Petr Baudis <pasky@ucw.cz>
Cc: Kay Sievers <kay.sievers@vrfy.org>, git@vger.kernel.org, junkio@cox.net
Subject: Re: Broken adding of cache entries
Date: Sat, 07 May 2005 12:22:21 -0700	[thread overview]
Message-ID: <7vhdhealjm.fsf@assigned-by-dhcp.cox.net> (raw)
In-Reply-To: <20050507152849.GD9495@pasky.ji.cz> (Petr Baudis's message of "Sat, 7 May 2005 17:28:50 +0200")

I'll keep this in git-jc repository until Linus returns.  Pasky
and Kay could you give it a try?

-- test case --

$ ls -a
./  ../
$ git-init-db
defaulting to local storage area
$ date >path
$ git-update-cache --add path
$ rm path
$ mkdir path
$ date >path/file
$ git-update-cache --add path/file
$ git-ls-files --stage
100644 1738f2536b1201218c41153941da065cc26174c9 0 path
100644 620c72f1c1de15f56ff9d63d6d7cdc69e828f1e3 0 path/file
$ git-ls-tree $(git-write-tree)                     ;# using old one
100644	blob	1738f2536b1201218c41153941da065cc26174c9	path
040000	tree	ec116937f223e3df95aeac9f076902ae1618ae98	path
$ ../git-write-tree                                 ;# using new one
You have both path and path/file
fatal: write-tree: not able to write tree
$ exit

----------------------------------------------------------------
Notice index that has path and path/file and refuse to write such a tree.

Kay Sievers noticed that you can have both path and path/file in
the cache and write-tree happily creates a tree object from such
a state.  Since a merge can result in such situation and the
user should be able to see the situation by looking at the
cache, rather than forbidding add_cache_entry() to create such
conflicts, fix it by making write-tree refuse to write such an
nonsensical tree.

Signed-off-by: Junio C Hamano <junkio@cox.net>
---

write-tree.c |   35 +++++++++++++++++++++++++++++++----
1 files changed, 31 insertions(+), 4 deletions(-)

--- a/write-tree.c
+++ b/write-tree.c
@@ -84,7 +84,7 @@ static int write_tree(struct cache_entry
 
 int main(int argc, char **argv)
 {
-	int i, unmerged;
+	int i, funny;
 	int entries = read_cache();
 	unsigned char sha1[20];
 
@@ -92,18 +92,45 @@ int main(int argc, char **argv)
 		die("write-tree: no cache contents to write");
 
 	/* Verify that the tree is merged */
-	unmerged = 0;
+	funny = 0;
 	for (i = 0; i < entries; i++) {
 		struct cache_entry *ce = active_cache[i];
 		if (ntohs(ce->ce_flags) & ~CE_NAMEMASK) {
-			if (++unmerged > 10) {
+			if (10 < ++funny) {
 				fprintf(stderr, "...\n");
 				break;
 			}
 			fprintf(stderr, "%s: unmerged (%s)\n", ce->name, sha1_to_hex(ce->sha1));
 		}
 	}
-	if (unmerged)
+	if (funny)
+		die("write-tree: not able to write tree");
+
+	/* Also verify that the cache does not have path and path/file
+	 * at the same time.  At this point we know the cache has only
+	 * stage 0 entries.
+	 */
+	funny = 0;
+	for (i = 0; i < entries - 1; i++) {
+		/* path/file always comes after path because of the way
+		 * the cache is sorted.  Also path can appear only once,
+		 * which means conflicting one would immediately follow.
+		 */
+		const char *this_name = active_cache[i]->name;
+		const char *next_name = active_cache[i+1]->name;
+		int this_len = strlen(this_name);
+		if (this_len < strlen(next_name) &&
+		    strncmp(this_name, next_name, this_len) == 0 &&
+		    next_name[this_len] == '/') {
+			if (10 < ++funny) {
+				fprintf(stderr, "...\n");
+				break;
+			}
+			fprintf(stderr, "You have both %s and %s\n",
+				this_name, next_name);
+		}
+	}
+	if (funny)
 		die("write-tree: not able to write tree");
 
 	/* Ok, write it out */


  parent reply	other threads:[~2005-05-07 19:15 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1115408460.32065.37.camel@localhost.localdomain>
     [not found] ` <20050506231447.GG32629@pasky.ji.cz>
     [not found]   ` <1115421933.32065.111.camel@localhost.localdomain>
     [not found]     ` <20050506233003.GJ32629@pasky.ji.cz>
     [not found]       ` <1115423450.32065.138.camel@localhost.localdomain>
     [not found]         ` <20050507001409.GP32629@pasky.ji.cz>
     [not found]           ` <1115431767.32065.182.camel@localhost.localdomain>
2005-05-07 15:28             ` Broken adding of cache entries Petr Baudis
2005-05-07 18:42               ` Junio C Hamano
2005-05-07 19:22               ` Junio C Hamano [this message]
2005-05-07 22:41                 ` Petr Baudis
2005-05-08  0:43                   ` Junio C Hamano
2005-05-08  1:50                     ` Junio C Hamano
2005-05-08  5:22                       ` Junio C Hamano
2005-05-08 16:59                         ` Petr Baudis
2005-05-08 21:06                           ` Junio C Hamano
2005-05-08 21:22                             ` Petr Baudis
2005-05-08 22:18                               ` Junio C Hamano
2005-05-08 22:22                                 ` Junio C Hamano
2005-05-08 22:42                                   ` Junio C Hamano

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=7vhdhealjm.fsf@assigned-by-dhcp.cox.net \
    --to=junkio@cox.net \
    --cc=git@vger.kernel.org \
    --cc=kay.sievers@vrfy.org \
    --cc=pasky@ucw.cz \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox