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

Re: git should not use a default user.email config value

From
Jeff King <peff@peff.net>
Date
Aug 13, 2013, 12:05 UTC
Message-ID
<20130813120530.GA622@sigill.intra.peff.net>
In-Reply-To
<20130813114635.GA16506@sigill.intra.peff.net>
On Tue, Aug 13, 2013 at 07:46:35AM -0400, Jeff King wrote:
Show 15 quoted lines
> The only downside I can think of is that we might want to use the
> subsection in "include.SUBSECTION.*" for some other limiting conditions
> (e.g., "only include this config when running version >= X.Y", or even
> "include only when environment variable FOO is true").
> 
> I guess we could do something like:
> 
>   [include "repo:...your regex here..."]
>     path = .gitconfig-only-for-some-repos
>   [include "env:USE_MY_MAGIC_CONFIG"]
>     path = .gitconfig-only-when-magic-env-set
> 
> Adding the "repo:" prefix for this repo-dir matching is pretty trivial.
> Adding a similar env-matching is only slightly less trivial; but does
> anybody actually want it?
Here it is with the "repo:" prefix, if you want to build on that.
Adding the "env" spec is as easy as doing this on top:
	diff --git a/config.c b/config.c
	index f1ca6fa..64ba141 100644
	--- a/config.c
	+++ b/config.c
	@@ -150,6 +150,8 @@ static int match_config_include(const char *spec)
	 	const char *val;
	 	if ((val = skip_prefix(spec, "repo:")))
	 		return match_repo_path(val);
	+	if ((val = skip_prefix(spec, "env:")))
	+		return git_env_bool(val, 0);
	 
	 	/* Unknown specs are considered "no match". */
	 	return 0;
---
diff --git a/config.c b/config.c
index e13a7b6..f1ca6fa 100644
--- a/config.c
+++ b/config.c
@@ -119,10 +119,55 @@ int git_config_include(const char *var, const char *value, void *data)
 	return ret;
 }
 
+static NORETURN void die_bad_regex(int err, regex_t *re)
+{
+	char errbuf[1024];
+	regerror(err, re, errbuf, sizeof(errbuf));
+	if (cf && cf->name)
+		die("bad regex (at %s:%d): %s", cf->name, cf->linenr, errbuf);
+	else
+		die("bad regex: %s", errbuf);
+}
+
+static int match_repo_path(const char *re_str)
+{
+	regex_t re;
+	int ret;
+	const char *repo_path;
+
+	ret = regcomp(&re, re_str, REG_EXTENDED);
+	if (ret)
+		die_bad_regex(ret, &re);
+
+	repo_path = absolute_path(get_git_dir());
+	ret = regexec(&re, repo_path, 0, NULL, 0);
+	regfree(&re);
+	return !ret;
+}
+
+static int match_config_include(const char *spec)
+{
+	const char *val;
+	if ((val = skip_prefix(spec, "repo:")))
+		return match_repo_path(val);
+
+	/* Unknown specs are considered "no match". */
+	return 0;
+}
+
+static int match_config_include_mem(const char *spec, int spec_len)
+{
+	char *spec_str = xmemdupz(spec, spec_len);
+	int ret = match_config_include(spec_str);
+	free(spec_str);
+	return ret;
+}
+
 int git_config_include(const char *var, const char *value, void *data)
 {
 	struct config_include_data *inc = data;
-	const char *type;
+	const char *match, *type;
+	int match_len;
 	int ret;
 
 	/*
@@ -133,8 +178,9 @@ int git_config_include(const char *var, const char *value, void *data)
 	if (ret < 0)
 		return ret;
 
-	type = skip_prefix(var, "include.");
-	if (!type)
+	if (parse_config_key(var, "include", &match, &match_len, &type))
+		return ret;
+	if (match && !match_config_include_mem(match, match_len))
 		return ret;
 
 	if (!strcmp(type, "path"))
Previous: Jeff KingNext: Andrew Ardill
Message 29 of 46 in “Re: git should not use a default user.email config value”
  1. Jonathan NiederAug 9, 2013
  2. Thorsten GlaserAug 9, 2013
  3. Felipe ContrerasAug 9, 2013
  4. Matthieu MoyAug 13, 2013
  5. Jeff KingAug 9, 2013
  6. Junio C HamanoAug 9, 2013
  7. Jeff KingAug 10, 2013
  8. Jonathan NiederAug 10, 2013
  9. Jeff KingAug 10, 2013
  10. Jonathan NiederAug 10, 2013
  11. Jeff KingAug 10, 2013
  12. Jonathan NiederAug 9, 2013
  13. Jeff KingAug 10, 2013
  14. Michael HaggertyAug 10, 2013
  15. Jeff KingAug 10, 2013
  16. Michael HaggertyAug 10, 2013
  17. Thorsten GlaserAug 10, 2013
  18. Andreas SchwabAug 10, 2013
  19. Greg TroxelAug 12, 2013
  20. Junio C HamanoAug 10, 2013
  21. Andrew ArdillAug 12, 2013
  22. Jeff KingAug 12, 2013
  23. Michael HaggertyAug 12, 2013
  24. Jeff KingAug 12, 2013
  25. Andrew ArdillAug 12, 2013
  26. Jeff KingAug 12, 2013
  27. Andrew ArdillAug 13, 2013
  28. Jeff KingAug 13, 2013
  29. Jeff KingAug 13, 2013
  30. Andrew ArdillAug 13, 2013
  31. Jeff KingAug 13, 2013
  32. Junio C HamanoAug 13, 2013
  33. Matthieu MoyAug 14, 2013
  34. Jeff KingAug 14, 2013
  35. Matthieu MoyAug 14, 2013
  36. Junio C HamanoAug 14, 2013
  37. Matthieu MoyAug 14, 2013
  38. conditional config syntaxJeff King, Aug 14, 2013
  39. Junio C HamanoAug 14, 2013
  40. Michael HaggertyAug 14, 2013
  41. Jeff KingAug 14, 2013
  42. Junio C HamanoAug 13, 2013
  43. Matthieu MoyAug 13, 2013
  44. Aaron SchrabAug 11, 2013
  45. Matthieu MoyAug 13, 2013
  46. Thorsten GlaserAug 13, 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.