Re: [PATCH v2 01/19] parse-options-cb: implement parse_opt_pass_strbuf()

2015-06-09 Thread Junio C Hamano
Paul Tan pyoka...@gmail.com writes:

 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.
 + */

This is to be used to create a single argument, not an entire
command line to be executed via run_command() or a shell, right?
It wasn't very clear from the above command.

If the same option is given more than once, the earlier ones are
discarded and the last one wins.  That may be OK for your expected
callers, and I do not think this needs to have an option to instead
accumulate them, but it needs to be made clear in the comment and
the API documentation for future developers who want to use the
parse-options API.

 +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))
--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v2 01/19] parse-options-cb: implement parse_opt_pass_strbuf()

2015-06-03 Thread Paul Tan
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 johannes.schinde...@gmx.de
Signed-off-by: Paul Tan pyoka...@gmail.com
---

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

--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html