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

[PATCH v6 3/6] config: add generic callback wrapper to parse section.<url>.key

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 31, 2013, 19:26 UTC
Message-ID
<1375298768-7740-4-git-send-email-gitster@pobox.com>
In-Reply-To
<1375298768-7740-1-git-send-email-gitster@pobox.com>

Existing configuration parsing functions (e.g. http_options() in http.c) know how to parse two-level configuration variable names. We would like to exploit them and parse something like this:

	[http]
		sslVerify = true
	[http "https://weak.example.com"]
		sslVerify = false

and pretend as if http.sslVerify were set to false when talking to "https://weak.example.com/path".

Introduce `urlmatch_config_entry()` wrapper that:
 - is called with the target URL (e.g. "https://weak.example.com/path"),
   and the two-level variable parser (e.g. `http_options`);
 - uses `url_normalize()` and `match_urls()` to see if configuration
   data matches the target URL; and
 - calls the traditional two-level configuration variable parser
   only for the configuration data whose <url> part matches the
   target URL (and if there are multiple matches, only do so if the
   current match is a better match than the ones previously seen).
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 urlmatch.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 urlmatch.h | 18 +++++++++++++++++
 2 files changed, 85 insertions(+)
diff --git a/urlmatch.c b/urlmatch.c
index e1b03ee..073fdd3 100644
--- a/urlmatch.c
+++ b/urlmatch.c
@@ -466,3 +466,70 @@ int match_urls(const struct url_info *url,
 		*exactusermatch = usermatched;
 	return pathmatchlen;
 }
+
+int urlmatch_config_entry(const char *var, const char *value, void *cb)
+{
+	struct string_list_item *item;
+	struct urlmatch_config *collect = cb;
+	struct urlmatch_item *matched;
+	struct url_info *url = &collect->url;
+	const char *key, *dot;
+	struct strbuf synthkey = STRBUF_INIT;
+	size_t matched_len = 0;
+	int user_matched = 0;
+	int retval;
+
+	key = skip_prefix(var, collect->section);
+	if (!key || *(key++) != '.') {
+		if (collect->cascade_fn)
+			return collect->cascade_fn(var, value, cb);
+		return 0; /* not interested */
+	}
+	dot = strrchr(key, '.');
+	if (dot) {
+		char *config_url, *norm_url;
+		struct url_info norm_info;
+
+		config_url = xmemdupz(key, dot - key);
+		norm_url = url_normalize(config_url, &norm_info);
+		free(config_url);
+		if (!norm_url)
+			return 0;
+		matched_len = match_urls(url, &norm_info, &user_matched);
+		free(norm_url);
+		if (!matched_len)
+			return 0;
+		key = dot + 1;
+	}
+
+	if (collect->key && strcmp(key, collect->key))
+		return 0;
+
+	item = string_list_insert(&collect->vars, key);
+	if (!item->util) {
+		matched = xcalloc(1, sizeof(*matched));
+		item->util = matched;
+	} else {
+		matched = item->util;
+		/*
+		 * Is our match shorter?  Is our match the same
+		 * length, and without user while the current
+		 * candidate is with user?  Then we cannot use it.
+		 */
+		if (matched_len < matched->matched_len ||
+		    ((matched_len == matched->matched_len) &&
+		     (!user_matched && matched->user_matched)))
+			return 0;
+		/* Otherwise, replace it with this one. */
+	}
+
+	matched->matched_len = matched_len;
+	matched->user_matched = user_matched;
+	strbuf_addstr(&synthkey, collect->section);
+	strbuf_addch(&synthkey, '.');
+	strbuf_addstr(&synthkey, key);
+	retval = collect->collect_fn(synthkey.buf, value, collect->cb);
+
+	strbuf_release(&synthkey);
+	return retval;
+}
diff --git a/urlmatch.h b/urlmatch.h
index b67f57f..b461dfd 100644
--- a/urlmatch.h
+++ b/urlmatch.h
@@ -33,4 +33,22 @@ struct url_info {
 extern char *url_normalize(const char *, struct url_info *);
 extern int match_urls(const struct url_info *url, const struct url_info *url_prefix, int *exactusermatch);
 
+struct urlmatch_item {
+	size_t matched_len;
+	char user_matched;
+};
+
+struct urlmatch_config {
+	struct string_list vars;
+	struct url_info url;
+	const char *section;
+	const char *key;
+
+	void *cb;
+	int (*collect_fn)(const char *var, const char *value, void *cb);
+	int (*cascade_fn)(const char *var, const char *value, void *cb);
+};
+
+extern int urlmatch_config_entry(const char *var, const char *value, void *cb);
+
 #endif /* URL_MATCH_H */
-- 
1.8.4-rc0-153-g9820077
Previous: Kyle J. McKayNext: Junio C Hamano
Message 10 of 23 in “http.<url>.<key> and friends”
  1. 0/6 http.<url>.<key> and friendsJunio C Hamano, Jul 31, 2013
  2. 1/6 http.c: fix parsing of http.sslCertPasswordProtected variableJunio C Hamano, Jul 31, 2013
  3. 2/6 config: add helper to normalize and match URLsJunio C Hamano, Jul 31, 2013
  4. Kyle J. McKayJul 31, 2013
  5. 0/2 http.<url>.<key> and friendsKyle J. McKay, Jul 31, 2013
  6. 2/4 config: add helper to normalize and match URLsKyle J. McKay, Jul 31, 2013
  7. 4/4 config: parse http.<url>.<variable> using urlmatchKyle J. McKay, Jul 31, 2013
  8. Junio C HamanoJul 31, 2013
  9. 4/6 config: parse http.<url>.<variable> using urlmatchKyle J. McKay, Jul 31, 2013
  10. 3/6 config: add generic callback wrapper to parse section.<url>.keyJunio C Hamano, Jul 31, 2013
  11. 4/6 config: parse http.<url>.<variable> using urlmatchJunio C Hamano, Jul 31, 2013
  12. Kyle J. McKayJul 31, 2013
  13. 5/6 builtin/config: refactor collect_config()Junio C Hamano, Jul 31, 2013
  14. 6/6 config: "git config --get-urlmatch" parses section.<url>.keyJunio C Hamano, Jul 31, 2013
  15. Jeff KingJul 31, 2013
  16. Kyle J. McKayJul 31, 2013
  17. Jeff KingJul 31, 2013
  18. Junio C HamanoAug 1, 2013
  19. Jeff KingAug 1, 2013
  20. 4/6 config: parse http.<url>.<variable> using urlmatchKyle J. McKay, Aug 5, 2013
  21. Junio C HamanoAug 5, 2013
  22. Kyle J. McKayAug 5, 2013
  23. Junio C HamanoJul 31, 2013

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.