diff-parseopt: convert -C|--find-copies
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
Feb 21, 2019 at 18:16 UTC
7f64850d364b471f27f3123c4334397f9e03b09c
1 file changed
+25
-34
diff.c
+25
-34
@@ -4617,8 +4617,6 @@ static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *va
4617
return 1;
4618
}
4619
4620
-static int diff_scoreopt_parse(const char *opt);
4621
-
4620
static inline int short_opt(char opt, const char **argv,
4621
const char **optarg)
4622
{
@@ -4909,6 +4907,26 @@ static int diff_opt_dirstat(const struct option *opt,
4907
return 0;
4908
}
4909
4910
+static int diff_opt_find_copies(const struct option *opt,
4911
+ const char *arg, int unset)
4912
+{
4913
+ struct diff_options *options = opt->value;
4914
+
4915
+ BUG_ON_OPT_NEG(unset);
4916
+ if (!arg)
4917
+ arg = "";
4918
+ options->rename_score = parse_rename_score(&arg);
4919
+ if (*arg != 0)
4920
+ return error(_("invalid argument to %s"), opt->long_name);
4921
+
4922
+ if (options->detect_rename == DIFF_DETECT_COPY)
4923
+ options->flags.find_copies_harder = 1;
4924
+ else
4925
+ options->detect_rename = DIFF_DETECT_COPY;
4926
+
4927
+ return 0;
4928
+}
4929
+
4930
static int diff_opt_find_renames(const struct option *opt,
4931
const char *arg, int unset)
4932
{
@@ -5063,6 +5081,10 @@ static void prep_parse_options(struct diff_options *options)
5081
OPT_SET_INT_F('D', "irreversible-delete", &options->irreversible_delete,
5082
N_("omit the preimage for deletes"),
5083
1, PARSE_OPT_NONEG),
5084
+ OPT_CALLBACK_F('C', "find-copies", options, N_("<n>"),
5085
+ N_("detect copies"),
5086
+ PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5087
+ diff_opt_find_copies),
5088
5089
OPT_GROUP(N_("Diff other options")),
5090
{ OPTION_CALLBACK, 0, "output", options, N_("<file>"),
@@ -5097,15 +5119,7 @@ int diff_opt_parse(struct diff_options *options,
5119
return ac;
5120
5121
/* renames options */
5100
- if (starts_with(arg, "-C") ||
5101
- skip_to_optional_arg(arg, "--find-copies", NULL)) {
5102
- if (options->detect_rename == DIFF_DETECT_COPY)
5103
- options->flags.find_copies_harder = 1;
5104
- if ((options->rename_score = diff_scoreopt_parse(arg)) == -1)
5105
- return error("invalid argument to -C: %s", arg+2);
5106
- options->detect_rename = DIFF_DETECT_COPY;
5107
- }
5108
- else if (!strcmp(arg, "--no-renames"))
5122
+ if (!strcmp(arg, "--no-renames"))
5123
options->detect_rename = 0;
5124
else if (!strcmp(arg, "--rename-empty"))
5125
options->flags.rename_empty = 1;
@@ -5365,29 +5379,6 @@ int parse_rename_score(const char **cp_p)
5379
return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale));
5380
}
5381
5368
-static int diff_scoreopt_parse(const char *opt)
5369
-{
5370
- int opt1, cmd;
5371
-
5372
- if (*opt++ != '-')
5373
- return -1;
5374
- cmd = *opt++;
5375
- if (cmd == '-') {
5376
- /* convert the long-form arguments into short-form versions */
5377
- if (skip_prefix(opt, "find-copies", &opt)) {
5378
- if (*opt == 0 || *opt++ == '=')
5379
- cmd = 'C';
5380
- }
5381
- }
5382
- if (cmd != 'C')
5383
- return -1; /* that is not a -M option */
5384
-
5385
- opt1 = parse_rename_score(&opt);
5386
- if (*opt != 0)
5387
- return -1;
5388
- return opt1;
5389
-}
5390
-
5382
struct diff_queue_struct diff_queued_diff;
5383
5384
void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)