clone: report duplicate entries on case-insensitive filesystems
authorDuy Nguyen <pclouds@gmail.com>
Fri, 17 Aug 2018 18:00:39 +0000 (20:00 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 17 Aug 2018 19:10:37 +0000 (12:10 -0700)
Paths that only differ in case work fine in a case-sensitive
filesystems, but if those repos are cloned in a case-insensitive one,
you'll get problems. The first thing to notice is "git status" will
never be clean with no indication what exactly is "dirty".

This patch helps the situation a bit by pointing out the problem at
clone time. Even though this patch talks about case sensitivity, the
patch makes no assumption about folding rules by the filesystem. It
simply observes that if an entry has been already checked out at clone
time when we're about to write a new path, some folding rules are
behind this.

In the case that we can't rely on filesystem (via inode number) to do
this check, fall back to fspathcmp() which is not perfect but should
not give false positives.

This patch is tested with vim-colorschemes and Sublime-Gitignore
repositories on a JFS partition with case insensitive support on
Linux.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/clone.c
cache.h
entry.c
t/t5601-clone.sh
unpack-trees.c
unpack-trees.h
index 5c439f1394c9a097e48f24d73790f276a958bf2d..0702b0e9d025bc9f353b6b7673a106be1f604124 100644 (file)
@@ -747,6 +747,7 @@ static int checkout(int submodule_progress)
        memset(&opts, 0, sizeof opts);
        opts.update = 1;
        opts.merge = 1;
+       opts.clone = 1;
        opts.fn = oneway_merge;
        opts.verbose_update = (option_verbosity >= 0);
        opts.src_index = &the_index;
diff --git a/cache.h b/cache.h
index 8b447652a7e94ad31fd28134e0f548baa37265db..6d6138f4f1cb8ef4e2e9cb3e22502f9f8c01c940 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -1455,6 +1455,7 @@ struct checkout {
        unsigned force:1,
                 quiet:1,
                 not_new:1,
+                clone:1,
                 refresh_cache:1;
 };
 #define CHECKOUT_INIT { NULL, "" }
diff --git a/entry.c b/entry.c
index b5d1d3cf2312f61b551258759fcc4b217d8a6fa7..8766e2725564b6fc12c28f57e641a507b43735b9 100644 (file)
--- a/entry.c
+++ b/entry.c
@@ -399,6 +399,34 @@ static int check_path(const char *path, int len, struct stat *st, int skiplen)
        return lstat(path, st);
 }
 
+static void mark_colliding_entries(const struct checkout *state,
+                                  struct cache_entry *ce, struct stat *st)
+{
+       int i, trust_ino = check_stat;
+
+#if defined(GIT_WINDOWS_NATIVE)
+       trust_ino = 0;
+#endif
+
+       ce->ce_flags |= CE_MATCHED;
+
+       for (i = 0; i < state->istate->cache_nr; i++) {
+               struct cache_entry *dup = state->istate->cache[i];
+
+               if (dup == ce)
+                       break;
+
+               if (dup->ce_flags & (CE_MATCHED | CE_VALID | CE_SKIP_WORKTREE))
+                       continue;
+
+               if ((trust_ino && dup->ce_stat_data.sd_ino == st->st_ino) ||
+                   (!trust_ino && !fspathcmp(ce->name, dup->name))) {
+                       dup->ce_flags |= CE_MATCHED;
+                       break;
+               }
+       }
+}
+
 /*
  * Write the contents from ce out to the working tree.
  *
@@ -455,6 +483,9 @@ int checkout_entry(struct cache_entry *ce,
                        return -1;
                }
 
+               if (state->clone)
+                       mark_colliding_entries(state, ce, &st);
+
                /*
                 * We unlink the old file, to get the new one with the
                 * right permissions (including umask, which is nasty
index 0b620377448bc3b97d39d2b6beceb4d185edb7ba..f2eb73bc74ae70e3e3515c7346f4f39659c86928 100755 (executable)
@@ -624,10 +624,16 @@ test_expect_success 'clone on case-insensitive fs' '
                        git hash-object -w -t tree --stdin) &&
                c=$(git commit-tree -m bogus $t) &&
                git update-ref refs/heads/bogus $c &&
-               git clone -b bogus . bogus
+               git clone -b bogus . bogus 2>warning
        )
 '
 
+test_expect_success !MINGW,!CYGWIN,CASE_INSENSITIVE_FS 'colliding file detection' '
+       grep X icasefs/warning &&
+       grep x icasefs/warning &&
+       test_i18ngrep "the following paths have collided" icasefs/warning
+'
+
 partial_clone () {
               SERVER="$1" &&
               URL="$2" &&
index cd0680f11e3580ef24e9140dd00a1e38f02557d8..213da8bbb4f90c5e1cbf572bd8640c6093d22e28 100644 (file)
@@ -345,6 +345,46 @@ static struct progress *get_progress(struct unpack_trees_options *o)
        return start_delayed_progress(_("Checking out files"), total);
 }
 
+static void setup_collided_checkout_detection(struct checkout *state,
+                                             struct index_state *index)
+{
+       int i;
+
+       state->clone = 1;
+       for (i = 0; i < index->cache_nr; i++)
+               index->cache[i]->ce_flags &= ~CE_MATCHED;
+}
+
+static void report_collided_checkout(struct index_state *index)
+{
+       struct string_list list = STRING_LIST_INIT_NODUP;
+       int i;
+
+       for (i = 0; i < index->cache_nr; i++) {
+               struct cache_entry *ce = index->cache[i];
+
+               if (!(ce->ce_flags & CE_MATCHED))
+                       continue;
+
+               string_list_append(&list, ce->name);
+               ce->ce_flags &= ~CE_MATCHED;
+       }
+
+       list.cmp = fspathcmp;
+       string_list_sort(&list);
+
+       if (list.nr) {
+               warning(_("the following paths have collided (e.g. case-sensitive paths\n"
+                         "on a case-insensitive filesystem) and only one from the same\n"
+                         "colliding group is in the working tree:\n"));
+
+               for (i = 0; i < list.nr; i++)
+                       fprintf(stderr, "  '%s'\n", list.items[i].string);
+       }
+
+       string_list_clear(&list, 0);
+}
+
 static int check_updates(struct unpack_trees_options *o)
 {
        unsigned cnt = 0;
@@ -359,6 +399,9 @@ static int check_updates(struct unpack_trees_options *o)
        state.refresh_cache = 1;
        state.istate = index;
 
+       if (o->clone)
+               setup_collided_checkout_detection(&state, index);
+
        progress = get_progress(o);
 
        if (o->update)
@@ -423,6 +466,10 @@ static int check_updates(struct unpack_trees_options *o)
        errs |= finish_delayed_checkout(&state);
        if (o->update)
                git_attr_set_direction(GIT_ATTR_CHECKIN, NULL);
+
+       if (o->clone)
+               report_collided_checkout(index);
+
        return errs != 0;
 }
 
index c2b434c60648fb0e287c100a32d9346a765ab74f..d940f1c5c287a7232e18628c6a7e80e74c73b544 100644 (file)
@@ -42,6 +42,7 @@ struct unpack_trees_options {
        unsigned int reset,
                     merge,
                     update,
+                    clone,
                     index_only,
                     nontrivial_merge,
                     trivial_merges_only,