parse-options: stop abusing 'callback' for lowlevel callbacks

Lowlevel callbacks have different function signatures. Add a new field in 'struct option' with the right type for lowlevel callbacks. 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 bf3ff338a25b7353ec6d39d31e14d081be9e3471
6 files changed +32 -13
builtin/blame.c
+1 -1
@@ -814,7 +814,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
814 * and are only included here to get included in the "-h"
815 * output:
816 */
817 - { OPTION_LOWLEVEL_CALLBACK, 0, "indent-heuristic", NULL, NULL, N_("Use an experimental heuristic to improve diffs"), PARSE_OPT_NOARG, parse_opt_unknown_cb },
817 + { OPTION_LOWLEVEL_CALLBACK, 0, "indent-heuristic", NULL, NULL, N_("Use an experimental heuristic to improve diffs"), PARSE_OPT_NOARG, NULL, 0, parse_opt_unknown_cb },
818
819 OPT_BIT(0, "minimal", &xdl_opts, N_("Spend extra cycles to find better match"), XDF_NEED_MINIMAL),
820 OPT_STRING('S', NULL, &revs_file, N_("file"), N_("Use revisions from <file> instead of calling git-rev-list")),
builtin/merge.c
+1 -1
@@ -261,7 +261,7 @@ static struct option builtin_merge_options[] = {
261 option_parse_message),
262 { OPTION_LOWLEVEL_CALLBACK, 'F', "file", &merge_msg, N_("path"),
263 N_("read message from file"), PARSE_OPT_NONEG,
264 - (parse_opt_cb *) option_read_message },
264 + NULL, 0, option_read_message },
265 OPT__VERBOSITY(&verbosity),
266 OPT_BOOL(0, "abort", &abort_current_merge,
267 N_("abort the current in-progress merge")),
builtin/update-index.c
+6 -5
@@ -985,7 +985,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
985 N_("add the specified entry to the index"),
986 PARSE_OPT_NOARG | /* disallow --cacheinfo=<mode> form */
987 PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
988 - (parse_opt_cb *) cacheinfo_callback},
988 + NULL, 0,
989 + cacheinfo_callback},
990 {OPTION_CALLBACK, 0, "chmod", &set_executable_bit, "(+|-)x",
991 N_("override the executable bit of the listed files"),
992 PARSE_OPT_NONEG,
@@ -1011,19 +1012,19 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
1012 {OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &read_from_stdin, NULL,
1013 N_("read list of paths to be updated from standard input"),
1014 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
1014 - (parse_opt_cb *) stdin_callback},
1015 + NULL, 0, stdin_callback},
1016 {OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &nul_term_line, NULL,
1017 N_("add entries from standard input to the index"),
1018 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
1018 - (parse_opt_cb *) stdin_cacheinfo_callback},
1019 + NULL, 0, stdin_cacheinfo_callback},
1020 {OPTION_LOWLEVEL_CALLBACK, 0, "unresolve", &has_errors, NULL,
1021 N_("repopulate stages #2 and #3 for the listed paths"),
1022 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
1022 - (parse_opt_cb *) unresolve_callback},
1023 + NULL, 0, unresolve_callback},
1024 {OPTION_LOWLEVEL_CALLBACK, 'g', "again", &has_errors, NULL,
1025 N_("only update entries that differ from HEAD"),
1026 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
1026 - (parse_opt_cb *) reupdate_callback},
1027 + NULL, 0, reupdate_callback},
1028 OPT_BIT(0, "ignore-missing", &refresh_args.flags,
1029 N_("ignore files missing from worktree"),
1030 REFRESH_IGNORE_MISSING),
parse-options-cb.c
+2 -1
@@ -170,7 +170,8 @@ int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
170 * "-h" output even if it's not being handled directly by
171 * parse_options().
172 */
173 -int parse_opt_unknown_cb(const struct option *opt, const char *arg, int unset)
173 +int parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx,
174 + const struct option *opt, int unset)
175 {
176 return -2;
177 }
parse-options.c
+14 -1
@@ -93,7 +93,7 @@ static int get_value(struct parse_opt_ctx_t *p,
93
94 switch (opt->type) {
95 case OPTION_LOWLEVEL_CALLBACK:
96 - return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
96 + return opt->ll_callback(p, opt, unset);
97
98 case OPTION_BIT:
99 if (unset)
@@ -408,6 +408,19 @@ static void parse_options_check(const struct option *opts)
408 if ((opts->flags & PARSE_OPT_OPTARG) ||
409 !(opts->flags & PARSE_OPT_NOARG))
410 err |= optbug(opts, "should not accept an argument");
411 + break;
412 + case OPTION_CALLBACK:
413 + if (!opts->callback)
414 + BUG("OPTION_CALLBACK needs a callback");
415 + if (opts->ll_callback)
416 + BUG("OPTION_CALLBACK needs no ll_callback");
417 + break;
418 + case OPTION_LOWLEVEL_CALLBACK:
419 + if (!opts->ll_callback)
420 + BUG("OPTION_LOWLEVEL_CALLBACK needs a callback");
421 + if (opts->callback)
422 + BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback");
423 + break;
424 default:
425 ; /* ok. (usually accepts an argument) */
426 }
parse-options.h
+8 -4
@@ -100,13 +100,16 @@ typedef int parse_opt_ll_cb(struct parse_opt_ctx_t *ctx,
100 * the option takes optional argument.
101 *
102 * `callback`::
103 - * pointer to the callback to use for OPTION_CALLBACK or
104 - * OPTION_LOWLEVEL_CALLBACK.
103 + * pointer to the callback to use for OPTION_CALLBACK
104 *
105 * `defval`::
106 * default value to fill (*->value) with for PARSE_OPT_OPTARG.
107 * OPTION_{BIT,SET_INT} store the {mask,integer} to put in the value when met.
108 * CALLBACKS can use it like they want.
109 + *
110 + * `ll_callback`::
111 + * pointer to the callback to use for OPTION_LOWLEVEL_CALLBACK
112 + *
113 */
114 struct option {
115 enum parse_opt_type type;
@@ -119,6 +122,7 @@ struct option {
122 int flags;
123 parse_opt_cb *callback;
124 intptr_t defval;
125 + parse_opt_ll_cb *ll_callback;
126 intptr_t extra;
127 };
128
@@ -137,7 +141,7 @@ struct option {
141 #define OPT_BIT(s, l, v, h, b) OPT_BIT_F(s, l, v, h, b, 0)
142 #define OPT_BITOP(s, l, v, h, set, clear) { OPTION_BITOP, (s), (l), (v), NULL, (h), \
143 PARSE_OPT_NOARG|PARSE_OPT_NONEG, NULL, \
140 - (set), (clear) }
144 + (set), NULL, (clear) }
145 #define OPT_NEGBIT(s, l, v, h, b) { OPTION_NEGBIT, (s), (l), (v), NULL, \
146 (h), PARSE_OPT_NOARG, NULL, (b) }
147 #define OPT_COUNTUP(s, l, v, h) OPT_COUNTUP_F(s, l, v, h, 0)
@@ -263,7 +267,7 @@ int parse_opt_commits(const struct option *, const char *, int);
267 int parse_opt_tertiary(const struct option *, const char *, int);
268 int parse_opt_string_list(const struct option *, const char *, int);
269 int parse_opt_noop_cb(const struct option *, const char *, int);
266 -int parse_opt_unknown_cb(const struct option *, const char *, int);
270 +int parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx, const struct option *, int);
271 int parse_opt_passthru(const struct option *, const char *, int);
272 int parse_opt_passthru_argv(const struct option *, const char *, int);
273