parse-options: stop abusing 'callback' for lowlevel callbacks
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Sun, 27 Jan 2019 00:35:26 +0000 (07:35 +0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 28 Jan 2019 00:28:18 +0000 (16:28 -0800)
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>
builtin/blame.c
builtin/merge.c
builtin/update-index.c
parse-options-cb.c
parse-options.c
parse-options.h
index 6d798f99392e54b4392713846652111d457787bb..8dcc55dffa9f2dd4d66ea973d3f67115fe9d7b56 100644 (file)
@@ -814,7 +814,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
                 * and are only included here to get included in the "-h"
                 * output:
                 */
-               { OPTION_LOWLEVEL_CALLBACK, 0, "indent-heuristic", NULL, NULL, N_("Use an experimental heuristic to improve diffs"), PARSE_OPT_NOARG, parse_opt_unknown_cb },
+               { 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 },
 
                OPT_BIT(0, "minimal", &xdl_opts, N_("Spend extra cycles to find better match"), XDF_NEED_MINIMAL),
                OPT_STRING('S', NULL, &revs_file, N_("file"), N_("Use revisions from <file> instead of calling git-rev-list")),
index dc0b7cc521c26f4c0ef1c6440d17c0f35ac1c983..07839b0bb8f922e5bab1757c8739b2c20082a874 100644 (file)
@@ -261,7 +261,7 @@ static struct option builtin_merge_options[] = {
                option_parse_message),
        { OPTION_LOWLEVEL_CALLBACK, 'F', "file", &merge_msg, N_("path"),
                N_("read message from file"), PARSE_OPT_NONEG,
-               (parse_opt_cb *) option_read_message },
+               NULL, 0, option_read_message },
        OPT__VERBOSITY(&verbosity),
        OPT_BOOL(0, "abort", &abort_current_merge,
                N_("abort the current in-progress merge")),
index e19da77edcaa024cc2bffc6fa8818c2b1cf0d4b6..727a8118b8895381e12f7bcc61d7feaf611aad04 100644 (file)
@@ -985,7 +985,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
                        N_("add the specified entry to the index"),
                        PARSE_OPT_NOARG | /* disallow --cacheinfo=<mode> form */
                        PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
-                       (parse_opt_cb *) cacheinfo_callback},
+                       NULL, 0,
+                       cacheinfo_callback},
                {OPTION_CALLBACK, 0, "chmod", &set_executable_bit, "(+|-)x",
                        N_("override the executable bit of the listed files"),
                        PARSE_OPT_NONEG,
@@ -1011,19 +1012,19 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
                {OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &read_from_stdin, NULL,
                        N_("read list of paths to be updated from standard input"),
                        PARSE_OPT_NONEG | PARSE_OPT_NOARG,
-                       (parse_opt_cb *) stdin_callback},
+                       NULL, 0, stdin_callback},
                {OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &nul_term_line, NULL,
                        N_("add entries from standard input to the index"),
                        PARSE_OPT_NONEG | PARSE_OPT_NOARG,
-                       (parse_opt_cb *) stdin_cacheinfo_callback},
+                       NULL, 0, stdin_cacheinfo_callback},
                {OPTION_LOWLEVEL_CALLBACK, 0, "unresolve", &has_errors, NULL,
                        N_("repopulate stages #2 and #3 for the listed paths"),
                        PARSE_OPT_NONEG | PARSE_OPT_NOARG,
-                       (parse_opt_cb *) unresolve_callback},
+                       NULL, 0, unresolve_callback},
                {OPTION_LOWLEVEL_CALLBACK, 'g', "again", &has_errors, NULL,
                        N_("only update entries that differ from HEAD"),
                        PARSE_OPT_NONEG | PARSE_OPT_NOARG,
-                       (parse_opt_cb *) reupdate_callback},
+                       NULL, 0, reupdate_callback},
                OPT_BIT(0, "ignore-missing", &refresh_args.flags,
                        N_("ignore files missing from worktree"),
                        REFRESH_IGNORE_MISSING),
