threads / discuss / 61160

Feature request: highlight local worktree in `worktree list`

Subject: Feature request: highlight local worktree in `worktree list`

## tl;dr

7 messages between Mar 20, 2024 and Mar 21, 2024.

replies: 6people: 5as markdown or json

Matt Hickford· Mar 20, 2024, 08:00 UTC · lore

Hi. `git branch` lists branches. It highlights the current branch with an asterisk and (for me) the colour green. This is handy for quick reading.

`git worktree list` lists worktrees. It would be neat to highlight the local worktree.

Another idea: `git branch -v` shows the commit subject and ahead/behind counts for each branch, eg. "[ahead 1, behind 1] avoid RegexReplace". It would be neat for `git worktree list -v` (or similar) to show the commit subject and ahead/behind counts.

Jeff King· Mar 20, 2024, 10:02 UTC · re: Matt Hickford · lore

Re: Feature request: highlight local worktree in `worktree list`

On Wed, Mar 20, 2024 at 08:00:00AM +0000, Matt Hickford wrote:
Show 11 quoted lines
> Hi. `git branch` lists branches. It highlights the current branch with
> an asterisk and (for me) the colour green. This is handy for quick
> reading.
> 
> `git worktree list` lists worktrees. It would be neat to highlight the
> local worktree.
> 
> Another idea: `git branch -v` shows the commit subject and
> ahead/behind counts for each branch, eg. "[ahead 1, behind 1] avoid
> RegexReplace". It would be neat for `git worktree list -v` (or
> similar) to show the commit subject and ahead/behind counts.

I'm not sure if this is a dumb idea or not, but: what if git-branch learned to display branches associated with worktrees (just like "-r" lists remote branches, etc).

Then you'd get the all of the display niceties you want, along with other features like sorting, limiting by merge, etc. You can even show the worktree paths with a custom format that uses %(worktreepath).

I think the only thing it wouldn't do is show worktrees that are detached from any branch at all. I'm not sure if that's important to your workflow or not.

-Peff
Junio C Hamano· Mar 20, 2024, 14:38 UTC · re: Jeff King · lore

Re: Feature request: highlight local worktree in `worktree list`

Jeff King <peff@peff.net> writes:
> I'm not sure if this is a dumb idea or not, but: what if git-branch
> learned to display branches associated with worktrees (just like "-r"
> lists remote branches, etc).

A branch usually, but not necessarily, is checked out in at most one worktree, so the display may become a bit more cluttered when more than one checks out the same branch, but it is similar to how a commit pointed at by multiple refs are shown with --decorate, so I do not think it is such a huge deal.

> I think the only thing it wouldn't do is show worktrees that are
> detached from any branch at all. I'm not sure if that's important to
> your workflow or not.

If such a mode in "git branch --list" were written, I would imagine that the output from such a mode will include detached HEAD from other worktrees as well. Perhaps something like this?

    $ git branch --list --worktrees
      (HEAD detached from ffa877cd2f, checked out in worktree 'buildtest')
      jch (checked out in worktree 'integration')
      maint
    * master (checked out in the primary worktree)
      next
      seen
      jk/branch-list-with-worktree
Jeff King· Mar 20, 2024, 20:39 UTC · re: Junio C Hamano · lore

Re: Feature request: highlight local worktree in `worktree list`

On Wed, Mar 20, 2024 at 07:38:57AM -0700, Junio C Hamano wrote:
Show 7 quoted lines
> > I think the only thing it wouldn't do is show worktrees that are
> > detached from any branch at all. I'm not sure if that's important to
> > your workflow or not.
> 
> If such a mode in "git branch --list" were written, I would imagine
> that the output from such a mode will include detached HEAD from
> other worktrees as well.  Perhaps something like this?

Good point. I was thinking it only iterated on refs, but of course we already show a detached HEAD in the main worktree.

-Peff
Matt Hickford· Mar 21, 2024, 13:00 UTC · re: Jeff King · lore

Re: Feature request: highlight local worktree in `worktree list`

