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>
Shaoxuan Yuan committed
Jun 30, 2022 at 10:37 UTC
24ea81d9ac401731c222b28097b339aa0d2d316d
1 file changed
+17
-8
builtin/mv.c
+17
-8
@@ -20,6 +20,13 @@ static const char * const builtin_mv_usage[] = {
20
NULL
21
};
22
23
+enum update_mode {
24
+ BOTH = 0,
25
+ WORKING_DIRECTORY = (1 << 1),
26
+ INDEX = (1 << 2),
27
+ SPARSE = (1 << 3),
28
+};
29
+
30
#define DUP_BASENAME 1
31
#define KEEP_TRAILING_SLASH 2
32
@@ -130,7 +137,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
137
OPT_END(),
138
};
139
const char **source, **destination, **dest_path, **submodule_gitfile;
133
- enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX, SPARSE } *modes;
140
+ enum update_mode *modes;
141
struct stat st;
142
struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
143
struct lock_file lock_file = LOCK_INIT;
@@ -192,7 +199,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
199
pos = cache_name_pos(src, length);
200
if (pos < 0) {
201
/* only error if existence is expected. */
195
- if (modes[i] != SPARSE)
202
+ if (!(modes[i] & SPARSE))
203
bad = _("bad source");
204
goto act_on_entry;
205
}
@@ -208,14 +215,14 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
215
}
216
/* Check if dst exists in index */
217
if (cache_name_pos(dst, strlen(dst)) < 0) {
211
- modes[i] = SPARSE;
218
+ modes[i] |= SPARSE;
219
goto act_on_entry;
220
}
221
if (!force) {
222
bad = _("destination exists");
223
goto act_on_entry;
224
}
218
- modes[i] = SPARSE;
225
+ modes[i] |= SPARSE;
226
goto act_on_entry;
227
}
228
if (!strncmp(src, dst, length) &&
@@ -243,7 +250,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
250
}
251
252
/* last - first >= 1 */
246
- modes[i] = WORKING_DIRECTORY;
253
+ modes[i] |= WORKING_DIRECTORY;
254
n = argc + last - first;
255
REALLOC_ARRAY(source, n);
256
REALLOC_ARRAY(destination, n);
@@ -259,7 +266,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
266
source[argc + j] = path;
267
destination[argc + j] =
268
prefix_path(dst, dst_len, path + length + 1);
262
- modes[argc + j] = ce_skip_worktree(ce) ? SPARSE : INDEX;
269
+ memset(modes + argc + j, 0, sizeof(enum update_mode));
270
+ modes[argc + j] |= ce_skip_worktree(ce) ? SPARSE : INDEX;
271
submodule_gitfile[argc + j] = NULL;
272
}
273
argc += last - first;
@@ -361,7 +369,8 @@ remove_entry:
369
printf(_("Renaming %s to %s\n"), src, dst);
370
if (show_only)
371
continue;
364
- if (mode != INDEX && mode != SPARSE && rename(src, dst) < 0) {
372
+ if (!(mode & (INDEX | SPARSE)) &&
373
+ rename(src, dst) < 0) {
374
if (ignore_errors)
375
continue;
376
die_errno(_("renaming '%s' failed"), src);
@@ -375,7 +384,7 @@ remove_entry:
384
1);
385
}
386
378
- if (mode == WORKING_DIRECTORY)
387
+ if (mode & (WORKING_DIRECTORY))
388
continue;
389
390
pos = cache_name_pos(src, strlen(src));