Skip to content

Commit d78b0f3

Browse files
dschoJunio C Hamano
authored andcommitted
[PATCH] git-mv: add more path normalization
We already use the normalization from get_pathspec(), but now we also remove a trailing slash. So, git mv some_path/ into_some_path/ works now. Also, move the "can not move directory into itself" test before the subdirectory expansion. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 53e1a76 commit d78b0f3

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

builtin-mv.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,19 @@ static const char builtin_mv_usage[] =
1717
static const char **copy_pathspec(const char *prefix, const char **pathspec,
1818
int count, int base_name)
1919
{
20+
int i;
2021
const char **result = xmalloc((count + 1) * sizeof(const char *));
2122
memcpy(result, pathspec, count * sizeof(const char *));
2223
result[count] = NULL;
23-
if (base_name) {
24-
int i;
25-
for (i = 0; i < count; i++) {
24+
for (i = 0; i < count; i++) {
25+
int length = strlen(result[i]);
26+
if (length > 0 && result[i][length - 1] == '/') {
27+
char *without_slash = xmalloc(length);
28+
memcpy(without_slash, result[i], length - 1);
29+
without_slash[length] = '\0';
30+
result[i] = without_slash;
31+
}
32+
if (base_name) {
2633
const char *last_slash = strrchr(result[i], '/');
2734
if (last_slash)
2835
result[i] = last_slash + 1;
@@ -129,6 +136,12 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
129136
if (lstat(source[i], &st) < 0)
130137
bad = "bad source";
131138

139+
if (!bad &&
140+
(length = strlen(source[i])) >= 0 &&
141+
!strncmp(destination[i], source[i], length) &&
142+
(destination[i][length] == 0 || destination[i][length] == '/'))
143+
bad = "can not move directory into itself";
144+
132145
if (S_ISDIR(st.st_mode)) {
133146
const char *dir = source[i], *dest_dir = destination[i];
134147
int first, last, len = strlen(dir);
@@ -204,12 +217,6 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
204217
}
205218
}
206219

207-
if (!bad &&
208-
(length = strlen(source[i])) >= 0 &&
209-
!strncmp(destination[i], source[i], length) &&
210-
(destination[i][length] == 0 || destination[i][length] == '/'))
211-
bad = "can not move directory into itself";
212-
213220
if (!bad && cache_name_pos(source[i], strlen(source[i])) < 0)
214221
bad = "not under version control";
215222

0 commit comments

Comments
 (0)