Skip to content

Commit 6c37400

Browse files
committed
diff_opt: track whether flags have been set explicitly
The diff_opt infrastructure sets flags based on defaults and command line options. It is impossible to tell whether a flag has been set as a default or on explicit request. Update the structure so that this detection is possible: * Add an extra "opt->touched_flags" that keeps track of all the fields that have been touched by DIFF_OPT_SET and DIFF_OPT_CLR. * You may continue setting the default values to the flags, like commands in the "log" family do in cmd_log_init_defaults(), but after you finished setting the defaults, you clear the touched_flags field; * And then you let the usual callchain call diff_opt_parse(), allowing the opt->flags be set or unset, while keeping track of which bits the user touched; * There is an optional callback "opt->set_default" that is called at the very beginning to let you inspect touched_flags and update opt->flags appropriately, before the remainder of the diffcore machinery is set up, taking the opt->flags value into account. Signed-off-by: Michael J Gruber <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4bd52d0 commit 6c37400

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

Documentation/technical/api-diff.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ Calling sequence
2828

2929
* Call `diff_setup_done()`; this inspects the options set up so far for
3030
internal consistency and make necessary tweaking to it (e.g. if
31-
textual patch output was asked, recursive behaviour is turned on).
31+
textual patch output was asked, recursive behaviour is turned on);
32+
the callback set_default in diff_options can be used to tweak this more.
3233

3334
* As you find different pairs of files, call `diff_change()` to feed
3435
modified files, `diff_addremove()` to feed created or deleted files,
@@ -115,6 +116,13 @@ Notable members are:
115116
operation, but some do not have anything to do with the diffcore
116117
library.
117118

119+
`touched_flags`::
120+
Records whether a flag has been changed due to user request
121+
(rather than just set/unset by default).
122+
123+
`set_default`::
124+
Callback which allows tweaking the options in diff_setup_done().
125+
118126
BINARY, TEXT;;
119127
Affects the way how a file that is seemingly binary is treated.
120128

builtin/log.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ static void cmd_log_init_defaults(struct rev_info *rev)
9191

9292
if (default_date_mode)
9393
rev->date_mode = parse_date_format(default_date_mode);
94+
rev->diffopt.touched_flags = 0;
9495
}
9596

9697
static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,

diff.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3213,6 +3213,9 @@ void diff_setup_done(struct diff_options *options)
32133213
{
32143214
int count = 0;
32153215

3216+
if (options->set_default)
3217+
options->set_default(options);
3218+
32163219
if (options->output_format & DIFF_FORMAT_NAME)
32173220
count++;
32183221
if (options->output_format & DIFF_FORMAT_NAME_STATUS)

diff.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ typedef struct strbuf *(*diff_prefix_fn_t)(struct diff_options *opt, void *data)
8787
#define DIFF_OPT_PICKAXE_IGNORE_CASE (1 << 30)
8888

8989
#define DIFF_OPT_TST(opts, flag) ((opts)->flags & DIFF_OPT_##flag)
90-
#define DIFF_OPT_SET(opts, flag) ((opts)->flags |= DIFF_OPT_##flag)
91-
#define DIFF_OPT_CLR(opts, flag) ((opts)->flags &= ~DIFF_OPT_##flag)
90+
#define DIFF_OPT_TOUCHED(opts, flag) ((opts)->touched_flags & DIFF_OPT_##flag)
91+
#define DIFF_OPT_SET(opts, flag) (((opts)->flags |= DIFF_OPT_##flag),((opts)->touched_flags |= DIFF_OPT_##flag))
92+
#define DIFF_OPT_CLR(opts, flag) (((opts)->flags &= ~DIFF_OPT_##flag),((opts)->touched_flags |= DIFF_OPT_##flag))
9293
#define DIFF_XDL_TST(opts, flag) ((opts)->xdl_opts & XDF_##flag)
9394
#define DIFF_XDL_SET(opts, flag) ((opts)->xdl_opts |= XDF_##flag)
9495
#define DIFF_XDL_CLR(opts, flag) ((opts)->xdl_opts &= ~XDF_##flag)
@@ -109,6 +110,7 @@ struct diff_options {
109110
const char *single_follow;
110111
const char *a_prefix, *b_prefix;
111112
unsigned flags;
113+
unsigned touched_flags;
112114
int use_color;
113115
int context;
114116
int interhunkcontext;
@@ -145,6 +147,8 @@ struct diff_options {
145147
/* to support internal diff recursion by --follow hack*/
146148
int found_follow;
147149

150+
void (*set_default)(struct diff_options *);
151+
148152
FILE *file;
149153
int close_file;
150154

0 commit comments

Comments
 (0)