Thread View: linux.kernel.git
2 messages
2 total messages
Started by Phillip Wood
Mon, 16 Nov 2026 16:42
Re: [PATCH v5 4/5] ref-filter: add %(currentworktree)
Author: Phillip Wood
Date: Mon, 16 Nov 2026 16:42
Date: Mon, 16 Nov 2026 16:42
253 lines
21808 bytes
21808 bytes
Hi Jasper
On 11/11/2026 15:28, Jasper Mayone wrote:
> %(worktreepath) names the worktree that has a branch checked out, or
> is rebasing or bisecting it, but a format cannot tell if that worktree
> is the current one. Comparing %(worktreepath) with a fixed path in
> %(if:equals=...) does not work for every path, because a ")" ends the
> atom.
>
> Add %(currentworktree), which gives the path of the current worktree
> when that worktree has the branch, and an empty string otherwise.
> branch.c records these branches when it reads the worktrees, in a
> second map next to the one that %(worktreepath) uses, so the lookup is
> one map lookup per ref.
When I suggested %(currentworktree) I was imagining it would be a
boolean format like %(HEAD) (i.e. it would print '*' if the ref was
checked out in the current worktree). If we want to print the path
instead, we should call it %(currentworktreepath) to match
%(worktreepath). However, I'm not sure there is much value in printing
the path, as that can be obtained with %(worktreepath).
While writing this email, I've just realized that %(HEAD) effectively
tells us if we're in the current worktree, so another approach would be
to extend that to handle branches that are being rebased or bisected,
rather than adding a new format specifier. I haven't thought about it
enough to have an opinion on whether that is a good idea.
A couple of thoughts on the implementation
1 - if a branch is checked out in two different worktrees it would be
helpful if %(currentworktree) and %(worktreepath) agreed on which
worktree to report. In practice that would mean %(worktreepath)
always reporting the path of the current worktree if the branch is
checked out there. With the implementation here, it is possible for
them to report different worktrees.
2 - why are we using a hashmap to store a single entry? It would be more
efficient to simply record the refname and path in separate strings
and use strcmp(refname, current_worktree_branch) to determine
whether refname is checked out in the current worktree. If we change
to a boolean format we don't even need to record the worktree path.
Thanks
Phillip
> The next commit uses it in "git branch".
>
> Suggested-by: Phillip Wood <phillip.wood123@gmail.com>
> Signed-off-by: Jasper Mayone <me@jaspermayone.com>
> ---
> Documentation/git-for-each-ref.adoc | 7 +++++++
> branch.c | 25 ++++++++++++++++++-------
> branch.h | 7 +++++++
> ref-filter.c | 10 ++++++++++
> t/t6302-for-each-ref-filter.sh | 21 +++++++++++++++++++++
> 5 files changed, 63 insertions(+), 7 deletions(-)
>
> diff --git a/Documentation/git-for-each-ref.adoc b/Documentation/git-for-each-ref.adoc
> index 4456b4809a..5e2da92ee2 100644
> --- a/Documentation/git-for-each-ref.adoc
> +++ b/Documentation/git-for-each-ref.adoc
> @@ -193,6 +193,13 @@ but if used together the last option is selected.
> the `NUL` after the path and then for the `LF` that ends each
> record.
>
> +`currentworktree`::
> + The absolute path to the current worktree, if the ref is checked
> + out there, in the same sense as for `worktreepath`. Empty string
> + otherwise, also when the ref is checked out only in other
> + worktrees. Use it with `%(if)` to treat the current worktree
> + differently from the others.
> +
> `ahead-behind:<commit-ish>`::
> Two integers, separated by a space, demonstrating the number of
> commits ahead and behind, respectively, when comparing the output
> diff --git a/branch.c b/branch.c
> index 87f3ffc35f..4de789b7ae 100644
> --- a/branch.c
> +++ b/branch.c
> @@ -386,6 +386,8 @@ static int initialized_checked_out_branches;
> static struct strmap current_checked_out_branches = STRMAP_INIT;
> /* Like current_checked_out_branches, without the "update-ref" branches. */
> static struct strmap checked_out_worktrees = STRMAP_INIT;
> +/* The subset of checked_out_worktrees that the current worktree has. */
> +static struct strmap current_worktree_branches = STRMAP_INIT;
>
> enum branch_checkout_kind {
> BRANCH_CHECKOUT_KIND_CHECKOUT,
> @@ -404,11 +406,11 @@ static struct checked_out_branch *checked_out_branches;
> static size_t checked_out_branches_alloc, checked_out_branches_nr;
>
> static void register_checked_out_branch(const char *prefix, const char *name,
> - const char *path,
> + const struct worktree *wt,
> enum branch_checkout_kind kind)
> {
> char *refname = xstrfmt("%s%s", prefix, name);
> - char *path_copy = xstrdup(path);
> + char *path_copy = xstrdup(wt->path);
>
> ALLOC_GROW(checked_out_branches, checked_out_branches_nr + 1,
> checked_out_branches_alloc);
> @@ -418,8 +420,11 @@ static void register_checked_out_branch(const char *prefix, const char *name,
> checked_out_branches_nr++;
>
> strmap_put(¤t_checked_out_branches, refname, path_copy);
> - if (kind != BRANCH_CHECKOUT_KIND_UPDATE_REF)
> + if (kind != BRANCH_CHECKOUT_KIND_UPDATE_REF) {
> strmap_put(&checked_out_worktrees, refname, path_copy);
> + if (wt->is_current)
> + strmap_put(¤t_worktree_branches, refname, path_copy);
> + }
> }
>
> static void prepare_checked_out_branches(void)
> @@ -443,7 +448,7 @@ static void prepare_checked_out_branches(void)
> continue;
>
> if (wt->head_ref) {
> - register_checked_out_branch("", wt->head_ref, wt->path,
> + register_checked_out_branch("", wt->head_ref, wt,
> BRANCH_CHECKOUT_KIND_CHECKOUT);
> }
>
> @@ -451,7 +456,7 @@ static void prepare_checked_out_branches(void)
> (state.rebase_in_progress || state.rebase_interactive_in_progress) &&
> state.branch) {
> register_checked_out_branch("refs/heads/", state.branch,
> - wt->path,
> + wt,
> BRANCH_CHECKOUT_KIND_REBASE);
> }
> wt_status_state_free_buffers(&state);
> @@ -460,7 +465,7 @@ static void prepare_checked_out_branches(void)
> state.bisecting_from) {
> register_checked_out_branch("refs/heads/",
> state.bisecting_from,
> - wt->path,
> + wt,
> BRANCH_CHECKOUT_KIND_BISECT);
> }
> wt_status_state_free_buffers(&state);
> @@ -471,7 +476,7 @@ static void prepare_checked_out_branches(void)
> struct string_list_item *item;
> for_each_string_list_item(item, &update_refs) {
> register_checked_out_branch("", item->string,
> - wt->path,
> + wt,
> BRANCH_CHECKOUT_KIND_UPDATE_REF);
> }
> string_list_clear(&update_refs, 1);
> @@ -495,6 +500,12 @@ const char *branch_checked_out_worktree(const char *refname)
> return strmap_get(&checked_out_worktrees, refname);
> }
>
> +const char *branch_checked_out_current_worktree(const char *refname)
> +{
> + prepare_checked_out_branches();
> + return strmap_get(¤t_worktree_branches, refname);
> +}
> +
> const char *branch_bisecting(const char *refname)
> {
> prepare_checked_out_branches();
> diff --git a/branch.h b/branch.h
> index 60cb92a3e4..744e1eec1d 100644
> --- a/branch.h
> +++ b/branch.h
> @@ -115,6 +115,13 @@ const char *branch_checked_out(const char *refname);
> */
> const char *branch_checked_out_worktree(const char *refname);
>
> +/*
> + * Like branch_checked_out_worktree(), but only for the current
> + * worktree: return its path if it has the branch 'refname' checked
> + * out, or is rebasing or bisecting it. Otherwise return NULL.
> + */
> +const char *branch_checked_out_current_worktree(const char *refname);
> +
> /*
> * If the branch at 'refname' is currently used for bisecting in a
> * worktree, then return the path to that worktree.
> diff --git a/ref-filter.c b/ref-filter.c
> index b636e56199..498d09da8f 100644
> --- a/ref-filter.c
> +++ b/ref-filter.c
> @@ -142,6 +142,7 @@ enum atom_type {
> ATOM_HEAD,
> ATOM_COLOR,
> ATOM_WORKTREEPATH,
> + ATOM_CURRENTWORKTREE,
> ATOM_ALIGN,
> ATOM_END,
> ATOM_IF,
> @@ -960,6 +961,7 @@ static struct {
> [ATOM_HEAD] = { "HEAD", SOURCE_NONE, FIELD_STR, head_atom_parser },
> [ATOM_COLOR] = { "color", SOURCE_NONE, FIELD_STR, color_atom_parser },
> [ATOM_WORKTREEPATH] = { "worktreepath", SOURCE_NONE },
> + [ATOM_CURRENTWORKTREE] = { "currentworktree", SOURCE_NONE },
> [ATOM_ALIGN] = { "align", SOURCE_NONE, FIELD_STR, align_atom_parser },
> [ATOM_END] = { "end", SOURCE_NONE },
> [ATOM_IF] = { "if", SOURCE_NONE, FIELD_STR, if_atom_parser },
> @@ -2420,6 +2422,14 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
> v->s = xstrdup("");
> continue;
> }
> + else if (atom_type == ATOM_CURRENTWORKTREE) {
> + const char *path = NULL;
> +
> + if (ref->kind == FILTER_REFS_BRANCHES)
> + path = branch_checked_out_current_worktree(ref->refname);
> + v->s = xstrdup(path ? path : "");
> + continue;
> + }
> else if (atom_type == ATOM_SYMREF)
> refname = get_symref(atom, ref);
> else if (atom_type == ATOM_UPSTREAM) {
> diff --git a/t/t6302-for-each-ref-filter.sh b/t/t6302-for-each-ref-filter.sh
> index 317ed6f6cd..32522f1ff3 100755
> --- a/t/t6302-for-each-ref-filter.sh
> +++ b/t/t6302-for-each-ref-filter.sh
> @@ -617,6 +617,27 @@ test_expect_success 'worktree atom ignores the HEAD of a bare repository' '
> test_cmp expect actual
> '
>
> +test_expect_success 'currentworktree atom' '
> + test_when_finished "git -C current rebase --abort; rm -rf current; git worktree prune; git branch -D current" &&
> + git worktree add -b current current main &&
> + git -C current commit --allow-empty -m rebase-me &&
> + test_must_fail git -C current rebase -f -x false main &&
> + git for-each-ref --format="%(refname:short) [%(currentworktree)]" \
> + refs/heads/main refs/heads/current >actual &&
> + cat >expect <<-EOF &&
> + current []
> + main [$(pwd)]
> + EOF
> + test_cmp expect actual &&
> + git -C current for-each-ref --format="%(refname:short) [%(currentworktree)]" \
> + refs/heads/main refs/heads/current >actual &&
> + cat >expect <<-EOF &&
> + current [$(pwd)/current]
> + main []
> + EOF
> + test_cmp expect actual
> +'
> +
> test_expect_success 'start after with empty value' '
> cat >expect <<-\EOF &&
> refs/heads/main
Re: [PATCH v5 4/5] ref-filter: add %(currentworktree)
Author: Phillip Wood
Date: Tue, 01 Dec 2026 14:58
Date: Tue, 01 Dec 2026 14:58
35 lines
12368 bytes
12368 bytes
Hi Jasper Sorry for the slow reply On 19/11/2026 15:14, Jasper Mayone wrote: > Hi Phillip > > On 16/11/2026 16:42, Phillip Wood wrote: >> When I suggested %(currentworktree) I was imagining it would be a >> boolean format like %(HEAD) (i.e. it would print '*' if the ref was >> checked out in the current worktree). > > Thank you for the review. Our emails crossed: Junio was against 5/5, > because in a long branch list he wants the "+" next to the branch that > his own worktree is bisecting, and I agreed to stop the series at 3/5 > [1]. 4/5 existed only for 5/5, so it is dropped too. That makes sense. I'd misunderstood what was happening as I thought that we were printing "* (no branch, rebasing topic) + topic" on the same entry, but in fact "* (no branch, rebasing topic)" is printed with HEAD and "+ topic" is the entry for the branch so it makes sense to keep the "+" Thanks Phillip > Your idea to extend %(HEAD) to branches that are being rebased or > bisected is interesting. I will keep it, and your two points on the > implementation, in mind if I come back to this later. > > [1] https://lore.kernel.org/git/20261116161553.56756-1-me@jaspermayone.com/ > > Thanks > Jasper
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads