git-mv: search more precisely for source directory in index
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>
Johannes Schindelin committed
Dec 3, 2006 at 20:42 UTC
aca085e577688108a2480b96a2f7077424a74e4d
2 files changed
+20
-4
builtin-mv.c
+7
-4
@@ -146,21 +146,24 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
146
&& lstat(dst, &st) == 0)
147
bad = "cannot move directory over file";
148
else if (src_is_dir) {
149
+ const char *src_w_slash = add_slash(src);
150
+ int len_w_slash = length + 1;
151
int first, last;
152
153
modes[i] = WORKING_DIRECTORY;
154
153
- first = cache_name_pos(src, length);
155
+ first = cache_name_pos(src_w_slash, len_w_slash);
156
if (first >= 0)
155
- die ("Huh? %s/ is in index?", src);
157
+ die ("Huh? %.*s is in index?",
158
+ len_w_slash, src_w_slash);
159
160
first = -1 - first;
161
for (last = first; last < active_nr; last++) {
162
const char *path = active_cache[last]->name;
160
- if (strncmp(path, src, length)
161
- || path[length] != '/')
163
+ if (strncmp(path, src_w_slash, len_w_slash))
164
break;
165
}
166
+ free((char *)src_w_slash);
167
168
if (last - first < 1)
169
bad = "source directory is empty";
t/t7001-mv.sh
+13
@@ -105,4 +105,17 @@ test_expect_success "Michael Cassar's test case" '
105
}
106
'
107
108
+rm -fr papers partA path?
109
+
110
+test_expect_success "Sergey Vlasov's test case" '
111
+ rm -fr .git &&
112
+ git init-db &&
113
+ mkdir ab &&
114
+ date >ab.c &&
115
+ date >ab/d &&
116
+ git add ab.c ab &&
117
+ git commit -m 'initial' &&
118
+ git mv ab a
119
+'
120
+
121
test_done