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

Re: [PATCH 08/11] pager.c: replace `git_config()` with `git_config_get_value()`

From
Matthieu Moy <matthieu.moy@grenoble-inp.fr>
Date
Aug 4, 2014, 20:36 UTC
Message-ID
<vpqsilc9elm.fsf@anie.imag.fr>
In-Reply-To
<1407177229-30081-9-git-send-email-tanayabh@gmail.com>
Tanay Abhra <tanayabh@gmail.com> writes:
Show 6 quoted lines
> Use `git_config_get_value()` instead of `git_config()` to take advantage of
> the config-set API which provides a cleaner control flow.
>
> Signed-off-by: Tanay Abhra <tanayabh@gmail.com>
> ---
>  pager.c | 40 +++++++++++++---------------------------

I find the patch more readable with --histogram, cut-and-pasted below in case other reviewers find it better too.

diff --git a/pager.c b/pager.c
index 8b5cbc5..b7eb7e7 100644
--- a/pager.c
+++ b/pager.c
@@ -6,12 +6,6 @@
 #define DEFAULT_PAGER "less"
 #endif
 
-struct pager_config {
-	const char *cmd;
-	int want;
-	char *value;
-};
-
 /*
  * This is split up from the rest of git so that we can do
  * something different on Windows.
@@ -155,30 +149,22 @@ int decimal_width(int number)
 	return width;
 }
 
-static int pager_command_config(const char *var, const char *value, void *data)
-{
-	struct pager_config *c = data;
-	if (starts_with(var, "pager.") && !strcmp(var + 6, c->cmd)) {
-		int b = git_config_maybe_bool(var, value);
-		if (b >= 0)
-			c->want = b;
-		else {
-			c->want = 1;
-			c->value = xstrdup(value);
-		}
-	}
-	return 0;
-}
-
 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
 int check_pager_config(const char *cmd)
 {
-	struct pager_config c;
-	c.cmd = cmd;
-	c.want = -1;
-	c.value = NULL;
-	git_config(pager_command_config, &c);
-	if (c.value)
-		pager_program = c.value;
-	return c.want;
+	int want = -1;
+	struct strbuf key = STRBUF_INIT;
+	const char *value = NULL;
+	strbuf_addf(&key, "pager.%s", cmd);
+	if (!git_config_get_value(key.buf, &value)) {
+		int b = git_config_maybe_bool(key.buf, value);
+		if (b >= 0)
+			want = b;
+		else {
+			want = 1;
+			pager_program = xstrdup(value);
+		}
+	}
+	strbuf_release(&key);
+	return want;
 }


I first wondered why you were not using git_config_get_maybe_bool(), but
you want to interpret non-boolean values as command-names, so you do
need this two-steps get_value -> maybe_bool.
-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Previous: Tanay AbhraNext: Tanay Abhra
Message 16 of 20 in “git_config callers rewritten with the new config-set API”
  1. 00/11 git_config callers rewritten with the new config-set APITanay Abhra, Aug 4, 2014
  2. 01/11 daemon.c: replace `git_config()` with `git_config_get_bool()` familyTanay Abhra, Aug 4, 2014
  3. Matthieu MoyAug 4, 2014
  4. 02/11 http-backend.c: replace `git_config()` with `git_config_get_bool()` familyTanay Abhra, Aug 4, 2014
  5. Eric SunshineAug 4, 2014
  6. Tanay AbhraAug 6, 2014
  7. Matthieu MoyAug 6, 2014
  8. Eric SunshineAug 6, 2014
  9. 03/11 read-cache.c: replace `git_config()` with `git_config_get_*()` familyTanay Abhra, Aug 4, 2014
  10. 04/11 archive.c: replace `git_config()` with `git_config_get_bool()` familyTanay Abhra, Aug 4, 2014
  11. 05/11 fetchpack.c: replace `git_config()` with `git_config_get_*()` familyTanay Abhra, Aug 4, 2014
  12. 06/11 rerere.c: replace `git_config()` with `git_config_get_*()` familyTanay Abhra, Aug 4, 2014
  13. 07/11 builtin/gc.c: replace `git_config()` with `git_config_get_*()` familyTanay Abhra, Aug 4, 2014
  14. Matthieu MoyAug 4, 2014
  15. 08/11 pager.c: replace `git_config()` with `git_config_get_value()`Tanay Abhra, Aug 4, 2014
  16. Matthieu MoyAug 4, 2014
  17. 09/11 imap-send.c: replace `git_config()` with `git_config_get_*()` familyTanay Abhra, Aug 4, 2014
  18. 10/11 alias.c: replace `git_config()` with `git_config_get_string()`Tanay Abhra, Aug 4, 2014
  19. 11/11 branch.c: replace `git_config()` with `git_config_get_string()Tanay Abhra, Aug 4, 2014
  20. Matthieu MoyAug 4, 2014

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.