Skip to content

Commit 1d6249e

Browse files
dschoJunio C Hamano
authored andcommitted
git-mv: succeed even if source is a prefix of destination
As noted by Fredrik Kuivinen, without this patch, git-mv fails on git-mv README README-renamed because "README" is a prefix of "README-renamed". Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 66c4509 commit 1d6249e

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

builtin-mv.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
119119

120120
/* Checking */
121121
for (i = 0; i < count; i++) {
122+
int length;
122123
const char *bad = NULL;
123124

124125
if (show_only)
@@ -204,7 +205,9 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
204205
}
205206

206207
if (!bad &&
207-
!strncmp(destination[i], source[i], strlen(source[i])))
208+
(length = strlen(source[i])) >= 0 &&
209+
!strncmp(destination[i], source[i], length) &&
210+
(destination[i][length] == 0 || destination[i][length] == '/'))
208211
bad = "can not move directory into itself";
209212

210213
if (!bad && cache_name_pos(source[i], strlen(source[i])) < 0)

t/t7001-mv.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ test_expect_success \
5959
git-diff-tree -r -M --name-status HEAD^ HEAD | \
6060
grep -E "^R100.+path0/README.+path2/README"'
6161

62+
test_expect_success \
63+
'succeed when source is a prefix of destination' \
64+
'git-mv path2/COPYING path2/COPYING-renamed'
65+
6266
test_expect_success \
6367
'moving whole subdirectory into subdirectory' \
6468
'git-mv path2 path1'

0 commit comments

Comments
 (0)