@@ -126,48 +126,43 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
126
126
127
127
/* Checking */
128
128
for (i = 0 ; i < count ; i ++ ) {
129
- int length ;
129
+ const char * src = source [i ], * dst = destination [i ];
130
+ int length , src_is_dir ;
130
131
const char * bad = NULL ;
131
132
132
133
if (show_only )
133
- printf ("Checking rename of '%s' to '%s'\n" ,
134
- source [i ], destination [i ]);
134
+ printf ("Checking rename of '%s' to '%s'\n" , src , dst );
135
135
136
- if (lstat (source [i ], & st ) < 0 )
136
+ length = strlen (src );
137
+ if (lstat (src , & st ) < 0 )
137
138
bad = "bad source" ;
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 ] == '/' ))
139
+ else if (!strncmp (src , dst , length ) &&
140
+ (dst [length ] == 0 || dst [length ] == '/' )) {
143
141
bad = "can not move directory into itself" ;
144
-
145
- if (S_ISDIR (st .st_mode )) {
146
- const char * dir = source [i ], * dest_dir = destination [i ];
147
- int first , last , len = strlen (dir );
148
-
149
- if (lstat (dest_dir , & st ) == 0 ) {
150
- bad = "cannot move directory over file" ;
151
- goto next ;
152
- }
142
+ } else if ((src_is_dir = S_ISDIR (st .st_mode ))
143
+ && lstat (dst , & st ) == 0 )
144
+ bad = "cannot move directory over file" ;
145
+ else if (src_is_dir ) {
146
+ int first , last ;
153
147
154
148
modes [i ] = WORKING_DIRECTORY ;
155
149
156
- first = cache_name_pos (source [ i ], len );
150
+ first = cache_name_pos (src , length );
157
151
if (first >= 0 )
158
- die ("Huh? %s/ is in index?" , dir );
152
+ die ("Huh? %s/ is in index?" , src );
159
153
160
154
first = -1 - first ;
161
155
for (last = first ; last < active_nr ; last ++ ) {
162
156
const char * path = active_cache [last ]-> name ;
163
- if (strncmp (path , dir , len ) || path [len ] != '/' )
157
+ if (strncmp (path , src , length )
158
+ || path [length ] != '/' )
164
159
break ;
165
160
}
166
161
167
162
if (last - first < 1 )
168
163
bad = "source directory is empty" ;
169
- else if (! bad ) {
170
- int j , dst_len = strlen ( dest_dir ) ;
164
+ else {
165
+ int j , dst_len ;
171
166
172
167
if (last - first > 0 ) {
173
168
source = realloc (source ,
@@ -181,24 +176,21 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
181
176
* sizeof (enum update_mode ));
182
177
}
183
178
184
- dest_dir = add_slash (dest_dir );
179
+ dst = add_slash (dst );
180
+ dst_len = strlen (dst ) - 1 ;
185
181
186
182
for (j = 0 ; j < last - first ; j ++ ) {
187
183
const char * path =
188
184
active_cache [first + j ]-> name ;
189
185
source [count + j ] = path ;
190
186
destination [count + j ] =
191
- prefix_path (dest_dir , dst_len ,
192
- path + len );
187
+ prefix_path (dst , dst_len ,
188
+ path + length );
193
189
modes [count + j ] = INDEX ;
194
190
}
195
191
count += last - first ;
196
192
}
197
-
198
- goto next ;
199
- }
200
-
201
- if (!bad && lstat (destination [i ], & st ) == 0 ) {
193
+ } else if (lstat (dst , & st ) == 0 ) {
202
194
bad = "destination exists" ;
203
195
if (force ) {
204
196
/*
@@ -210,24 +202,17 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
210
202
" will overwrite!\n" ,
211
203
bad );
212
204
bad = NULL ;
213
- path_list_insert (destination [i ],
214
- & overwritten );
205
+ path_list_insert (dst , & overwritten );
215
206
} else
216
207
bad = "Cannot overwrite" ;
217
208
}
218
- }
219
-
220
- if (!bad && cache_name_pos (source [i ], strlen (source [i ])) < 0 )
209
+ } else if (cache_name_pos (src , length ) < 0 )
221
210
bad = "not under version control" ;
211
+ else if (path_list_has_path (& src_for_dst , dst ))
212
+ bad = "multiple sources for the same target" ;
213
+ else
214
+ path_list_insert (dst , & src_for_dst );
222
215
223
- if (!bad ) {
224
- if (path_list_has_path (& src_for_dst , destination [i ]))
225
- bad = "multiple sources for the same target" ;
226
- else
227
- path_list_insert (destination [i ], & src_for_dst );
228
- }
229
-
230
- next :
231
216
if (bad ) {
232
217
if (ignore_errors ) {
233
218
if (-- count > 0 ) {
@@ -239,33 +224,32 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
239
224
}
240
225
} else
241
226
die ("%s, source=%s, destination=%s" ,
242
- bad , source [ i ], destination [ i ] );
227
+ bad , src , dst );
243
228
}
244
229
}
245
230
246
231
for (i = 0 ; i < count ; i ++ ) {
232
+ const char * src = source [i ], * dst = destination [i ];
233
+ enum update_mode mode = modes [i ];
247
234
if (show_only || verbose )
248
- printf ("Renaming %s to %s\n" ,
249
- source [i ], destination [i ]);
250
- if (!show_only && modes [i ] != INDEX &&
251
- rename (source [i ], destination [i ]) < 0 &&
252
- !ignore_errors )
253
- die ("renaming %s failed: %s" ,
254
- source [i ], strerror (errno ));
255
-
256
- if (modes [i ] == WORKING_DIRECTORY )
235
+ printf ("Renaming %s to %s\n" , src , dst );
236
+ if (!show_only && mode != INDEX &&
237
+ rename (src , dst ) < 0 && !ignore_errors )
238
+ die ("renaming %s failed: %s" , src , strerror (errno ));
239
+
240
+ if (mode == WORKING_DIRECTORY )
257
241
continue ;
258
242
259
- if (cache_name_pos (source [ i ] , strlen (source [ i ] )) >= 0 ) {
260
- path_list_insert (source [ i ] , & deleted );
243
+ if (cache_name_pos (src , strlen (src )) >= 0 ) {
244
+ path_list_insert (src , & deleted );
261
245
262
246
/* destination can be a directory with 1 file inside */
263
- if (path_list_has_path (& overwritten , destination [ i ] ))
264
- path_list_insert (destination [ i ] , & changed );
247
+ if (path_list_has_path (& overwritten , dst ))
248
+ path_list_insert (dst , & changed );
265
249
else
266
- path_list_insert (destination [ i ] , & added );
250
+ path_list_insert (dst , & added );
267
251
} else
268
- path_list_insert (destination [ i ] , & added );
252
+ path_list_insert (dst , & added );
269
253
}
270
254
271
255
if (show_only ) {
0 commit comments