worktree: simplify find_shared_symref() memory ownership model

Storing the worktrees list in a static variable meant that
find_shared_symref() had to rebuild the list on each call (which is
inefficient when the call site is in a loop), and also that each call
invalidated the pointer returned by the previous call (which is
confusing).

Instead, make it the caller’s responsibility to pass in the worktrees
list and manage its lifetime.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Anders Kaseorg
2021-12-01 14:15:43 -08:00
committed by Junio C Hamano
parent 7435e7e2e7
commit c8dd491fa5
6 changed files with 65 additions and 38 deletions

View File

@ -404,17 +404,13 @@ int is_worktree_being_bisected(const struct worktree *wt,
* bisect). New commands that do similar things should update this
* function as well.
*/
const struct worktree *find_shared_symref(const char *symref,
const struct worktree *find_shared_symref(struct worktree **worktrees,
const char *symref,
const char *target)
{
const struct worktree *existing = NULL;
static struct worktree **worktrees;
int i = 0;
if (worktrees)
free_worktrees(worktrees);
worktrees = get_worktrees();
for (i = 0; worktrees[i]; i++) {
struct worktree *wt = worktrees[i];
const char *symref_target;