Skip to content

Commit 31824d1

Browse files
pcloudsgitster
authored andcommitted
branch: fix branch renaming not updating HEADs correctly
There are two bugs that sort of work together and cause problems. Let's start with one in replace_each_worktree_head_symref. Before fa099d2 (worktree.c: kill parse_ref() in favor of refs_resolve_ref_unsafe() - 2017-04-24), this code looks like this: if (strcmp(oldref, worktrees[i]->head_ref)) continue; set_worktree_head_symref(...); After fa099d2, it is possible that head_ref can be NULL. However, the updated code takes the wrong exit. In the error case (NULL head_ref), we should "continue;" to the next worktree. The updated code makes us _skip_ "continue;" and update HEAD anyway. The NULL head_ref is triggered by the second bug in add_head_info (in the same commit). With the flag RESOLVE_REF_READING, resolve_ref_unsafe() will abort if it cannot resolve the target ref. For orphan checkouts, HEAD always points to an unborned branch, resolving target ref will always fail. Now we have NULL head_ref. Now we always update HEAD. Correct the logic in replace_ function so that we don't accidentally update HEAD on error. As it turns out, correcting the logic bug above breaks branch renaming completely, thanks to the second bug. "git branch -[Mm]" does two steps (on a normal checkout, no orphan!): - rename the branch on disk (e.g. refs/heads/abc to refs/heads/def) - update HEAD if it points to the branch being renamed. At the second step, since the branch pointed to by HEAD (e.g. "abc") no longer exists on disk, we run into a temporary orphan checkout situation that has been just corrected to _not_ update HEAD. But we need to update HEAD since it's not actually an orphan checkout. We need to update HEAD to move out of that orphan state. Correct add_head_info(), remove RESOLVE_REF_READING flag. With the flag gone, we should always return good "head_ref" in orphan checkouts (either temporary or permanent). With good head_ref, things start to work again. Noticed-by: Nish Aravamudan <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d026a25 commit 31824d1

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

branch.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,9 @@ int replace_each_worktree_head_symref(const char *oldref, const char *newref,
357357

358358
if (worktrees[i]->is_detached)
359359
continue;
360-
if (worktrees[i]->head_ref &&
361-
strcmp(oldref, worktrees[i]->head_ref))
360+
if (!worktrees[i]->head_ref)
361+
continue;
362+
if (strcmp(oldref, worktrees[i]->head_ref))
362363
continue;
363364

364365
refs = get_worktree_ref_store(worktrees[i]);

t/t3200-branch.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ test_expect_success 'git branch -M baz bam should add entries to .git/logs/HEAD'
145145
grep "^0\{40\}.*$msg$" .git/logs/HEAD
146146
'
147147

148+
test_expect_success 'git branch -M should leave orphaned HEAD alone' '
149+
git init orphan &&
150+
(
151+
cd orphan &&
152+
test_commit initial &&
153+
git checkout --orphan lonely &&
154+
grep lonely .git/HEAD &&
155+
test_path_is_missing .git/refs/head/lonely &&
156+
git branch -M master mistress &&
157+
grep lonely .git/HEAD
158+
)
159+
'
160+
148161
test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' '
149162
git checkout master &&
150163
git worktree add -b baz bazdir &&

worktree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static void add_head_info(struct worktree *wt)
2929

3030
target = refs_resolve_ref_unsafe(get_worktree_ref_store(wt),
3131
"HEAD",
32-
RESOLVE_REF_READING,
32+
0,
3333
wt->head_sha1, &flags);
3434
if (!target)
3535
return;

0 commit comments

Comments
 (0)