]> git.ipfire.org Git - thirdparty/git.git/commitdiff
git-mv: search more precisely for source directory in index
authorJohannes Schindelin <Johannes.Schindelin@gmx.de>
Sun, 3 Dec 2006 19:42:47 +0000 (20:42 +0100)
committerJunio C Hamano <junkio@cox.net>
Mon, 4 Dec 2006 08:55:12 +0000 (00:55 -0800)
A move of a directory should find the entries in the index by
searching for the name _including_ the slash. Otherwise, the
directory can be shadowed by a file when it matches the prefix
and is lexicographically smaller, e.g. "ab.c" shadows "ab/".

Noticed by Sergey Vlasov.

[jc: added Sergey's original reproduction recipe as a test case
 at the end of t7001.]

Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Junio C Hamano <junkio@cox.net>
builtin-mv.c
t/t7001-mv.sh

index 54dd3bfe8ac787fda32f3557faac00c65a61de96..d14a4a7f5c66c5f8712399d4b1fc566e0891125a 100644 (file)
@@ -146,21 +146,24 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
                                && lstat(dst, &st) == 0)
                        bad = "cannot move directory over file";
                else if (src_is_dir) {
+                       const char *src_w_slash = add_slash(src);
+                       int len_w_slash = length + 1;
                        int first, last;
 
                        modes[i] = WORKING_DIRECTORY;
 
-                       first = cache_name_pos(src, length);
+                       first = cache_name_pos(src_w_slash, len_w_slash);
                        if (first >= 0)
-                               die ("Huh? %s/ is in index?", src);
+                               die ("Huh? %.*s is in index?",
+                                               len_w_slash, src_w_slash);
 
                        first = -1 - first;
                        for (last = first; last < active_nr; last++) {
                                const char *path = active_cache[last]->name;
-                               if (strncmp(path, src, length)
-                                               || path[length] != '/')
+                               if (strncmp(path, src_w_slash, len_w_slash))
                                        break;
                        }
+                       free((char *)src_w_slash);
 
                        if (last - first < 1)
                                bad = "source directory is empty";
index 23a1eff3bba4505d449bd5fdffb3daf7f9b23ddf..2f4ff82e149c497d79583f6b431b29d597e4a1ae 100755 (executable)
@@ -105,4 +105,17 @@ test_expect_success "Michael Cassar's test case" '
        }
 '
 
+rm -fr papers partA path?
+
+test_expect_success "Sergey Vlasov's test case" '
+       rm -fr .git &&
+       git init-db &&
+       mkdir ab &&
+       date >ab.c &&
+       date >ab/d &&
+       git add ab.c ab &&
+       git commit -m 'initial' &&
+       git mv ab a
+'
+
 test_done