Skip to content

Commit aca085e

Browse files
dschoJunio C Hamano
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6173c19 commit aca085e

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

builtin-mv.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,21 +146,24 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
146146
&& lstat(dst, &st) == 0)
147147
bad = "cannot move directory over file";
148148
else if (src_is_dir) {
149+
const char *src_w_slash = add_slash(src);
150+
int len_w_slash = length + 1;
149151
int first, last;
150152

151153
modes[i] = WORKING_DIRECTORY;
152154

153-
first = cache_name_pos(src, length);
155+
first = cache_name_pos(src_w_slash, len_w_slash);
154156
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);
156159

157160
first = -1 - first;
158161
for (last = first; last < active_nr; last++) {
159162
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))
162164
break;
163165
}
166+
free((char *)src_w_slash);
164167

165168
if (last - first < 1)
166169
bad = "source directory is empty";

t/t7001-mv.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,17 @@ test_expect_success "Michael Cassar's test case" '
105105
}
106106
'
107107

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+
108121
test_done

0 commit comments

Comments
 (0)