Re: [RFC] ident: support per-path configs by matching the path against a pattern
- From
Jeff King <peff@peff.net>
- Date
- Jul 10, 2015, 16:46 UTC
- Message-ID
- <20150710164652.GA30113@peff.net>
- In-Reply-To
- <20150710154308.GA29395@peff.net>
On Fri, Jul 10, 2015 at 11:43:08AM -0400, Jeff King wrote:
Show 10 quoted lines
> But something like:
>
> [include "gitdir:bar"]
> path = foo
>
> could do so only when the "gitdir:bar" conditional is satisfied (where
> that is just a syntax I made up to mean fnmatch("bar", $GIT_DIR)). So
> like user.<pattern>.*, we still put our section-specific hack into one
> special section, but that one place is capable of chaining to multiple
> other config keys. :)Here's a sketch if anybody is inclined to pick it up and run with it. Note that I did not think too hard about little things like the de-anchoring.
---
diff --git a/config.c b/config.c index 29fa012..47b01f0 100644 --- a/config.c +++ b/config.c @@ -139,9 +139,45 @@ static int handle_path_include(const char *path, struct config_include_data *inc return ret; } +static int include_condition_is_true(const char *cond, int cond_len) +{ + const char *value; + + /* no condition (i.e., "include.path") is always true */ + if (!cond) + return 1; + + /* + * It's OK to run over cond_len in our checks here, as that just pushes + * us past the final ".", which cannot match any of our prefixes. + */ + if (skip_prefix(cond, "gitdir:", &value)) { + struct strbuf text = STRBUF_INIT; + struct strbuf pattern = STRBUF_INIT; + int ret; + + strbuf_add_absolute_path(&text, get_git_dir()); + + /* de-anchor match for convenience */ + strbuf_addstr(&pattern, "**"); + strbuf_add(&pattern, value, cond_len - (value - cond)); + strbuf_addstr(&pattern, "**"); + + ret = !wildmatch(pattern.buf, text.buf, 0, NULL); + strbuf_release(&pattern); + strbuf_release(&text); + return ret; + } + + /* unknown conditionals are always false */ + return 0; +} + int git_config_include(const char *var, const char *value, void *data) { struct config_include_data *inc = data; + const char *cond, *key; + int cond_len; int ret; /* @@ -152,8 +188,12 @@ int git_config_include(const char *var, const char *value, void *data) if (ret < 0) return ret; - if (!strcmp(var, "include.path")) - ret = handle_path_include(value, inc); + if (!parse_config_key(var, "include", &cond, &cond_len, &key) && + include_condition_is_true(cond, cond_len)) { + if (!strcmp(key, "path")) + ret = handle_path_include(value, inc); + /* else we do not know about this type of include; ignore */ + } return ret; }