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

[PATCH v2 4/8] config: Disallow multiple variable types.

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Feb 17, 2009, 00:54 UTC
Message-ID
<1234832094-15541-4-git-send-email-felipe.contreras@gmail.com>
In-Reply-To
<1234832094-15541-3-git-send-email-felipe.contreras@gmail.com>

So now only either --bool, --int, or --bool-or-int can be used, but not any combination of them.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 builtin-config.c |   32 ++++++++++++++++++++------------
 1 files changed, 20 insertions(+), 12 deletions(-)
diff --git a/builtin-config.c b/builtin-config.c
index 084222a..30de93e 100644
--- a/builtin-config.c
+++ b/builtin-config.c
@@ -19,11 +19,10 @@ static int seen;
 static char delim = '=';
 static char key_delim = ' ';
 static char term = '\n';
-static enum { T_RAW, T_INT, T_BOOL, T_BOOL_OR_INT } type = T_RAW;
 
 static int use_global_config, use_system_config;
 static const char *given_config_file;
-static int actions;
+static int actions, types;
 static const char *get_color_slot, *get_colorbool_slot;
 static int end_null;
 
@@ -39,6 +38,10 @@ static int end_null;
 #define ACTION_LIST (1<<9)
 #define ACTION_EDIT (1<<10)
 
+#define TYPE_BOOL (1<<0)
+#define TYPE_INT (1<<1)
+#define TYPE_BOOL_OR_INT (1<<2)
+
 static struct option builtin_config_options[] = {
 	OPT_GROUP("Config file location"),
 	OPT_BOOLEAN(0, "global", &use_global_config, "use global config file"),
@@ -57,9 +60,9 @@ static struct option builtin_config_options[] = {
 	OPT_BIT('l', "list", &actions, "list all", ACTION_LIST),
 	OPT_BIT('e', "edit", &actions, "opens an editor", ACTION_EDIT),
 	OPT_GROUP("Type"),
-	OPT_SET_INT(0, "bool", &type, "value is \"true\" or \"false\"", T_BOOL),
-	OPT_SET_INT(0, "int", &type, "value is decimal number", T_INT),
-	OPT_SET_INT(0, "bool-or-int", &type, NULL, T_BOOL_OR_INT),
+	OPT_BIT(0, "bool", &types, "value is \"true\" or \"false\"", TYPE_BOOL),
+	OPT_BIT(0, "int", &types, "value is decimal number", TYPE_INT),
+	OPT_BIT(0, "bool-or-int", &types, NULL, TYPE_BOOL_OR_INT),
 	OPT_STRING(0, "get-color", &get_color_slot, "slot", "find the color configured: [default]"),
 	OPT_STRING(0, "get-colorbool", &get_colorbool_slot, "slot", "find the color setting: [stdout-is-tty]"),
 	OPT_GROUP("Other"),
@@ -105,11 +108,11 @@ static int show_config(const char* key_, const char* value_, void *cb)
 	}
 	if (seen && !do_all)
 		dup_error = 1;
-	if (type == T_INT)
+	if (types & TYPE_INT)
 		sprintf(value, "%d", git_config_int(key_, value_?value_:""));
-	else if (type == T_BOOL)
+	else if (types & TYPE_BOOL)
 		vptr = git_config_bool(key_, value_) ? "true" : "false";
-	else if (type == T_BOOL_OR_INT) {
+	else if (types & TYPE_BOOL_OR_INT) {
 		int is_bool, v;
 		v = git_config_bool_or_int(key_, value_, &is_bool);
 		if (is_bool)
@@ -208,18 +211,18 @@ static char *normalize_value(const char *key, const char *value)
 	if (!value)
 		return NULL;
 
-	if (type == T_RAW)
+	if (types == 0)
 		normalized = xstrdup(value);
 	else {
 		normalized = xmalloc(64);
-		if (type == T_INT) {
+		if (types & TYPE_INT) {
 			int v = git_config_int(key, value);
 			sprintf(normalized, "%d", v);
 		}
-		else if (type == T_BOOL)
+		else if (types & TYPE_BOOL)
 			sprintf(normalized, "%s",
 				git_config_bool(key, value) ? "true" : "false");
-		else if (type == T_BOOL_OR_INT) {
+		else if (types & TYPE_BOOL_OR_INT) {
 			int is_bool, v;
 			v = git_config_bool_or_int(key, value, &is_bool);
 			if (!is_bool)
@@ -336,6 +339,11 @@ int cmd_config(int argc, const char **argv, const char *unused_prefix)
 		key_delim = '\n';
 	}
 
+	if (HAS_MULTI_BITS(types)) {
+		error("only one type at a time.");
+		usage_with_options(builtin_config_usage, builtin_config_options);
+	}
+
 	if (HAS_MULTI_BITS(actions)) {
 		error("only one action at a time.");
 		usage_with_options(builtin_config_usage, builtin_config_options);
-- 
1.6.1.3
Previous: Felipe ContrerasNext: Felipe Contreras
Message 4 of 24 in “config: Trivial rename in preparation for parseopt.”
  1. 1/8 config: Trivial rename in preparation for parseopt.Felipe Contreras, Feb 17, 2009
  2. 2/8 config: Reorganize get_color*.Felipe Contreras, Feb 17, 2009
  3. 3/8 config: Use parseopt.Felipe Contreras, Feb 17, 2009
  4. 4/8 config: Disallow multiple variable types.Felipe Contreras, Feb 17, 2009
  5. 5/8 config: Disallow multiple config file locations.Felipe Contreras, Feb 17, 2009
  6. 6/8 config: Don't allow extra arguments for -e or -l.Felipe Contreras, Feb 17, 2009
  7. 7/8 config: Codestyle cleanups.Felipe Contreras, Feb 17, 2009
  8. 8/8 config: Cleanup editor action.Felipe Contreras, Feb 17, 2009
  9. Junio C HamanoFeb 17, 2009
  10. Junio C HamanoFeb 17, 2009
  11. Junio C HamanoFeb 17, 2009
  12. Felipe ContrerasFeb 17, 2009
  13. Junio C HamanoFeb 17, 2009
  14. Felipe ContrerasFeb 17, 2009
  15. Johannes SchindelinFeb 17, 2009
  16. Felipe ContrerasFeb 17, 2009
  17. Felipe ContrerasFeb 17, 2009
  18. Junio C HamanoFeb 17, 2009
  19. Junio C HamanoFeb 17, 2009
  20. Felipe ContrerasFeb 17, 2009
  21. Johannes SchindelinFeb 17, 2009
  22. Felipe ContrerasFeb 17, 2009
  23. Gerrit PapeFeb 17, 2009
  24. Johannes SchindelinFeb 17, 2009

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.