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