worktree: introduce is_shared_symref()

Add a new function, is_shared_symref(), which contains the heart of find_shared_symref(). Refactor find_shared_symref() to use the new function is_shared_symref(). Soon, we will use is_shared_symref() to search for symref beyond the first worktree that matches. Signed-off-by: Rubén Justo <rjusto@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Rubén Justo committed Feb 25, 2023 at 15:21 UTC 662078caacd450f93faa72e0e7d8580c42621415
2 files changed +37 -31
worktree.c
+31 -31
@@ -403,44 +403,44 @@ int is_worktree_being_bisected(const struct worktree *wt,
403 * bisect). New commands that do similar things should update this
404 * function as well.
405 */
406 -const struct worktree *find_shared_symref(struct worktree **worktrees,
407 - const char *symref,
408 - const char *target)
406 +int is_shared_symref(const struct worktree *wt, const char *symref,
407 + const char *target)
408 {
410 - const struct worktree *existing = NULL;
411 - int i = 0;
409 + const char *symref_target;
410 + struct ref_store *refs;
411 + int flags;
412
413 - for (i = 0; worktrees[i]; i++) {
414 - struct worktree *wt = worktrees[i];
415 - const char *symref_target;
416 - struct ref_store *refs;
417 - int flags;
413 + if (wt->is_bare)
414 + return 0;
415
419 - if (wt->is_bare)
420 - continue;
416 + if (wt->is_detached && !strcmp(symref, "HEAD")) {
417 + if (is_worktree_being_rebased(wt, target))
418 + return 1;
419 + if (is_worktree_being_bisected(wt, target))
420 + return 1;
421 + }
422
422 - if (wt->is_detached && !strcmp(symref, "HEAD")) {
423 - if (is_worktree_being_rebased(wt, target)) {
424 - existing = wt;
425 - break;
426 - }
427 - if (is_worktree_being_bisected(wt, target)) {
428 - existing = wt;
429 - break;
430 - }
431 - }
423 + refs = get_worktree_ref_store(wt);
424 + symref_target = refs_resolve_ref_unsafe(refs, symref, 0,
425 + NULL, &flags);
426 + if ((flags & REF_ISSYMREF) &&
427 + symref_target && !strcmp(symref_target, target))
428 + return 1;
429
433 - refs = get_worktree_ref_store(wt);
434 - symref_target = refs_resolve_ref_unsafe(refs, symref, 0,
435 - NULL, &flags);
436 - if ((flags & REF_ISSYMREF) &&
437 - symref_target && !strcmp(symref_target, target)) {
438 - existing = wt;
439 - break;
440 - }
430 + return 0;
431 +}
432 +
433 +const struct worktree *find_shared_symref(struct worktree **worktrees,
434 + const char *symref,
435 + const char *target)
436 +{
437 +
438 + for (int i = 0; worktrees[i]; i++) {
439 + if (is_shared_symref(worktrees[i], symref, target))
440 + return worktrees[i];
441 }
442
443 - return existing;
443 + return NULL;
444 }
445
446 int submodule_uses_worktrees(const char *path)
worktree.h
+6
@@ -149,6 +149,12 @@ const struct worktree *find_shared_symref(struct worktree **worktrees,
149 const char *symref,
150 const char *target);
151
152 +/*
153 + * Returns true if a symref points to a ref in a worktree.
154 + */
155 +int is_shared_symref(const struct worktree *wt,
156 + const char *symref, const char *target);
157 +
158 /*
159 * Similar to head_ref() for all HEADs _except_ one from the current
160 * worktree, which is covered by head_ref().