aboutsummaryrefslogtreecommitdiffstats
path: root/branch.c
diff options
context:
space:
mode:
authorAnders Kaseorg <andersk@mit.edu>2021-12-01 14:15:43 -0800
committerJunio C Hamano <gitster@pobox.com>2021-12-01 22:18:25 -0800
commitc8dd491fa5b57ecfd9ef33ae5291eb2a9402cd59 (patch)
treeb686f1bb1a587d214f3a19efa6d56b7c65d07ae2 /branch.c
parent7435e7e2e7645124679eedbfb1443b8408f29219 (diff)
downloadgit-c8dd491fa5b57ecfd9ef33ae5291eb2a9402cd59.tar.gz
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>
Diffstat (limited to 'branch.c')
-rw-r--r--branch.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/branch.c b/branch.c
index 85777b939b..c66b222abd 100644
--- a/branch.c
+++ b/branch.c
@@ -357,14 +357,16 @@ void remove_branch_state(struct repository *r, int verbose)
void die_if_checked_out(const char *branch, int ignore_current_worktree)
{
+ struct worktree **worktrees = get_worktrees();
const struct worktree *wt;
- wt = find_shared_symref("HEAD", branch);
- if (!wt || (ignore_current_worktree && wt->is_current))
- return;
- skip_prefix(branch, "refs/heads/", &branch);
- die(_("'%s' is already checked out at '%s'"),
- branch, wt->path);
+ wt = find_shared_symref(worktrees, "HEAD", branch);
+ if (wt && (!ignore_current_worktree || !wt->is_current)) {
+ skip_prefix(branch, "refs/heads/", &branch);
+ die(_("'%s' is already checked out at '%s'"), branch, wt->path);
+ }
+
+ free_worktrees(worktrees);
}
int replace_each_worktree_head_symref(const char *oldref, const char *newref,