@@ -17,12 +17,19 @@ static const char builtin_mv_usage[] =
17
17
static const char * * copy_pathspec (const char * prefix , const char * * pathspec ,
18
18
int count , int base_name )
19
19
{
20
+ int i ;
20
21
const char * * result = xmalloc ((count + 1 ) * sizeof (const char * ));
21
22
memcpy (result , pathspec , count * sizeof (const char * ));
22
23
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 ) {
26
33
const char * last_slash = strrchr (result [i ], '/' );
27
34
if (last_slash )
28
35
result [i ] = last_slash + 1 ;
@@ -129,6 +136,12 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
129
136
if (lstat (source [i ], & st ) < 0 )
130
137
bad = "bad source" ;
131
138
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
+
132
145
if (S_ISDIR (st .st_mode )) {
133
146
const char * dir = source [i ], * dest_dir = destination [i ];
134
147
int first , last , len = strlen (dir );
@@ -204,12 +217,6 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
204
217
}
205
218
}
206
219
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
-
213
220
if (!bad && cache_name_pos (source [i ], strlen (source [i ])) < 0 )
214
221
bad = "not under version control" ;
215
222
0 commit comments