Skip to content

Commit 24ea81d

Browse files
ffyuandagitster
authored andcommitted
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/[email protected]/ Helped-by: Victoria Dye <[email protected]> Helped-by: Derrick Stolee <[email protected]> Signed-off-by: Shaoxuan Yuan <[email protected]> Acked-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8a26a39 commit 24ea81d

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

builtin/mv.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ static const char * const builtin_mv_usage[] = {
2020
NULL
2121
};
2222

23+
enum update_mode {
24+
BOTH = 0,
25+
WORKING_DIRECTORY = (1 << 1),
26+
INDEX = (1 << 2),
27+
SPARSE = (1 << 3),
28+
};
29+
2330
#define DUP_BASENAME 1
2431
#define KEEP_TRAILING_SLASH 2
2532

@@ -130,7 +137,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
130137
OPT_END(),
131138
};
132139
const char **source, **destination, **dest_path, **submodule_gitfile;
133-
enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX, SPARSE } *modes;
140+
enum update_mode *modes;
134141
struct stat st;
135142
struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
136143
struct lock_file lock_file = LOCK_INIT;
@@ -192,7 +199,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
192199
pos = cache_name_pos(src, length);
193200
if (pos < 0) {
194201
/* only error if existence is expected. */
195-
if (modes[i] != SPARSE)
202+
if (!(modes[i] & SPARSE))
196203
bad = _("bad source");
197204
goto act_on_entry;
198205
}
@@ -208,14 +215,14 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
208215
}
209216
/* Check if dst exists in index */
210217
if (cache_name_pos(dst, strlen(dst)) < 0) {
211-
modes[i] = SPARSE;
218+
modes[i] |= SPARSE;
212219
goto act_on_entry;
213220
}
214221
if (!force) {
215222
bad = _("destination exists");
216223
goto act_on_entry;
217224
}
218-
modes[i] = SPARSE;
225+
modes[i] |= SPARSE;
219226
goto act_on_entry;
220227
}
221228
if (!strncmp(src, dst, length) &&
@@ -243,7 +250,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
243250
}
244251

245252
/* last - first >= 1 */
246-
modes[i] = WORKING_DIRECTORY;
253+
modes[i] |= WORKING_DIRECTORY;
247254
n = argc + last - first;
248255
REALLOC_ARRAY(source, n);
249256
REALLOC_ARRAY(destination, n);
@@ -259,7 +266,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
259266
source[argc + j] = path;
260267
destination[argc + j] =
261268
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;
263271
submodule_gitfile[argc + j] = NULL;
264272
}
265273
argc += last - first;
@@ -361,7 +369,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
361369
printf(_("Renaming %s to %s\n"), src, dst);
362370
if (show_only)
363371
continue;
364-
if (mode != INDEX && mode != SPARSE && rename(src, dst) < 0) {
372+
if (!(mode & (INDEX | SPARSE)) &&
373+
rename(src, dst) < 0) {
365374
if (ignore_errors)
366375
continue;
367376
die_errno(_("renaming '%s' failed"), src);
@@ -375,7 +384,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
375384
1);
376385
}
377386

378-
if (mode == WORKING_DIRECTORY)
387+
if (mode & (WORKING_DIRECTORY))
379388
continue;
380389

381390
pos = cache_name_pos(src, strlen(src));

0 commit comments

Comments
 (0)