diff-parseopt: convert -B|--break-rewrites
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
ced4e179feaadd2c0b43fa963acedc7243d09a58
1 file changed
+36
-26
diff.c
+36
-26
@@ -4841,6 +4841,30 @@ static int parse_objfind_opt(struct diff_options *opt, const char *arg)
4841
return 1;
4842
}
4843
4844
+static int diff_opt_break_rewrites(const struct option *opt,
4845
+ const char *arg, int unset)
4846
+{
4847
+ int *break_opt = opt->value;
4848
+ int opt1, opt2;
4849
+
4850
+ BUG_ON_OPT_NEG(unset);
4851
+ if (!arg)
4852
+ arg = "";
4853
+ opt1 = parse_rename_score(&arg);
4854
+ if (*arg == 0)
4855
+ opt2 = 0;
4856
+ else if (*arg != '/')
4857
+ return error(_("%s expects <n>/<m> form"), opt->long_name);
4858
+ else {
4859
+ arg++;
4860
+ opt2 = parse_rename_score(&arg);
4861
+ }
4862
+ if (*arg != 0)
4863
+ return error(_("%s expects <n>/<m> form"), opt->long_name);
4864
+ *break_opt = opt1 | (opt2 << 16);
4865
+ return 0;
4866
+}
4867
+
4868
static int diff_opt_char(const struct option *opt,
4869
const char *arg, int unset)
4870
{
@@ -5011,6 +5035,12 @@ static void prep_parse_options(struct diff_options *options)
5035
N_("specify the character to indicate a context instead of ' '"),
5036
PARSE_OPT_NONEG, diff_opt_char),
5037
5038
+ OPT_GROUP(N_("Diff rename options")),
5039
+ OPT_CALLBACK_F('B', "break-rewrites", &options->break_opt, N_("<n>[/<m>]"),
5040
+ N_("break complete rewrite changes into pairs of delete and create"),
5041
+ PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5042
+ diff_opt_break_rewrites),
5043
+
5044
OPT_GROUP(N_("Diff other options")),
5045
{ OPTION_CALLBACK, 0, "output", options, N_("<file>"),
5046
N_("Output to a specific file"),
@@ -5044,12 +5074,7 @@ int diff_opt_parse(struct diff_options *options,
5074
return ac;
5075
5076
/* renames options */
5047
- if (starts_with(arg, "-B") ||
5048
- skip_to_optional_arg(arg, "--break-rewrites", NULL)) {
5049
- if ((options->break_opt = diff_scoreopt_parse(arg)) == -1)
5050
- return error("invalid argument to -B: %s", arg+2);
5051
- }
5052
- else if (starts_with(arg, "-M") ||
5077
+ if (starts_with(arg, "-M") ||
5078
skip_to_optional_arg(arg, "--find-renames", NULL)) {
5079
if ((options->rename_score = diff_scoreopt_parse(arg)) == -1)
5080
return error("invalid argument to -M: %s", arg+2);
@@ -5328,17 +5353,14 @@ int parse_rename_score(const char **cp_p)
5353
5354
static int diff_scoreopt_parse(const char *opt)
5355
{
5331
- int opt1, opt2, cmd;
5356
+ int opt1, cmd;
5357
5358
if (*opt++ != '-')
5359
return -1;
5360
cmd = *opt++;
5361
if (cmd == '-') {
5362
/* convert the long-form arguments into short-form versions */
5338
- if (skip_prefix(opt, "break-rewrites", &opt)) {
5339
- if (*opt == 0 || *opt++ == '=')
5340
- cmd = 'B';
5341
- } else if (skip_prefix(opt, "find-copies", &opt)) {
5363
+ if (skip_prefix(opt, "find-copies", &opt)) {
5364
if (*opt == 0 || *opt++ == '=')
5365
cmd = 'C';
5366
} else if (skip_prefix(opt, "find-renames", &opt)) {
@@ -5346,25 +5368,13 @@ static int diff_scoreopt_parse(const char *opt)
5368
cmd = 'M';
5369
}
5370
}
5349
- if (cmd != 'M' && cmd != 'C' && cmd != 'B')
5350
- return -1; /* that is not a -M, -C, or -B option */
5371
+ if (cmd != 'M' && cmd != 'C')
5372
+ return -1; /* that is not a -M, or -C option */
5373
5374
opt1 = parse_rename_score(&opt);
5353
- if (cmd != 'B')
5354
- opt2 = 0;
5355
- else {
5356
- if (*opt == 0)
5357
- opt2 = 0;
5358
- else if (*opt != '/')
5359
- return -1; /* we expect -B80/99 or -B80 */
5360
- else {
5361
- opt++;
5362
- opt2 = parse_rename_score(&opt);
5363
- }
5364
- }
5375
if (*opt != 0)
5376
return -1;
5367
- return opt1 | (opt2 << 16);
5377
+ return opt1;
5378
}
5379
5380
struct diff_queue_struct diff_queued_diff;