Code

revision traversal: show full history with merge simplification
authorJunio C Hamano <gitster@pobox.com>
Thu, 31 Jul 2008 08:17:41 +0000 (01:17 -0700)
committerJunio C Hamano <gitster@pobox.com>
Sat, 2 Aug 2008 07:33:15 +0000 (00:33 -0700)
The --full-history traversal keeps all merges in addition to non-merge
commits that touch paths in the given pathspec.  This is useful to view
both sides of a merge in a topology like this:

        A---M---o
       /   /
   ---O---B

even when A and B makes identical change to the given paths.  The revision
traversal without --full-history aims to come up with the simplest history
to explain the final state of the tree, and one of the side branches can
be pruned away.

The behaviour to keep all merges however is inconvenient if neither A nor
B touches the paths we are interested in.  --full-history reduces the
topology to:

   ---O---M---o

in such a case, without removing M.

This adds a post processing phase on top of --full-history traversal to
remove needless merges from the resulting history.

The idea is to compute, for each commit in the "full history" result set,
the commit that should replace it in the simplified history.  The commit
to replace it in the final history is determined as follows:

 * In any case, we first figure out the replacement commits of parents of
   the commit we are looking at.  The commit we are looking at is
   rewritten as if the replacement commits of its original parents are its
   parents.  While doing so, we reduce the redundant parents from the
   rewritten parent list by not just removing the identical ones, but also
   removing a parent that is an ancestor of another parent.

 * After the above parent simplification, if the commit is a root commit,
   an UNINTERESTING commit, a merge commit, or modifies the paths we are
   interested in, then the replacement commit of the commit is itself.  In
   other words, such a commit is not dropped from the final result.

The first point above essentially means that the history is rewritten in
the bottom up direction.  We can rewrite the parent list of a commit only
after we know how all of its parents are rewritten.  This means that the
processing needs to happen on the full history (i.e. after limit_list()).

Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/rev-list-options.txt
revision.c
revision.h

index 3aa38097e6350a02c50873d5c670e108003fab22..ee6822a85df9965901403e99d353fc3c0e1a9eed 100644 (file)
@@ -193,12 +193,18 @@ endif::git-rev-list[]
 
 --full-history::
 
-       Show also parts of history irrelevant to current state of given
-       path. This turns off history simplification, which removed merges
+       Show also parts of history irrelevant to current state of given
+       paths. This turns off history simplification, which removed merges
        which didn't change anything at all at some child. It will still actually
        simplify away merges that didn't change anything at all into either
        child.
 
+--simplify-merges::
+
+       Simplify away commits that did not change the given paths, similar
+       to `--full-history`, and further remove merges none of whose
+       parent history changes the given paths.
+
 --no-merges::
 
        Do not print commits with more than one parent.
