Skip to content

Commit 43134fc

Browse files
dschoJunio C Hamano
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 55c3eb4 commit 43134fc

File tree

1 file changed

+44
-60
lines changed

1 file changed

+44
-60
lines changed

builtin-mv.c

Lines changed: 44 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -126,48 +126,43 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
126126

127127
/* Checking */
128128
for (i = 0; i < count; i++) {
129-
int length;
129+
const char *src = source[i], *dst = destination[i];
130+
int length, src_is_dir;
130131
const char *bad = NULL;
131132

132133
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);
135135

136-
if (lstat(source[i], &st) < 0)
136+
length = strlen(src);
137+
if (lstat(src, &st) < 0)
137138
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] == '/')) {
143141
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;
153147

154148
modes[i] = WORKING_DIRECTORY;
155149

156-
first = cache_name_pos(source[i], len);
150+
first = cache_name_pos(src, length);
157151
if (first >= 0)
158-
die ("Huh? %s/ is in index?", dir);
152+
die ("Huh? %s/ is in index?", src);
159153

160154
first = -1 - first;
161155
for (last = first; last < active_nr; last++) {
162156
const char *path = active_cache[last]->name;
163-
if (strncmp(path, dir, len) || path[len] != '/')
157+
if (strncmp(path, src, length)
158+
|| path[length] != '/')
164159
break;
165160
}
166161

167162
if (last - first < 1)
168163
bad = "source directory is empty";
169-
else if (!bad) {
170-
int j, dst_len = strlen(dest_dir);
164+
else {
165+
int j, dst_len;
171166

172167
if (last - first > 0) {
173168
source = realloc(source,
@@ -181,24 +176,21 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
181176
* sizeof(enum update_mode));
182177
}
183178

184-
dest_dir = add_slash(dest_dir);
179+
dst = add_slash(dst);
180+
dst_len = strlen(dst) - 1;
185181

186182
for (j = 0; j < last - first; j++) {
187183
const char *path =
188184
active_cache[first + j]->name;
189185
source[count + j] = path;
190186
destination[count + j] =
191-
prefix_path(dest_dir, dst_len,
192-
path + len);
187+
prefix_path(dst, dst_len,
188+
path + length);
193189
modes[count + j] = INDEX;
194190
}
195191
count += last - first;
196192
}
197-
198-
goto next;
199-
}
200-
201-
if (!bad && lstat(destination[i], &st) == 0) {
193+
} else if (lstat(dst, &st) == 0) {
202194
bad = "destination exists";
203195
if (force) {
204196
/*
@@ -210,24 +202,17 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
210202
" will overwrite!\n",
211203
bad);
212204
bad = NULL;
213-
path_list_insert(destination[i],
214-
&overwritten);
205+
path_list_insert(dst, &overwritten);
215206
} else
216207
bad = "Cannot overwrite";
217208
}
218-
}
219-
220-
if (!bad && cache_name_pos(source[i], strlen(source[i])) < 0)
209+
} else if (cache_name_pos(src, length) < 0)
221210
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);
222215

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:
231216
if (bad) {
232217
if (ignore_errors) {
233218
if (--count > 0) {
@@ -239,33 +224,32 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
239224
}
240225
} else
241226
die ("%s, source=%s, destination=%s",
242-
bad, source[i], destination[i]);
227+
bad, src, dst);
243228
}
244229
}
245230

246231
for (i = 0; i < count; i++) {
232+
const char *src = source[i], *dst = destination[i];
233+
enum update_mode mode = modes[i];
247234
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)
257241
continue;
258242

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);
261245

262246
/* 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);
265249
else
266-
path_list_insert(destination[i], &added);
250+
path_list_insert(dst, &added);
267251
} else
268-
path_list_insert(destination[i], &added);
252+
path_list_insert(dst, &added);
269253
}
270254

271255
if (show_only) {

0 commit comments

Comments
 (0)