[PATCH v2 01/19] parse-options-cb: implement parse_opt_pass_strbuf()
From: Paul Tan <hidden>
Date: 2016-06-15 23:05:07
Subsystem:
the rest · Maintainer:
Linus Torvalds
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_pass_strbuf() parse-options callback, which will
reconstruct the command-line option into an strbuf, such that it can be
passed to another git command.
Helped-by: Johannes Schindelin [off-list ref]
Signed-off-by: Paul Tan <redacted>
---
Notes:
v2
* Previously implemented as a static function in builtin/pull.c. It now
uses an strbuf instead of returning newly-allocated strings, to make
memory management easier.
* We don't use defval anymore. Instead, we use long_name and short_name.
parse-options-cb.c | 29 +++++++++++++++++++++++++++++
parse-options.h | 1 +
2 files changed, 30 insertions(+)
diff --git a/parse-options-cb.c b/parse-options-cb.c
index be8c413..5b1dbcf 100644
--- a/parse-options-cb.c
+++ b/parse-options-cb.c@@ -134,3 +134,32 @@ int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset) { return 0; } + +/** + * For an option opt, recreates the command-line option in opt->value which + * must be an strbuf. This is useful when we need to pass the command-line + * option to another command. + */ +int parse_opt_pass_strbuf(const struct option *opt, const char *arg, int unset) +{ + struct strbuf *sb = opt->value; + + strbuf_reset(sb); + + if (opt->long_name) { + strbuf_addstr(sb, unset ? "--no-" : "--"); + strbuf_addstr(sb, opt->long_name); + if (arg) { + strbuf_addch(sb, '='); + strbuf_addstr(sb, arg); + } + } else if (opt->short_name && !unset) { + strbuf_addch(sb, '-'); + strbuf_addch(sb, opt->short_name); + if (arg) + strbuf_addstr(sb, arg); + } else + return -1; + + return 0; +}
diff --git a/parse-options.h b/parse-options.h
index c71e9da..1d21398 100644
--- a/parse-options.h
+++ b/parse-options.h@@ -224,6 +224,7 @@ extern int parse_opt_with_commit(const struct option *, const char *, int); extern int parse_opt_tertiary(const struct option *, const char *, int); extern int parse_opt_string_list(const struct option *, const char *, int); extern int parse_opt_noop_cb(const struct option *, const char *, int); +extern int parse_opt_pass_strbuf(const struct option *, const char *, int); #define OPT__VERBOSE(var, h) OPT_COUNTUP('v', "verbose", (var), (h)) #define OPT__QUIET(var, h) OPT_COUNTUP('q', "quiet", (var), (h))
--
2.1.4