git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] log: use true parents for diff when walking reflogs
@ 2013-08-03 10:36 Thomas Rast
  2013-08-03 10:40 ` Thomas Rast
  2013-08-05 19:16 ` Junio C Hamano
  0 siblings, 2 replies; 3+ messages in thread
From: Thomas Rast @ 2013-08-03 10:36 UTC (permalink / raw)
  To: Jeff King; +Cc: Junio C Hamano, git, Uwe Kleine-König, Ramsay Jones

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>
---

Jeff King <peff@peff.net> wrote:
> 
> Your description (and solution) make a lot of sense to me. Another code
> path that has a similar problem is the "-g" reflog walker. It rewrites
> the parents based on the reflog, and the diffs it produces are mostly
> useless (e.g., try "git stash list -p").
> 
> Should we be applying the same technique there?

Good point.  This is how.  It applies on top of the other patch.

It doesn't really help for 'git stash list -p', though, because
stashes are merge commits.  Now they just don't show anything.  You
could try 'git stash list -p -m', though.


 revision.c             | 28 +++++++++++++++++++++++++---
 t/t1411-reflog-show.sh | 22 ++++++++++++++++++++++
 2 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/revision.c b/revision.c
index e3ca936..ac20d1a 100644
--- a/revision.c
+++ b/revision.c
@@ -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);
 		}
@@ -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;
@@ -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)
diff --git a/t/t1411-reflog-show.sh b/t/t1411-reflog-show.sh
index 9a105fe..6f47c0d 100755
--- a/t/t1411-reflog-show.sh
+++ b/t/t1411-reflog-show.sh
@@ -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
-- 
1.8.4.rc1.414.g34bc5b2

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] log: use true parents for diff when walking reflogs
  2013-08-03 10:36 [PATCH] log: use true parents for diff when walking reflogs Thomas Rast
@ 2013-08-03 10:40 ` Thomas Rast
  2013-08-05 19:16 ` Junio C Hamano
  1 sibling, 0 replies; 3+ messages in thread
From: Thomas Rast @ 2013-08-03 10:40 UTC (permalink / raw)
  To: Jeff King; +Cc: Junio C Hamano, git, Uwe Kleine-König, Ramsay Jones

Doh, once again I forgot the in-reply-to.  This patch continues the
thread after <20130731225520.GB25882@sigill.intra.peff.net>.  This is --
strangely enough -- missing from gmane; its immediate predecessor is
this:

  http://thread.gmane.org/gmane.comp.version-control.git/230968/focus=231453

Sorry for the noise.

-- 
Thomas Rast
trast@{inf,student}.ethz.ch

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] log: use true parents for diff when walking reflogs
  2013-08-03 10:36 [PATCH] log: use true parents for diff when walking reflogs Thomas Rast
  2013-08-03 10:40 ` Thomas Rast
@ 2013-08-05 19:16 ` Junio C Hamano
  1 sibling, 0 replies; 3+ messages in thread
From: Junio C Hamano @ 2013-08-05 19:16 UTC (permalink / raw)
  To: Thomas Rast; +Cc: Jeff King, git, Uwe Kleine-König, Ramsay Jones

Thomas Rast <trast@inf.ethz.ch> writes:

> 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>
> ---
>
> Jeff King <peff@peff.net> wrote:
>> 
>> Your description (and solution) make a lot of sense to me. Another code
>> path that has a similar problem is the "-g" reflog walker. It rewrites
>> the parents based on the reflog, and the diffs it produces are mostly
>> useless (e.g., try "git stash list -p").
>> 
>> Should we be applying the same technique there?
>
> Good point.  This is how.  It applies on top of the other patch.

Thanks.

> It doesn't really help for 'git stash list -p', though, because
> stashes are merge commits.  Now they just don't show anything.
> could try 'git stash list -p -m', though.

Using --first-parent may be more convenient and useful.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-08-05 19:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-08-03 10:36 [PATCH] log: use true parents for diff when walking reflogs Thomas Rast
2013-08-03 10:40 ` Thomas Rast
2013-08-05 19:16 ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).