Skip to content

Commit 4c3fe82

Browse files
Denton-Lgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 308d7a7 commit 4c3fe82

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

builtin/diff-index.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ COMMON_DIFF_OPTIONS_HELP;
1515
int cmd_diff_index(int argc, const char **argv, const char *prefix)
1616
{
1717
struct rev_info rev;
18-
int cached = 0;
18+
unsigned int option = 0;
1919
int i;
2020
int result;
2121

@@ -32,7 +32,7 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
3232
const char *arg = argv[i];
3333

3434
if (!strcmp(arg, "--cached"))
35-
cached = 1;
35+
option |= DIFF_INDEX_CACHED;
3636
else
3737
usage(diff_cache_usage);
3838
}
@@ -46,7 +46,7 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
4646
if (rev.pending.nr != 1 ||
4747
rev.max_count != -1 || rev.min_age != -1 || rev.max_age != -1)
4848
usage(diff_cache_usage);
49-
if (!cached) {
49+
if (!(option & DIFF_INDEX_CACHED)) {
5050
setup_work_tree();
5151
if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
5252
perror("read_cache_preload");
@@ -56,7 +56,7 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
5656
perror("read_cache");
5757
return -1;
5858
}
59-
result = run_diff_index(&rev, cached);
59+
result = run_diff_index(&rev, option);
6060
UNLEAK(rev);
6161
return diff_result_code(&rev.diffopt, result);
6262
}

builtin/diff.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ static int builtin_diff_blobs(struct rev_info *revs,
134134
static int builtin_diff_index(struct rev_info *revs,
135135
int argc, const char **argv)
136136
{
137-
int cached = 0;
137+
unsigned int option = 0;
138138
while (1 < argc) {
139139
const char *arg = argv[1];
140140
if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
141-
cached = 1;
141+
option |= DIFF_INDEX_CACHED;
142142
else
143143
usage(builtin_diff_usage);
144144
argv++; argc--;
@@ -151,7 +151,7 @@ static int builtin_diff_index(struct rev_info *revs,
151151
revs->max_count != -1 || revs->min_age != -1 ||
152152
revs->max_age != -1)
153153
usage(builtin_diff_usage);
154-
if (!cached) {
154+
if (!(option & DIFF_INDEX_CACHED)) {
155155
setup_work_tree();
156156
if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
157157
perror("read_cache_preload");
@@ -161,7 +161,7 @@ static int builtin_diff_index(struct rev_info *revs,
161161
perror("read_cache");
162162
return -1;
163163
}
164-
return run_diff_index(revs, cached);
164+
return run_diff_index(revs, option);
165165
}
166166

167167
static int builtin_diff_tree(struct rev_info *revs,

diff-lib.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,10 @@ static int diff_cache(struct rev_info *revs,
512512
return unpack_trees(1, &t, &opts);
513513
}
514514

515-
int run_diff_index(struct rev_info *revs, int cached)
515+
int run_diff_index(struct rev_info *revs, unsigned int option)
516516
{
517517
struct object_array_entry *ent;
518+
int cached = !!(option & DIFF_INDEX_CACHED);
518519

519520
if (revs->pending.nr != 1)
520521
BUG("run_diff_index must be passed exactly one tree");

diff.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,9 @@ const char *diff_aligned_abbrev(const struct object_id *sha1, int);
585585
/* report racily-clean paths as modified */
586586
#define DIFF_RACY_IS_MODIFIED 02
587587
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);
589591

590592
int do_diff_cache(const struct object_id *, struct diff_options *);
591593
int diff_flush_patch_id(struct diff_options *, struct object_id *, int, int);

0 commit comments

Comments
 (0)