Subject: [BUG] checkout --orphan breaks HEAD reflog

From: Aleksander Nitecki <[email-masked]>
Date: Mon, 14 Jul 2014 21:44:25 +0200

Hi, this is my first mail to this list (and I'm not a native English speaker), so let me apologise from the start for any slips in following message.

Ok, so I think I've found a bug in git.
It concerns orphan checkouts breaking (gapping in non-traversable way) the HEAD reflog display.

% git --version
git version 2.0.1

# Repro commands; output omitted.

% mkdir /tmp/git-orphan-checkout-reflog-bug
% cd /tmp/git-orphan-checkout-reflog-bug
% git init
% echo a  > foo; git add foo; git commit -m 'newly created master; rev. a'
% echo b >> foo; git add foo; git commit -m 'master; rev. b'
% echo c >> foo; git add foo; git commit -m 'master; rev. c'
% git checkout --orphan some_orphan_branch
% echo 1  > bar; git add bar; git commit -m 'newly created orphan branch; rev. 1'
% echo 2 >> bar; git add bar; git commit -m 'orphan branch; rev. 2'
% echo 3 >> bar; git add bar; git commit -m 'orphan branch; rev. 3'

# Actual (IMHO buggy) output

% git reflog HEAD
1af4e3c HEAD@{0}: commit: orphan branch; rev. 3
6d200b4 HEAD@{1}: commit: orphan branch; rev. 2
ed4ad67 HEAD@{2}: commit (initial): newly created orphan branch; rev. 1

% git reflog HEAD@{3}
warning: Log .git/logs/HEAD has gap after Mon, 14 Jul 2014 20:55:05 +0200.
0cada62 HEAD@{3}: commit: master; rev. c
42d7cc3 HEAD@{4}: commit: master; rev. b
222a0b3 HEAD@{5}: commit (initial): newly created master; rev. a

# Expected output:

% git reflog HEAD
1af4e3c HEAD@{0}: commit: orphan branch; rev. 3
6d200b4 HEAD@{1}: commit: orphan branch; rev. 2
ed4ad67 HEAD@{2}: commit (initial): newly created orphan branch; rev. 1
warning: Log .git/logs/HEAD has gap after Mon, 14 Jul 2014 20:55:05 +0200.
0cada62 HEAD@{3}: commit: master; rev. c
42d7cc3 HEAD@{4}: commit: master; rev. b
222a0b3 HEAD@{5}: commit (initial): newly created master; rev. a

Well, actually git doesn't seem to show warning messages when traversing reflogs with other gaps, but only when started from point where a gap is.
Whether those warnings should be displayed while traversing the reflog, not only when starting from gapped entries is another matter, but I think it could be useful.

# Raw reflog file (shortened to fit in 80 columns).
# Omissions marked with ##. Full contents in attachment.
# (does sending attachments to mailing list even work? Sorry, if it doesn't.)

% cat .git/logs/HEAD
00000## 222a0## ## commit (initial): newly created master; rev. a
222a0## 42d7c## ## commit: master; rev. b
42d7c## 0cada## ## commit: master; rev. c
00000## ed4ad## ## commit (initial): newly created orphan branch; rev. 1
ed4ad## 6d200## ## commit: orphan branch; rev. 2
6d200## 1af4e## ## commit: orphan branch; rev. 3

So the problem seems to be that git stops traversing the reflog after finding first initial commit (all-0s hash) and completely ignores rest of log.
The problem is that this commit is initial only to the orphaned branch, not to the whole repo.