Skip to content

Commit

Permalink
log: use true parents for diff when walking reflogs
Browse files Browse the repository at this point in the history
The reflog walking logic (git log -g) replaces the true parent list
with the preceding commit in the reflog.  This results in bogus commit
diffs when combined with options such as -p; the diff is against the
reflog predecessor, not the parent of the commit.

Save the true parents on the side, extending the functions from the
previous commit.  The diff logic picks them up and uses them to show
the correct diffs.

We do have to be somewhat careful about repeated calling of
save_parents(), since the reflog may list a commit more than once.  We
now store (commit_list*)-1 to distinguish the "not saved yet" and
"root commit" cases.  This lets us preserve an empty parent list even
if save_parents() is repeatedly called.

Suggested-by: Jeff King <peff@peff.net>
Signed-off-by: Thomas Rast <trast@inf.ethz.ch>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Thomas Rast authored and Junio C Hamano committed Aug 5, 2013
1 parent 53d00b3 commit 838f9a1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
28 changes: 25 additions & 3 deletions revision.c
Original file line number Diff line number Diff line change
Expand Up @@ -2848,6 +2848,7 @@ static struct commit *get_revision_1(struct rev_info *revs)
free(entry);

if (revs->reflog_info) {
save_parents(revs, commit);
fake_reflog_parent(revs->reflog_info, commit);
commit->object.flags &= ~(ADDED | SEEN | SHOWN);
}
Expand Down Expand Up @@ -3083,6 +3084,8 @@ void put_revision_mark(const struct rev_info *revs, const struct commit *commit)

define_commit_slab(saved_parents, struct commit_list *);

#define EMPTY_PARENT_LIST ((struct commit_list *)-1)

void save_parents(struct rev_info *revs, struct commit *commit)
{
struct commit_list **pp;
Expand All @@ -3093,16 +3096,35 @@ void save_parents(struct rev_info *revs, struct commit *commit)
}

pp = saved_parents_at(revs->saved_parents_slab, commit);
assert(*pp == NULL);
*pp = copy_commit_list(commit->parents);

/*
* When walking with reflogs, we may visit the same commit
* several times: once for each appearance in the reflog.
*
* In this case, save_parents() will be called multiple times.
* We want to keep only the first set of parents. We need to
* store a sentinel value for an empty (i.e., NULL) parent
* list to distinguish it from a not-yet-saved list, however.
*/
if (*pp)
return;
if (commit->parents)
*pp = copy_commit_list(commit->parents);
else
*pp = EMPTY_PARENT_LIST;
}

struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
{
struct commit_list *parents;

if (!revs->saved_parents_slab)
return commit->parents;

return *saved_parents_at(revs->saved_parents_slab, commit);
parents = *saved_parents_at(revs->saved_parents_slab, commit);
if (parents == EMPTY_PARENT_LIST)
return NULL;
return parents;
}

void free_saved_parents(struct rev_info *revs)
Expand Down
22 changes: 22 additions & 0 deletions t/t1411-reflog-show.sh
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,26 @@ test_expect_success 'empty reflog file' '
test_cmp expect actual
'

# This guards against the alternative of showing the diffs vs. the
# reflog ancestor. The reflog used is designed to list the commits
# more than once, so as to exercise the corresponding logic.
test_expect_success 'git log -g -p shows diffs vs. parents' '
test_commit two &&
git branch flipflop &&
git update-ref refs/heads/flipflop -m flip1 HEAD^ &&
git update-ref refs/heads/flipflop -m flop1 HEAD &&
git update-ref refs/heads/flipflop -m flip2 HEAD^ &&
git log -g -p flipflop >reflog &&
grep -v ^Reflog reflog >actual &&
git log -1 -p HEAD^ >log.one &&
git log -1 -p HEAD >log.two &&
(
cat log.one; echo
cat log.two; echo
cat log.one; echo
cat log.two
) >expect &&
test_cmp expect actual
'

test_done

0 comments on commit 838f9a1

Please sign in to comment.