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

[PATCH 10/16] pager: use callbacks instead of configset

From
Jeff King <peff@peff.net>
Date
Sep 13, 2016, 03:23 UTC
Message-ID
<20160913032352.khrhkxpa3y4k22vz@sigill.intra.peff.net>
In-Reply-To
<20160913032242.coyuhyhn6uklewuk@sigill.intra.peff.net>

While the cached configset interface is more pleasant to use, it is not appropriate for "early" config like pager setup, which must sometimes do tricky things like reading from ".git/config" even when we have not set up the repository.

As a preparatory step to handling these cases better, let's switch back to using the callback interface, which gives us more control.

Note that this is essentially a revert of 586f414 (pager.c: replace `git_config()` with `git_config_get_value()`, 2014-08-07), but with some minor style fixups and modernizations.

Signed-off-by: Jeff King <peff@peff.net>
---
 pager.c | 47 +++++++++++++++++++++++++++++++++--------------
 1 file changed, 33 insertions(+), 14 deletions(-)
diff --git a/pager.c b/pager.c
index 8aa0a82..46cc411 100644
--- a/pager.c
+++ b/pager.c
@@ -183,23 +183,42 @@ int decimal_width(uintmax_t number)
 	return width;
 }
 
-/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
-int check_pager_config(const char *cmd)
+struct pager_command_config_data {
+	const char *cmd;
+	int want;
+	char *value;
+};
+
+static int pager_command_config(const char *var, const char *value, void *vdata)
 {
-	int want = -1;
-	struct strbuf key = STRBUF_INIT;
-	const char *value = NULL;
-	strbuf_addf(&key, "pager.%s", cmd);
-	if (git_config_key_is_valid(key.buf) &&
-	    !git_config_get_value(key.buf, &value)) {
-		int b = git_config_maybe_bool(key.buf, value);
+	struct pager_command_config_data *data = vdata;
+	const char *cmd;
+
+	if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
+		int b = git_config_maybe_bool(var, value);
 		if (b >= 0)
-			want = b;
+			data->want = b;
 		else {
-			want = 1;
-			pager_program = xstrdup(value);
+			data->want = 1;
+			data->value = xstrdup(value);
 		}
 	}
-	strbuf_release(&key);
-	return want;
+
+	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_command_config_data data;
+
+	data.cmd = cmd;
+	data.want = -1;
+	data.value = NULL;
+
+	git_config(pager_command_config, &data);
+
+	if (data.value)
+		pager_program = data.value;
+	return data.want;
 }
-- 
2.10.0.230.g6f8d04b
Previous: Jeff KingNext: Jeff King
Message 14 of 23 in “fix config-reading in non-repos”
  1. 0/16 fix config-reading in non-reposJeff King, Sep 13, 2016
  2. 01/16 t1007: factor out repeated setupJeff King, Sep 13, 2016
  3. Stefan BellerSep 13, 2016
  4. 02/16 hash-object: always try to set up the git repositoryJeff King, Sep 13, 2016
  5. 06/16 diff: always try to set up the repositoryJeff King, Sep 13, 2016
  6. Stefan BellerSep 13, 2016
  7. Jeff KingSep 13, 2016
  8. 04/16 diff: skip implicit no-index check when given --no-indexJeff King, Sep 13, 2016
  9. 07/16 pager: remove obsolete commentJeff King, Sep 13, 2016
  10. 03/16 patch-id: use RUN_SETUP_GENTLYJeff King, Sep 13, 2016
  11. 05/16 diff: handle --no-index prefixes consistentlyJeff King, Sep 13, 2016
  12. 08/16 pager: stop loading git_default_config()Jeff King, Sep 13, 2016
  13. 09/16 pager: make pager_program a file-local staticJeff King, Sep 13, 2016
  14. 10/16 pager: use callbacks instead of configsetJeff King, Sep 13, 2016
  15. 11/16 pager: handle early configJeff King, Sep 13, 2016
  16. 12/16 t1302: use "git -C"Jeff King, Sep 13, 2016
  17. 13/16 test-config: setup git directoryJeff King, Sep 13, 2016
  18. 14/16 config: only read .git/config from configured reposJeff King, Sep 13, 2016
  19. 15/16 init: expand comments explaining config trickeryJeff King, Sep 13, 2016
  20. 16/16 init: reset cached config when entering new repoJeff King, Sep 13, 2016
  21. Stefan BellerSep 13, 2016
  22. Dennis KaarsemakerSep 14, 2016
  23. Jeff KingSep 14, 2016

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.