@matheusbsbs / matheus-bagliano / commits / 4c3fe82ef1
Fork 0

diff-lib: accept option flags in run_diff_index()

In a future commit, we will teach run_diff_index() to accept more options via flag bits. For now, change `cached` into a flag in the `option` bitfield. The behaviour should remain exactly the same. Signed-off-by: Denton Liu <liu.denton@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Denton Liu committed Sep 20, 2020 at 04:22 UTC 4c3fe82ef17a6636c742b95cd292b83b02876e08
4 files changed +13 -10
builtin/diff-index.c
+4 -4
@@ -15,7 +15,7 @@ COMMON_DIFF_OPTIONS_HELP;
15 int cmd_diff_index(int argc, const char **argv, const char *prefix)
16 {
17 struct rev_info rev;
18 - int cached = 0;
18 + unsigned int option = 0;
19 int i;
20 int result;
21
@@ -32,7 +32,7 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
32 const char *arg = argv[i];
33
34 if (!strcmp(arg, "--cached"))
35 - cached = 1;
35 + option |= DIFF_INDEX_CACHED;
36 else
37 usage(diff_cache_usage);
38 }
@@ -46,7 +46,7 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
46 if (rev.pending.nr != 1 ||
47 rev.max_count != -1 || rev.min_age != -1 || rev.max_age != -1)
48 usage(diff_cache_usage);
49 - if (!cached) {
49 + if (!(option & DIFF_INDEX_CACHED)) {
50 setup_work_tree();
51 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
52 perror("read_cache_preload");
@@ -56,7 +56,7 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
56 perror("read_cache");
57 return -1;
58 }
59 - result = run_diff_index(&rev, cached);
59 + result = run_diff_index(&rev, option);
60 UNLEAK(rev);
61 return diff_result_code(&rev.diffopt, result);
62 }
builtin/diff.c
+4 -4
@@ -134,11 +134,11 @@ static int builtin_diff_blobs(struct rev_info *revs,
134 static int builtin_diff_index(struct rev_info *revs,
135 int argc, const char **argv)
136 {
137 - int cached = 0;
137 + unsigned int option = 0;
138 while (1 < argc) {
139 const char *arg = argv[1];
140 if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
141 - cached = 1;
141 + option |= DIFF_INDEX_CACHED;
142 else
143 usage(builtin_diff_usage);
144 argv++; argc--;
@@ -151,7 +151,7 @@ static int builtin_diff_index(struct rev_info *revs,
151 revs->max_count != -1 || revs->min_age != -1 ||
152 revs->max_age != -1)
153 usage(builtin_diff_usage);
154 - if (!cached) {
154 + if (!(option & DIFF_INDEX_CACHED)) {
155 setup_work_tree();
156 if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
157 perror("read_cache_preload");
@@ -161,7 +161,7 @@ static int builtin_diff_index(struct rev_info *revs,
161 perror("read_cache");
162 return -1;
163 }
164 - return run_diff_index(revs, cached);
164 + return run_diff_index(revs, option);
165 }
166
167 static int builtin_diff_tree(struct rev_info *revs,
diff-lib.c
+2 -1
@@ -512,9 +512,10 @@ static int diff_cache(struct rev_info *revs,
512 return unpack_trees(1, &t, &opts);
513 }
514
515 -int run_diff_index(struct rev_info *revs, int cached)
515 +int run_diff_index(struct rev_info *revs, unsigned int option)
516 {
517 struct object_array_entry *ent;
518 + int cached = !!(option & DIFF_INDEX_CACHED);
519
520 if (revs->pending.nr != 1)
521 BUG("run_diff_index must be passed exactly one tree");
diff.h
+3 -1
@@ -585,7 +585,9 @@ const char *diff_aligned_abbrev(const struct object_id *sha1, int);
585 /* report racily-clean paths as modified */
586 #define DIFF_RACY_IS_MODIFIED 02
587 int run_diff_files(struct rev_info *revs, unsigned int option);
588 -int run_diff_index(struct rev_info *revs, int cached);
588 +
589 +#define DIFF_INDEX_CACHED 01
590 +int run_diff_index(struct rev_info *revs, unsigned int option);
591
592 int do_diff_cache(const struct object_id *, struct diff_options *);
593 int diff_flush_patch_id(struct diff_options *, struct object_id *, int, int);