1 #include "git-compat-util.h"
2 #include "parse-options.h"
6 #include "string-list.h"
9 /*----- some often used options -----*/
11 int parse_opt_abbrev_cb(const struct option
*opt
, const char *arg
, int unset
)
16 v
= unset
? 0 : DEFAULT_ABBREV
;
18 v
= strtol(arg
, (char **)&arg
, 10);
20 return opterror(opt
, "expects a numerical value", 0);
21 if (v
&& v
< MINIMUM_ABBREV
)
26 *(int *)(opt
->value
) = v
;
30 int parse_opt_approxidate_cb(const struct option
*opt
, const char *arg
,
33 *(unsigned long *)(opt
->value
) = approxidate(arg
);
37 int parse_opt_color_flag_cb(const struct option
*opt
, const char *arg
,
43 arg
= unset
? "never" : (const char *)opt
->defval
;
44 value
= git_config_colorbool(NULL
, arg
);
47 "expects \"always\", \"auto\", or \"never\"", 0);
48 *(int *)opt
->value
= value
;
52 int parse_opt_verbosity_cb(const struct option
*opt
, const char *arg
,
55 int *target
= opt
->value
;
58 /* --no-quiet, --no-verbose */
60 else if (opt
->short_name
== 'v') {
74 int parse_opt_with_commit(const struct option
*opt
, const char *arg
, int unset
)
76 unsigned char sha1
[20];
77 struct commit
*commit
;
81 if (get_sha1(arg
, sha1
))
82 return error("malformed object name %s", arg
);
83 commit
= lookup_commit_reference(sha1
);
85 return error("no such commit %s", arg
);
86 commit_list_insert(commit
, opt
->value
);
90 int parse_opt_with_notes(const struct option
*opt
, const char *arg
, int unset
)
92 const char *notes_ref
;
94 notes_ref
= default_notes_ref();
96 struct strbuf buf
= STRBUF_INIT
;
97 if (!prefixcmp(arg
, "refs/"))
99 else if (!prefixcmp(arg
, "notes/"))
100 strbuf_addstr(&buf
, "refs/");
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
;
110 int parse_opt_tertiary(const struct option
*opt
, const char *arg
, int unset
)
112 int *target
= opt
->value
;
113 *target
= unset
? 2 : 1;
117 int parse_options_concat(struct option
*dst
, size_t dst_size
, struct option
*src
)
121 for (i
= 0; i
< dst_size
; i
++)
122 if (dst
[i
].type
== OPTION_END
)
124 for (j
= 0; i
< dst_size
; i
++, j
++) {
126 if (src
[j
].type
== OPTION_END
)
132 int parse_opt_string_list(const struct option
*opt
, const char *arg
, int unset
)
134 struct string_list
*v
= opt
->value
;
137 string_list_clear(v
, 0);
144 string_list_append(v
, xstrdup(arg
));
148 int parse_opt_noop_cb(const struct option
*opt
, const char *arg
, int unset
)