Skip to content

Commit 2272d3e

Browse files
peffgitster
authored andcommitted
reflog-walk: skip over double-null oid due to HEAD rename
Since 39ee4c6 (branch: record creation of renamed branch in HEAD's log, 2017-02-20), a rename on the currently checked out branch will create two entries in the HEAD reflog: one where the branch goes away (switching to the null oid), and one where it comes back (switching away from the null oid). This confuses the reflog-walk code. When walking backwards, it first sees the null oid in the "old" field of the second entry. Thanks to the "root commit" logic added by 71abeb7 (reflog: continue walking the reflog past root commits, 2016-06-03), we keep looking for the next entry by scanning the "new" field from the previous entry. But that field is also null! We need to go just a tiny bit further, and look at its "old" field. But with the current code, we decide the reflog has nothing else to show and just give up. To the user this looks like the reflog was truncated by the rename operation, when in fact those entries are still there. This patch does the absolute minimal fix, which is to look back that one extra level and keep traversing. The resulting behavior may not be the _best_ thing to do in the long run (for example, we show both reflog entries each with the same commit id), but it's a simple way to fix the problem without risking further regressions. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8c8e978 commit 2272d3e

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

reflog-walk.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)
259259
/* a root commit, but there are still more entries to show */
260260
reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
261261
logobj = parse_object(reflog->noid.hash);
262+
if (!logobj)
263+
logobj = parse_object(reflog->ooid.hash);
262264
}
263265

264266
if (!logobj || logobj->type != OBJ_COMMIT) {

t/t3200-branch.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,17 @@ 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 'resulting reflog can be shown by log -g' '
166+
oid=$(git rev-parse HEAD) &&
167+
cat >expect <<-EOF &&
168+
HEAD@{0} $oid $msg
169+
HEAD@{1} $oid $msg
170+
HEAD@{2} $oid checkout: moving from foo to baz
171+
EOF
172+
git log -g --format="%gd %H %gs" -3 HEAD >actual &&
173+
test_cmp expect actual
174+
'
175+
165176
test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' '
166177
git checkout master &&
167178
git worktree add -b baz bazdir &&

0 commit comments

Comments
 (0)