includeIf: add "worktree" condition for matching working tree path
v5 cover includeIf: add "worktree" condition for matching working tree path
The includeIf mechanism already supports matching on the .git directory path (gitdir) and the currently checked out branch (onbranch). But in multi-worktree setups the .git directory of a linked worktree points into the main repository's .git/worktrees/ area, which makes gitdir patterns cumbersome when one wants to include config based on the working tree's checkout path instead.
Introduce two new condition keywords:
- worktree:<pattern> matches the realpath of the current worktree's working directory against a glob pattern.
- worktree/i:<pattern> is the case-insensitive variant.
Supported pattern features: glob wildcards, **/ and /**, ~ expansion, ./ relative paths, and trailing-/ prefix matching. The condition never matches in a bare repository.
---
Changes in v5:
- Fix Windows CI failure: use ** glob pattern instead of / in the
"worktree without repository" tests, since / as a path pattern is
Unix-specific and does not match Windows paths.
Github CI pass: https://github.com/black-desk/git/actions/runs/26380466288
- Add a test verifying case-sensitive matching by default, with the !CASE_INSENSITIVE_FS prerequisite (suggested by Patrick Steinhardt).
- Link to v4: https://lore.kernel.org/r/[email protected]
Changes in v4:
- Deduplicate the worktree pattern documentation by referencing the
gitdir syntax instead of repeating the full pattern description
(suggested by Patrick Steinhardt).
- Add documentation comparing includeIf "worktree:" with
extensions.worktreeConfig, including a concrete use case example
(suggested by Phillip Wood, Junio C Hamano).
- Add a test verifying that the worktree condition does not match
during early config reading (suggested by Patrick Steinhardt).
- Add tests for the non-repository (nongit) scenario (suggested by
Patrick Steinhardt).
- Add a test for the case-insensitive "worktree/i" variant
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Apply Junio's suggestion.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Add missing signed-off-by lines.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Chen Linxuan (2):
config: refactor include_by_gitdir() into include_by_path()
config: add "worktree" and "worktree/i" includeIf conditions
Documentation/config.adoc | 48 ++++++++++++++++++ config.c | 25 +++++---- t/t1305-config-include.sh | 126 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 188 insertions(+), 11 deletions(-)
---
base-commit: 56a4f3c3a221adf1df9b39da69b8a6890f803157
change-id: 20260401-includeif-worktree-fcb64950dfba
Best regards,
Signature · 1 line
Chen Linxuan <[email protected]>
PATCH v5 1/2 config: refactor include_by_gitdir() into include_by_path()
The include_by_gitdir() function matches the realpath of a given path against a glob pattern, but its interface is tightly coupled to the gitdir condition: it takes a struct config_options *opts and extracts opts->git_dir internally.
Refactor it into a more generic include_by_path() helper that takes a const char *path parameter directly, and update the gitdir and gitdir/i callers to pass opts->git_dir explicitly. No behavior change, just preparing for the addition of a new worktree condition that will reuse the same path-matching logic with a different path.
--- config.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-)
config.c +8 -11
@@ -235,23 +235,20 @@ static int prepare_include_condition_pattern(const struct key_value_info *kvi,return 0 ; } -static int include_by_gitdir (const struct key_value_info *kvi, -const struct config_options *opts, -const char *cond,size_t cond_len,int icase) +static int include_by_path (const struct key_value_info *kvi, +const char *path, +const char *cond,size_t cond_len,int icase) {struct strbuf text =STRBUF_INIT ;struct strbuf pattern =STRBUF_INIT ;size_t prefix;int ret =0 ; -const char *git_dir;int already_tried_absolute =0 ; -if (opts->git_dir) - git_dir = opts->git_dir; -else +if (!path) goto done; -strbuf_realpath (&text, git_dir,1 ); +strbuf_realpath (&text, path,1 );strbuf_add (&pattern, cond, cond_len); ret =prepare_include_condition_pattern (kvi, &pattern, &prefix);if (ret <0 )
@@ -284,7 +281,7 @@ static int include_by_gitdir(const struct key_value_info *kvi,* which'll do the right thing */ strbuf_reset (&text); -strbuf_add_absolute_path (&text, git_dir); +strbuf_add_absolute_path (&text, path); already_tried_absolute =1 ; goto again; }
@@ -400,9 +397,9 @@ static int include_condition_is_true(const struct key_value_info *kvi,const struct config_options *opts = inc->opts;if (skip_prefix_mem (cond, cond_len,"gitdir:" , &cond, &cond_len)) -return include_by_gitdir (kvi, opts, cond, cond_len,0 ); +return include_by_path (kvi, opts->git_dir, cond, cond_len,0 );else if (skip_prefix_mem (cond, cond_len,"gitdir/i:" , &cond, &cond_len)) -return include_by_gitdir (kvi, opts, cond, cond_len,1 ); +return include_by_path (kvi, opts->git_dir, cond, cond_len,1 );else if (skip_prefix_mem (cond, cond_len,"onbranch:" , &cond, &cond_len))return include_by_branch (inc, cond, cond_len); else if (skip_prefix_mem(cond, cond_len, "hasconfig:remote.*.url:", &cond,
Signature · 1 line
2.53.0
PATCH v5 2/2 config: add "worktree" and "worktree/i" includeIf conditions
In reply to Chen LinxuanThe includeIf mechanism already supports matching on the .git directory path (gitdir) and the currently checked out branch (onbranch). But in multi-worktree setups the .git directory of a linked worktree points into the main repository's .git/worktrees/ area, which makes gitdir patterns cumbersome when one wants to include config based on the working tree's checkout path instead.
Introduce two new condition keywords:
- worktree:<pattern> matches the realpath of the current worktree's working directory (i.e. repo_get_work_tree()) against a glob pattern. This is the path returned by git rev-parse --show-toplevel.
- worktree/i:<pattern> is the case-insensitive variant.
The implementation reuses the include_by_path() helper introduced in the previous commit, passing the worktree path in place of the gitdir. The condition never matches in bare repositories (where there is no worktree) or during early config reading (where no repository is available).
Add documentation describing the new conditions, including a comparison with extensions.worktreeConfig. Add tests covering bare repositories, multiple worktrees, symlinked worktree paths, case-sensitive and case-insensitive matching, early config reading, and non-repository scenarios.
--- Documentation/config.adoc | 48 ++++++++++++++++++ config.c | 6 +++ t/t1305-config-include.sh | 126 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 180 insertions(+)
Documentation/config.adoc +48 -0
@@ -146,6 +146,46 @@ 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 systems) +`worktree`:: + The data that follows the keyword `worktree` and a colon is used as a + glob pattern. If the working directory of the current worktree matches + the pattern, the include condition is met. ++ +The worktree location is the path where files are checked out (as returned +by `git rev-parse --show-toplevel`). This is different from `gitdir`, which +matches the `.git` directory path. In a linked worktree, the worktree path +is the directory where that worktree's files are located, not the main +repository's `.git` directory. ++ +The pattern uses the same glob syntax as `gitdir` (including `~/`, `./`, +`**/`, and trailing-`/` prefix matching). This condition will never match +in a bare repository (which has no worktree). ++ +This is useful when you want to apply configuration based on where the +working tree is located on the filesystem. For example, a contributor who +works on the same project both personally and as an employee can use +different `user.name` and `user.email` values depending on which directory +the worktree is checked out under: ++ +---- +[includeIf "worktree:/home/user/work/"] + path = ~/.config/git/work.inc +[includeIf "worktree:/home/user/personal/"] + path = ~/.config/git/personal.inc +---- ++ +While `extensions.worktreeConfig` (see linkgit:git-worktree[1]) also supports +per-worktree configuration, it stores the config inside each repository's +`.git/config.worktree` file and requires running `git config --worktree` +inside each worktree individually. In contrast, `includeIf "worktree:..."` +can be set once in a global or system-level configuration file (e.g. +`~/.config/git/config`) and applies to all repositories at once based on +their worktree location. + +`worktree/i`:: + This is the same as `worktree` except that matching is done + case-insensitively (e.g. on case-insensitive file systems) + `onbranch`:: The data that follows the keyword `onbranch` and a colon is taken to be a pattern with standard globbing wildcards and two additional
@@ -244,6 +284,14 @@ Example [includeIf "gitdir:~/to/group/"] path = /path/to/foo.inc +; include if the worktree is at /path/to/project-build +[includeIf "worktree:/path/to/project-build"] + path = build-config.inc + +; include for all worktrees inside /path/to/group +[includeIf "worktree:/path/to/group/"] + path = group-config.inc + ; relative paths are always relative to the including ; file (if the condition is true); their location is not ; affected by the condition
config.c +6 -0
@@ -400,6 +400,12 @@ static int include_condition_is_true(const struct key_value_info *kvi,return include_by_path (kvi, opts->git_dir, cond, cond_len,0 ); else if (skip_prefix_mem(cond, cond_len, "gitdir/i:", &cond, &cond_len)) return include_by_path(kvi, opts->git_dir, cond, cond_len, 1); + else if (skip_prefix_mem(cond, cond_len, "worktree:", &cond, &cond_len)) +return include_by_path (kvi, inc->repo ?repo_get_work_tree (inc->repo) :NULL , + cond, cond_len,0 ); + else if (skip_prefix_mem(cond, cond_len, "worktree/i:", &cond, &cond_len)) + return include_by_path(kvi, inc->repo ? repo_get_work_tree(inc->repo) : NULL, + cond, cond_len, 1); else if (skip_prefix_mem(cond, cond_len, "onbranch:", &cond, &cond_len))return include_by_branch (inc, cond, cond_len); else if (skip_prefix_mem(cond, cond_len, "hasconfig:remote.*.url:", &cond,
t/t1305-config-include.sh +126 -0
130 lines not on this page · raw message
Signature · 1 line
2.53.0
Chen Linxuan via B4 Relay <[email protected]>
Quoted 4 lines · writes:
Changes in v5:
- Fix Windows CI failure: use**glob pattern instead of/in the "worktree without repository" tests, since/as a path pattern is Unix-specific and does not match Windows paths.
Would it have worked if you used something like "[/\\].path", instead of "/.path", to cover directory delimiters for both systems?
I am not asking to make further changes. I am trying to understand what the extent of the problem was.
There are tons of [includeIf] that spells path patterns with the assumption that '/' can be used as the directory separator, like these lines taken from <master:t/t1305-config-include.sh>:
echo "[includeIf \"gitdir:foo/\"]path=bar" >>.git/config &&
echo "[includeIf \"gitdir:~/foo/\"]path=bar2" >>.git/config &&
echo "[includeIf \"gitdir:**/foo/**\"]path=bar3" >>.git/config &&
echo "[includeIf \"gitdir:./foo/.git\"]path=bar4" >>.gitconfig &&
echo "[includeIf \"gitdir/i:FOO/\"]path=bar5" >>.git/config &&
echo "[includeIf \"gitdir:foo/\"]path=bar6" >>.git/config &&
[includeIf "gitdir:**/foo/**/bar/**"]
echo "[includeIf \"gitdir:~/foo/\"]path=bar2" >>.git/config &&
echo "[includeIf \"gitdir:./foo/.git\"]path=bar4" >home/.gitconfig &&
echo "[includeIf \"gitdir:bar/\"]path=bar7" >>.git/config &&
echo "[includeIf \"gitdir/i:BAR/\"]path=bar8" >>.git/config &&
echo "[includeIf \"onbranch:foo-branch\"]path=bar9" >>.git/config &&
echo "[includeIf \"onbranch:?oo-*/**\"]path=bar10" >>.git/config &&
echo "[includeIf \"onbranch:foo-dir/\"]path=bar11" >>.git/config &&and there is none, as far as I can tell, that uses a backslash as directory separator.
Shoudln't the new worktree location code normalize the pathname before doing a pattern matching so that it would allow '/'-separated path pattern to match?
FWIW, here is the diff between v4 and v5.
t/t1305-config-include.sh | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
t/t1305-config-include.sh +15 -2
@@ -462,6 +462,19 @@ test_expect_success SYMLINKS 'conditional include, worktree resolves symlinks' ' ) ' +test_expect_success !CASE_INSENSITIVE_FS 'conditional include, worktree, case sensitive' ' + git init wt-case && + ( + cd wt-case && + test_commit initial && + wt_path="$(pwd)" && + wt_upper=$(echo "$wt_path" | tr a-z A-Z) && + echo "[includeIf \"worktree:$wt_upper\"]path=case-inc" >>.git/config && + echo "[test]wtcase=1" >.git/case-inc && + test_must_fail git config test.wtcase + ) +' + test_expect_success 'conditional include, worktree, icase' ' git init wt-icase && (
@@ -495,7 +508,7 @@ test_expect_success 'conditional include, worktree does not match in early confi test_expect_success 'conditional include, worktree without repository' ' test_when_finished "rm -f .gitconfig config.inc" && - git config set -f .gitconfig "includeIf.worktree:/.path" config.inc && + git config set -f .gitconfig "includeIf.worktree:**.path" config.inc && git config set -f config.inc foo.bar baz && git config get foo.bar && test_must_fail nongit git config get foo.bar
@@ -503,7 +516,7 @@ test_expect_success 'conditional include, worktree without repository' ' test_expect_success 'conditional include, worktree without repository but explicit nonexistent Git directory' ' test_when_finished "rm -f .gitconfig config.inc" && - git config set -f .gitconfig "includeIf.worktree:/.path" config.inc && + git config set -f .gitconfig "includeIf.worktree:**.path" config.inc && git config set -f config.inc foo.bar baz && git config get foo.bar && test_must_fail nongit git --git-dir=nonexistent config get foo.bar
Quoted 9 lines · Junio C Hamano
Chen Linxuan via B4 Relay <[email protected]>
Quoted 4 lines · writes:
Changes in v5:
- Fix Windows CI failure: use**glob pattern instead of/in the "worktree without repository" tests, since/as a path pattern is Unix-specific and does not match Windows paths.
Quoted 5 lines
Would it have worked if you used something like "[/\\].path", instead of "/.path", to cover directory delimiters for both systems?
I am not asking to make further changes. I am trying to understand what the extent of the problem was.
The root cause is that on Windows,strbuf_realpath() returns paths with a drive letter prefix (e.g. D:/a/git/...), which does not start with /.
Here is the trace output from the Windows CI [1]:
include_by_path: text='D:/a/git/git/t/trash directory.t1305-config-include', pattern='/**', prefix=0
The pattern worktree:/ becomes /** after add_trailing_starstar_for_dir(). Then wildmatch("/**", "D:/a/git/...", WM_PATHNAME) fails because the text does not start with /.
[1] https://github.com/black-desk/git/actions/runs/26391768962/job/77683708185
Quoted 13 lines · Chen Linxuan
Quoted 12 lines · Junio C Hamano
Chen Linxuan via B4 Relay <[email protected]>
Quoted 4 lines · writes:
Changes in v5:
- Fix Windows CI failure: use**glob pattern instead of/in the "worktree without repository" tests, since/as a path pattern is Unix-specific and does not match Windows paths.Would it have worked if you used something like "[/\\].path", instead of "/.path", to cover directory delimiters for both systems?
Quoted 6 lines
Quoted 2 lines
I am not asking to make further changes. I am trying to understand what the extent of the problem was.
The root cause is that on Windows,
strbuf_realpath()returns paths with a drive letter prefix (e.g. D:/a/git/...), which does not start with /.
Ahh, OK, so the "Changes in v5" description was misleading.
This exchange suggests that the use of **/.path in the test deserves some in-code comment to explain why we use such an unusual and loose construct.
Thanks.