Skip to content

Commit a675ad1

Browse files
rjustogitster
authored andcommitted
branch: rename orphan branches in any worktree
In cfaff3a (branch -m: allow renaming a yet-unborn branch, 2020-12-13) we added support for renaming an orphan branch when that branch is checked out in the current worktree. Let's also allow renaming an orphan branch checked out in a worktree different than the current one. Signed-off-by: Rubén Justo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7a6ccdf commit a675ad1

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

builtin/branch.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,7 @@ static int replace_each_worktree_head_symref(struct worktree **worktrees,
539539
}
540540

541541
#define IS_HEAD 1
542+
#define IS_ORPHAN 2
542543

543544
static void copy_or_rename_branch(const char *oldname, const char *newname, int copy, int force)
544545
{
@@ -565,6 +566,8 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
565566

566567
if (wt->head_ref && !strcmp(oldref.buf, wt->head_ref)) {
567568
oldref_usage |= IS_HEAD;
569+
if (is_null_oid(&wt->head_oid))
570+
oldref_usage |= IS_ORPHAN;
568571
break;
569572
}
570573
}
@@ -599,8 +602,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
599602
strbuf_addf(&logmsg, "Branch: renamed %s to %s",
600603
oldref.buf, newref.buf);
601604

602-
if (!copy &&
603-
(!head || strcmp(oldname, head) || !is_null_oid(&head_oid)) &&
605+
if (!copy && !(oldref_usage & IS_ORPHAN) &&
604606
rename_ref(oldref.buf, newref.buf, logmsg.buf))
605607
die(_("Branch rename failed"));
606608
if (copy && copy_existing_ref(oldref.buf, newref.buf, logmsg.buf))

t/t3200-branch.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,20 @@ test_expect_success 'git branch -M and -C fail on detached HEAD' '
294294
test_cmp expect err
295295
'
296296

297+
test_expect_success 'git branch -m should work with orphan branches' '
298+
test_when_finished git checkout - &&
299+
test_when_finished git worktree remove -f wt &&
300+
git worktree add wt --detach &&
301+
# rename orphan in another worktreee
302+
git -C wt checkout --orphan orphan-foo-wt &&
303+
git branch -m orphan-foo-wt orphan-bar-wt &&
304+
test orphan-bar-wt=$(git -C orphan-worktree branch --show-current) &&
305+
# rename orphan in the current worktree
306+
git checkout --orphan orphan-foo &&
307+
git branch -m orphan-foo orphan-bar &&
308+
test orphan-bar=$(git branch --show-current)
309+
'
310+
297311
test_expect_success 'git branch -d on orphan HEAD (merged)' '
298312
test_when_finished git checkout main &&
299313
git checkout --orphan orphan &&

0 commit comments

Comments
 (0)