Re: [PATCH v2] config: learn the "onbranch:" includeIf condition
From: Duy Nguyen <hidden>
Date: 2019-06-05 10:08:33
On Sat, Jun 1, 2019 at 2:34 AM Denton Liu [off-list ref] wrote:
Currently, if a user wishes to have individual settings per branch, they are required to manually keep track of the settings in their head and manually set the options on the command-line or change the config at each branch. Teach config the "onbranch:" includeIf condition so that it can conditionally include configuration files if the branch that is checked out in the current worktree matches the pattern given.
Just wondering, anybody wants special settings on detached head (and if so, what syntax should it be)?
quoted hunk ↗ jump to hunk
diff --git a/Documentation/config.txt b/Documentation/config.txt index 7e2a6f61f5..93aa4323c6 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt@@ -144,6 +144,13 @@ refer to linkgit:gitignore[5] for details. For convenience: This is the same as `gitdir` except that matching is done case-insensitively (e.g. on case-insensitive file sytems) +`onbranch`:: + The data that follows the keyword `onbranch:` is taken to be a pattern + with standard globbing wildcards and two additional ones, `**/` and + `/**`, that can match multiple path components. If we are in a worktree + where the name of the branch that is currently checked out matches the + pattern, the include condition is met.
Supporting transforming "foo/" to "foo/**" would be nice (gitdir and gitdir/i do this). Useful when you organize your branches into "subdirs".
quoted hunk ↗ jump to hunk
@@ -264,6 +265,24 @@ static int include_by_gitdir(const struct config_options *opts, return ret; } +static int include_by_branch(const char *cond, size_t cond_len) +{ + int flags; + int ret; + struct strbuf pattern = STRBUF_INIT; + const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, &flags); + const char *shortname; + + if (!refname || !(flags & REF_ISSYMREF) || + !skip_prefix(refname, "refs/heads/", &shortname))
We probably don't even need to check more than !refname. Symbolic refs from HEAD must be refs/heads/* or it's an error (fsck will complain). But it's probably also ok to stay strict.
+ return 0; + + strbuf_add(&pattern, cond, cond_len); + ret = !wildmatch(pattern.buf, shortname, WM_PATHNAME); + strbuf_release(&pattern); + return ret; +}
-- Duy