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

Re: [RFC PATCH 2/2] config: include file if remote URL matches a glob

From
Jeff King <peff@peff.net>
Date
Oct 12, 2021, 23:30 UTC
Message-ID
<YWYafdpemaiAjvUV@coredump.intra.peff.net>
In-Reply-To
<967680d27aa7a2709e528ff989a9dd534886efba.1634077795.git.jonathantanmy@google.com>
On Tue, Oct 12, 2021 at 03:57:23PM -0700, Jonathan Tan wrote:
Show 9 quoted lines
> This is a feature that supports config file inclusion conditional on
> whether the repo has a remote with a URL that matches a glob.
> 
> Similar to my previous work on remote-suggested hooks [1], the main
> motivation is to allow remote repo administrators to provide recommended
> configs in a way that can be consumed more easily (e.g. through a
> package installable by a package manager). But this can also be used by,
> say, an individual that wants certain configs to apply to a certain set
> of local repos but not others.

OK. I was a little wary after reading the subject that this would be "when we are using such a URL", which is full of all kinds of odd corner cases. But if it is "a remote is defined with a matching URL" that makes it a property of the repository, not the operation.

I think in general this kind of feature is currently served by just correlating filesystem paths with their function. So with your patch I could do:

  [includeIf "hasremoteurl:https://myjob.example.com"]
  path = foo.conf

But in general, I'd imagine most people put their repository in ~/work or similar, and just do:

  [includeIf "gitdir:~/work"]
  path = foo.conf

(and of course you can imagine more subdivisions as necessary). So I find the use-case only sort-of compelling. In general, I'm in favor of adding new includeIf directions even if they're only moderately convenient. But this one is rather sticky, because it is dependent on other config keys being defined. So it introduces a new and complicated ordering issue. Is it worth it? Maybe I'm not being imaginative enough in seeing the use cases.

Show 7 quoted lines
> I marked this as RFC because there are some design points that need to
> be resolved:
> 
>  - The existing "include" and "includeIf" instructions are executed
>    immediately, whereas in order to be useful, the execution of
>    "includeIf hasremoteurl" needs to be delayed until all config files
>    are read. Are there better ways to do this?

Note that this violates the "as if they had been found at the location of the include directive" rule which we advertise to users. I'd imagine that most of the time it doesn't matter, but this is a pretty big exception we'll need to document.

Just brainstorming some alternatives:
  - We could stop the world while we are parsing and do a _new_ parse
    that just looks at the remote config (in fact, this is the natural
    thing if you were consulting the regular remote.c code for the list
    of remotes, because it does its own config parse).
    That does mean that the remote-conditional includes cannot
    themselves define new remotes. But I think that is already the case
    with your patch (and violating that gets you into weird circular
    problems).
  - We could simply document that if you want to depend on conditional
    includes based on a particular remote.*.url existing, then that
    remote config must appear earlier in the sequence.
    This is a bit ugly, because I'm sure it will bite somebody
    eventually. But at the same time, it resolves all of the weird
    timing issues, and does so in a way that will be easy to match if we
    have any other config dependencies.
Show 8 quoted lines
>  - Is the conditionally-included file allowed to have its own
>    "include{,If}" instructions? I'm thinking that we should forbid it
>    because, for example, if we had 4 files as follows: A includes B and
>    C includes D, and we include A and C in our main config (in that
>    order), it wouldn't be clear whether B (because A was first included)
>    or C (because we should execute everything at the same depth first)
>    should be executed first. (In this patch, I didn't do anything about
>    includes.)

