Skip to content

Commit d7f4ca6

Browse files
rjustogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2e8af49 commit d7f4ca6

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

builtin/branch.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,9 @@ static void print_current_branch_name(void)
486486
die(_("HEAD (%s) points outside of refs/heads/"), refname);
487487
}
488488

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)
490491
{
491-
struct worktree **worktrees = get_worktrees();
492492
int i;
493493

494494
for (i = 0; worktrees[i]; i++) {
@@ -505,20 +505,18 @@ static void reject_rebase_or_bisect_branch(const char *target)
505505
die(_("Branch %s is being bisected at %s"),
506506
target, wt->path);
507507
}
508-
509-
free_worktrees(worktrees);
510508
}
511509

512510
/*
513511
* Update all per-worktree HEADs pointing at the old ref to point the new ref.
514512
* This will be used when renaming a branch. Returns 0 if successful, non-zero
515513
* otherwise.
516514
*/
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,
518517
const char *logmsg)
519518
{
520519
int ret = 0;
521-
struct worktree **worktrees = get_worktrees();
522520
int i;
523521

524522
for (i = 0; worktrees[i]; i++) {
@@ -537,7 +535,6 @@ static int replace_each_worktree_head_symref(const char *oldref, const char *new
537535
worktrees[i]->path);
538536
}
539537

540-
free_worktrees(worktrees);
541538
return ret;
542539
}
543540

@@ -548,6 +545,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
548545
const char *interpreted_oldname = NULL;
549546
const char *interpreted_newname = NULL;
550547
int recovery = 0;
548+
struct worktree **worktrees = get_worktrees();
551549

552550
if (strbuf_check_branch_ref(&oldref, oldname)) {
553551
/*
@@ -576,7 +574,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
576574
else
577575
validate_new_branchname(newname, &newref, force);
578576

579-
reject_rebase_or_bisect_branch(oldref.buf);
577+
reject_rebase_or_bisect_branch(worktrees, oldref.buf);
580578

581579
if (!skip_prefix(oldref.buf, "refs/heads/", &interpreted_oldname) ||
582580
!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
607605
}
608606

609607
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))
611610
die(_("Branch renamed to %s, but HEAD is not updated!"), newname);
612611

613612
strbuf_release(&logmsg);
@@ -622,6 +621,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
622621
strbuf_release(&newref);
623622
strbuf_release(&oldsection);
624623
strbuf_release(&newsection);
624+
free_worktrees(worktrees);
625625
}
626626

627627
static GIT_PATH_FUNC(edit_description, "EDIT_DESCRIPTION")

0 commit comments

Comments
 (0)