Skip to content

Commit af82559

Browse files
committed
git-mv: fix moving more than one source to a single destination
The code used as if return value from basename(3) were stable, but often the function is implemented to return a pointer to a static storage internal to it. Because basename(3) is also allowed to modify its input parameter in place, casting constness away from the strings we obtained from the caller and giving them to basename is a no-no. Reported, and initial fix and test supplied by David Rydh. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 30c9e91 commit af82559

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

builtin-mv.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ static const char **copy_pathspec(const char *prefix, const char **pathspec,
2424
result[count] = NULL;
2525
for (i = 0; i < count; i++) {
2626
int length = strlen(result[i]);
27-
if (length > 0 && is_dir_sep(result[i][length - 1]))
28-
result[i] = xmemdupz(result[i], length - 1);
29-
if (base_name)
30-
result[i] = basename((char *)result[i]);
27+
int to_copy = length;
28+
while (to_copy > 0 && is_dir_sep(result[i][to_copy - 1]))
29+
to_copy--;
30+
if (to_copy != length || base_name) {
31+
char *it = xmemdupz(result[i], to_copy);
32+
result[i] = base_name ? strdup(basename(it)) : it;
33+
}
3134
}
3235
return get_pathspec(prefix, result);
3336
}

t/t7001-mv.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,18 @@ test_expect_success 'absolute pathname outside should fail' '(
189189
190190
)'
191191

192+
test_expect_success 'git mv to move multiple sources into a directory' '
193+
rm -fr .git && git init &&
194+
mkdir dir other &&
195+
>dir/a.txt &&
196+
>dir/b.txt &&
197+
git add dir/?.txt &&
198+
git mv dir/a.txt dir/b.txt other &&
199+
git ls-files >actual &&
200+
{ echo other/a.txt; echo other/b.txt; } >expect &&
201+
test_cmp expect actual
202+
'
203+
192204
test_expect_success 'git mv should not change sha1 of moved cache entry' '
193205
194206
rm -fr .git &&

0 commit comments

Comments
 (0)