Skip to content

Commit 5253ad1

Browse files
committed
Merge branch 'nd/worktree-kill-parse-ref' into maint
"git branch -M a b" while on a branch that is completely unrelated to either branch a or branch b misbehaved when multiple worktree was in use. This has been fixed. * nd/worktree-kill-parse-ref: branch: fix branch renaming not updating HEADs correctly
2 parents 4c2224e + 31824d1 commit 5253ad1

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
@@ -162,6 +162,19 @@ test_expect_success 'git branch -M baz bam should add entries to .git/logs/HEAD'
162162
grep "^0\{40\}.*$msg$" .git/logs/HEAD
163163
'
164164

165+
test_expect_success 'git branch -M should leave orphaned HEAD alone' '
166+
git init orphan &&
167+
(
168+
cd orphan &&
169+
test_commit initial &&
170+
git checkout --orphan lonely &&
171+
grep lonely .git/HEAD &&
172+
test_path_is_missing .git/refs/head/lonely &&
173+
git branch -M master mistress &&
174+
grep lonely .git/HEAD
175+
)
176+
'
177+
165178
test_expect_success 'resulting reflog can be shown by log -g' '
166179
oid=$(git rev-parse HEAD) &&
167180
cat >expect <<-EOF &&

worktree.c

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

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

0 commit comments

Comments
 (0)