diff.c: convert -U|--unified

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 Jan 27, 2019 at 07:35 UTC d473e2e0e8eedf2ab07ddc545e00bff7c8f450e7
3 files changed +24 -6
Documentation/diff-options.txt
+1 -1
@@ -36,7 +36,7 @@ endif::git-format-patch[]
36 -U<n>::
37 --unified=<n>::
38 Generate diffs with <n> lines of context instead of
39 - the usual three.
39 + the usual three. Implies `--patch`.
40 ifndef::git-format-patch[]
41 Implies `-p`.
42 endif::git-format-patch[]
diff.c
+20 -3
@@ -4867,6 +4867,22 @@ static int parse_objfind_opt(struct diff_options *opt, const char *arg)
4867 return 1;
4868 }
4869
4870 +static int diff_opt_unified(const struct option *opt,
4871 + const char *arg, int unset)
4872 +{
4873 + struct diff_options *options = opt->value;
4874 + char *s;
4875 +
4876 + BUG_ON_OPT_NEG(unset);
4877 +
4878 + options->context = strtol(arg, &s, 10);
4879 + if (*s)
4880 + return error(_("%s expects a numerical value"), "--unified");
4881 + enable_patch_output(&options->output_format);
4882 +
4883 + return 0;
4884 +}
4885 +
4886 static void prep_parse_options(struct diff_options *options)
4887 {
4888 struct option parseopts[] = {
@@ -4877,6 +4893,9 @@ static void prep_parse_options(struct diff_options *options)
4893 OPT_BITOP('u', NULL, &options->output_format,
4894 N_("generate patch"),
4895 DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT),
4896 + OPT_CALLBACK_F('U', "unified", options, N_("<n>"),
4897 + N_("generate diffs with <n> lines context"),
4898 + PARSE_OPT_NONEG, diff_opt_unified),
4899 OPT_END()
4900 };
4901
@@ -4905,9 +4924,7 @@ int diff_opt_parse(struct diff_options *options,
4924 return ac;
4925
4926 /* Output format options */
4908 - if (opt_arg(arg, 'U', "unified", &options->context))
4909 - enable_patch_output(&options->output_format);
4910 - else if (!strcmp(arg, "--raw"))
4927 + if (!strcmp(arg, "--raw"))
4928 options->output_format |= DIFF_FORMAT_RAW;
4929 else if (!strcmp(arg, "--patch-with-raw")) {
4930 enable_patch_output(&options->output_format);
parse-options.h
+3 -2
@@ -134,6 +134,8 @@ struct option {
134 #define OPT_SET_INT_F(s, l, v, h, i, f) { OPTION_SET_INT, (s), (l), (v), NULL, \
135 (h), PARSE_OPT_NOARG | (f), NULL, (i) }
136 #define OPT_BOOL_F(s, l, v, h, f) OPT_SET_INT_F(s, l, v, h, 1, f)
137 +#define OPT_CALLBACK_F(s, l, v, a, h, f, cb) \
138 + { OPTION_CALLBACK, (s), (l), (v), (a), (h), (f), (cb) }
139
140 #define OPT_END() { OPTION_END }
141 #define OPT_ARGUMENT(l, h) { OPTION_ARGUMENT, 0, (l), NULL, NULL, \
@@ -164,8 +166,7 @@ struct option {
166 #define OPT_EXPIRY_DATE(s, l, v, h) \
167 { OPTION_CALLBACK, (s), (l), (v), N_("expiry-date"),(h), 0, \
168 parse_opt_expiry_date_cb }
167 -#define OPT_CALLBACK(s, l, v, a, h, f) \
168 - { OPTION_CALLBACK, (s), (l), (v), (a), (h), 0, (f) }
169 +#define OPT_CALLBACK(s, l, v, a, h, f) OPT_CALLBACK_F(s, l, v, a, h, 0, f)
170 #define OPT_NUMBER_CALLBACK(v, h, f) \
171 { OPTION_NUMBER, 0, NULL, (v), NULL, (h), \
172 PARSE_OPT_NOARG | PARSE_OPT_NONEG, (f) }