Imported Upstream version 2.24.2
[platform/upstream/git.git] / parse-options-cb.c
index 1681883..1240a85 100644 (file)
@@ -16,29 +16,30 @@ int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
        if (!arg) {
                v = unset ? 0 : DEFAULT_ABBREV;
        } else {
+               if (!*arg)
+                       return error(_("option `%s' expects a numerical value"),
+                                    opt->long_name);
                v = strtol(arg, (char **)&arg, 10);
                if (*arg)
-                       return opterror(opt, "expects a numerical value", 0);
+                       return error(_("option `%s' expects a numerical value"),
+                                    opt->long_name);
                if (v && v < MINIMUM_ABBREV)
                        v = MINIMUM_ABBREV;
-               else if (v > 40)
-                       v = 40;
+               else if (v > the_hash_algo->hexsz)
+                       v = the_hash_algo->hexsz;
        }
        *(int *)(opt->value) = v;
        return 0;
 }
 
-int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
-                            int unset)
-{
-       *(unsigned long *)(opt->value) = approxidate(arg);
-       return 0;
-}
-
 int parse_opt_expiry_date_cb(const struct option *opt, const char *arg,
                             int unset)
 {
-       return parse_expiry_date(arg, (unsigned long *)opt->value);
+       if (unset)
+               arg = "never";
+       if (parse_expiry_date(arg, (timestamp_t *)opt->value))
+               die(_("malformed expiration date '%s'"), arg);
+       return 0;
 }
 
 int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
@@ -50,8 +51,8 @@ int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
                arg = unset ? "never" : (const char *)opt->defval;
        value = git_config_colorbool(NULL, arg);
        if (value < 0)
-               return opterror(opt,
-                       "expects \"always\", \"auto\", or \"never\"", 0);
+               return error(_("option `%s' expects \"always\", \"auto\", or \"never\""),
+                            opt->long_name);
        *(int *)opt->value = value;
        return 0;
 }
@@ -61,6 +62,8 @@ int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
 {
        int *target = opt->value;
 
+       BUG_ON_OPT_ARG(arg);
+
        if (unset)
                /* --no-quiet, --no-verbose */
                *target = 0;
@@ -80,43 +83,99 @@ int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
 
 int parse_opt_commits(const struct option *opt, const char *arg, int unset)
 {
-       unsigned char sha1[20];
+       struct object_id oid;
        struct commit *commit;
 
+       BUG_ON_OPT_NEG(unset);
+
        if (!arg)
                return -1;
-       if (get_sha1(arg, sha1))
+       if (get_oid(arg, &oid))
                return error("malformed object name %s", arg);
-       commit = lookup_commit_reference(sha1);
+       commit = lookup_commit_reference(the_repository, &oid);
        if (!commit)
                return error("no such commit %s", arg);
        commit_list_insert(commit, opt->value);
        return 0;
 }
 
+int parse_opt_commit(const struct option *opt, const char *arg, int unset)
+{
+       struct object_id oid;
+       struct commit *commit;
+       struct commit **target = opt->value;
+
+       if (!arg)
+               return -1;
+       if (get_oid(arg, &oid))
+               return error("malformed object name %s", arg);
+       commit = lookup_commit_reference(the_repository, &oid);
+       if (!commit)
+               return error("no such commit %s", arg);
+       *target = commit;
+       return 0;
+}
+
 int parse_opt_object_name(const struct option *opt, const char *arg, int unset)
 {
-       unsigned char sha1[20];
+       struct object_id oid;
+
+       if (unset) {
+               oid_array_clear(opt->value);
+               return 0;
+       }
+       if (!arg)
+               return -1;
+       if (get_oid(arg, &oid))
+               return error(_("malformed object name '%s'"), arg);
+       oid_array_append(opt->value, &oid);
+       return 0;
+}
+
+int parse_opt_object_id(const struct option *opt, const char *arg, int unset)
+{
+       struct object_id oid;
+       struct object_id *target = opt->value;
 
        if (unset) {
-               sha1_array_clear(opt->value);
+               *target = null_oid;
                return 0;
        }
        if (!arg)
                return -1;
-       if (get_sha1(arg, sha1))
+       if (get_oid(arg, &oid))
                return error(_("malformed object name '%s'"), arg);
-       sha1_array_append(opt->value, sha1);
+       *target = oid;
        return 0;
 }
 
 int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
 {
        int *target = opt->value;
+
+       BUG_ON_OPT_ARG(arg);
+
        *target = unset ? 2 : 1;
        return 0;
 }
 
+struct option *parse_options_dup(const struct option *o)
+{
+       struct option *opts;
+       int nr = 0;
+
+       while (o && o->type != OPTION_END) {
+               nr++;
+               o++;
+       }
+
+       ALLOC_ARRAY(opts, nr + 1);
+       memcpy(opts, o - nr, sizeof(*o) * nr);
+       memset(opts + nr, 0, sizeof(*opts));
+       opts[nr].type = OPTION_END;
+       return opts;
+}
+
 struct option *parse_options_concat(struct option *a, struct option *b)
 {
        struct option *ret;
@@ -159,6 +218,21 @@ int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
 }
 
 /**
+ * Report that the option is unknown, so that other code can handle
+ * it. This can be used as a callback together with
+ * OPTION_LOWLEVEL_CALLBACK to allow an option to be documented in the
+ * "-h" output even if it's not being handled directly by
+ * parse_options().
+ */
+enum parse_opt_result parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx,
+                                          const struct option *opt,
+                                          const char *arg, int unset)
+{
+       BUG_ON_OPT_ARG(arg);
+       return PARSE_OPT_UNKNOWN;
+}
+
+/**
  * Recreates the command-line option in the strbuf.
  */
 static int recreate_opt(struct strbuf *sb, const struct option *opt,