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 "sha1-array.h"
89
/*----- some often used options -----*/
1011
int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
12{
13int v;
1415
if (!arg) {
16v = unset ? 0 : DEFAULT_ABBREV;
17} else {
18v = strtol(arg, (char **)&arg, 10);
19if (*arg)
20return opterror(opt, "expects a numerical value", 0);
21if (v && v < MINIMUM_ABBREV)
22v = MINIMUM_ABBREV;
23else if (v > 40)
24v = 40;
25}
26*(int *)(opt->value) = v;
27return 0;
28}
2930
int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
31int unset)
32{
33*(unsigned long *)(opt->value) = approxidate(arg);
34return 0;
35}
3637
int parse_opt_expiry_date_cb(const struct option *opt, const char *arg,
38int unset)
39{
40return parse_expiry_date(arg, (unsigned long *)opt->value);
41}
4243
int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
44int unset)
45{
46int value;
4748
if (!arg)
49arg = unset ? "never" : (const char *)opt->defval;
50value = git_config_colorbool(NULL, arg);
51if (value < 0)
52return opterror(opt,
53"expects \"always\", \"auto\", or \"never\"", 0);
54*(int *)opt->value = value;
55return 0;
56}
5758
int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
59int unset)
60{
61int *target = opt->value;
6263
if (unset)
64/* --no-quiet, --no-verbose */
65*target = 0;
66else if (opt->short_name == 'v') {
67if (*target >= 0)
68(*target)++;
69else
70*target = 1;
71} else {
72if (*target <= 0)
73(*target)--;
74else
75*target = -1;
76}
77return 0;
78}
7980
int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
81{
82unsigned char sha1[20];
83struct commit *commit;
8485
if (!arg)
86return -1;
87if (get_sha1(arg, sha1))
88return error("malformed object name %s", arg);
89commit = lookup_commit_reference(sha1);
90if (!commit)
91return error("no such commit %s", arg);
92commit_list_insert(commit, opt->value);
93return 0;
94}
9596
int parse_opt_object_name(const struct option *opt, const char *arg, int unset)
97{
98unsigned char sha1[20];
99100
if (unset) {
101sha1_array_clear(opt->value);
102return 0;
103}
104if (!arg)
105return -1;
106if (get_sha1(arg, sha1))
107return error(_("malformed object name '%s'"), arg);
108sha1_array_append(opt->value, sha1);
109return 0;
110}
111112
int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
113{
114int *target = opt->value;
115*target = unset ? 2 : 1;
116return 0;
117}
118119
int parse_options_concat(struct option *dst, size_t dst_size, struct option *src)
120{
121int i, j;
122123
for (i = 0; i < dst_size; i++)
124if (dst[i].type == OPTION_END)
125break;
126for (j = 0; i < dst_size; i++, j++) {
127dst[i] = src[j];
128if (src[j].type == OPTION_END)
129return 0;
130}
131return -1;
132}
133134
int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
135{
136struct string_list *v = opt->value;
137138
if (unset) {
139string_list_clear(v, 0);
140return 0;
141}
142143
if (!arg)
144return -1;
145146
string_list_append(v, xstrdup(arg));
147return 0;
148}
149150
int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
151{
152return 0;
153}