mv: decouple if/else-if checks using goto

Previous if/else-if chain are highly nested and hard to develop/extend.

Refactor to decouple this if/else-if chain by using goto to jump ahead.

Suggested-by: Derrick Stolee <derrickstolee@github.com>
Signed-off-by: Shaoxuan Yuan <shaoxuan.yuan02@gmail.com>
Acked-by: Derrick Stolee <derrickstolee@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Shaoxuan Yuan
2022-06-30 10:37:33 +08:00
committed by Junio C Hamano
parent 707fa2f76a
commit 7889755bae

View File

@ -187,26 +187,36 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
length = strlen(src); length = strlen(src);
if (lstat(src, &st) < 0) { if (lstat(src, &st) < 0) {
/* only error if existence is expected. */ /* only error if existence is expected. */
if (modes[i] != SPARSE) if (modes[i] != SPARSE) {
bad = _("bad source"); bad = _("bad source");
} else if (!strncmp(src, dst, length) && goto act_on_entry;
}
}
if (!strncmp(src, dst, length) &&
(dst[length] == 0 || dst[length] == '/')) { (dst[length] == 0 || dst[length] == '/')) {
bad = _("can not move directory into itself"); bad = _("can not move directory into itself");
} else if ((src_is_dir = S_ISDIR(st.st_mode)) goto act_on_entry;
&& lstat(dst, &st) == 0) }
if ((src_is_dir = S_ISDIR(st.st_mode))
&& lstat(dst, &st) == 0) {
bad = _("cannot move directory over file"); bad = _("cannot move directory over file");
else if (src_is_dir) { goto act_on_entry;
}
if (src_is_dir) {
int j, dst_len, n;
int first = cache_name_pos(src, length), last; int first = cache_name_pos(src, length), last;
if (first >= 0) if (first >= 0) {
prepare_move_submodule(src, first, prepare_move_submodule(src, first,
submodule_gitfile + i); submodule_gitfile + i);
else if (index_range_of_same_dir(src, length, goto act_on_entry;
&first, &last) < 1) } else if (index_range_of_same_dir(src, length,
&first, &last) < 1) {
bad = _("source directory is empty"); bad = _("source directory is empty");
else { /* last - first >= 1 */ goto act_on_entry;
int j, dst_len, n; }
/* last - first >= 1 */
modes[i] = WORKING_DIRECTORY; modes[i] = WORKING_DIRECTORY;
n = argc + last - first; n = argc + last - first;
REALLOC_ARRAY(source, n); REALLOC_ARRAY(source, n);
@ -227,12 +237,17 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
submodule_gitfile[argc + j] = NULL; submodule_gitfile[argc + j] = NULL;
} }
argc += last - first; argc += last - first;
goto act_on_entry;
} }
} else if (!(ce = cache_file_exists(src, length, 0))) { if (!(ce = cache_file_exists(src, length, 0))) {
bad = _("not under version control"); bad = _("not under version control");
} else if (ce_stage(ce)) { goto act_on_entry;
}
if (ce_stage(ce)) {
bad = _("conflicted"); bad = _("conflicted");
} else if (lstat(dst, &st) == 0 && goto act_on_entry;
}
if (lstat(dst, &st) == 0 &&
(!ignore_case || strcasecmp(src, dst))) { (!ignore_case || strcasecmp(src, dst))) {
bad = _("destination exists"); bad = _("destination exists");
if (force) { if (force) {
@ -247,11 +262,17 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
} else } else
bad = _("Cannot overwrite"); bad = _("Cannot overwrite");
} }
} else if (string_list_has_string(&src_for_dst, dst)) goto act_on_entry;
}
if (string_list_has_string(&src_for_dst, dst)) {
bad = _("multiple sources for the same target"); bad = _("multiple sources for the same target");
else if (is_dir_sep(dst[strlen(dst) - 1])) goto act_on_entry;
}
if (is_dir_sep(dst[strlen(dst) - 1])) {
bad = _("destination directory does not exist"); bad = _("destination directory does not exist");
else { goto act_on_entry;
}
/* /*
* We check if the paths are in the sparse-checkout * We check if the paths are in the sparse-checkout
* definition as a very final check, since that * definition as a very final check, since that
@ -273,8 +294,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
goto remove_entry; goto remove_entry;
string_list_insert(&src_for_dst, dst); string_list_insert(&src_for_dst, dst);
}
act_on_entry:
if (!bad) if (!bad)
continue; continue;
if (!ignore_errors) if (!ignore_errors)