Skip to content

Commit af2f368

Browse files
pcloudsgitster
authored andcommitted
diff-parseopt: convert --output-*
This also validates that the user specifies a single character in --output-indicator-*, not a string. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 7d7942b commit af2f368

File tree

2 files changed

+63
-18
lines changed

2 files changed

+63
-18
lines changed

Documentation/diff-options.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ ifndef::git-format-patch[]
4141
Implies `-p`.
4242
endif::git-format-patch[]
4343

44+
--output=<file>::
45+
Output to a specific file instead of stdout.
46+
47+
--output-indicator-new=<char>::
48+
--output-indicator-old=<char>::
49+
--output-indicator-context=<char>::
50+
Specify the character used to indicate new, old or context
51+
lines in the generated patch. Normally they are '+', '-' and
52+
' ' respectively.
53+
4454
ifndef::git-format-patch[]
4555
--raw::
4656
ifndef::git-log[]

diff.c

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4841,6 +4841,19 @@ static int parse_objfind_opt(struct diff_options *opt, const char *arg)
48414841
return 1;
48424842
}
48434843

4844+
static int diff_opt_char(const struct option *opt,
4845+
const char *arg, int unset)
4846+
{
4847+
char *value = opt->value;
4848+
4849+
BUG_ON_OPT_NEG(unset);
4850+
if (arg[1])
4851+
return error(_("%s expects a character, got '%s'"),
4852+
opt->long_name, arg);
4853+
*value = arg[0];
4854+
return 0;
4855+
}
4856+
48444857
static int diff_opt_compact_summary(const struct option *opt,
48454858
const char *arg, int unset)
48464859
{
@@ -4872,6 +4885,23 @@ static int diff_opt_dirstat(const struct option *opt,
48724885
return 0;
48734886
}
48744887

4888+
static enum parse_opt_result diff_opt_output(struct parse_opt_ctx_t *ctx,
4889+
const struct option *opt,
4890+
const char *arg, int unset)
4891+
{
4892+
struct diff_options *options = opt->value;
4893+
char *path;
4894+
4895+
BUG_ON_OPT_NEG(unset);
4896+
path = prefix_filename(ctx->prefix, arg);
4897+
options->file = xfopen(path, "w");
4898+
options->close_file = 1;
4899+
if (options->use_color != GIT_COLOR_ALWAYS)
4900+
options->use_color = GIT_COLOR_NEVER;
4901+
free(path);
4902+
return 0;
4903+
}
4904+
48754905
static int diff_opt_unified(const struct option *opt,
48764906
const char *arg, int unset)
48774907
{
@@ -4965,6 +4995,27 @@ static void prep_parse_options(struct diff_options *options)
49654995
OPT_CALLBACK_F(0, "compact-summary", options, NULL,
49664996
N_("generate compact summary in diffstat"),
49674997
PARSE_OPT_NOARG, diff_opt_compact_summary),
4998+
OPT_CALLBACK_F(0, "output-indicator-new",
4999+
&options->output_indicators[OUTPUT_INDICATOR_NEW],
5000+
N_("<char>"),
5001+
N_("specify the character to indicate a new line instead of '+'"),
5002+
PARSE_OPT_NONEG, diff_opt_char),
5003+
OPT_CALLBACK_F(0, "output-indicator-old",
5004+
&options->output_indicators[OUTPUT_INDICATOR_OLD],
5005+
N_("<char>"),
5006+
N_("specify the character to indicate an old line instead of '-'"),
5007+
PARSE_OPT_NONEG, diff_opt_char),
5008+
OPT_CALLBACK_F(0, "output-indicator-context",
5009+
&options->output_indicators[OUTPUT_INDICATOR_CONTEXT],
5010+
N_("<char>"),
5011+
N_("specify the character to indicate a context instead of ' '"),
5012+
PARSE_OPT_NONEG, diff_opt_char),
5013+
5014+
OPT_GROUP(N_("Diff other options")),
5015+
{ OPTION_CALLBACK, 0, "output", options, N_("<file>"),
5016+
N_("Output to a specific file"),
5017+
PARSE_OPT_NONEG, NULL, 0, diff_opt_output },
5018+
49685019
OPT_END()
49695020
};
49705021

@@ -4992,16 +5043,8 @@ int diff_opt_parse(struct diff_options *options,
49925043
if (ac)
49935044
return ac;
49945045

4995-
/* Output format options */
4996-
if (skip_prefix(arg, "--output-indicator-new=", &arg))
4997-
options->output_indicators[OUTPUT_INDICATOR_NEW] = arg[0];
4998-
else if (skip_prefix(arg, "--output-indicator-old=", &arg))
4999-
options->output_indicators[OUTPUT_INDICATOR_OLD] = arg[0];
5000-
else if (skip_prefix(arg, "--output-indicator-context=", &arg))
5001-
options->output_indicators[OUTPUT_INDICATOR_CONTEXT] = arg[0];
5002-
50035046
/* renames options */
5004-
else if (starts_with(arg, "-B") ||
5047+
if (starts_with(arg, "-B") ||
50055048
skip_to_optional_arg(arg, "--break-rewrites", NULL)) {
50065049
if ((options->break_opt = diff_scoreopt_parse(arg)) == -1)
50075050
return error("invalid argument to -B: %s", arg+2);
@@ -5242,15 +5285,7 @@ int diff_opt_parse(struct diff_options *options,
52425285
else if (opt_arg(arg, '\0', "inter-hunk-context",
52435286
&options->interhunkcontext))
52445287
;
5245-
else if ((argcount = parse_long_opt("output", av, &optarg))) {
5246-
char *path = prefix_filename(prefix, optarg);
5247-
options->file = xfopen(path, "w");
5248-
options->close_file = 1;
5249-
if (options->use_color != GIT_COLOR_ALWAYS)
5250-
options->use_color = GIT_COLOR_NEVER;
5251-
free(path);
5252-
return argcount;
5253-
} else
5288+
else
52545289
return 0;
52555290
return 1;
52565291
}

0 commit comments

Comments
 (0)