diff-parseopt: convert --word-diff
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Mar 5, 2019 at 19:30 UTC
e9fb39b668d5f2a083e70bb15e2cdf191cb10205
1 file changed
+29
-18
diff.c
+29
-18
@@ -5066,6 +5066,32 @@ static int diff_opt_unified(const struct option *opt,
5066
return 0;
5067
}
5068
5069
+static int diff_opt_word_diff(const struct option *opt,
5070
+ const char *arg, int unset)
5071
+{
5072
+ struct diff_options *options = opt->value;
5073
+
5074
+ BUG_ON_OPT_NEG(unset);
5075
+ if (arg) {
5076
+ if (!strcmp(arg, "plain"))
5077
+ options->word_diff = DIFF_WORDS_PLAIN;
5078
+ else if (!strcmp(arg, "color")) {
5079
+ options->use_color = 1;
5080
+ options->word_diff = DIFF_WORDS_COLOR;
5081
+ }
5082
+ else if (!strcmp(arg, "porcelain"))
5083
+ options->word_diff = DIFF_WORDS_PORCELAIN;
5084
+ else if (!strcmp(arg, "none"))
5085
+ options->word_diff = DIFF_WORDS_NONE;
5086
+ else
5087
+ return error(_("bad --word-diff argument: %s"), arg);
5088
+ } else {
5089
+ if (options->word_diff == DIFF_WORDS_NONE)
5090
+ options->word_diff = DIFF_WORDS_PLAIN;
5091
+ }
5092
+ return 0;
5093
+}
5094
+
5095
static void prep_parse_options(struct diff_options *options)
5096
{
5097
struct option parseopts[] = {
@@ -5228,6 +5254,9 @@ static void prep_parse_options(struct diff_options *options)
5254
OPT_CALLBACK_F(0, "anchored", options, N_("<text>"),
5255
N_("generate diff using the \"anchored diff\" algorithm"),
5256
PARSE_OPT_NONEG, diff_opt_anchored),
5257
+ OPT_CALLBACK_F(0, "word-diff", options, N_("<mode>"),
5258
+ N_("show word diff, using <mode> to delimit changed words"),
5259
+ PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_word_diff),
5260
5261
OPT_GROUP(N_("Diff other options")),
5262
OPT_CALLBACK_F(0, "relative", options, N_("<prefix>"),
@@ -5291,24 +5320,6 @@ int diff_opt_parse(struct diff_options *options,
5320
options->use_color = 1;
5321
options->word_diff = DIFF_WORDS_COLOR;
5322
}
5294
- else if (!strcmp(arg, "--word-diff")) {
5295
- if (options->word_diff == DIFF_WORDS_NONE)
5296
- options->word_diff = DIFF_WORDS_PLAIN;
5297
- }
5298
- else if (skip_prefix(arg, "--word-diff=", &arg)) {
5299
- if (!strcmp(arg, "plain"))
5300
- options->word_diff = DIFF_WORDS_PLAIN;
5301
- else if (!strcmp(arg, "color")) {
5302
- options->use_color = 1;
5303
- options->word_diff = DIFF_WORDS_COLOR;
5304
- }
5305
- else if (!strcmp(arg, "porcelain"))
5306
- options->word_diff = DIFF_WORDS_PORCELAIN;
5307
- else if (!strcmp(arg, "none"))
5308
- options->word_diff = DIFF_WORDS_NONE;
5309
- else
5310
- die("bad --word-diff argument: %s", arg);
5311
- }
5323
else if ((argcount = parse_long_opt("word-diff-regex", av, &optarg))) {
5324
if (options->word_diff == DIFF_WORDS_NONE)
5325
options->word_diff = DIFF_WORDS_PLAIN;