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
43134fcb350fb70d26634be163db1f71c14db19f
1 file changed
+44
-60
builtin-mv.c
+44
-60
@@ -126,48 +126,43 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
126
127
/* Checking */
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;
131
const char *bad = NULL;
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
136
- if (lstat(source[i], &st) < 0)
136
+ length = strlen(src);
137
+ if (lstat(src, &st) < 0)
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] == '/')) {
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;
147
148
modes[i] = WORKING_DIRECTORY;
149
156
- first = cache_name_pos(source[i], len);
150
+ first = cache_name_pos(src, length);
151
if (first >= 0)
158
- die ("Huh? %s/ is in index?", dir);
152
+ die ("Huh? %s/ is in index?", src);
153
154
first = -1 - first;
155
for (last = first; last < active_nr; last++) {
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] != '/')
159
break;
160
}
161
162
if (last - first < 1)
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;
166
167
if (last - first > 0) {
168
source = realloc(source,
@@ -181,24 +176,21 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
176
* sizeof(enum update_mode));
177
}
178
184
- dest_dir = add_slash(dest_dir);
179
+ dst = add_slash(dst);
180
+ dst_len = strlen(dst) - 1;
181
182
for (j = 0; j < last - first; j++) {
183
const char *path =
184
active_cache[first + j]->name;
185
source[count + j] = path;
186
destination[count + j] =
191
- prefix_path(dest_dir, dst_len,
192
- path + len);
187
+ prefix_path(dst, dst_len,
188
+ path + length);
189
modes[count + j] = INDEX;
190
}
191
count += last - first;
192
}
197
-
198
- goto next;
199
- }
200
-
201
- if (!bad && lstat(destination[i], &st) == 0) {
193
+ } else if (lstat(dst, &st) == 0) {
194
bad = "destination exists";
195
if (force) {
196
/*
@@ -210,24 +202,17 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
202
" will overwrite!\n",
203
bad);
204
bad = NULL;
213
- path_list_insert(destination[i],
214
- &overwritten);
205
+ path_list_insert(dst, &overwritten);
206
} else
207
bad = "Cannot overwrite";
208
}
218
- }
219
-
220
- if (!bad && cache_name_pos(source[i], strlen(source[i])) < 0)
209
+ } else if (cache_name_pos(src, length) < 0)
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);
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:
216
if (bad) {
217
if (ignore_errors) {
218
if (--count > 0) {
@@ -239,33 +224,32 @@ next:
224
}
225
} else
226
die ("%s, source=%s, destination=%s",
242
- bad, source[i], destination[i]);
227
+ bad, src, dst);
228
}
229
}
230
231
for (i = 0; i < count; i++) {
232
+ const char *src = source[i], *dst = destination[i];
233
+ enum update_mode mode = modes[i];
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)
241
continue;
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);
245
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);
249
else
266
- path_list_insert(destination[i], &added);
250
+ path_list_insert(dst, &added);
251
} else
268
- path_list_insert(destination[i], &added);
252
+ path_list_insert(dst, &added);
253
}
254
255
if (show_only) {