git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/3] config: add git_config_option_string()

From
Jay Soffian <jaysoffian@gmail.com>
Date
Mar 6, 2009, 22:15 UTC
Message-ID
<12addb53ef5c0e62ee22847591c8e7b884dc0bd2.1236377358.git.jaysoffian@gmail.com>
In-Reply-To
<cover.1236377358.git.jaysoffian@gmail.com>

This patch teaches config a new function, git_config_option_string(), which parses a string using parse-options.

This is useful for any command that allows command-line options to appear in a config.

Signed-off-by: Jay Soffian <jaysoffian@gmail.com>
---
Originally I had just factored this into its own function in
builtin-merge, but it seemed that it might be generally useful, so I
moved it to config.c
 cache.h         |    2 ++
 config.c        |   39 +++++++++++++++++++++++++++++++++++++++
 parse-options.c |    2 ++
 3 files changed, 43 insertions(+), 0 deletions(-)
diff --git a/cache.h b/cache.h
index 189151d..c6d3f05 100644
--- a/cache.h
+++ b/cache.h
@@ -854,6 +854,8 @@ extern unsigned long git_config_ulong(const char *, const char *);
 extern int git_config_bool_or_int(const char *, const char *, int *);
 extern int git_config_bool(const char *, const char *);
 extern int git_config_string(const char **, const char *, const char *);
+struct option;
+extern int git_config_option_string(const struct option *, int, const char *, const char *);
 extern int git_config_set(const char *, const char *);
 extern int git_config_set_multivar(const char *, const char *, const char *, int);
 extern int git_config_rename_section(const char *, const char *);
diff --git a/config.c b/config.c
index 0c8c76f..29cfd5b 100644
--- a/config.c
+++ b/config.c
@@ -7,6 +7,7 @@
  */
 #include "cache.h"
 #include "exec_cmd.h"
+#include "parse-options.h"
 
 #define MAXNAME (256)
 
@@ -353,6 +354,44 @@ int git_config_string(const char **dest, const char *var, const char *value)
 	return 0;
 }
 
+int git_config_option_string(const struct option *options, int flags,
+			     const char *var, const char *value)
+{
+	int argc, ret;
+	const char **argv;
+	char *buf;
+	struct parse_opt_ctx_t ctx;
+
+	if (!value)
+		return config_error_nonbool(var);
+
+	buf = xstrdup(value);
+	if ((argc = split_cmdline(buf, &argv)) < 0) {
+		free(buf);
+		return error("Malformed value for %s", var);
+	}
+	argv = xrealloc(argv, sizeof(*argv) * (argc + 2));
+	memmove(argv + 1, argv, sizeof(*argv) * (argc + 1));
+	argc++;
+
+	parse_options_start(&ctx, argc, argv, flags);
+	switch (parse_options_step(&ctx, options, NULL)) {
+	case PARSE_OPT_DONE:
+		ret = parse_options_end(&ctx);
+		break;
+	case PARSE_OPT_HELP: /* not supported in a config */
+	default: /* PARSE_OPT_UNKNOWN */
+		if (ctx.argv[0][1] == '-') {
+			ret = error("unknown option `%s'", ctx.argv[0] + 2);
+		} else {
+			ret = error("unknown switch `%c'", *ctx.opt);
+		}
+		break;
+	}
+	free(buf);
+	return ret;
+}
+
 static int git_default_core_config(const char *var, const char *value)
 {
 	/* This needs a better name */
diff --git a/parse-options.c b/parse-options.c
index 4c5d09d..7996b50 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -356,6 +356,8 @@ int parse_options(int argc, const char **argv, const struct option *options,
 int usage_with_options_internal(const char * const *usagestr,
 				const struct option *opts, int full)
 {
+	if (!usagestr)
+		return PARSE_OPT_HELP;
 	fprintf(stderr, "usage: %s\n", *usagestr++);
 	while (*usagestr && **usagestr)
 		fprintf(stderr, "   or: %s\n", *usagestr++);
-- 
1.6.2.rc2.332.g5d21b
Previous: Jay SoffianNext: Jay Soffian
Message 2 of 15 in “Re: how to have --no-ff be the default for all branch”
  1. 0/3 Re: how to have --no-ff be the default for all branchJay Soffian, Mar 6, 2009
  2. 1/3 config: add git_config_option_string()Jay Soffian, Mar 6, 2009
  3. 2/3 builtin-merge: refactor to use git_config_option_stringJay Soffian, Mar 6, 2009
  4. 3/3 builtin-merge: add support for default merge optionsJay Soffian, Mar 6, 2009
  5. Junio C HamanoMar 6, 2009
  6. Jay SoffianMar 6, 2009
  7. 3/3 builtin-merge: add support for default merge optionsJay Soffian, Mar 7, 2009
  8. Junio C HamanoMar 7, 2009
  9. Jay SoffianMar 7, 2009
  10. Junio C HamanoMar 7, 2009
  11. Jay SoffianMar 7, 2009
  12. jean-luc maletMar 7, 2009
  13. jean-luc maletMar 19, 2010
  14. Jay SoffianMar 19, 2010
  15. jean-luc maletApr 2, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.