parse-options-cb: implement parse_opt_passthru()

Certain git commands, such as git-pull, are simply wrappers around other git commands like git-fetch, git-merge and git-rebase. As such, these wrapper commands will typically need to "pass through" command-line options of the commands they wrap. Implement the parse_opt_passthru() parse-options callback, which will reconstruct the command-line option into an char* string, such that it can be passed to another git command. Helped-by: Johannes Schindelin <johannes.schindelin@gmx.de> Helped-by: Junio C Hamano <gitster@pobox.com> Helped-by: Stefan Beller <sbeller@google.com> Signed-off-by: Paul Tan <pyokagan@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Paul Tan committed Jun 14, 2015 at 16:41 UTC 6b3ee18dc5c620d7cb4324e009339b5ca9ac488c
3 files changed +59
Documentation/technical/api-parse-options.txt
+7
@@ -212,6 +212,13 @@ There are some macros to easily define options:
212 Use it to hide deprecated options that are still to be recognized
213 and ignored silently.
214
215 +`OPT_PASSTHRU(short, long, &char_var, arg_str, description, flags)`::
216 + Introduce an option that will be reconstructed into a char* string,
217 + which must be initialized to NULL. This is useful when you need to
218 + pass the command-line option to another command. Any previous value
219 + will be overwritten, so this should only be used for options where
220 + the last one specified on the command line wins.
221 +
222
223 The last element of the array must be `OPT_END()`.
224
parse-options-cb.c
+49
@@ -134,3 +134,52 @@ int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
134 {
135 return 0;
136 }
137 +
138 +/**
139 + * Recreates the command-line option in the strbuf.
140 + */
141 +static int recreate_opt(struct strbuf *sb, const struct option *opt,
142 + const char *arg, int unset)
143 +{
144 + strbuf_reset(sb);
145 +
146 + if (opt->long_name) {
147 + strbuf_addstr(sb, unset ? "--no-" : "--");
148 + strbuf_addstr(sb, opt->long_name);
149 + if (arg) {
150 + strbuf_addch(sb, '=');
151 + strbuf_addstr(sb, arg);
152 + }
153 + } else if (opt->short_name && !unset) {
154 + strbuf_addch(sb, '-');
155 + strbuf_addch(sb, opt->short_name);
156 + if (arg)
157 + strbuf_addstr(sb, arg);
158 + } else
159 + return -1;
160 +
161 + return 0;
162 +}
163 +
164 +/**
165 + * For an option opt, recreates the command-line option in opt->value which
166 + * must be an char* initialized to NULL. This is useful when we need to pass
167 + * the command-line option to another command. Since any previous value will be
168 + * overwritten, this callback should only be used for options where the last
169 + * one wins.
170 + */
171 +int parse_opt_passthru(const struct option *opt, const char *arg, int unset)
172 +{
173 + static struct strbuf sb = STRBUF_INIT;
174 + char **opt_value = opt->value;
175 +
176 + if (recreate_opt(&sb, opt, arg, unset) < 0)
177 + return -1;
178 +
179 + if (*opt_value)
180 + free(*opt_value);
181 +
182 + *opt_value = strbuf_detach(&sb, NULL);
183 +
184 + return 0;
185 +}
parse-options.h
+3
@@ -224,6 +224,7 @@ extern int parse_opt_with_commit(const struct option *, const char *, int);
224 extern int parse_opt_tertiary(const struct option *, const char *, int);
225 extern int parse_opt_string_list(const struct option *, const char *, int);
226 extern int parse_opt_noop_cb(const struct option *, const char *, int);
227 +extern int parse_opt_passthru(const struct option *, const char *, int);
228
229 #define OPT__VERBOSE(var, h) OPT_COUNTUP('v', "verbose", (var), (h))
230 #define OPT__QUIET(var, h) OPT_COUNTUP('q', "quiet", (var), (h))
@@ -242,5 +243,7 @@ extern int parse_opt_noop_cb(const struct option *, const char *, int);
243 OPT_COLOR_FLAG(0, "color", (var), (h))
244 #define OPT_COLUMN(s, l, v, h) \
245 { OPTION_CALLBACK, (s), (l), (v), N_("style"), (h), PARSE_OPT_OPTARG, parseopt_column_callback }
246 +#define OPT_PASSTHRU(s, l, v, a, h, f) \
247 + { OPTION_CALLBACK, (s), (l), (v), (a), (h), (f), parse_opt_passthru }
248
249 #endif