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 <git@drmicha.warpmail.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed May 10, 2013 at 17:10 UTC 6c374008b1a4e3b66469168aace47003e9771e2d
4 files changed +19 -3
Documentation/technical/api-diff.txt
+9 -1
@@ -28,7 +28,8 @@ Calling sequence
28
29 * Call `diff_setup_done()`; this inspects the options set up so far for
30 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.
33
34 * As you find different pairs of files, call `diff_change()` to feed
35 modified files, `diff_addremove()` to feed created or deleted files,
@@ -115,6 +116,13 @@ Notable members are:
116 operation, but some do not have anything to do with the diffcore
117 library.
118
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 +
126 BINARY, TEXT;;
127 Affects the way how a file that is seemingly binary is treated.
128
builtin/log.c
+1
@@ -91,6 +91,7 @@ static void cmd_log_init_defaults(struct rev_info *rev)
91
92 if (default_date_mode)
93 rev->date_mode = parse_date_format(default_date_mode);
94 + rev->diffopt.touched_flags = 0;
95 }
96
97 static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
diff.c
+3
@@ -3213,6 +3213,9 @@ void diff_setup_done(struct diff_options *options)
3213 {
3214 int count = 0;
3215
3216 + if (options->set_default)
3217 + options->set_default(options);
3218 +
3219 if (options->output_format & DIFF_FORMAT_NAME)
3220 count++;
3221 if (options->output_format & DIFF_FORMAT_NAME_STATUS)
diff.h
+6 -2
@@ -87,8 +87,9 @@ typedef struct strbuf *(*diff_prefix_fn_t)(struct diff_options *opt, void *data)
87 #define DIFF_OPT_PICKAXE_IGNORE_CASE (1 << 30)
88
89 #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))
93 #define DIFF_XDL_TST(opts, flag) ((opts)->xdl_opts & XDF_##flag)
94 #define DIFF_XDL_SET(opts, flag) ((opts)->xdl_opts |= XDF_##flag)
95 #define DIFF_XDL_CLR(opts, flag) ((opts)->xdl_opts &= ~XDF_##flag)
@@ -109,6 +110,7 @@ struct diff_options {
110 const char *single_follow;
111 const char *a_prefix, *b_prefix;
112 unsigned flags;
113 + unsigned touched_flags;
114 int use_color;
115 int context;
116 int interhunkcontext;
@@ -145,6 +147,8 @@ struct diff_options {
147 /* to support internal diff recursion by --follow hack*/
148 int found_follow;
149
150 + void (*set_default)(struct diff_options *);
151 +
152 FILE *file;
153 int close_file;
154