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:
parent
707fa2f76a
commit
7889755bae
55
builtin/mv.c
55
builtin/mv.c
@ -187,26 +187,36 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
|
||||
length = strlen(src);
|
||||
if (lstat(src, &st) < 0) {
|
||||
/* only error if existence is expected. */
|
||||
if (modes[i] != SPARSE)
|
||||
if (modes[i] != SPARSE) {
|
||||
bad = _("bad source");
|
||||
} else if (!strncmp(src, dst, length) &&
|
||||
goto act_on_entry;
|
||||
}
|
||||
}
|
||||
if (!strncmp(src, dst, length) &&
|
||||
(dst[length] == 0 || dst[length] == '/')) {
|
||||
bad = _("can not move directory into itself");
|
||||
} else if ((src_is_dir = S_ISDIR(st.st_mode))
|
||||
&& lstat(dst, &st) == 0)
|
||||
goto act_on_entry;
|
||||
}
|
||||
if ((src_is_dir = S_ISDIR(st.st_mode))
|
||||
&& lstat(dst, &st) == 0) {
|
||||
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;
|
||||
|
||||
if (first >= 0)
|
||||
if (first >= 0) {
|
||||
prepare_move_submodule(src, first,
|
||||
submodule_gitfile + i);
|
||||
else if (index_range_of_same_dir(src, length,
|
||||
&first, &last) < 1)
|
||||
goto act_on_entry;
|
||||
} else if (index_range_of_same_dir(src, length,
|
||||
&first, &last) < 1) {
|
||||
bad = _("source directory is empty");
|
||||
else { /* last - first >= 1 */
|
||||
int j, dst_len, n;
|
||||
goto act_on_entry;
|
||||
}
|
||||
|
||||
/* last - first >= 1 */
|
||||
modes[i] = WORKING_DIRECTORY;
|
||||
n = argc + last - first;
|
||||
REALLOC_ARRAY(source, n);
|
||||
@ -227,12 +237,17 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
|
||||
submodule_gitfile[argc + j] = NULL;
|
||||
}
|
||||
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");
|
||||
} else if (ce_stage(ce)) {
|
||||
goto act_on_entry;
|
||||
}
|
||||
if (ce_stage(ce)) {
|
||||
bad = _("conflicted");
|
||||
} else if (lstat(dst, &st) == 0 &&
|
||||
goto act_on_entry;
|
||||
}
|
||||
if (lstat(dst, &st) == 0 &&
|
||||
(!ignore_case || strcasecmp(src, dst))) {
|
||||
bad = _("destination exists");
|
||||
if (force) {
|
||||
@ -247,11 +262,17 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
|
||||
} else
|
||||
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");
|
||||
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");
|
||||
else {
|
||||
goto act_on_entry;
|
||||
}
|
||||
|
||||
/*
|
||||
* We check if the paths are in the sparse-checkout
|
||||
* 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;
|
||||
|
||||
string_list_insert(&src_for_dst, dst);
|
||||
}
|
||||
|
||||
act_on_entry:
|
||||
if (!bad)
|
||||
continue;
|
||||
if (!ignore_errors)
|
||||
|
Loading…
Reference in New Issue
Block a user