Skip to content

Commit 662078c

Browse files
rjustogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 56c8fb1 commit 662078c

File tree

2 files changed

+37
-31
lines changed

2 files changed

+37
-31
lines changed

worktree.c

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -403,44 +403,44 @@ int is_worktree_being_bisected(const struct worktree *wt,
403403
* bisect). New commands that do similar things should update this
404404
* function as well.
405405
*/
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)
409408
{
410-
const struct worktree *existing = NULL;
411-
int i = 0;
409+
const char *symref_target;
410+
struct ref_store *refs;
411+
int flags;
412412

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;
418415

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+
}
421422

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;
432429

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];
441441
}
442442

443-
return existing;
443+
return NULL;
444444
}
445445

446446
int submodule_uses_worktrees(const char *path)

worktree.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ const struct worktree *find_shared_symref(struct worktree **worktrees,
149149
const char *symref,
150150
const char *target);
151151

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+
152158
/*
153159
* Similar to head_ref() for all HEADs _except_ one from the current
154160
* worktree, which is covered by head_ref().

0 commit comments

Comments
 (0)