68bc5933412dfe72c3fbdbd7b848bbba62dbbbdd
   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
   8/*----- some often used options -----*/
   9
  10int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
  11{
  12        int v;
  13
  14        if (!arg) {
  15                v = unset ? 0 : DEFAULT_ABBREV;
  16        } else {
  17                v = strtol(arg, (char **)&arg, 10);
  18                if (*arg)
  19                        return opterror(opt, "expects a numerical value", 0);
  20                if (v && v < MINIMUM_ABBREV)
  21                        v = MINIMUM_ABBREV;
  22                else if (v > 40)
  23                        v = 40;
  24        }
  25        *(int *)(opt->value) = v;
  26        return 0;
  27}
  28
  29int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
  30                             int unset)
  31{
  32        *(unsigned long *)(opt->value) = approxidate(arg);
  33        return 0;
  34}
  35
  36int parse_opt_expiry_date_cb(const struct option *opt, const char *arg,
  37                             int unset)
  38{
  39        return parse_expiry_date(arg, (unsigned long *)opt->value);
  40}
  41
  42int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
  43                            int unset)
  44{
  45        int value;
  46
  47        if (!arg)
  48                arg = unset ? "never" : (const char *)opt->defval;
  49        value = git_config_colorbool(NULL, arg);
  50        if (value < 0)
  51                return opterror(opt,
  52                        "expects \"always\", \"auto\", or \"never\"", 0);
  53        *(int *)opt->value = value;
  54        return 0;
  55}
  56
  57int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
  58                           int unset)
  59{
  60        int *target = opt->value;
  61
  62        if (unset)
  63                /* --no-quiet, --no-verbose */
  64                *target = 0;
  65        else if (opt->short_name == 'v') {
  66                if (*target >= 0)
  67                        (*target)++;
  68                else
  69                        *target = 1;
  70        } else {
  71                if (*target <= 0)
  72                        (*target)--;
  73                else
  74                        *target = -1;
  75        }
  76        return 0;
  77}
  78
  79int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
  80{
  81        unsigned char sha1[20];
  82        struct commit *commit;
  83
  84        if (!arg)
  85                return -1;
  86        if (get_sha1(arg, sha1))
  87                return error("malformed object name %s", arg);
  88        commit = lookup_commit_reference(sha1);
  89        if (!commit)
  90                return error("no such commit %s", arg);
  91        commit_list_insert(commit, opt->value);
  92        return 0;
  93}
  94
  95int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
  96{
  97        int *target = opt->value;
  98        *target = unset ? 2 : 1;
  99        return 0;
 100}
 101
 102int parse_options_concat(struct option *dst, size_t dst_size, struct option *src)
 103{
 104        int i, j;
 105
 106        for (i = 0; i < dst_size; i++)
 107                if (dst[i].type == OPTION_END)
 108                        break;
 109        for (j = 0; i < dst_size; i++, j++) {
 110                dst[i] = src[j];
 111                if (src[j].type == OPTION_END)
 112                        return 0;
 113        }
 114        return -1;
 115}
 116
 117int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
 118{
 119        struct string_list *v = opt->value;
 120
 121        if (unset) {
 122                string_list_clear(v, 0);
 123                return 0;
 124        }
 125
 126        if (!arg)
 127                return -1;
 128
 129        string_list_append(v, xstrdup(arg));
 130        return 0;
 131}
 132
 133int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
 134{
 135        return 0;
 136}
 137
 138/**
 139 * Recreates the command-line option in the strbuf.
 140 */
 141static int recreate_opt(struct strbuf *sb, const struct option *opt,
 142                const char *arg, int unset)
 143{
 144        strbuf_reset(sb);
 145
 146        if (opt->long_name) {
 147                strbuf_addstr(sb, unset ? "--no-" : "--");
 148                strbuf_addstr(sb, opt->long_name);
 149                if (arg) {
 150                        strbuf_addch(sb, '=');
 151                        strbuf_addstr(sb, arg);
 152                }
 153        } else if (opt->short_name && !unset) {
 154                strbuf_addch(sb, '-');
 155                strbuf_addch(sb, opt->short_name);
 156                if (arg)
 157                        strbuf_addstr(sb, arg);
 158        } else
 159                return -1;
 160
 161        return 0;
 162}
 163
 164/**
 165 * For an option opt, recreates the command-line option in opt->value which
 166 * must be an char* initialized to NULL. This is useful when we need to pass
 167 * the command-line option to another command. Since any previous value will be
 168 * overwritten, this callback should only be used for options where the last
 169 * one wins.
 170 */
 171int parse_opt_passthru(const struct option *opt, const char *arg, int unset)
 172{
 173        static struct strbuf sb = STRBUF_INIT;
 174        char **opt_value = opt->value;
 175
 176        if (recreate_opt(&sb, opt, arg, unset) < 0)
 177                return -1;
 178
 179        if (*opt_value)
 180                free(*opt_value);
 181
 182        *opt_value = strbuf_detach(&sb, NULL);
 183
 184        return 0;
 185}