Skip to content

Commit

Permalink
diff: add helper to set algorithm
Browse files Browse the repository at this point in the history
Setting the diff algorithm can be done in multiple places. The config,
the command line, and in the subsequent commit, through attributes.

Refactor the logic that sets the diff algorithm out of
diff_opt_diff_algorithm() into a helper that will allow multiple
callsites to set the diff algorithm.

Also introduce a new macro that can be used to easily check if any diff
algorithm or the minimal algorithm has been set.

Signed-off-by: John Cai <johncai86@gmail.com>
  • Loading branch information
John Cai committed Feb 3, 2023
1 parent 2fc9e9c commit df25166
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
28 changes: 17 additions & 11 deletions diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -3437,6 +3437,22 @@ static int diff_filepair_is_phoney(struct diff_filespec *one,
return !DIFF_FILE_VALID(one) && !DIFF_FILE_VALID(two);
}

static int set_diff_algorithm(struct diff_options *opts,
const char *alg)
{
long value = parse_algorithm_value(alg);

if (value < 0)
return error(_("option diff-algorithm accepts \"myers\", "
"\"minimal\", \"patience\" and \"histogram\""));

/* clear out previous settings */
opts->xdl_opts &= ~XDF_DIFF_ALG_OR_MIN_MASK;
opts->xdl_opts |= value;

return 0;
}

static void builtin_diff(const char *name_a,
const char *name_b,
struct diff_filespec *one,
Expand Down Expand Up @@ -5106,19 +5122,9 @@ static int diff_opt_compact_summary(const struct option *opt,
static int diff_opt_diff_algorithm(const struct option *opt,
const char *arg, int unset)
{
struct diff_options *options = opt->value;
long value = parse_algorithm_value(arg);

BUG_ON_OPT_NEG(unset);
if (value < 0)
return error(_("option diff-algorithm accepts \"myers\", "
"\"minimal\", \"patience\" and \"histogram\""));

/* clear out previous settings */
DIFF_XDL_CLR(options, NEED_MINIMAL);
options->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;
options->xdl_opts |= value;
return 0;
return set_diff_algorithm(opt->value, arg);
}

static int diff_opt_dirstat(const struct option *opt,
Expand Down
1 change: 1 addition & 0 deletions xdiff/xdiff.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ extern "C" {
#define XDF_PATIENCE_DIFF (1 << 14)
#define XDF_HISTOGRAM_DIFF (1 << 15)
#define XDF_DIFF_ALGORITHM_MASK (XDF_PATIENCE_DIFF | XDF_HISTOGRAM_DIFF)
#define XDF_DIFF_ALG_OR_MIN_MASK (XDF_DIFF_ALGORITHM_MASK | XDF_NEED_MINIMAL)
#define XDF_DIFF_ALG(x) ((x) & XDF_DIFF_ALGORITHM_MASK)

#define XDF_INDENT_HEURISTIC (1 << 23)
Expand Down

0 comments on commit df25166

Please sign in to comment.