diff options
author | Junio C Hamano <gitster@pobox.com> | 2023-03-20 01:03:11 +0300 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2023-03-20 01:03:11 +0300 |
commit | 96a806f87a379fbc54b5cdb889518e9540d954c9 (patch) | |
tree | f0318d5114575e8f6d8b6fca5579d2b1e13feaa1 /worktree.c | |
parent | c79786c486cb84e2f9dad41a04d982bb23075815 (diff) | |
parent | 894ea945095b74542c6c4f4aefbe20f5b68be437 (diff) |
Merge branch 'rj/avoid-switching-to-already-used-branch'
A few subcommands have been taught to stop users from working on a
branch that is being used in another worktree linked to the same
repository.
* rj/avoid-switching-to-already-used-branch:
switch: reject if the branch is already checked out elsewhere (test)
rebase: refuse to switch to a branch already checked out elsewhere (test)
branch: fix die_if_checked_out() when ignore_current_worktree
worktree: introduce is_shared_symref()
Diffstat (limited to 'worktree.c')
-rw-r--r-- | worktree.c | 63 |
1 files changed, 31 insertions, 32 deletions
diff --git a/worktree.c b/worktree.c index c99939a4d1..e10594ef33 100644 --- a/worktree.c +++ b/worktree.c @@ -404,44 +404,43 @@ 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(struct worktree **worktrees, - const char *symref, - const char *target) +int is_shared_symref(const struct worktree *wt, const char *symref, + const char *target) { - const struct worktree *existing = NULL; - int i = 0; + const char *symref_target; + struct ref_store *refs; + int flags; - for (i = 0; worktrees[i]; i++) { - struct worktree *wt = worktrees[i]; - const char *symref_target; - struct ref_store *refs; - int flags; + if (wt->is_bare) + return 0; - if (wt->is_bare) - continue; + if (wt->is_detached && !strcmp(symref, "HEAD")) { + if (is_worktree_being_rebased(wt, target)) + return 1; + if (is_worktree_being_bisected(wt, target)) + return 1; + } - if (wt->is_detached && !strcmp(symref, "HEAD")) { - if (is_worktree_being_rebased(wt, target)) { - existing = wt; - break; - } - if (is_worktree_being_bisected(wt, target)) { - existing = wt; - break; - } - } + refs = get_worktree_ref_store(wt); + symref_target = refs_resolve_ref_unsafe(refs, symref, 0, + NULL, &flags); + if ((flags & REF_ISSYMREF) && + symref_target && !strcmp(symref_target, target)) + return 1; - refs = get_worktree_ref_store(wt); - symref_target = refs_resolve_ref_unsafe(refs, symref, 0, - NULL, &flags); - if ((flags & REF_ISSYMREF) && - symref_target && !strcmp(symref_target, target)) { - existing = wt; - break; - } - } + return 0; +} - return existing; +const struct worktree *find_shared_symref(struct worktree **worktrees, + const char *symref, + const char *target) +{ + + for (int i = 0; worktrees[i]; i++) + if (is_shared_symref(worktrees[i], symref, target)) + return worktrees[i]; + + return NULL; } int submodule_uses_worktrees(const char *path) |