Re: [PATCH v2] config: learn the "onbranch:" includeIf condition
- From
Duy Nguyen <pclouds@gmail.com>
- Date
- Jun 5, 2019, 10:08 UTC
- Message-ID
- <CACsJy8BaYhcnj_nofxmDB3txBCgSe0aDeR+LYPNqkHYg4OroHg@mail.gmail.com>
- In-Reply-To
- <3573995264441c50ea9529b3ee926d1ed3ec4ac8.1559330905.git.liu.denton@gmail.com>
On Sat, Jun 1, 2019 at 2:34 AM Denton Liu <liu.denton@gmail.com> wrote:
Show 9 quoted lines
> > 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)?
Show 14 quoted lines
> 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".
Show 14 quoted lines
> @@ -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.
Show 7 quoted lines
> + return 0; > + > + strbuf_add(&pattern, cond, cond_len); > + ret = !wildmatch(pattern.buf, shortname, WM_PATHNAME); > + strbuf_release(&pattern); > + return ret; > +}
-- Duy