Skip to content

Commit 744dacd

Browse files
committed
builtin-mv: minimum fix to avoid losing files
An incorrect command "git mv subdir /outer/space" threw the subdirectory to outside of the repository and then noticed that /outer/space/subdir/ would be outside of the repository. The error checking is backwards. This fixes the issue by being careful about use of the return value of get_pathspec(). Since the implementation already has handcrafted loop to munge each path on the command line, we use prefix_path() instead. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 1abf095 commit 744dacd

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

builtin-mv.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ static const char **copy_pathspec(const char *prefix, const char **pathspec,
1919
int count, int base_name)
2020
{
2121
int i;
22+
int len = prefix ? strlen(prefix) : 0;
2223
const char **result = xmalloc((count + 1) * sizeof(const char *));
2324
memcpy(result, pathspec, count * sizeof(const char *));
2425
result[count] = NULL;
@@ -32,8 +33,11 @@ static const char **copy_pathspec(const char *prefix, const char **pathspec,
3233
if (last_slash)
3334
result[i] = last_slash + 1;
3435
}
36+
result[i] = prefix_path(prefix, len, result[i]);
37+
if (!result[i])
38+
exit(1); /* error already given */
3539
}
36-
return get_pathspec(prefix, result);
40+
return result;
3741
}
3842

3943
static void show_list(const char *label, struct path_list *list)

t/t7001-mv.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,42 @@ test_expect_success "Sergey Vlasov's test case" '
118118
git mv ab a
119119
'
120120

121+
test_expect_success 'absolute pathname' '(
122+
123+
rm -fr mine &&
124+
mkdir mine &&
125+
cd mine &&
126+
test_create_repo one &&
127+
cd one &&
128+
mkdir sub &&
129+
>sub/file &&
130+
git add sub/file &&
131+
132+
git mv sub "$(pwd)/in" &&
133+
! test -d sub &&
134+
test -d in &&
135+
git ls-files --error-unmatch in/file
136+
137+
138+
)'
139+
140+
test_expect_success 'absolute pathname outside should fail' '(
141+
142+
rm -fr mine &&
143+
mkdir mine &&
144+
cd mine &&
145+
out=$(pwd) &&
146+
test_create_repo one &&
147+
cd one &&
148+
mkdir sub &&
149+
>sub/file &&
150+
git add sub/file &&
151+
152+
! git mv sub "$out/out" &&
153+
test -d sub &&
154+
! test -d ../in &&
155+
git ls-files --error-unmatch sub/file
156+
157+
)'
158+
121159
test_done

0 commit comments

Comments
 (0)