Skip to content

Commit 71abeb7

Browse files
szedergitster
authored andcommitted
reflog: continue walking the reflog past root commits
If a repository contains more than one root commit, then its HEAD reflog may contain multiple "creation events", i.e. entries whose "from" value is the null sha1. Listing such a reflog currently stops prematurely at the first such entry, even when the reflog still contains older entries. This can scare users into thinking that their reflog got truncated after 'git checkout --orphan'. Continue walking the reflog past such creation events based on the preceeding reflog entry's "new" value. The test 'symbolic-ref writes reflog entry' in t1401-symbolic-ref implicitly relies on the current behavior of the reflog walker to stop at a root commit and thus to list only the reflog entries that are relevant for that test. Adjust the test to explicitly specify the number of relevant reflog entries to be listed. Reported-by: Patrik Gustafsson <[email protected]> Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 49fa3dc commit 71abeb7

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

reflog-walk.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)
241241
logobj = parse_object(reflog->osha1);
242242
} while (commit_reflog->recno && (logobj && logobj->type != OBJ_COMMIT));
243243

244+
if (!logobj && commit_reflog->recno >= 0 && is_null_sha1(reflog->osha1)) {
245+
/* a root commit, but there are still more entries to show */
246+
reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
247+
logobj = parse_object(reflog->nsha1);
248+
}
249+
244250
if (!logobj || logobj->type != OBJ_COMMIT) {
245251
commit_info->commit = NULL;
246252
commit->parents = NULL;

t/t1401-symbolic-ref.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ test_expect_success 'symbolic-ref writes reflog entry' '
110110
update
111111
create
112112
EOF
113-
git log --format=%gs -g >actual &&
113+
git log --format=%gs -g -2 >actual &&
114114
test_cmp expect actual
115115
'
116116

t/t1410-reflog.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,4 +348,26 @@ test_expect_success 'reflog expire operates on symref not referrent' '
348348
git reflog expire --expire=all the_symref
349349
'
350350

351+
test_expect_success 'continue walking past root commits' '
352+
git init orphanage &&
353+
(
354+
cd orphanage &&
355+
cat >expect <<-\EOF &&
356+
HEAD@{0} commit (initial): orphan2-1
357+
HEAD@{1} commit: orphan1-2
358+
HEAD@{2} commit (initial): orphan1-1
359+
HEAD@{3} commit (initial): initial
360+
EOF
361+
test_commit initial &&
362+
git reflog &&
363+
git checkout --orphan orphan1 &&
364+
test_commit orphan1-1 &&
365+
test_commit orphan1-2 &&
366+
git checkout --orphan orphan2 &&
367+
test_commit orphan2-1 &&
368+
git log -g --format="%gd %gs" >actual &&
369+
test_cmp expect actual
370+
)
371+
'
372+
351373
test_done

0 commit comments

Comments
 (0)