Subject: Re: bug#79024: 31.0.50; Multiple working trees support for VC

From: Spencer Baugh <[email-masked]>
Date: Fri, 25 Jul 2025 10:53:50 -0400

Sean Whitton <[email-masked]> writes:

> Hello Spencer,
>
> I just tried implementing an other-working-tree-p backend function for
> Git and Hg.  It introduces a new and quite significant performance
> problem for Git.  Consider prompting for another working tree like this:
>
>     (completing-read prompt
>                      (cl-remove-if-not #'vc-git-other-working-tree-p
>                                        (project-known-project-roots))
>                      nil t)
>
> This will call vc-git-other-working-tree-p multiple times, but the
> obvious implementation of vc-git-other-working-tree-p invokes 'git
> worktree list' to find its answer.  So this involves invoking 'git
> worktree list' over and over again, which is really slow.
>
> This is VC-specific code which could be adapted somehow; the problem is
> worse if we are considering a project-prompter API where the project
> prompter receives only an opaque predicate function.  The
> project-prompter has no choice but to call the predicate repeatedly.
>
> We could set up some caching of the results in vc-git but I can't think
> of a cache invalidation strategy that wouldn't break Lisp code trying to
> manipulate/maintain multiple worktrees.

True, using "git worktree list" to implement this is of course slow.
But my suggested implementation would be to simply read .git (which is a
file rather than a directory in git worktrees); then the performance
should be much better (just one file read).