index 71f0bea899eda0998268db3b50494876f1e20ddf..662d66be7e23792c88907142a48dbc7fbdc7f70e 100644 (file)
@@ -1045,6 +1045,11 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
        } else if (!strcmp(arg, "--topo-order")) {
                revs->lifo = 1;
                revs->topo_order = 1;
+       } else if (!strcmp(arg, "--simplify-merges")) {
+               revs->simplify_merges = 1;
+               revs->rewrite_parents = 1;
+               revs->simplify_history = 0;
+               revs->limited = 1;
        } else if (!strcmp(arg, "--date-order")) {
                revs->lifo = 0;
                revs->topo_order = 1;
@@ -1378,6 +1383,150 @@ static void add_child(struct rev_info *revs, struct commit *parent, struct commi
        l->next = add_decoration(&revs->children, &parent->object, l);
 }
 
+static int remove_duplicate_parents(struct commit *commit)
+{
+       struct commit_list **pp, *p;
+       int surviving_parents;
+
+       /* Examine existing parents while marking ones we have seen... */
+       pp = &commit->parents;
+       while ((p = *pp) != NULL) {
+               struct commit *parent = p->item;
+               if (parent->object.flags & TMP_MARK) {
+                       *pp = p->next;
+                       continue;
+               }
+               parent->object.flags |= TMP_MARK;
+               pp = &p->next;
+       }
+       /* count them while clearing the temporary mark */
+       surviving_parents = 0;
+       for (p = commit->parents; p; p = p->next) {
+               p->item->object.flags &= ~TMP_MARK;
+               surviving_parents++;
+       }
+       return surviving_parents;
+}
+
+static struct commit_list **simplify_one(struct commit *commit, struct commit_list **tail)
+{
+       struct commit_list *p;
+       int cnt;
+
+       /*
+        * We store which commit each one simplifies to in its util field.
+        * Have we handled this one?
+        */
+       if (commit->util)
+               return tail;
+
+       /*
+        * An UNINTERESTING commit simplifies to itself, so does a
+        * root commit.  We do not rewrite parents of such commit
+        * anyway.
+        */
+       if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
+               commit->util = commit;
+               return tail;
+       }
+
+       /*
+        * Do we know what commit all of our parents should be rewritten to?
+        * Otherwise we are not ready to rewrite this one yet.
+        */
+       for (cnt = 0, p = commit->parents; p; p = p->next) {
+               if (!p->item->util) {
+                       tail = &commit_list_insert(p->item, tail)->next;
+                       cnt++;
+               }
+       }
+       if (cnt)
+               return tail;
+
+       /*
+        * Rewrite our list of parents.
+        */
+       for (p = commit->parents; p; p = p->next)
+               p->item = p->item->util;
+       cnt = remove_duplicate_parents(commit);
+
+       /*
+        * It is possible that we are a merge and one side branch
+        * does not have any commit that touches the given paths;
+        * in such a case, the immediate parents will be rewritten
+        * to different commits.
+        *
+        *      o----X          X: the commit we are looking at;
+        *     /    /           o: a commit that touches the paths;
+        * ---o----'
+        *
+        * Further reduce the parents by removing redundant parents.
+        */
+       if (1 < cnt) {
+               struct commit_list *h = reduce_heads(commit->parents);
+               cnt = commit_list_count(h);
+               free_commit_list(commit->parents);
+               commit->parents = h;
+       }
+
+       /*
+        * A commit simplifies to itself if it is a root, if it is
+        * UNINTERESTING, if it touches the given paths, or if it is a
+        * merge and its parents simplifies to more than one commits
+        * (the first two cases are already handled at the beginning of
+        * this function).
+        *
+        * Otherwise, it simplifies to what its sole parent simplifies to.
+        */
+       if (!cnt ||
+           (commit->object.flags & UNINTERESTING) ||
+           !(commit->object.flags & TREESAME) ||
+           (1 < cnt))
+               commit->util = commit;
+       else
+               commit->util = commit->parents->item->util;
+       return tail;
+}
+
+static void simplify_merges(struct rev_info *revs)
+{
+       struct commit_list *list;
+       struct commit_list *yet_to_do, **tail;
+
+       /* feed the list reversed */
+       yet_to_do = NULL;
+       for (list = revs->commits; list; list = list->next)
+               commit_list_insert(list->item, &yet_to_do);
+       while (yet_to_do) {
+               list = yet_to_do;
+               yet_to_do = NULL;
+               tail = &yet_to_do;
+               while (list) {
+                       struct commit *commit = list->item;
+                       struct commit_list *next = list->next;
+                       free(list);
+                       list = next;
+                       tail = simplify_one(commit, tail);
+               }
+       }
+
+       /* clean up the result, removing the simplified ones */
+       list = revs->commits;
+       revs->commits = NULL;
+       tail = &revs->commits;
+       while (list) {
+               struct commit *commit = list->item;
+               struct commit_list *next = list->next;
+               free(list);
+               list = next;
+               if (commit->util == commit)
+                       tail = &commit_list_insert(commit, tail)->next;
+       }
+
+       /* sort topologically at the end */
+       sort_in_topological_order(&revs->commits, revs->lifo);
+}
+
 static void set_children(struct rev_info *revs)
 {
        struct commit_list *l;
@@ -1418,6 +1567,8 @@ int prepare_revision_walk(struct rev_info *revs)
                        return -1;
        if (revs->topo_order)
                sort_in_topological_order(&revs->commits, revs->lifo);
+       if (revs->simplify_merges)
+               simplify_merges(revs);
        if (revs->children.name)
                set_children(revs);
        return 0;
@@ -1450,26 +1601,6 @@ static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp
        }
 }
 
-static void remove_duplicate_parents(struct commit *commit)
-{
-       struct commit_list **pp, *p;
-
-       /* Examine existing parents while marking ones we have seen... */
-       pp = &commit->parents;
-       while ((p = *pp) != NULL) {
-               struct commit *parent = p->item;
-               if (parent->object.flags & TMP_MARK) {
-                       *pp = p->next;
-                       continue;
-               }
-               parent->object.flags |= TMP_MARK;
-               pp = &p->next;
-       }
-       /* ... and clear the temporary mark */
-       for (p = commit->parents; p; p = p->next)
-               p->item->object.flags &= ~TMP_MARK;
-}
-
 static int rewrite_parents(struct rev_info *revs, struct commit *commit)
 {
        struct commit_list **pp = &commit->parents;
index f64e8ce7ff999e9fe4a01205ae51775827484ed4..dfa06b52106da5ac3dfc65676ea5dd043c95d3cf 100644 (file)
@@ -41,6 +41,7 @@ struct rev_info {
                        simplify_history:1,
                        lifo:1,
                        topo_order:1,
+                       simplify_merges:1,
                        tag_objects:1,
                        tree_objects:1,
                        blob_objects:1,