{"thread":{"id":"65425","subject":"[PATCH v3 1/2] config: refactor include_by_gitdir() into include_by_path()","startedAt":"2026-04-03T07:02:59Z","lastAt":"2026-05-13T05:57:05Z","messageCount":11,"participants":["Chen Linxuan via B4 Relay","Junio C Hamano","Patrick Steinhardt","Phillip Wood","Chen Linxuan"],"isPatch":true,"patchVersion":3,"patchTotal":2},"messages":[{"id":"540838","messageId":"20260403-includeif-worktree-v3-1-109ce5782b03@black-desk.cn","threadId":"65425","inReplyTo":"20260403-includeif-worktree-v3-0-109ce5782b03@black-desk.cn","subject":"[PATCH v3 1/2] config: refactor include_by_gitdir() into include_by_path()","fromName":"Chen Linxuan via B4 Relay","fromEmail":"devnull+me.black-desk.cn@kernel.org","sentAt":"2026-04-03T07:02:28Z","receivedAt":"2026-04-03T07:02:59Z","isPatch":true,"body":"From: Chen Linxuan <me@black-desk.cn>\n\nThe include_by_gitdir() function matches the realpath of a given\npath against a glob pattern, but its interface is tightly coupled to\nthe gitdir condition: it takes a struct config_options *opts and\nextracts opts->git_dir internally.\n\nRefactor it into a more generic include_by_path() helper that takes\na const char *path parameter directly, and update the gitdir and\ngitdir/i callers to pass opts->git_dir explicitly.  No behavior\nchange, just preparing for the addition of a new worktree condition\nthat will reuse the same path-matching logic with a different path.\n\nSigned-off-by: Chen Linxuan <me@black-desk.cn>\n---\n config.c | 19 ++++++++-----------\n 1 file changed, 8 insertions(+), 11 deletions(-)\n\ndiff --git a/config.c b/config.c\nindex 156f2a24fa00..7d5dae0e8450 100644\n--- a/config.c\n+++ b/config.c\n@@ -235,23 +235,20 @@ static int prepare_include_condition_pattern(const struct key_value_info *kvi,\n \treturn 0;\n }\n \n-static int include_by_gitdir(const struct key_value_info *kvi,\n-\t\t\t     const struct config_options *opts,\n-\t\t\t     const char *cond, size_t cond_len, int icase)\n+static int include_by_path(const struct key_value_info *kvi,\n+\t\t\t   const char *path,\n+\t\t\t   const char *cond, size_t cond_len, int icase)\n {\n \tstruct strbuf text = STRBUF_INIT;\n \tstruct strbuf pattern = STRBUF_INIT;\n \tsize_t prefix;\n \tint ret = 0;\n-\tconst char *git_dir;\n \tint already_tried_absolute = 0;\n \n-\tif (opts->git_dir)\n-\t\tgit_dir = opts->git_dir;\n-\telse\n+\tif (!path)\n \t\tgoto done;\n \n-\tstrbuf_realpath(&text, git_dir, 1);\n+\tstrbuf_realpath(&text, path, 1);\n \tstrbuf_add(&pattern, cond, cond_len);\n \tret = prepare_include_condition_pattern(kvi, &pattern, &prefix);\n \tif (ret < 0)\n@@ -284,7 +281,7 @@ static int include_by_gitdir(const struct key_value_info *kvi,\n \t\t * which'll do the right thing\n \t\t */\n \t\tstrbuf_reset(&text);\n-\t\tstrbuf_add_absolute_path(&text, git_dir);\n+\t\tstrbuf_add_absolute_path(&text, path);\n \t\talready_tried_absolute = 1;\n \t\tgoto again;\n \t}\n@@ -400,9 +397,9 @@ static int include_condition_is_true(const struct key_value_info *kvi,\n \tconst struct config_options *opts = inc->opts;\n \n \tif (skip_prefix_mem(cond, cond_len, \"gitdir:\", &cond, &cond_len))\n-\t\treturn include_by_gitdir(kvi, opts, cond, cond_len, 0);\n+\t\treturn include_by_path(kvi, opts->git_dir, cond, cond_len, 0);\n \telse if (skip_prefix_mem(cond, cond_len, \"gitdir/i:\", &cond, &cond_len))\n-\t\treturn include_by_gitdir(kvi, opts, cond, cond_len, 1);\n+\t\treturn include_by_path(kvi, opts->git_dir, cond, cond_len, 1);\n \telse if (skip_prefix_mem(cond, cond_len, \"onbranch:\", &cond, &cond_len))\n \t\treturn include_by_branch(inc, cond, cond_len);\n \telse if (skip_prefix_mem(cond, cond_len, \"hasconfig:remote.*.url:\", &cond,\n\n-- \n2.53.0\n\n\n"},{"id":"540839","messageId":"20260403-includeif-worktree-v3-0-109ce5782b03@black-desk.cn","threadId":"65425","inReplyTo":null,"subject":"[PATCH v3 0/2] includeIf: add \"worktree\" condition for matching working tree path","fromName":"Chen Linxuan via B4 Relay","fromEmail":"devnull+me.black-desk.cn@kernel.org","sentAt":"2026-04-03T07:02:27Z","receivedAt":"2026-04-03T07:02:59Z","isPatch":true,"body":"The `includeIf` mechanism already supports matching on the `.git`\ndirectory path (`gitdir`) and the currently checked out branch\n(`onbranch`).  But in multi-worktree setups the `.git` directory of a\nlinked worktree points into the main repository's `.git/worktrees/`\narea, which makes `gitdir` patterns cumbersome when one wants to\ninclude config based on the working tree's checkout path instead.\n\nIntroduce two new condition keywords:\n\n  - `worktree:<pattern>` matches the realpath of the current worktree's\n    working directory against a glob pattern.\n  - `worktree/i:<pattern>` is the case-insensitive variant.\n\nSupported pattern features: glob wildcards, `**/` and `/**`, `~`\nexpansion, `./` relative paths, and trailing-`/` prefix matching.\nThe condition never matches in a bare repository.\n\nSigned-off-by: Chen Linxuan <me@black-desk.cn>\n---\nChanges in v3:\n- Apply Junio's suggestion.\n- Link to v2: https://lore.kernel.org/r/20260402-includeif-worktree-v2-0-36e339b898d7@black-desk.cn\n\nChanges in v2:\n\n- Add missing signed-off-by lines.\n- Link to v1: https://lore.kernel.org/r/20260401-includeif-worktree-v1-0-906db69f2c79@black-desk.cn\n\n---\nChen Linxuan (2):\n      config: refactor include_by_gitdir() into include_by_path()\n      config: add \"worktree\" and \"worktree/i\" includeIf conditions\n\n Documentation/config.adoc | 50 +++++++++++++++++++++++++++++++++++\n config.c                  | 25 ++++++++++--------\n t/t1305-config-include.sh | 66 +++++++++++++++++++++++++++++++++++++++++++++++\n 3 files changed, 130 insertions(+), 11 deletions(-)\n---\nbase-commit: 270e10ad6dda3379ea0da7efd11e4fbf2cd7a325\nchange-id: 20260401-includeif-worktree-fcb64950dfba\n\nBest regards,\n-- \nChen Linxuan <me@black-desk.cn>\n\n\n"},{"id":"540840","messageId":"20260403-includeif-worktree-v3-2-109ce5782b03@black-desk.cn","threadId":"65425","inReplyTo":"20260403-includeif-worktree-v3-0-109ce5782b03@black-desk.cn","subject":"[PATCH v3 2/2] config: add \"worktree\" and \"worktree/i\" includeIf conditions","fromName":"Chen Linxuan via B4 Relay","fromEmail":"devnull+me.black-desk.cn@kernel.org","sentAt":"2026-04-03T07:02:29Z","receivedAt":"2026-04-03T07:02:59Z","isPatch":true,"body":"From: Chen Linxuan <me@black-desk.cn>\n\nThe includeIf mechanism already supports matching on the .git\ndirectory path (gitdir) and the currently checked out branch\n(onbranch).  But in multi-worktree setups the .git directory of a\nlinked worktree points into the main repository's .git/worktrees/\narea, which makes gitdir patterns cumbersome when one wants to\ninclude config based on the working tree's checkout path instead.\n\nIntroduce two new condition keywords:\n\n  - worktree:<pattern> matches the realpath of the current worktree's\n    working directory (i.e. repo_get_work_tree()) against a glob\n    pattern.  This is the path returned by git rev-parse\n    --show-toplevel.\n\n  - worktree/i:<pattern> is the case-insensitive variant.\n\nThe implementation reuses the include_by_path() helper introduced in\nthe previous commit, passing the worktree path in place of the\ngitdir.  The condition never matches in bare repositories (where\nthere is no worktree) or during early config reading (where no\nrepository is available).\n\nAdd documentation describing the new conditions and their supported\npattern features (glob wildcards, **/ and /**, ~ expansion, ./\nrelative paths, and trailing-/ prefix matching).  Add tests covering\nbare repositories, multiple worktrees, and symlinked worktree paths.\n\nSigned-off-by: Chen Linxuan <me@black-desk.cn>\n---\n Documentation/config.adoc | 50 +++++++++++++++++++++++++++++++++++\n config.c                  |  6 +++++\n t/t1305-config-include.sh | 66 +++++++++++++++++++++++++++++++++++++++++++++++\n 3 files changed, 122 insertions(+)\n\ndiff --git a/Documentation/config.adoc b/Documentation/config.adoc\nindex 62eebe7c5450..a4f3ec905098 100644\n--- a/Documentation/config.adoc\n+++ b/Documentation/config.adoc\n@@ -146,6 +146,48 @@ refer to linkgit:gitignore[5] for details. For convenience:\n \tThis is the same as `gitdir` except that matching is done\n \tcase-insensitively (e.g. on case-insensitive file systems)\n \n+`worktree`::\n+\tThe data that follows the keyword `worktree` and a colon is used as a\n+\tglob pattern. If the working directory of the current worktree matches\n+\tthe pattern, the include condition is met.\n++\n+The worktree location is the path where files are checked out (as returned\n+by `git rev-parse --show-toplevel`). This is different from `gitdir`, which\n+matches the `.git` directory path. In a linked worktree, the worktree path\n+is the directory where that worktree's files are located, not the main\n+repository's `.git` directory.\n++\n+The pattern can contain standard globbing wildcards and two additional\n+ones, `**/` and `/**`, that can match multiple path components. Please\n+refer to linkgit:gitignore[5] for details. For convenience:\n+\n+ * If the pattern starts with `~/`, `~` will be substituted with the\n+   content of the environment variable `HOME`.\n+\n+ * If the pattern starts with `./`, it is replaced with the directory\n+   containing the current config file.\n+\n+ * If the pattern does not start with either `~/`, `./` or `/`, `**/`\n+   will be automatically prepended. For example, the pattern `foo/bar`\n+   becomes `**/foo/bar` and would match `/any/path/to/foo/bar`.\n+\n+ * If the pattern ends with `/`, `**` will be automatically added. For\n+   example, the pattern `foo/` becomes `foo/**`. In other words, it\n+   matches \"foo\" and everything inside, recursively.\n++\n+This condition will never match in a bare repository (which has no worktree).\n++\n+This is useful when you need to use different `user.name`, `user.email`, or\n+GPG keys in different worktrees of the same repository. While\n+`extensions.worktreeConfig` also allows per-worktree configuration, it\n+requires changes inside each repository. This condition can be set in the\n+user's global configuration file (e.g. `~/.config/git/config`) and applies\n+to multiple repositories at once.\n+\n+`worktree/i`::\n+\tThis is the same as `worktree` except that matching is done\n+\tcase-insensitively (e.g. on case-insensitive file systems)\n+\n `onbranch`::\n \tThe data that follows the keyword `onbranch` and a colon is taken to be a\n \tpattern with standard globbing wildcards and two additional\n@@ -244,6 +286,14 @@ Example\n [includeIf \"gitdir:~/to/group/\"]\n \tpath = /path/to/foo.inc\n \n+; include if the worktree is at /path/to/project-build\n+[includeIf \"worktree:/path/to/project-build\"]\n+\tpath = build-config.inc\n+\n+; include for all worktrees inside /path/to/group\n+[includeIf \"worktree:/path/to/group/\"]\n+\tpath = group-config.inc\n+\n ; relative paths are always relative to the including\n ; file (if the condition is true); their location is not\n ; affected by the condition\ndiff --git a/config.c b/config.c\nindex 7d5dae0e8450..6d0c2d0725e4 100644\n--- a/config.c\n+++ b/config.c\n@@ -400,6 +400,12 @@ static int include_condition_is_true(const struct key_value_info *kvi,\n \t\treturn include_by_path(kvi, opts->git_dir, cond, cond_len, 0);\n \telse if (skip_prefix_mem(cond, cond_len, \"gitdir/i:\", &cond, &cond_len))\n \t\treturn include_by_path(kvi, opts->git_dir, cond, cond_len, 1);\n+\telse if (skip_prefix_mem(cond, cond_len, \"worktree:\", &cond, &cond_len))\n+\t\treturn include_by_path(kvi, inc->repo ? repo_get_work_tree(inc->repo) : NULL,\n+\t\t\t\t       cond, cond_len, 0);\n+\telse if (skip_prefix_mem(cond, cond_len, \"worktree/i:\", &cond, &cond_len))\n+\t\treturn include_by_path(kvi, inc->repo ? repo_get_work_tree(inc->repo) : NULL,\n+\t\t\t\t       cond, cond_len, 1);\n \telse if (skip_prefix_mem(cond, cond_len, \"onbranch:\", &cond, &cond_len))\n \t\treturn include_by_branch(inc, cond, cond_len);\n \telse if (skip_prefix_mem(cond, cond_len, \"hasconfig:remote.*.url:\", &cond,\ndiff --git a/t/t1305-config-include.sh b/t/t1305-config-include.sh\nindex 6e51f892f320..8a5ba4b884d3 100755\n--- a/t/t1305-config-include.sh\n+++ b/t/t1305-config-include.sh\n@@ -396,4 +396,70 @@ test_expect_success 'onbranch without repository but explicit nonexistent Git di\n \ttest_must_fail nongit git --git-dir=nonexistent config get foo.bar\n '\n \n+# worktree: conditional include tests\n+\n+test_expect_success 'conditional include, worktree bare repo' '\n+\tgit init --bare wt-bare &&\n+\t(\n+\t\tcd wt-bare &&\n+\t\techo \"[includeIf \\\"worktree:/\\\"]path=bar-bare\" >>config &&\n+\t\techo \"[test]wtbare=1\" >bar-bare &&\n+\t\ttest_must_fail git config test.wtbare\n+\t)\n+'\n+\n+test_expect_success 'conditional include, worktree multiple worktrees' '\n+\tgit init wt-multi &&\n+\t(\n+\t\tcd wt-multi &&\n+\t\ttest_commit initial &&\n+\t\tgit worktree add -b linked-branch ../wt-linked HEAD &&\n+\t\tgit worktree add -b prefix-branch ../wt-prefix/linked HEAD\n+\t) &&\n+\twt_main=\"$(cd wt-multi && pwd)\" &&\n+\twt_linked=\"$(cd wt-linked && pwd)\" &&\n+\twt_prefix_parent=\"$(cd wt-prefix && pwd)\" &&\n+\tcat >>wt-multi/.git/config <<-EOF &&\n+\t[includeIf \"worktree:$wt_main\"]\n+\t\tpath = main-config\n+\t[includeIf \"worktree:$wt_linked\"]\n+\t\tpath = linked-config\n+\t[includeIf \"worktree:$wt_prefix_parent/\"]\n+\t\tpath = prefix-config\n+\tEOF\n+\techo \"[test]mainvar=main\" >wt-multi/.git/main-config &&\n+\techo \"[test]linkedvar=linked\" >wt-multi/.git/linked-config &&\n+\techo \"[test]prefixvar=prefix\" >wt-multi/.git/prefix-config &&\n+\techo main >expect &&\n+\tgit -C wt-multi config test.mainvar >actual &&\n+\ttest_cmp expect actual &&\n+\ttest_must_fail git -C wt-multi config test.linkedvar &&\n+\ttest_must_fail git -C wt-multi config test.prefixvar &&\n+\techo linked >expect &&\n+\tgit -C wt-linked config test.linkedvar >actual &&\n+\ttest_cmp expect actual &&\n+\ttest_must_fail git -C wt-linked config test.mainvar &&\n+\ttest_must_fail git -C wt-linked config test.prefixvar &&\n+\techo prefix >expect &&\n+\tgit -C wt-prefix/linked config test.prefixvar >actual &&\n+\ttest_cmp expect actual &&\n+\ttest_must_fail git -C wt-prefix/linked config test.mainvar &&\n+\ttest_must_fail git -C wt-prefix/linked config test.linkedvar\n+'\n+\n+test_expect_success SYMLINKS 'conditional include, worktree resolves symlinks' '\n+\tmkdir real-wt &&\n+\tln -s real-wt link-wt &&\n+\tgit init link-wt/repo &&\n+\t(\n+\t\tcd link-wt/repo &&\n+\t\t# repo->worktree resolves symlinks, so use real path in pattern\n+\t\techo \"[includeIf \\\"worktree:**/real-wt/repo\\\"]path=bar-link\" >>.git/config &&\n+\t\techo \"[test]wtlink=2\" >.git/bar-link &&\n+\t\techo 2 >expect &&\n+\t\tgit config test.wtlink >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n test_done\n\n-- \n2.53.0\n\n\n"},{"id":"543144","messageId":"xmqq4ikdnn8g.fsf@gitster.g","threadId":"65425","inReplyTo":"20260403-includeif-worktree-v3-0-109ce5782b03@black-desk.cn","subject":"Re: [PATCH v3 0/2] includeIf: add \"worktree\" condition for matching working tree path","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-05-12T06:41:35Z","receivedAt":"2026-05-12T06:41:43Z","isPatch":true,"body":"Chen Linxuan via B4 Relay <devnull+me.black-desk.cn@kernel.org>\nwrites:\n\n> The `includeIf` mechanism already supports matching on the `.git`\n> directory path (`gitdir`) and the currently checked out branch\n> (`onbranch`).  But in multi-worktree setups the `.git` directory of a\n> linked worktree points into the main repository's `.git/worktrees/`\n> area, which makes `gitdir` patterns cumbersome when one wants to\n> include config based on the working tree's checkout path instead.\n>\n> Introduce two new condition keywords:\n>\n>   - `worktree:<pattern>` matches the realpath of the current worktree's\n>     working directory against a glob pattern.\n>   - `worktree/i:<pattern>` is the case-insensitive variant.\n>\n> Supported pattern features: glob wildcards, `**/` and `/**`, `~`\n> expansion, `./` relative paths, and trailing-`/` prefix matching.\n> The condition never matches in a bare repository.\n>\n> Signed-off-by: Chen Linxuan <me@black-desk.cn>\n> ---\n> Changes in v3:\n> - Apply Junio's suggestion.\n> - Link to v2: https://lore.kernel.org/r/20260402-includeif-worktree-v2-0-36e339b898d7@black-desk.cn\n>\n> Changes in v2:\n>\n> - Add missing signed-off-by lines.\n> - Link to v1: https://lore.kernel.org/r/20260401-includeif-worktree-v1-0-906db69f2c79@black-desk.cn\n\nCan we have a volunteer (or two) to review these patches?  The\nfeature sounds like a worthwhile thing to have, and the code on the\nsurface looks OK-ish to me, but I am not fully back up to speed and\ncan use an extra set of eyeballs.\n\nThanks.\n\n>\n> ---\n> Chen Linxuan (2):\n>       config: refactor include_by_gitdir() into include_by_path()\n>       config: add \"worktree\" and \"worktree/i\" includeIf conditions\n>\n>  Documentation/config.adoc | 50 +++++++++++++++++++++++++++++++++++\n>  config.c                  | 25 ++++++++++--------\n>  t/t1305-config-include.sh | 66 +++++++++++++++++++++++++++++++++++++++++++++++\n>  3 files changed, 130 insertions(+), 11 deletions(-)\n> ---\n> base-commit: 270e10ad6dda3379ea0da7efd11e4fbf2cd7a325\n> change-id: 20260401-includeif-worktree-fcb64950dfba\n>\n> Best regards,\n"},{"id":"543148","messageId":"agLTNbrXvwLifhYi@pks.im","threadId":"65425","inReplyTo":"20260403-includeif-worktree-v3-1-109ce5782b03@black-desk.cn","subject":"Re: [PATCH v3 1/2] config: refactor include_by_gitdir() into include_by_path()","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-05-12T07:13:57Z","receivedAt":"2026-05-12T07:14:06Z","isPatch":true,"body":"On Fri, Apr 03, 2026 at 03:02:28PM +0800, Chen Linxuan via B4 Relay wrote:\n> From: Chen Linxuan <me@black-desk.cn>\n> \n> The include_by_gitdir() function matches the realpath of a given\n> path against a glob pattern, but its interface is tightly coupled to\n> the gitdir condition: it takes a struct config_options *opts and\n> extracts opts->git_dir internally.\n> \n> Refactor it into a more generic include_by_path() helper that takes\n> a const char *path parameter directly, and update the gitdir and\n> gitdir/i callers to pass opts->git_dir explicitly.  No behavior\n> change, just preparing for the addition of a new worktree condition\n> that will reuse the same path-matching logic with a different path.\n\nGood preparatory step.\n\nPatrick\n"},{"id":"543147","messageId":"agLTO0amktCWMsiE@pks.im","threadId":"65425","inReplyTo":"20260403-includeif-worktree-v3-2-109ce5782b03@black-desk.cn","subject":"Re: [PATCH v3 2/2] config: add \"worktree\" and \"worktree/i\" includeIf conditions","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-05-12T07:14:03Z","receivedAt":"2026-05-12T07:14:09Z","isPatch":true,"body":"On Fri, Apr 03, 2026 at 03:02:29PM +0800, Chen Linxuan via B4 Relay wrote:\n> From: Chen Linxuan <me@black-desk.cn>\n> \n> The includeIf mechanism already supports matching on the .git\n> directory path (gitdir) and the currently checked out branch\n> (onbranch).  But in multi-worktree setups the .git directory of a\n> linked worktree points into the main repository's .git/worktrees/\n> area, which makes gitdir patterns cumbersome when one wants to\n> include config based on the working tree's checkout path instead.\n> \n> Introduce two new condition keywords:\n> \n>   - worktree:<pattern> matches the realpath of the current worktree's\n>     working directory (i.e. repo_get_work_tree()) against a glob\n>     pattern.  This is the path returned by git rev-parse\n>     --show-toplevel.\n> \n>   - worktree/i:<pattern> is the case-insensitive variant.\n\nSeems sensible.\n\n> The implementation reuses the include_by_path() helper introduced in\n> the previous commit, passing the worktree path in place of the\n> gitdir.  The condition never matches in bare repositories (where\n> there is no worktree) or during early config reading (where no\n> repository is available).\n\nRight. This is because `repo_get_work_tree()` would return a NULL\npointer in these cases, and `include_by_path()` exits early in that\ncase.\n\n> diff --git a/Documentation/config.adoc b/Documentation/config.adoc\n> index 62eebe7c5450..a4f3ec905098 100644\n> --- a/Documentation/config.adoc\n> +++ b/Documentation/config.adoc\n> @@ -146,6 +146,48 @@ refer to linkgit:gitignore[5] for details. For convenience:\n>  \tThis is the same as `gitdir` except that matching is done\n>  \tcase-insensitively (e.g. on case-insensitive file systems)\n>  \n> +`worktree`::\n> +\tThe data that follows the keyword `worktree` and a colon is used as a\n> +\tglob pattern. If the working directory of the current worktree matches\n> +\tthe pattern, the include condition is met.\n> ++\n> +The worktree location is the path where files are checked out (as returned\n> +by `git rev-parse --show-toplevel`). This is different from `gitdir`, which\n> +matches the `.git` directory path. In a linked worktree, the worktree path\n> +is the directory where that worktree's files are located, not the main\n> +repository's `.git` directory.\n> ++\n> +The pattern can contain standard globbing wildcards and two additional\n> +ones, `**/` and `/**`, that can match multiple path components. Please\n> +refer to linkgit:gitignore[5] for details. For convenience:\n> +\n> + * If the pattern starts with `~/`, `~` will be substituted with the\n> +   content of the environment variable `HOME`.\n> +\n> + * If the pattern starts with `./`, it is replaced with the directory\n> +   containing the current config file.\n> +\n> + * If the pattern does not start with either `~/`, `./` or `/`, `**/`\n> +   will be automatically prepended. For example, the pattern `foo/bar`\n> +   becomes `**/foo/bar` and would match `/any/path/to/foo/bar`.\n> +\n> + * If the pattern ends with `/`, `**` will be automatically added. For\n> +   example, the pattern `foo/` becomes `foo/**`. In other words, it\n> +   matches \"foo\" and everything inside, recursively.\n\nThis whole listing here is the exact same as we have for the `gitdir`\ncondition. Can we maybe deduplicate these into a common section?\n\n> diff --git a/config.c b/config.c\n> index 7d5dae0e8450..6d0c2d0725e4 100644\n> --- a/config.c\n> +++ b/config.c\n> @@ -400,6 +400,12 @@ static int include_condition_is_true(const struct key_value_info *kvi,\n>  \t\treturn include_by_path(kvi, opts->git_dir, cond, cond_len, 0);\n>  \telse if (skip_prefix_mem(cond, cond_len, \"gitdir/i:\", &cond, &cond_len))\n>  \t\treturn include_by_path(kvi, opts->git_dir, cond, cond_len, 1);\n> +\telse if (skip_prefix_mem(cond, cond_len, \"worktree:\", &cond, &cond_len))\n> +\t\treturn include_by_path(kvi, inc->repo ? repo_get_work_tree(inc->repo) : NULL,\n> +\t\t\t\t       cond, cond_len, 0);\n> +\telse if (skip_prefix_mem(cond, cond_len, \"worktree/i:\", &cond, &cond_len))\n> +\t\treturn include_by_path(kvi, inc->repo ? repo_get_work_tree(inc->repo) : NULL,\n> +\t\t\t\t       cond, cond_len, 1);\n>  \telse if (skip_prefix_mem(cond, cond_len, \"onbranch:\", &cond, &cond_len))\n>  \t\treturn include_by_branch(inc, cond, cond_len);\n>  \telse if (skip_prefix_mem(cond, cond_len, \"hasconfig:remote.*.url:\", &cond,\n\nI feel like this is something that we might eventually want to convert\nto be table-driven. But I think that doesn't have to happen as part of\nthis patch series.\n\n> diff --git a/t/t1305-config-include.sh b/t/t1305-config-include.sh\n> index 6e51f892f320..8a5ba4b884d3 100755\n> --- a/t/t1305-config-include.sh\n> +++ b/t/t1305-config-include.sh\n\nJust because it was explicitly mentioned: we might also want to have a\ntest that verifies this works with early-config parsing. We already have\na similar test for \"gitdir:\" in \"conditional include, early config\nreading\".\n\nAnd should we also have a \"nongit\" branch where we verify outside a\nrepository?\n\nOther than that this series looks good to me, thanks!\n\nPatrick\n"},{"id":"543182","messageId":"d236de15-f03e-4bac-9eb1-c14e50a271f5@gmail.com","threadId":"65425","inReplyTo":"agLTO0amktCWMsiE@pks.im","subject":"Re: [PATCH v3 2/2] config: add \"worktree\" and \"worktree/i\" includeIf conditions","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-05-12T15:07:12Z","receivedAt":"2026-05-12T15:07:17Z","isPatch":true,"body":"\n\nOn 12/05/2026 08:14, Patrick Steinhardt wrote:\n> On Fri, Apr 03, 2026 at 03:02:29PM +0800, Chen Linxuan via B4 Relay wrote:\n>> From: Chen Linxuan <me@black-desk.cn>\n>>\n>> The includeIf mechanism already supports matching on the .git\n>> directory path (gitdir) and the currently checked out branch\n>> (onbranch).  But in multi-worktree setups the .git directory of a\n>> linked worktree points into the main repository's .git/worktrees/\n>> area, which makes gitdir patterns cumbersome when one wants to\n>> include config based on the working tree's checkout path instead.\n>>\n>> Introduce two new condition keywords:\n>>\n>>    - worktree:<pattern> matches the realpath of the current worktree's\n>>      working directory (i.e. repo_get_work_tree()) against a glob\n>>      pattern.  This is the path returned by git rev-parse\n>>      --show-toplevel.\n>>\n>>    - worktree/i:<pattern> is the case-insensitive variant.\n> \n> Seems sensible.\n\nWe already support per-worktree config settings via \nextensions.worktreeConfig, so it would be helpful to explain why it is \nmore convenient to set the config based on the worktree's path, rather \nthan just running \"git config --worktree\" inside the worktree. Do you \nhave multiple repositories with worktrees checked out under a common \nprefix that you want to share the same config setting?\n\nThanks\n\nPhillip\n\n>> The implementation reuses the include_by_path() helper introduced in\n>> the previous commit, passing the worktree path in place of the\n>> gitdir.  The condition never matches in bare repositories (where\n>> there is no worktree) or during early config reading (where no\n>> repository is available).\n> \n> Right. This is because `repo_get_work_tree()` would return a NULL\n> pointer in these cases, and `include_by_path()` exits early in that\n> case.\n> \n>> diff --git a/Documentation/config.adoc b/Documentation/config.adoc\n>> index 62eebe7c5450..a4f3ec905098 100644\n>> --- a/Documentation/config.adoc\n>> +++ b/Documentation/config.adoc\n>> @@ -146,6 +146,48 @@ refer to linkgit:gitignore[5] for details. For convenience:\n>>   \tThis is the same as `gitdir` except that matching is done\n>>   \tcase-insensitively (e.g. on case-insensitive file systems)\n>>   \n>> +`worktree`::\n>> +\tThe data that follows the keyword `worktree` and a colon is used as a\n>> +\tglob pattern. If the working directory of the current worktree matches\n>> +\tthe pattern, the include condition is met.\n>> ++\n>> +The worktree location is the path where files are checked out (as returned\n>> +by `git rev-parse --show-toplevel`). This is different from `gitdir`, which\n>> +matches the `.git` directory path. In a linked worktree, the worktree path\n>> +is the directory where that worktree's files are located, not the main\n>> +repository's `.git` directory.\n>> ++\n>> +The pattern can contain standard globbing wildcards and two additional\n>> +ones, `**/` and `/**`, that can match multiple path components. Please\n>> +refer to linkgit:gitignore[5] for details. For convenience:\n>> +\n>> + * If the pattern starts with `~/`, `~` will be substituted with the\n>> +   content of the environment variable `HOME`.\n>> +\n>> + * If the pattern starts with `./`, it is replaced with the directory\n>> +   containing the current config file.\n>> +\n>> + * If the pattern does not start with either `~/`, `./` or `/`, `**/`\n>> +   will be automatically prepended. For example, the pattern `foo/bar`\n>> +   becomes `**/foo/bar` and would match `/any/path/to/foo/bar`.\n>> +\n>> + * If the pattern ends with `/`, `**` will be automatically added. For\n>> +   example, the pattern `foo/` becomes `foo/**`. In other words, it\n>> +   matches \"foo\" and everything inside, recursively.\n> \n> This whole listing here is the exact same as we have for the `gitdir`\n> condition. Can we maybe deduplicate these into a common section?\n> \n>> diff --git a/config.c b/config.c\n>> index 7d5dae0e8450..6d0c2d0725e4 100644\n>> --- a/config.c\n>> +++ b/config.c\n>> @@ -400,6 +400,12 @@ static int include_condition_is_true(const struct key_value_info *kvi,\n>>   \t\treturn include_by_path(kvi, opts->git_dir, cond, cond_len, 0);\n>>   \telse if (skip_prefix_mem(cond, cond_len, \"gitdir/i:\", &cond, &cond_len))\n>>   \t\treturn include_by_path(kvi, opts->git_dir, cond, cond_len, 1);\n>> +\telse if (skip_prefix_mem(cond, cond_len, \"worktree:\", &cond, &cond_len))\n>> +\t\treturn include_by_path(kvi, inc->repo ? repo_get_work_tree(inc->repo) : NULL,\n>> +\t\t\t\t       cond, cond_len, 0);\n>> +\telse if (skip_prefix_mem(cond, cond_len, \"worktree/i:\", &cond, &cond_len))\n>> +\t\treturn include_by_path(kvi, inc->repo ? repo_get_work_tree(inc->repo) : NULL,\n>> +\t\t\t\t       cond, cond_len, 1);\n>>   \telse if (skip_prefix_mem(cond, cond_len, \"onbranch:\", &cond, &cond_len))\n>>   \t\treturn include_by_branch(inc, cond, cond_len);\n>>   \telse if (skip_prefix_mem(cond, cond_len, \"hasconfig:remote.*.url:\", &cond,\n> \n> I feel like this is something that we might eventually want to convert\n> to be table-driven. But I think that doesn't have to happen as part of\n> this patch series.\n> \n>> diff --git a/t/t1305-config-include.sh b/t/t1305-config-include.sh\n>> index 6e51f892f320..8a5ba4b884d3 100755\n>> --- a/t/t1305-config-include.sh\n>> +++ b/t/t1305-config-include.sh\n> \n> Just because it was explicitly mentioned: we might also want to have a\n> test that verifies this works with early-config parsing. We already have\n> a similar test for \"gitdir:\" in \"conditional include, early config\n> reading\".\n> \n> And should we also have a \"nongit\" branch where we verify outside a\n> repository?\n> \n> Other than that this series looks good to me, thanks!\n> \n> Patrick\n> \n\n"},{"id":"543190","messageId":"xmqqo6iklid5.fsf@gitster.g","threadId":"65425","inReplyTo":"d236de15-f03e-4bac-9eb1-c14e50a271f5@gmail.com","subject":"Re: [PATCH v3 2/2] config: add \"worktree\" and \"worktree/i\" includeIf conditions","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-05-12T16:09:42Z","receivedAt":"2026-05-12T16:09:46Z","isPatch":true,"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> On 12/05/2026 08:14, Patrick Steinhardt wrote:\n> ...\n>>> Introduce two new condition keywords:\n>>>\n>>>    - worktree:<pattern> matches the realpath of the current worktree's\n>>>      working directory (i.e. repo_get_work_tree()) against a glob\n>>>      pattern.  This is the path returned by git rev-parse\n>>>      --show-toplevel.\n>>>\n>>>    - worktree/i:<pattern> is the case-insensitive variant.\n>> \n>> Seems sensible.\n>\n> We already support per-worktree config settings via \n> extensions.worktreeConfig, so it would be helpful to explain why it is \n> more convenient to set the config based on the worktree's path, rather \n> than just running \"git config --worktree\" inside the worktree. Do you \n> have multiple repositories with worktrees checked out under a common \n> prefix that you want to share the same config setting?\n\nExcellent point.  The documentation for the new feature may have to\nbe strengthened to suggest which one of the two approaches is more\nappropriate with what use cases.\n\nThanks.\n"},{"id":"543226","messageId":"DIH7FB91JHU1.3OOTDQ6QEZZJZ@black-desk.cn","threadId":"65425","inReplyTo":"agLTO0amktCWMsiE@pks.im","subject":"Re: [PATCH v3 2/2] config: add \"worktree\" and \"worktree/i\" includeIf conditions","fromName":"Chen Linxuan","fromEmail":"me@black-desk.cn","sentAt":"2026-05-13T02:47:48Z","receivedAt":"2026-05-13T02:47:53Z","isPatch":true,"body":"On Tue, 12 May 2026 09:14:03 +0200, Patrick Steinhardt wrote:\n> On Fri, Apr 03, 2026 at 03:02:29PM +0800, Chen Linxuan via B4 Relay wrote:\n> > ...\n> > The implementation reuses the include_by_path() helper introduced in\n> > the previous commit, passing the worktree path in place of the\n> > gitdir.  The condition never matches in bare repositories (where\n> > there is no worktree) or during early config reading (where no\n> > repository is available).\n> \n> Right. This is because `repo_get_work_tree()` would return a NULL\n> pointer in these cases, and `include_by_path()` exits early in that\n> case.\n> \n> ...\n> \n> This whole listing here is the exact same as we have for the `gitdir`\n> condition. Can we maybe deduplicate these into a common section?\n\nSure, will be updated in V4.\n\n> ...\n>\n> Just because it was explicitly mentioned: we might also want to have a\n> test that verifies this works with early-config parsing. We already have\n> a similar test for \"gitdir:\" in \"conditional include, early config\n> reading\".\n\nAs I wrote in the commit message, this is not going to work with\nearly-config parsing. I am working on the fix. But I am not quite\nsure that this is a must-fix issue or not.\n\n> And should we also have a \"nongit\" branch where we verify outside a\n> repository?\n\nSure, will be added in V4.\n\nChen Linxuan\n"},{"id":"543227","messageId":"CAC1kPDNAFRp+t+_KRUfV_nUXj6nFBNEHp05odiVXsUOw3+tcrA@mail.gmail.com","threadId":"65425","inReplyTo":"xmqqo6iklid5.fsf@gitster.g","subject":"Re: [PATCH v3 2/2] config: add \"worktree\" and \"worktree/i\" includeIf conditions","fromName":"Chen Linxuan","fromEmail":"me@black-desk.cn","sentAt":"2026-05-13T02:55:58Z","receivedAt":"2026-05-13T02:56:12Z","isPatch":true,"body":"On Wed, May 13, 2026 at 12:09 AM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n>\n> > On 12/05/2026 08:14, Patrick Steinhardt wrote:\n> > ...\n> >>> Introduce two new condition keywords:\n> >>>\n> >>>    - worktree:<pattern> matches the realpath of the current worktree's\n> >>>      working directory (i.e. repo_get_work_tree()) against a glob\n> >>>      pattern.  This is the path returned by git rev-parse\n> >>>      --show-toplevel.\n> >>>\n> >>>    - worktree/i:<pattern> is the case-insensitive variant.\n> >>\n> >> Seems sensible.\n> >\n> > We already support per-worktree config settings via\n> > extensions.worktreeConfig, so it would be helpful to explain why it is\n> > more convenient to set the config based on the worktree's path, rather\n> > than just running \"git config --worktree\" inside the worktree. Do you\n> > have multiple repositories with worktrees checked out under a common\n> > prefix that you want to share the same config setting?\n\nYes, that is exactly why I added this feature. I contribute to the\nLinux kernel both as an employee and as an individual. I want to\nautomatically use my company email address whenever I create a\nworktree under a specific directory.\n"},{"id":"543228","messageId":"agQSqPY0y9oTY_E8@pks.im","threadId":"65425","inReplyTo":"DIH7FB91JHU1.3OOTDQ6QEZZJZ@black-desk.cn","subject":"Re: [PATCH v3 2/2] config: add \"worktree\" and \"worktree/i\" includeIf conditions","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-05-13T05:56:56Z","receivedAt":"2026-05-13T05:57:05Z","isPatch":true,"body":"On Wed, May 13, 2026 at 10:47:48AM +0800, Chen Linxuan wrote:\n> On Tue, 12 May 2026 09:14:03 +0200, Patrick Steinhardt wrote:\n> > Just because it was explicitly mentioned: we might also want to have a\n> > test that verifies this works with early-config parsing. We already have\n> > a similar test for \"gitdir:\" in \"conditional include, early config\n> > reading\".\n> \n> As I wrote in the commit message, this is not going to work with\n> early-config parsing. I am working on the fix. But I am not quite\n> sure that this is a must-fix issue or not.\n\nIf it's not working we should have a test for this regardless, I think.\nWe should verify the current behaviour around it and either mark it as\n`test_expect_success` if that behaviour is intended, or with\n`test_expect_failure` if it's not.\n\nThanks!\n\nPatrick\n"}]}