Skip to content

Commit 60a6bf5

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 6e17886 commit 60a6bf5

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
@@ -129,48 +129,43 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
129129

130130
/* Checking */
131131
for (i = 0; i < count; i++) {
132-
int length;
132+
const char *src = source[i], *dst = destination[i];
133+
int length, src_is_dir;
133134
const char *bad = NULL;
134135

135136
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);
138138

139-
if (lstat(source[i], &st) < 0)
139+
length = strlen(src);
140+
if (lstat(src, &st) < 0)
140141
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] == '/')) {
146144
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;
156150

157151
modes[i] = WORKING_DIRECTORY;
158152

159-
first = cache_name_pos(source[i], len);
153+
first = cache_name_pos(src, length);
160154
if (first >= 0)
161-
die ("Huh? %s/ is in index?", dir);
155+
die ("Huh? %s/ is in index?", src);
162156

163157
first = -1 - first;
164158
for (last = first; last < active_nr; last++) {
165159
const char *path = active_cache[last]->name;
166-
if (strncmp(path, dir, len) || path[len] != '/')
160+
if (strncmp(path, src, length)
161+
|| path[length] != '/')
167162
break;
168163
}
169164

170165
if (last - first < 1)
171166
bad = "source directory is empty";
172-
else if (!bad) {
173-
int j, dst_len = strlen(dest_dir);
167+
else {
168+
int j, dst_len;
174169

175170
if (last - first > 0) {
176171
source = realloc(source,
@@ -184,24 +179,21 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
184179
* sizeof(enum update_mode));
185180
}
186181

187-
dest_dir = add_slash(dest_dir);
182+
dst = add_slash(dst);
183+
dst_len = strlen(dst) - 1;
188184

189185
for (j = 0; j < last - first; j++) {
190186
const char *path =
191187
active_cache[first + j]->name;
192188
source[count + j] = path;
193189
destination[count + j] =
194-
prefix_path(dest_dir, dst_len,
195-
path + len);
190+
prefix_path(dst, dst_len,
191+
path + length);
196192
modes[count + j] = INDEX;
197193
}
198194
count += last - first;
199195
}
200-
201-
goto next;
202-
}
203-
204-
if (!bad && lstat(destination[i], &st) == 0) {
196+
} else if (lstat(dst, &st) == 0) {
205197
bad = "destination exists";
206198
if (force) {
207199
/*
@@ -213,24 +205,17 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
213205
" will overwrite!\n",
214206
bad);
215207
bad = NULL;
216-
path_list_insert(destination[i],
217-
&overwritten);
208+
path_list_insert(dst, &overwritten);
218209
} else
219210
bad = "Cannot overwrite";
220211
}
221-
}
222-
223-
if (!bad && cache_name_pos(source[i], strlen(source[i])) < 0)
212+
} else if (cache_name_pos(src, length) < 0)
224213
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);
225218

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:
234219
if (bad) {
235220
if (ignore_errors) {
236221
if (--count > 0) {
@@ -242,33 +227,32 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
242227
}
243228
} else
244229
die ("%s, source=%s, destination=%s",
245-
bad, source[i], destination[i]);
230+
bad, src, dst);
246231
}
247232
}
248233

249234
for (i = 0; i < count; i++) {
235+
const char *src = source[i], *dst = destination[i];
236+
enum update_mode mode = modes[i];
250237
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)
260244
continue;
261245

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

265249
/* 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);
268252
else
269-
path_list_insert(destination[i], &added);
253+
path_list_insert(dst, &added);
270254
} else
271-
path_list_insert(destination[i], &added);
255+
path_list_insert(dst, &added);
272256
}
273257

274258
if (show_only) {

0 commit comments

Comments
 (0)