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

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

From
SZEDER Gábor <szeder@ira.uka.de>
Date
Aug 20, 2015, 14:14 UTC
Message-ID
<1440080062-26811-1-git-send-email-szeder@ira.uka.de>
In-Reply-To
<xmqqa8twj7b8.fsf@gitster.dls.corp.google.com>

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@ira.uka.de>
---
> 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
Previous: Junio C HamanoNext: Jeff King
Message 7 of 18 in “[PATCHv3 0/2] 'git config --names-only' to help the completion script”
  1. SZEDER GáborAug 10, 2015
  2. 1/2 config: add '--names-only' option to list only variable namesSZEDER Gábor, Aug 10, 2015
  3. Jeff KingAug 10, 2015
  4. Junio C HamanoAug 10, 2015
  5. SZEDER GáborAug 12, 2015
  6. Junio C HamanoAug 13, 2015
  7. config: restructure format_config() for better control flowSZEDER Gábor, Aug 20, 2015
  8. Jeff KingAug 20, 2015
  9. 1/3 format_config: don't init strbufJeff King, Aug 20, 2015
  10. 2/3 format_config: simplify buffer handlingJeff King, Aug 20, 2015
  11. SZEDER GáborAug 21, 2015
  12. Junio C HamanoAug 21, 2015
  13. Junio C HamanoAug 21, 2015
  14. Jeff KingAug 21, 2015
  15. 3/3 get_urlmatch: avoid useless strbuf writeJeff King, Aug 20, 2015
  16. Junio C HamanoAug 20, 2015
  17. 2/2 completion: list variable names reliably with 'git config --names-only'SZEDER Gábor, Aug 10, 2015
  18. Jeff KingAug 10, 2015

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.