Notice index that has path and path/file and refuse to write such a tree.
authorJunio C Hamano <junkio@cox.net>
Sat, 7 May 2005 19:22:02 +0000 (12:22 -0700)
committerJunio C Hamano <junkio@cox.net>
Sat, 7 May 2005 19:22:02 +0000 (12:22 -0700)
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.  Here is a test case.

-- 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

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

index 168352853d37bdca71d68ad8312b87b84477dea1..19b6ee5ea3bf17b81a22c0f76ae5da85998552d3 100644 (file)
@@ -84,7 +84,7 @@ static int write_tree(struct cache_entry **cachep, int maxentries, const char *b
 
 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 */