]> git.ipfire.org Git - thirdparty/git.git/blobdiff - parse-options-cb.c
worktree: give "should be pruned?" function more meaningful name
[thirdparty/git.git] / parse-options-cb.c
index e8236534ac8aa00fde2a5a9ae27e85c72d062f3a..a28b55be48dc84582f18df446b1eb36f26b17167 100644 (file)
@@ -16,25 +16,22 @@ 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)
-{
-       *(timestamp_t *)(opt->value) = approxidate(arg);
-       return 0;
-}
-
 int parse_opt_expiry_date_cb(const struct option *opt, const char *arg,
                             int unset)
 {
@@ -54,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;
 }
@@ -65,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;
@@ -87,6 +86,8 @@ int parse_opt_commits(const struct option *opt, const char *arg, int unset)
        struct object_id oid;
        struct commit *commit;
 
+       BUG_ON_OPT_NEG(unset);
+
        if (!arg)
                return -1;
        if (get_oid(arg, &oid))
@@ -98,6 +99,23 @@ int parse_opt_commits(const struct option *opt, const char *arg, int unset)
        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)
 {
        struct object_id oid;
@@ -114,29 +132,59 @@ int parse_opt_object_name(const struct option *opt, const char *arg, int unset)
        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) {
+               *target = null_oid;
+               return 0;
+       }
+       if (!arg)
+               return -1;
+       if (get_oid(arg, &oid))
+               return error(_("malformed object name '%s'"), arg);
+       *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_concat(struct option *a, struct option *b)
+static size_t parse_options_count(const struct option *opt)
 {
-       struct option *ret;
-       size_t i, a_len = 0, b_len = 0;
+       size_t n = 0;
 
-       for (i = 0; a[i].type != OPTION_END; i++)
-               a_len++;
-       for (i = 0; b[i].type != OPTION_END; i++)
-               b_len++;
+       for (; opt && opt->type != OPTION_END; opt++)
+               n++;
+       return n;
+}
+
+struct option *parse_options_dup(const struct option *o)
+{
+       struct option no_options[] = { OPT_END() };
+
+       return parse_options_concat(o, no_options);
+}
+
+struct option *parse_options_concat(const struct option *a,
+                                   const struct option *b)
+{
+       struct option *ret;
+       size_t a_len = parse_options_count(a);
+       size_t b_len = parse_options_count(b);
 
        ALLOC_ARRAY(ret, st_add3(a_len, b_len, 1));
-       for (i = 0; i < a_len; i++)
-               ret[i] = a[i];
-       for (i = 0; i < b_len; i++)
-               ret[a_len + i] = b[i];
-       ret[a_len + b_len] = b[b_len]; /* final OPTION_END */
+       COPY_ARRAY(ret, a, a_len);
+       COPY_ARRAY(ret + a_len, b, b_len + 1); /* + 1 for final OPTION_END */
 
        return ret;
 }
@@ -169,9 +217,12 @@ 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)
+enum parse_opt_result parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx,
+                                          const struct option *opt,
+                                          const char *arg, int unset)
 {
-       return -2;
+       BUG_ON_OPT_ARG(arg);
+       return PARSE_OPT_UNKNOWN;
 }
 
 /**