builtin-mv: readability patch
The old version was not liked at all. This is hopefully better. Oh, and it gets rid of the goto. Note that it does not change any functionality. Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>
Johannes Schindelin committed
Aug 19, 2006 at 16:52 UTC
60a6bf5f53635005f4f68d8b8a33172309193623
1 file changed
+44
-60
builtin-mv.c
+44
-60
@@ -129,48 +129,43 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
129
130
/* Checking */
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;
134
const char *bad = NULL;
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
139
- if (lstat(source[i], &st) < 0)
139
+ length = strlen(src);
140
+ if (lstat(src, &st) < 0)
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] == '/')) {
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;
150
151
modes[i] = WORKING_DIRECTORY;
152
159
- first = cache_name_pos(source[i], len);
153
+ first = cache_name_pos(src, length);
154
if (first >= 0)
161
- die ("Huh? %s/ is in index?", dir);
155
+ die ("Huh? %s/ is in index?", src);
156
157
first = -1 - first;
158
for (last = first; last < active_nr; last++) {
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] != '/')
162
break;
163
}
164
165
if (last - first < 1)
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;
169
170
if (last - first > 0) {
171
source = realloc(source,
@@ -184,24 +179,21 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
179
* sizeof(enum update_mode));
180
}
181
187
- dest_dir = add_slash(dest_dir);
182
+ dst = add_slash(dst);
183
+ dst_len = strlen(dst) - 1;
184
185
for (j = 0; j < last - first; j++) {
186
const char *path =
187
active_cache[first + j]->name;
188
source[count + j] = path;
189
destination[count + j] =
194
- prefix_path(dest_dir, dst_len,
195
- path + len);
190
+ prefix_path(dst, dst_len,
191
+ path + length);
192
modes[count + j] = INDEX;
193
}
194
count += last - first;
195
}
200
-
201
- goto next;
202
- }
203
-
204
- if (!bad && lstat(destination[i], &st) == 0) {
196
+ } else if (lstat(dst, &st) == 0) {
197
bad = "destination exists";
198
if (force) {
199
/*
@@ -213,24 +205,17 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
205
" will overwrite!\n",
206
bad);
207
bad = NULL;
216
- path_list_insert(destination[i],
217
- &overwritten);
208
+ path_list_insert(dst, &overwritten);
209
} else
210
bad = "Cannot overwrite";
211
}
221
- }
222
-
223
- if (!bad && cache_name_pos(source[i], strlen(source[i])) < 0)
212
+ } else if (cache_name_pos(src, length) < 0)
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);
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:
219
if (bad) {
220
if (ignore_errors) {
221
if (--count > 0) {
@@ -242,33 +227,32 @@ next:
227
}
228
} else
229
die ("%s, source=%s, destination=%s",
245
- bad, source[i], destination[i]);
230
+ bad, src, dst);
231
}
232
}
233
234
for (i = 0; i < count; i++) {
235
+ const char *src = source[i], *dst = destination[i];
236
+ enum update_mode mode = modes[i];
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)
244
continue;
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);
248
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);
252
else
269
- path_list_insert(destination[i], &added);
253
+ path_list_insert(dst, &added);
254
} else
271
- path_list_insert(destination[i], &added);
255
+ path_list_insert(dst, &added);
256
}
257
258
if (show_only) {