@matheusbsbs / matheus-bagliano / commits / 744dacd3f5
Fork 0

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>

Junio C Hamano committed Feb 3, 2008 at 23:59 UTC 744dacd3f5045240a304e687f3ef7135398e7865
2 files changed +43 -1
builtin-mv.c
+5 -1
@@ -19,6 +19,7 @@ static const char **copy_pathspec(const char *prefix, const char **pathspec,
19 int count, int base_name)
20 {
21 int i;
22 + int len = prefix ? strlen(prefix) : 0;
23 const char **result = xmalloc((count + 1) * sizeof(const char *));
24 memcpy(result, pathspec, count * sizeof(const char *));
25 result[count] = NULL;
@@ -32,8 +33,11 @@ static const char **copy_pathspec(const char *prefix, const char **pathspec,
33 if (last_slash)
34 result[i] = last_slash + 1;
35 }
36 + result[i] = prefix_path(prefix, len, result[i]);
37 + if (!result[i])
38 + exit(1); /* error already given */
39 }
36 - return get_pathspec(prefix, result);
40 + return result;
41 }
42
43 static void show_list(const char *label, struct path_list *list)
t/t7001-mv.sh
+38
@@ -118,4 +118,42 @@ test_expect_success "Sergey Vlasov's test case" '
118 git mv ab a
119 '
120
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 +
159 test_done