tag: show tag notes with --notes
[git/mjg.git] / parse-options-cb.c
blob345d77687241680e01168dc606cf6ab1c6a0059d
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 "notes.h"
9 /*----- some often used options -----*/
11 int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
13 int v;
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;
26 *(int *)(opt->value) = v;
27 return 0;
30 int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
31 int unset)
33 *(unsigned long *)(opt->value) = approxidate(arg);
34 return 0;
37 int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
38 int unset)
40 int value;
42 if (!arg)
43 arg = unset ? "never" : (const char *)opt->defval;
44 value = git_config_colorbool(NULL, arg);
45 if (value < 0)
46 return opterror(opt,
47 "expects \"always\", \"auto\", or \"never\"", 0);
48 *(int *)opt->value = value;
49 return 0;
52 int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
53 int unset)
55 int *target = opt->value;
57 if (unset)
58 /* --no-quiet, --no-verbose */
59 *target = 0;
60 else if (opt->short_name == 'v') {
61 if (*target >= 0)
62 (*target)++;
63 else
64 *target = 1;
65 } else {
66 if (*target <= 0)
67 (*target)--;
68 else
69 *target = -1;
71 return 0;
74 int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
76 unsigned char sha1[20];
77 struct commit *commit;
79 if (!arg)
80 return -1;
81 if (get_sha1(arg, sha1))
82 return error("malformed object name %s", arg);
83 commit = lookup_commit_reference(sha1);
84 if (!commit)
85 return error("no such commit %s", arg);
86 commit_list_insert(commit, opt->value);
87 return 0;
90 int parse_opt_with_notes(const struct option *opt, const char *arg, int unset)
92 const char *notes_ref;
93 if (!arg)
94 notes_ref = default_notes_ref();
95 else {
96 struct strbuf buf = STRBUF_INIT;
97 if (!prefixcmp(arg, "refs/"))
98 /* happy */;
99 else if (!prefixcmp(arg, "notes/"))
100 strbuf_addstr(&buf, "refs/");
101 else
102 strbuf_addstr(&buf, "refs/notes/");
103 strbuf_addstr(&buf, arg);
104 notes_ref = strbuf_detach(&buf, NULL);
106 *(const char **)(opt->value) = notes_ref;
107 return 0;
110 int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
112 int *target = opt->value;
113 *target = unset ? 2 : 1;
114 return 0;
117 int parse_options_concat(struct option *dst, size_t dst_size, struct option *src)
119 int i, j;
121 for (i = 0; i < dst_size; i++)
122 if (dst[i].type == OPTION_END)
123 break;
124 for (j = 0; i < dst_size; i++, j++) {
125 dst[i] = src[j];
126 if (src[j].type == OPTION_END)
127 return 0;
129 return -1;
132 int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
134 struct string_list *v = opt->value;
136 if (unset) {
137 string_list_clear(v, 0);
138 return 0;
141 if (!arg)
142 return -1;
144 string_list_append(v, xstrdup(arg));
145 return 0;
148 int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
150 return 0;