On Wed, 20 Mar 2024 at 10:02, Jeff King <peff@peff.net> wrote:
Show 26 quoted lines
>
> On Wed, Mar 20, 2024 at 08:00:00AM +0000, Matt Hickford wrote:
>
> > Hi. `git branch` lists branches. It highlights the current branch with
> > an asterisk and (for me) the colour green. This is handy for quick
> > reading.
> >
> > `git worktree list` lists worktrees. It would be neat to highlight the
> > local worktree.
> >
> > Another idea: `git branch -v` shows the commit subject and
> > ahead/behind counts for each branch, eg. "[ahead 1, behind 1] avoid
> > RegexReplace". It would be neat for `git worktree list -v` (or
> > similar) to show the commit subject and ahead/behind counts.
>
> I'm not sure if this is a dumb idea or not, but: what if git-branch
> learned to display branches associated with worktrees (just like "-r"
> lists remote branches, etc).
>
> Then you'd get the all of the display niceties you want, along with
> other features like sorting, limiting by merge, etc. You can even show
> the worktree paths with a custom format that uses %(worktreepath).
>
> I think the only thing it wouldn't do is show worktrees that are
> detached from any branch at all. I'm not sure if that's important to
> your workflow or not.

I frequently run `git worktree list` to review my work in each tree. Or to choose an inactive tree for new work. Example output:

E:/product1 ea601b57e (detached HEAD) E:/product2 b255bd1b6 [json] E:/product3 0d7623530 [optimise] E:/product4 7bf8297c7 (detached HEAD) E:/product5 a8f89cabc (detached HEAD) E:/product6 89a022651 [xml]

The more information the better -- highlighting the local workspace, ref names when HEAD is detached:

  E:/product1  ea601b57e (detached HEAD, main)
  E:/product2  b255bd1b6 [json]
  E:/product3  0d7623530 [optimise]
* E:/product4  7bf8297c7 (detached HEAD)
  E:/product5  a8f89cabc (detached HEAD, origin/random)
  E:/product6  89a022651 [xml]
A verbose option with commit subjects and ahead-behind counts:
  E:/product1  ea601b57e (detached HEAD, main)           Articulate splines
  E:/product2  b255bd1b6 [json]                          [ahead 1]
Correct syntax error
  E:/product3  0d7623530 [optimise]                      Optimise algorithm
* E:/product4  7bf8297c7 (detached HEAD)                 Reformat doc
  E:/product5  a8f89cabc (detached HEAD, origin/random)  Seed random generator
  E:/product6  89a022651 [xml]                           Replace with XML
Rubén Justo· Mar 20, 2024, 22:24 UTC · re: Matt Hickford · lore

Re: Feature request: highlight local worktree in `worktree list`

On Wed, Mar 20, 2024 at 08:00:00 +0000, Matt Hickford wrote:
Show 11 quoted lines
> Hi. `git branch` lists branches. It highlights the current branch with
> an asterisk and (for me) the colour green. This is handy for quick
> reading.
>
> `git worktree list` lists worktrees. It would be neat to highlight the
> local worktree.
>
> Another idea: `git branch -v` shows the commit subject and
> ahead/behind counts for each branch, eg. "[ahead 1, behind 1] avoid
> RegexReplace". It would be neat for `git worktree list -v` (or
> similar) to show the commit subject and ahead/behind counts.
Both ideas sound good.  And so do what Peff has suggested.

I want to point out that maybe "git branch [--list] -v -v" has gone unnoticed. It does not fully meet the expectations expressed in this thread, but perhaps it can be of some help:

    $ git init -b foo /tmp/main
    Initialized empty Git repository in /tmp/main/.git/
    $ cd /tmp/main
    $ git commit --allow-empty -m test
    [foo (root-commit) 87382f1] test
    $ git worktree add ../wt
    Preparing worktree (new branch 'wt')
    HEAD is now at 87382f1 test
    $ git branch --set-upstream-to=wt
    Branch 'foo' set up to track local branch 'wt'.
    $ cd /tmp/wt
    $ git branch -v -v
    + foo 87382f1 (/tmp/main) [wt] test
    * wt  87382f1 test
    $ git commit --allow-empty -m test_2
    [wt df39c54] test_2
    $ git branch -v -v
    + foo 87382f1 (/tmp/main) [wt: behind 1] test
    * wt  df39c54 test_2

"git branch --list" has a much richer machinery than "git worktree list". Perhaps the display machinery of both commands can converge in some way.

Chris Torek· Mar 21, 2024, 00:53 UTC · re: Rubén Justo · lore

Re: Feature request: highlight local worktree in `worktree list`

On Wed, Mar 20, 2024 at 3:24 PM Rubén Justo <rjusto@gmail.com> wrote:
> "git branch --list" has a much richer machinery than "git worktree list".
> Perhaps the display machinery of both commands can converge in some way.

I have no concrete suggestions for such, but I will note that there is a `git for-each-ref` plumbing command for refs, but no `git for-each-worktree` plumbing command for worktrees. Given the relationship here (each worktree has a worktree-specific set of refs), perhaps that's a point of leverage, as it were?

Chris

← back to recent threads