index e2f3eaed072f77d63890ec814d810199f57248d5..e05bcea8096b3e3c507126d19e4a535d316fc179 100644 (file)
@@ -170,7 +170,8 @@ int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
  * "-h" output even if it's not being handled directly by
  * parse_options().
  */
-int parse_opt_unknown_cb(const struct option *opt, const char *arg, int unset)
+int parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx,
+                        const struct option *opt, int unset)
 {
        return -2;
 }
index 62d94ca2e0323c5d543f9290610fdc52c243ca6a..37a56d079a36f0dfbafadd3f8c35d7c86371c7bd 100644 (file)
@@ -93,7 +93,7 @@ static int get_value(struct parse_opt_ctx_t *p,
 
        switch (opt->type) {
        case OPTION_LOWLEVEL_CALLBACK:
-               return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
+               return opt->ll_callback(p, opt, unset);
 
        case OPTION_BIT:
                if (unset)
@@ -408,6 +408,19 @@ static void parse_options_check(const struct option *opts)
                        if ((opts->flags & PARSE_OPT_OPTARG) ||
                            !(opts->flags & PARSE_OPT_NOARG))
                                err |= optbug(opts, "should not accept an argument");
+                       break;
+               case OPTION_CALLBACK:
+                       if (!opts->callback)
+                               BUG("OPTION_CALLBACK needs a callback");
+                       if (opts->ll_callback)
+                               BUG("OPTION_CALLBACK needs no ll_callback");
+                       break;
+               case OPTION_LOWLEVEL_CALLBACK:
+                       if (!opts->ll_callback)
+                               BUG("OPTION_LOWLEVEL_CALLBACK needs a callback");
+                       if (opts->callback)
+                               BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback");
+                       break;
                default:
                        ; /* ok. (usually accepts an argument) */
                }
index c97324f576a01e5ffe79c1f6aaa9728d28a1c5da..f1f246387cae57748a04efa139da7a1d4b8951be 100644 (file)
@@ -100,13 +100,16 @@ typedef int parse_opt_ll_cb(struct parse_opt_ctx_t *ctx,
  *                      the option takes optional argument.
  *
  * `callback`::
- *   pointer to the callback to use for OPTION_CALLBACK or
- *   OPTION_LOWLEVEL_CALLBACK.
+ *   pointer to the callback to use for OPTION_CALLBACK
  *
  * `defval`::
  *   default value to fill (*->value) with for PARSE_OPT_OPTARG.
  *   OPTION_{BIT,SET_INT} store the {mask,integer} to put in the value when met.
  *   CALLBACKS can use it like they want.
+ *
+ * `ll_callback`::
+ *   pointer to the callback to use for OPTION_LOWLEVEL_CALLBACK
+ *
  */
 struct option {
        enum parse_opt_type type;
@@ -119,6 +122,7 @@ struct option {
        int flags;
        parse_opt_cb *callback;
        intptr_t defval;
+       parse_opt_ll_cb *ll_callback;
        intptr_t extra;
 };
 
@@ -137,7 +141,7 @@ struct option {
 #define OPT_BIT(s, l, v, h, b)      OPT_BIT_F(s, l, v, h, b, 0)
 #define OPT_BITOP(s, l, v, h, set, clear) { OPTION_BITOP, (s), (l), (v), NULL, (h), \
                                            PARSE_OPT_NOARG|PARSE_OPT_NONEG, NULL, \
-                                           (set), (clear) }
+                                           (set), NULL, (clear) }
 #define OPT_NEGBIT(s, l, v, h, b)   { OPTION_NEGBIT, (s), (l), (v), NULL, \
                                      (h), PARSE_OPT_NOARG, NULL, (b) }
 #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);
 int parse_opt_tertiary(const struct option *, const char *, int);
 int parse_opt_string_list(const struct option *, const char *, int);
 int parse_opt_noop_cb(const struct option *, const char *, int);
-int parse_opt_unknown_cb(const struct option *, const char *, int);
+int parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx, const struct option *, int);
 int parse_opt_passthru(const struct option *, const char *, int);
 int parse_opt_passthru_argv(const struct option *, const char *, int);