I'd say that A would expand B at the moment it is parsed, by the usual as-if rule. If it has a recursive includeIf on remotes, then my head may explode. I'd argue that we should refuse to do recursive remote-ifs in that case (though all of this is a consequence of the after-the-fact parsing; I'd much prefer one of the alternatives I gave earlier).

>  - A small one: the exact format of the glob. I probably will treat the
>    URL like a path.

You might want to use the matcher from urlmatch.[ch], which understands things like wildcards. Of course remote "URLs" are not always real syntactically valid URLs, which may make that awkward.

Barring that the usual fnmatch glob is probably our best bet.
Show 15 quoted lines
> @@ -319,10 +331,18 @@ static int include_condition_is_true(const struct config_options *opts,
>  static int git_config_include(const char *var, const char *value, void *data)
>  {
>  	struct config_include_data *inc = data;
> +	const char *remote_name;
> +	size_t remote_name_len;
>  	const char *cond, *key;
>  	size_t cond_len;
>  	int ret;
>  
> +	if (!parse_config_key(var, "remote", &remote_name, &remote_name_len,
> +			      &key) &&
> +	    remote_name &&
> +	    !strcmp(key, "url"))
> +		string_list_append(&inc->remote_urls, value);

So we make a copy of every remote name on the off chance that somebody has an includeIf which looks at it. That feels wasteful, though in practice it's probably not that big a deal.

By doing the config parsing ourselves here we're missing out on any other forms of remote, like .git/remotes. Those are old and not widely used, and I'd be OK with skipping them. But we should clearly document that this is matching remote.*.url, not any of the other mechanisms.

> [...]

I only lightly read the rest of the patch. I didn't see anything obviously wrong, but I think the goal at this point is figuring out the design.

-Peff
Previous: Jonathan TanNext: Jonathan Tan
Message 9 of 87 in “Conditional config includes based on remote URL”
  1. 0/2 Conditional config includes based on remote URLJonathan Tan, Oct 12, 2021
  2. 1/2 config: make git_config_include() staticJonathan Tan, Oct 12, 2021
  3. Jeff KingOct 12, 2021
  4. Junio C HamanoOct 12, 2021
  5. Ævar Arnfjörð BjarmasonOct 13, 2021
  6. Junio C HamanoOct 13, 2021
  7. Jonathan TanOct 13, 2021
  8. 2/2 config: include file if remote URL matches a globJonathan Tan, Oct 12, 2021
  9. Jeff KingOct 12, 2021
  10. Jonathan TanOct 13, 2021
  11. Jeff KingOct 27, 2021
  12. Jonathan TanOct 27, 2021
  13. Junio C HamanoOct 12, 2021
  14. Jonathan TanOct 13, 2021
  15. brian m. carlsonOct 13, 2021
  16. Jonathan TanOct 13, 2021
  17. Jonathan TanOct 18, 2021
  18. Emily ShafferOct 22, 2021
  19. Jeff KingOct 27, 2021
  20. Jonathan TanOct 27, 2021
  21. Jeff KingOct 27, 2021
  22. Ævar Arnfjörð BjarmasonOct 25, 2021
  23. Jonathan TanOct 25, 2021
  24. Ævar Arnfjörð BjarmasonOct 26, 2021
  25. 0/2 Conditional config includes based on remote URLJonathan Tan, Oct 29, 2021
  26. 1/2 config: make git_config_include() staticJonathan Tan, Oct 29, 2021
  27. Emily ShafferNov 5, 2021
  28. 2/2 config: include file if remote URL matches a globJonathan Tan, Oct 29, 2021
  29. Emily ShafferNov 5, 2021
  30. Ævar Arnfjörð BjarmasonNov 6, 2021
  31. Jonathan TanNov 9, 2021
  32. Jonathan TanNov 9, 2021
  33. 0/2 Conditional config includes based on remote URLJonathan Tan, Nov 16, 2021
  34. 1/2 config: make git_config_include() staticJonathan Tan, Nov 16, 2021
  35. 2/2 config: include file if remote URL matches a globJonathan Tan, Nov 16, 2021
  36. Glen ChooNov 22, 2021
  37. Jonathan TanNov 29, 2021
  38. Junio C HamanoNov 23, 2021
  39. Jonathan TanNov 29, 2021
  40. Junio C HamanoDec 1, 2021
  41. Jonathan TanDec 2, 2021
  42. Ævar Arnfjörð BjarmasonNov 23, 2021
  43. Jonathan TanNov 29, 2021
  44. Ævar Arnfjörð BjarmasonNov 29, 2021
  45. 0/2 Conditional config includes based on remote URLJonathan Tan, Nov 29, 2021
  46. 1/2 config: make git_config_include() staticJonathan Tan, Nov 29, 2021
  47. Ævar Arnfjörð BjarmasonNov 29, 2021
  48. Junio C HamanoNov 30, 2021
  49. 2/2 config: include file if remote URL matches a globJonathan Tan, Nov 29, 2021
  50. Junio C HamanoDec 2, 2021
  51. Jonathan TanDec 2, 2021
  52. 0/2 Conditional config includes based on remote URLJonathan Tan, Dec 2, 2021
  53. 1/2 config: make git_config_include() staticJonathan Tan, Dec 2, 2021
  54. 2/2 config: include file if remote URL matches a globJonathan Tan, Dec 2, 2021
  55. Glen ChooDec 6, 2021
  56. Jonathan TanDec 7, 2021
  57. Ævar Arnfjörð BjarmasonDec 6, 2021
  58. Jonathan TanDec 7, 2021
  59. Ævar Arnfjörð BjarmasonDec 7, 2021
  60. Jonathan TanDec 7, 2021
  61. 0/2 Conditional config includes based on remote URLJonathan Tan, Dec 7, 2021
  62. 1/2 config: make git_config_include() staticJonathan Tan, Dec 7, 2021
  63. 2/2 config: include file if remote URL matches a globJonathan Tan, Dec 7, 2021
  64. Glen ChooDec 8, 2021
  65. Jonathan TanDec 9, 2021
  66. Glen ChooDec 8, 2021
  67. Jonathan TanDec 9, 2021
  68. Glen ChooDec 9, 2021
  69. Jonathan TanDec 13, 2021
  70. Junio C HamanoDec 10, 2021
  71. Jonathan TanDec 13, 2021
  72. 0/2 Conditional config includes based on remote URLJonathan Tan, Dec 14, 2021
  73. 1/2 config: make git_config_include() staticJonathan Tan, Dec 14, 2021
  74. 2/2 config: include file if remote URL matches a globJonathan Tan, Dec 14, 2021
  75. Glen ChooDec 16, 2021
  76. Elijah NewrenDec 28, 2021
  77. Jonathan TanJan 10, 2022
  78. Glen ChooDec 16, 2021
  79. Elijah NewrenDec 28, 2021
  80. Glen ChooDec 28, 2021
  81. Jonathan TanJan 10, 2022
  82. Elijah NewrenJan 10, 2022
  83. Scalar vs JGit, was Re: [PATCH v7 0/2] Conditional config includes based on remote URLJohannes Schindelin, Jan 25, 2022
  84. 0/2 Conditional config includes based on remote URLJonathan Tan, Jan 18, 2022
  85. 1/2 config: make git_config_include() staticJonathan Tan, Jan 18, 2022
  86. 2/2 config: include file if remote URL matches a globJonathan Tan, Jan 18, 2022
  87. Elijah NewrenJan 18, 2022

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.