]> git.ipfire.org Git - thirdparty/git.git/blame - parse-options-cb.c
Reduce parse-options.o dependencies
[thirdparty/git.git] / parse-options-cb.c
CommitLineData
06876284
DI
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_color_flag_cb(const struct option *opt, const char *arg,
37 int unset)
38{
39 int value;
40
41 if (!arg)
42 arg = unset ? "never" : (const char *)opt->defval;
43 value = git_config_colorbool(NULL, arg, -1);
44 if (value < 0)
45 return opterror(opt,
46 "expects \"always\", \"auto\", or \"never\"", 0);
47 *(int *)opt->value = value;
48 return 0;
49}
50
51int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
52 int unset)
53{
54 int *target = opt->value;
55
56 if (unset)
57 /* --no-quiet, --no-verbose */
58 *target = 0;
59 else if (opt->short_name == 'v') {
60 if (*target >= 0)
61 (*target)++;
62 else
63 *target = 1;
64 } else {
65 if (*target <= 0)
66 (*target)--;
67 else
68 *target = -1;
69 }
70 return 0;
71}
72
73int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
74{
75 unsigned char sha1[20];
76 struct commit *commit;
77
78 if (!arg)
79 return -1;
80 if (get_sha1(arg, sha1))
81 return error("malformed object name %s", arg);
82 commit = lookup_commit_reference(sha1);
83 if (!commit)
84 return error("no such commit %s", arg);
85 commit_list_insert(commit, opt->value);
86 return 0;
87}
88
89int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
90{
91 int *target = opt->value;
92 *target = unset ? 2 : 1;
93 return 0;
94}
95
96int parse_options_concat(struct option *dst, size_t dst_size, struct option *src)
97{
98 int i, j;
99
100 for (i = 0; i < dst_size; i++)
101 if (dst[i].type == OPTION_END)
102 break;
103 for (j = 0; i < dst_size; i++, j++) {
104 dst[i] = src[j];
105 if (src[j].type == OPTION_END)
106 return 0;
107 }
108 return -1;
109}
110
111int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
112{
113 struct string_list *v = opt->value;
114
115 if (unset) {
116 string_list_clear(v, 0);
117 return 0;
118 }
119
120 if (!arg)
121 return -1;
122
123 string_list_append(v, xstrdup(arg));
124 return 0;
125}