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 <k@rhe.jp> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kazuki Yamaguchi committed Mar 27, 2016 at 23:37 UTC 70999e9ceca47e03b8900bfb310b2f804125811e
4 files changed +53 -3
branch.c
+23
@@ -344,3 +344,26 @@ void die_if_checked_out(const char *branch)
344 die(_("'%s' is already checked out at '%s'"), branch, existing);
345 }
346 }
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
+7
@@ -60,4 +60,11 @@ extern int read_branch_desc(struct strbuf *, const char *branch_name);
60 */
61 extern void die_if_checked_out(const char *branch);
62
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 +
70 #endif
builtin/branch.c
+1 -2
@@ -552,8 +552,7 @@ static void rename_branch(const char *oldname, const char *newname, int force)
552 if (recovery)
553 warning(_("Renamed a misnamed branch '%s' away"), oldref.buf + 11);
554
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))
556 die(_("Branch renamed to %s, but HEAD is not updated!"), newname);
557
558 strbuf_addf(&oldsection, "branch.%s", oldref.buf + 11);
t/t3200-branch.sh
+22 -1
@@ -126,7 +126,28 @@ test_expect_success 'git branch -M foo bar should fail when bar is checked out'
126 test_expect_success 'git branch -M baz bam should succeed when baz is checked out' '
127 git checkout -b baz &&
128 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
151 '
152
153 test_expect_success 'git branch -M master should work when master is checked out' '