Skip to content

Commit ffa1eea

Browse files
peffgitster
authored andcommitted
reflogs: clear flags properly in corner case
The reflog-walking mechanism is based on the regular revision traversal. We just rewrite the parents of each commit in fake_reflog_parent to point to the commit in the next reflog entry instead of the real parents. However, the regular revision traversal tries not to show the same commit twice, and so sets the SHOWN flag on each commit it shows. In a reflog, however, we may want to see the same commit more than once if it appears in the reflog multiple times (which easily happens, for example, if you do a reset to a prior state). The fake_reflog_parent function takes care of this by clearing flags, including SHOWN. Unfortunately, it does so at the very end of the function, and it is possible to return early from the function if there is no fake parent to set up (e.g., because we are at the very first reflog entry on the branch). In such a case the flag is not cleared, and the entry is skipped by the revision traversal machinery as already shown. You can see this by walking the log of a ref which is set to its very first commit more than once (the test below shows such a situation). In this case the reflog walk will fail to show the entry for the initial creation of the ref. We don't want to simply move the flag-clearing to the top of the function; we want to make sure flags set during the fake-parent installation are also cleared. Instead, let's hoist the flag-clearing out of the fake_reflog_parent function entirely. It's not really about fake parents anyway, and the only caller is the get_revision machinery. Reported-by: Martin von Zweigbergk <[email protected]> Signed-off-by: Jeff King <[email protected]> Acked-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 520ea85 commit ffa1eea

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

reflog-walk.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)
239239

240240
commit->parents = xcalloc(sizeof(struct commit_list), 1);
241241
commit->parents->item = commit_info->commit;
242-
commit->object.flags &= ~(ADDED | SEEN | SHOWN);
243242
}
244243

245244
void get_reflog_selector(struct strbuf *sb,

revision.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1896,8 +1896,10 @@ static struct commit *get_revision_1(struct rev_info *revs)
18961896
revs->commits = entry->next;
18971897
free(entry);
18981898

1899-
if (revs->reflog_info)
1899+
if (revs->reflog_info) {
19001900
fake_reflog_parent(revs->reflog_info, commit);
1901+
commit->object.flags &= ~(ADDED | SEEN | SHOWN);
1902+
}
19011903

19021904
/*
19031905
* If we haven't done the list limiting, we need to look at

t/t1412-reflog-loop.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/sh
2+
3+
test_description='reflog walk shows repeated commits again'
4+
. ./test-lib.sh
5+
6+
test_expect_success 'setup commits' '
7+
test_tick &&
8+
echo content >file && git add file && git commit -m one &&
9+
git tag one &&
10+
echo content >>file && git add file && git commit -m two &&
11+
git tag two
12+
'
13+
14+
test_expect_success 'setup reflog with alternating commits' '
15+
git checkout -b topic &&
16+
git reset one &&
17+
git reset two &&
18+
git reset one &&
19+
git reset two
20+
'
21+
22+
test_expect_success 'reflog shows all entries' '
23+
cat >expect <<-\EOF
24+
topic@{0} two: updating HEAD
25+
topic@{1} one: updating HEAD
26+
topic@{2} two: updating HEAD
27+
topic@{3} one: updating HEAD
28+
topic@{4} branch: Created from HEAD
29+
EOF
30+
git log -g --format="%gd %gs" topic >actual &&
31+
test_cmp expect actual
32+
'
33+
34+
test_done

0 commit comments

Comments
 (0)