branch: use get_worktrees() in copy_or_rename_branch()

Obtaining the list of worktrees, using get_worktrees(), is not a lightweight operation, because it involves reading from disk. Let's stop calling get_worktrees() in reject_rebase_or_bisect_branch() and in replace_each_worktree_head_symref(). Make them receive the list of worktrees from their only caller, copy_or_rename_branch(). Signed-off-by: Rubén Justo <rjusto@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Rubén Justo committed Mar 27, 2023 at 00:33 UTC d7f4ca61b51da5655df09277309380794ba1bd19
1 file changed +9 -9
builtin/branch.c
+9 -9
@@ -486,9 +486,9 @@ static void print_current_branch_name(void)
486 die(_("HEAD (%s) points outside of refs/heads/"), refname);
487 }
488
489 -static void reject_rebase_or_bisect_branch(const char *target)
489 +static void reject_rebase_or_bisect_branch(struct worktree **worktrees,
490 + const char *target)
491 {
491 - struct worktree **worktrees = get_worktrees();
492 int i;
493
494 for (i = 0; worktrees[i]; i++) {
@@ -505,8 +505,6 @@ static void reject_rebase_or_bisect_branch(const char *target)
505 die(_("Branch %s is being bisected at %s"),
506 target, wt->path);
507 }
508 -
509 - free_worktrees(worktrees);
508 }
509
510 /*
@@ -514,11 +512,11 @@ static void reject_rebase_or_bisect_branch(const char *target)
512 * This will be used when renaming a branch. Returns 0 if successful, non-zero
513 * otherwise.
514 */
517 -static int replace_each_worktree_head_symref(const char *oldref, const char *newref,
515 +static int replace_each_worktree_head_symref(struct worktree **worktrees,
516 + const char *oldref, const char *newref,
517 const char *logmsg)
518 {
519 int ret = 0;
521 - struct worktree **worktrees = get_worktrees();
520 int i;
521
522 for (i = 0; worktrees[i]; i++) {
@@ -537,7 +535,6 @@ static int replace_each_worktree_head_symref(const char *oldref, const char *new
535 worktrees[i]->path);
536 }
537
540 - free_worktrees(worktrees);
538 return ret;
539 }
540
@@ -548,6 +545,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
545 const char *interpreted_oldname = NULL;
546 const char *interpreted_newname = NULL;
547 int recovery = 0;
548 + struct worktree **worktrees = get_worktrees();
549
550 if (strbuf_check_branch_ref(&oldref, oldname)) {
551 /*
@@ -576,7 +574,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
574 else
575 validate_new_branchname(newname, &newref, force);
576
579 - reject_rebase_or_bisect_branch(oldref.buf);
577 + reject_rebase_or_bisect_branch(worktrees, oldref.buf);
578
579 if (!skip_prefix(oldref.buf, "refs/heads/", &interpreted_oldname) ||
580 !skip_prefix(newref.buf, "refs/heads/", &interpreted_newname)) {
@@ -607,7 +605,8 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
605 }
606
607 if (!copy &&
610 - replace_each_worktree_head_symref(oldref.buf, newref.buf, logmsg.buf))
608 + replace_each_worktree_head_symref(worktrees, oldref.buf, newref.buf,
609 + logmsg.buf))
610 die(_("Branch renamed to %s, but HEAD is not updated!"), newname);
611
612 strbuf_release(&logmsg);
@@ -622,6 +621,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
621 strbuf_release(&newref);
622 strbuf_release(&oldsection);
623 strbuf_release(&newsection);
624 + free_worktrees(worktrees);
625 }
626
627 static GIT_PATH_FUNC(edit_description, "EDIT_DESCRIPTION")