summaryrefslogtreecommitdiff
path: root/builtin/mv.c
diff options
context:
space:
mode:
authorShaoxuan Yuan <shaoxuan.yuan02@gmail.com>2022-06-30 02:37:36 (GMT)
committerJunio C Hamano <gitster@pobox.com>2022-07-01 21:50:16 (GMT)
commit24ea81d9ac401731c222b28097b339aa0d2d316d (patch)
treeafb2ee25268265b4c6f465d41a3a486404c0f643 /builtin/mv.c
parent8a26a3915fbe1aab9786b28c535c8541f8df2a19 (diff)
downloadgit-24ea81d9ac401731c222b28097b339aa0d2d316d.zip
git-24ea81d9ac401731c222b28097b339aa0d2d316d.tar.gz
git-24ea81d9ac401731c222b28097b339aa0d2d316d.tar.bz2
mv: use flags mode for update_mode
As suggested by Derrick [1], move the in-line definition of "enum update_mode" to the top of the file and make it use "flags" mode (each state is a different bit in the word). Change the flag assignments from '=' (single assignment) to '|=' (additive). Also change flag evaluation from '==' to '&', etc. [1] https://lore.kernel.org/git/22aadea2-9330-aa9e-7b6a-834585189144@github.com/ Helped-by: Victoria Dye <vdye@github.com> Helped-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>
Diffstat (limited to 'builtin/mv.c')
-rw-r--r--builtin/mv.c25
1 files changed, 17 insertions, 8 deletions
diff --git a/builtin/mv.c b/builtin/mv.c
index 7d96279..b805a0d 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -20,6 +20,13 @@ static const char * const builtin_mv_usage[] = {
NULL
};
+enum update_mode {
+ BOTH = 0,
+ WORKING_DIRECTORY = (1 << 1),
+ INDEX = (1 << 2),
+ SPARSE = (1 << 3),
+};
+
#define DUP_BASENAME 1
#define KEEP_TRAILING_SLASH 2
@@ -130,7 +137,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
OPT_END(),
};
const char **source, **destination, **dest_path, **submodule_gitfile;
- enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX, SPARSE } *modes;
+ enum update_mode *modes;
struct stat st;
struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
struct lock_file lock_file = LOCK_INIT;
@@ -192,7 +199,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
pos = cache_name_pos(src, length);
if (pos < 0) {
/* only error if existence is expected. */
- if (modes[i] != SPARSE)
+ if (!(modes[i] & SPARSE))
bad = _("bad source");
goto act_on_entry;
}
@@ -208,14 +215,14 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
}
/* Check if dst exists in index */
if (cache_name_pos(dst, strlen(dst)) < 0) {
- modes[i] = SPARSE;
+ modes[i] |= SPARSE;
goto act_on_entry;
}
if (!force) {
bad = _("destination exists");
goto act_on_entry;
}
- modes[i] = SPARSE;
+ modes[i] |= SPARSE;
goto act_on_entry;
}
if (!strncmp(src, dst, length) &&
@@ -243,7 +250,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
}
/* last - first >= 1 */
- modes[i] = WORKING_DIRECTORY;
+ modes[i] |= WORKING_DIRECTORY;
n = argc + last - first;
REALLOC_ARRAY(source, n);
REALLOC_ARRAY(destination, n);
@@ -259,7 +266,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
source[argc + j] = path;
destination[argc + j] =
prefix_path(dst, dst_len, path + length + 1);
- modes[argc + j] = ce_skip_worktree(ce) ? SPARSE : INDEX;
+ memset(modes + argc + j, 0, sizeof(enum update_mode));
+ modes[argc + j] |= ce_skip_worktree(ce) ? SPARSE : INDEX;
submodule_gitfile[argc + j] = NULL;
}
argc += last - first;
@@ -361,7 +369,8 @@ remove_entry:
printf(_("Renaming %s to %s\n"), src, dst);
if (show_only)
continue;
- if (mode != INDEX && mode != SPARSE && rename(src, dst) < 0) {
+ if (!(mode & (INDEX | SPARSE)) &&
+ rename(src, dst) < 0) {
if (ignore_errors)
continue;
die_errno(_("renaming '%s' failed"), src);
@@ -375,7 +384,7 @@ remove_entry:
1);
}
- if (mode == WORKING_DIRECTORY)
+ if (mode & (WORKING_DIRECTORY))
continue;
pos = cache_name_pos(src, strlen(src));