6a61166b265caae09564586017460797ee928a43
   1#include "git-compat-util.h"
   2#include "parse-options.h"
   3#include "cache.h"
   4#include "commit.h"
   5#include "color.h"
   6#include "string-list.h"
   7#include "argv-array.h"
   8#include "sha1-array.h"
   9
  10/*----- some often used options -----*/
  11
  12int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
  13{
  14        int v;
  15
  16        if (!arg) {
  17                v = unset ? 0 : DEFAULT_ABBREV;
  18        } else {
  19                v = strtol(arg, (char **)&arg, 10);
  20                if (*arg)
  21                        return opterror(opt, "expects a numerical value", 0);
  22                if (v && v < MINIMUM_ABBREV)
  23                        v = MINIMUM_ABBREV;
  24                else if (v > 40)
  25                        v = 40;
  26        }
  27        *(int *)(opt->value) = v;
  28        return 0;
  29}
  30
  31int parse_opt_expiry_date_cb(const struct option *opt, const char *arg,
  32                             int unset)
  33{
  34        if (unset)
  35                arg = "never";
  36        if (parse_expiry_date(arg, (timestamp_t *)opt->value))
  37                die(_("malformed expiration date '%s'"), arg);
  38        return 0;
  39}
  40
  41int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
  42                            int unset)
  43{
  44        int value;
  45
  46        if (!arg)
  47                arg = unset ? "never" : (const char *)opt->defval;
  48        value = git_config_colorbool(NULL, arg);
  49        if (value < 0)
  50                return opterror(opt,
  51                        "expects \"always\", \"auto\", or \"never\"", 0);
  52        *(int *)opt->value = value;
  53        return 0;
  54}
  55
  56int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
  57                           int unset)
  58{
  59        int *target = opt->value;
  60
  61        if (unset)
  62                /* --no-quiet, --no-verbose */
  63                *target = 0;
  64        else if (opt->short_name == 'v') {
  65                if (*target >= 0)
  66                        (*target)++;
  67                else
  68                        *target = 1;
  69        } else {
  70                if (*target <= 0)
  71                        (*target)--;
  72                else
  73                        *target = -1;
  74        }
  75        return 0;
  76}
  77
  78int parse_opt_commits(const struct option *opt, const char *arg, int unset)
  79{
  80        struct object_id oid;
  81        struct commit *commit;
  82
  83        if (!arg)
  84                return -1;
  85        if (get_oid(arg, &oid))
  86                return error("malformed object name %s", arg);
  87        commit = lookup_commit_reference(the_repository, &oid);
  88        if (!commit)
  89                return error("no such commit %s", arg);
  90        commit_list_insert(commit, opt->value);
  91        return 0;
  92}
  93
  94int parse_opt_object_name(const struct option *opt, const char *arg, int unset)
  95{
  96        struct object_id oid;
  97
  98        if (unset) {
  99                oid_array_clear(opt->value);
 100                return 0;
 101        }
 102        if (!arg)
 103                return -1;
 104        if (get_oid(arg, &oid))
 105                return error(_("malformed object name '%s'"), arg);
 106        oid_array_append(opt->value, &oid);
 107        return 0;
 108}
 109
 110int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
 111{
 112        int *target = opt->value;
 113        *target = unset ? 2 : 1;
 114        return 0;
 115}
 116
 117struct option *parse_options_concat(struct option *a, struct option *b)
 118{
 119        struct option *ret;
 120        size_t i, a_len = 0, b_len = 0;
 121
 122        for (i = 0; a[i].type != OPTION_END; i++)
 123                a_len++;
 124        for (i = 0; b[i].type != OPTION_END; i++)
 125                b_len++;
 126
 127        ALLOC_ARRAY(ret, st_add3(a_len, b_len, 1));
 128        for (i = 0; i < a_len; i++)
 129                ret[i] = a[i];
 130        for (i = 0; i < b_len; i++)
 131                ret[a_len + i] = b[i];
 132        ret[a_len + b_len] = b[b_len]; /* final OPTION_END */
 133
 134        return ret;
 135}
 136
 137int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
 138{
 139        struct string_list *v = opt->value;
 140
 141        if (unset) {
 142                string_list_clear(v, 0);
 143                return 0;
 144        }
 145
 146        if (!arg)
 147                return -1;
 148
 149        string_list_append(v, arg);
 150        return 0;
 151}
 152
 153int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
 154{
 155        return 0;
 156}
 157
 158/**
 159 * Report that the option is unknown, so that other code can handle
 160 * it. This can be used as a callback together with
 161 * OPTION_LOWLEVEL_CALLBACK to allow an option to be documented in the
 162 * "-h" output even if it's not being handled directly by
 163 * parse_options().
 164 */
 165int parse_opt_unknown_cb(const struct option *opt, const char *arg, int unset)
 166{
 167        return -2;
 168}
 169
 170/**
 171 * Recreates the command-line option in the strbuf.
 172 */
 173static int recreate_opt(struct strbuf *sb, const struct option *opt,
 174                const char *arg, int unset)
 175{
 176        strbuf_reset(sb);
 177
 178        if (opt->long_name) {
 179                strbuf_addstr(sb, unset ? "--no-" : "--");
 180                strbuf_addstr(sb, opt->long_name);
 181                if (arg) {
 182                        strbuf_addch(sb, '=');
 183                        strbuf_addstr(sb, arg);
 184                }
 185        } else if (opt->short_name && !unset) {
 186                strbuf_addch(sb, '-');
 187                strbuf_addch(sb, opt->short_name);
 188                if (arg)
 189                        strbuf_addstr(sb, arg);
 190        } else
 191                return -1;
 192
 193        return 0;
 194}
 195
 196/**
 197 * For an option opt, recreates the command-line option in opt->value which
 198 * must be an char* initialized to NULL. This is useful when we need to pass
 199 * the command-line option to another command. Since any previous value will be
 200 * overwritten, this callback should only be used for options where the last
 201 * one wins.
 202 */
 203int parse_opt_passthru(const struct option *opt, const char *arg, int unset)
 204{
 205        static struct strbuf sb = STRBUF_INIT;
 206        char **opt_value = opt->value;
 207
 208        if (recreate_opt(&sb, opt, arg, unset) < 0)
 209                return -1;
 210
 211        free(*opt_value);
 212
 213        *opt_value = strbuf_detach(&sb, NULL);
 214
 215        return 0;
 216}
 217
 218/**
 219 * For an option opt, recreate the command-line option, appending it to
 220 * opt->value which must be a argv_array. This is useful when we need to pass
 221 * the command-line option, which can be specified multiple times, to another
 222 * command.
 223 */
 224int parse_opt_passthru_argv(const struct option *opt, const char *arg, int unset)
 225{
 226        static struct strbuf sb = STRBUF_INIT;
 227        struct argv_array *opt_value = opt->value;
 228
 229        if (recreate_opt(&sb, opt, arg, unset) < 0)
 230                return -1;
 231
 232        argv_array_push(opt_value, sb.buf);
 233
 234        return 0;
 235}