Skip to content

Commit 741a694

Browse files
committed
Merge branch 'ky/branch-m-worktree'
When "git worktree" feature is in use, "git branch -m" renamed a branch that is checked out in another worktree without adjusting the HEAD symbolic ref for the worktree. * ky/branch-m-worktree: set_worktree_head_symref(): fix error message branch -m: update all per-worktree HEADs refs: add a new function set_worktree_head_symref
2 parents b8b4d93 + 18eb3a9 commit 741a694

File tree

6 files changed

+98
-3
lines changed

6 files changed

+98
-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
@@ -558,8 +558,7 @@ static void rename_branch(const char *oldname, const char *newname, int force)
558558
if (recovery)
559559
warning(_("Renamed a misnamed branch '%s' away"), oldref.buf + 11);
560560

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

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

refs.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,15 @@ extern int rename_ref(const char *oldref, const char *newref, const char *logmsg
306306

307307
extern int create_symref(const char *refname, const char *target, const char *logmsg);
308308

309+
/*
310+
* Update HEAD of the specified gitdir.
311+
* Similar to create_symref("relative-git-dir/HEAD", target, NULL), but
312+
* this can update the main working tree's HEAD regardless of where
313+
* $GIT_DIR points to.
314+
* Return 0 if successful, non-zero otherwise.
315+
* */
316+
extern int set_worktree_head_symref(const char *gitdir, const char *target);
317+
309318
enum action_on_err {
310319
UPDATE_REFS_MSG_ON_ERR,
311320
UPDATE_REFS_DIE_ON_ERR,

refs/files-backend.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2894,6 +2894,42 @@ int create_symref(const char *refname, const char *target, const char *logmsg)
28942894
return ret;
28952895
}
28962896

2897+
int set_worktree_head_symref(const char *gitdir, const char *target)
2898+
{
2899+
static struct lock_file head_lock;
2900+
struct ref_lock *lock;
2901+
struct strbuf head_path = STRBUF_INIT;
2902+
const char *head_rel;
2903+
int ret;
2904+
2905+
strbuf_addf(&head_path, "%s/HEAD", absolute_path(gitdir));
2906+
if (hold_lock_file_for_update(&head_lock, head_path.buf,
2907+
LOCK_NO_DEREF) < 0) {
2908+
struct strbuf err = STRBUF_INIT;
2909+
unable_to_lock_message(head_path.buf, errno, &err);
2910+
error("%s", err.buf);
2911+
strbuf_release(&err);
2912+
strbuf_release(&head_path);
2913+
return -1;
2914+
}
2915+
2916+
/* head_rel will be "HEAD" for the main tree, "worktrees/wt/HEAD" for
2917+
linked trees */
2918+
head_rel = remove_leading_path(head_path.buf,
2919+
absolute_path(get_git_common_dir()));
2920+
/* to make use of create_symref_locked(), initialize ref_lock */
2921+
lock = xcalloc(1, sizeof(struct ref_lock));
2922+
lock->lk = &head_lock;
2923+
lock->ref_name = xstrdup(head_rel);
2924+
lock->orig_ref_name = xstrdup(head_rel);
2925+
2926+
ret = create_symref_locked(lock, head_rel, target, NULL);
2927+
2928+
unlock_ref(lock); /* will free lock */
2929+
strbuf_release(&head_path);
2930+
return ret;
2931+
}
2932+
28972933
int reflog_exists(const char *refname)
28982934
{
28992935
struct stat st;

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)