[PATCH] config: restructure format_config() for better control flow

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Commit 578625fa91 (config: add '--name-only' option to list only
variable names, 2015-08-10) modified format_config() such that it
returned from the middle of the function when showing only keys,
resulting in ugly code structure.

Reorganize the if statements and dealing with the key-value delimiter to
make the function easier to read.

Signed-off-by: SZEDER Gábor <szeder@xxxxxxxxxx>
---

> The topic is now in 'next'; I think I've locally fixed it up for
> these when I originally queued them a few days ago, so if there are
> any remaining issues, please throw incremental polishing patches.

OK, though it's not a major issue, I think this is still worth doing on
top.

 builtin/config.c | 78 +++++++++++++++++++++++++++-----------------------------
 1 file changed, 37 insertions(+), 41 deletions(-)

diff --git a/builtin/config.c b/builtin/config.c
index 631db458ec..810e104224 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -108,52 +108,48 @@ struct strbuf_list {
 
 static int format_config(struct strbuf *buf, const char *key_, const char *value_)
 {
-	int must_free_vptr = 0;
-	int must_print_delim = 0;
-	char value[256];
-	const char *vptr = value;
-
 	strbuf_init(buf, 0);
 
-	if (show_keys) {
+	if (show_keys)
 		strbuf_addstr(buf, key_);
-		must_print_delim = 1;
-	}
-	if (omit_values) {
-		strbuf_addch(buf, term);
-		return 0;
-	}
-	if (types == TYPE_INT)
-		sprintf(value, "%"PRId64,
-			git_config_int64(key_, value_ ? value_ : ""));
-	else if (types == TYPE_BOOL)
-		vptr = git_config_bool(key_, value_) ? "true" : "false";
-	else if (types == TYPE_BOOL_OR_INT) {
-		int is_bool, v;
-		v = git_config_bool_or_int(key_, value_, &is_bool);
-		if (is_bool)
-			vptr = v ? "true" : "false";
-		else
-			sprintf(value, "%d", v);
-	} else if (types == TYPE_PATH) {
-		if (git_config_pathname(&vptr, key_, value_) < 0)
-			return -1;
-		must_free_vptr = 1;
-	} else if (value_) {
-		vptr = value_;
-	} else {
-		/* Just show the key name */
-		vptr = "";
-		must_print_delim = 0;
-	}
+	if (!omit_values) {
+		int must_free_vptr = 0;
+		int must_add_delim = show_keys;
+		char value[256];
+		const char *vptr = value;
 
-	if (must_print_delim)
-		strbuf_addch(buf, key_delim);
-	strbuf_addstr(buf, vptr);
+		if (types == TYPE_INT)
+			sprintf(value, "%"PRId64,
+				git_config_int64(key_, value_ ? value_ : ""));
+		else if (types == TYPE_BOOL)
+			vptr = git_config_bool(key_, value_) ? "true" : "false";
+		else if (types == TYPE_BOOL_OR_INT) {
+			int is_bool, v;
+			v = git_config_bool_or_int(key_, value_, &is_bool);
+			if (is_bool)
+				vptr = v ? "true" : "false";
+			else
+				sprintf(value, "%d", v);
+		} else if (types == TYPE_PATH) {
+			if (git_config_pathname(&vptr, key_, value_) < 0)
+				return -1;
+			must_free_vptr = 1;
+		} else if (value_) {
+			vptr = value_;
+		} else {
+			/* Just show the key name */
+			vptr = "";
+			must_add_delim = 0;
+		}
+
+		if (must_add_delim)
+			strbuf_addch(buf, key_delim);
+		strbuf_addstr(buf, vptr);
+
+		if (must_free_vptr)
+			free((char *)vptr);
+	}
 	strbuf_addch(buf, term);
-
-	if (must_free_vptr)
-		free((char *)vptr);
 	return 0;
 }
 
-- 
2.5.0.415.g33d64ef

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



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]