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