Skip to content

Commit 70999e9

Browse files
rheniumgitster
authored andcommitted
branch -m: update all per-worktree HEADs
When renaming a branch, currently only the HEAD of current working tree is updated, but it must update HEADs of all working trees which point at the old branch. This is the current behavior, /path/to/wt's HEAD is not updated: % git worktree list /path/to 2c3c5f2 [master] /path/to/wt 2c3c5f2 [oldname] % git branch -m master master2 % git worktree list /path/to 2c3c5f2 [master2] /path/to/wt 2c3c5f2 [oldname] % git branch -m oldname newname % git worktree list /path/to 2c3c5f2 [master2] /path/to/wt 0000000 [oldname] This patch fixes this issue by updating all relevant worktree HEADs when renaming a branch. Signed-off-by: Kazuki Yamaguchi <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2233066 commit 70999e9

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

branch.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,26 @@ void die_if_checked_out(const char *branch)
344344
die(_("'%s' is already checked out at '%s'"), branch, existing);
345345
}
346346
}
347+
348+
int replace_each_worktree_head_symref(const char *oldref, const char *newref)
349+
{
350+
int ret = 0;
351+
struct worktree **worktrees = get_worktrees();
352+
int i;
353+
354+
for (i = 0; worktrees[i]; i++) {
355+
if (worktrees[i]->is_detached)
356+
continue;
357+
if (strcmp(oldref, worktrees[i]->head_ref))
358+
continue;
359+
360+
if (set_worktree_head_symref(worktrees[i]->git_dir, newref)) {
361+
ret = -1;
362+
error(_("HEAD of working tree %s is not updated"),
363+
worktrees[i]->path);
364+
}
365+
}
366+
367+
free_worktrees(worktrees);
368+
return ret;
369+
}

branch.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,11 @@ extern int read_branch_desc(struct strbuf *, const char *branch_name);
6060
*/
6161
extern void die_if_checked_out(const char *branch);
6262

63+
/*
64+
* Update all per-worktree HEADs pointing at the old ref to point the new ref.
65+
* This will be used when renaming a branch. Returns 0 if successful, non-zero
66+
* otherwise.
67+
*/
68+
extern int replace_each_worktree_head_symref(const char *oldref, const char *newref);
69+
6370
#endif

builtin/branch.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,7 @@ static void rename_branch(const char *oldname, const char *newname, int force)
552552
if (recovery)
553553
warning(_("Renamed a misnamed branch '%s' away"), oldref.buf + 11);
554554

555-
/* no need to pass logmsg here as HEAD didn't really move */
556-
if (!strcmp(oldname, head) && create_symref("HEAD", newref.buf, NULL))
555+
if (replace_each_worktree_head_symref(oldref.buf, newref.buf))
557556
die(_("Branch renamed to %s, but HEAD is not updated!"), newname);
558557

559558
strbuf_addf(&oldsection, "branch.%s", oldref.buf + 11);

t/t3200-branch.sh

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,28 @@ test_expect_success 'git branch -M foo bar should fail when bar is checked out'
126126
test_expect_success 'git branch -M baz bam should succeed when baz is checked out' '
127127
git checkout -b baz &&
128128
git branch bam &&
129-
git branch -M baz bam
129+
git branch -M baz bam &&
130+
test $(git rev-parse --abbrev-ref HEAD) = bam
131+
'
132+
133+
test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' '
134+
git checkout master &&
135+
git worktree add -b baz bazdir &&
136+
git worktree add -f bazdir2 baz &&
137+
git branch -M baz bam &&
138+
test $(git -C bazdir rev-parse --abbrev-ref HEAD) = bam &&
139+
test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam
140+
'
141+
142+
test_expect_success 'git branch -M baz bam should succeed within a worktree in which baz is checked out' '
143+
git checkout -b baz &&
144+
git worktree add -f bazdir3 baz &&
145+
(
146+
cd bazdir3 &&
147+
git branch -M baz bam &&
148+
test $(git rev-parse --abbrev-ref HEAD) = bam
149+
) &&
150+
test $(git rev-parse --abbrev-ref HEAD) = bam
130151
'
131152

132153
test_expect_success 'git branch -M master should work when master is checked out' '

0 commit comments

Comments
 (0)