[PATCH 4 of 5] graphlog: refactor common code from different graphers; support >2 parents

Peter Arrenbrecht peter.arrenbrecht at gmail.com
Thu Nov 6 06:40:25 CST 2008


# HG changeset patch
# User Peter Arrenbrecht <peter.arrenbrecht at gmail.com>
# Date 1225974619 -3600
graphlog: refactor common code from different graphers; support >2 parents

Extracts the column and edge determination code into a separate function 
usable on generic DAGs. In particular, adds support for >2 parents per DAG
node, which will be used by the pbranch extension.

nodegrapher() is very similar to graphmod.graph(). I shall look into merging
them when I try visualizing patch branches in hgweb. Right now I deferred
this because I don't know how happy hgweb is with the artificial nodes inserted
by the >2 parents support.

diff --git a/hgext/graphlog.py b/hgext/graphlog.py
--- a/hgext/graphlog.py
+++ b/hgext/graphlog.py
@@ -92,6 +92,9 @@
     for (node_ch, node_lines, node_index, edges, n_columns, n_columns_diff) in grapher:
         # node_lines is the list of all text lines to draw alongside the graph
 
+        if not (-2 < n_columns_diff < 2):
+            raise Exception('internal problem, nmorecols is %i' % n_columns_diff)
+
         if n_columns_diff == -1:
             # Transform
             #
@@ -173,112 +176,105 @@
         prev_n_columns_diff = n_columns_diff
 
 
-def get_rev_parents(repo, rev):
-    return [x for x in repo.changelog.parentrevs(rev) if x != nullrev]
+def nodegrapher(nodes, joinchar='\\'):
+    """grapher for asciigraph on a list of nodes and their parents
+    
+    nodes must generate tuples (node, parents, char, lines) where
+    
+     - parents must generate the parents of node, in sorted order,
+     - char is the char to print as the node symbol, and
+     - lines are the lines to display next to the node.  
+    
+    Inserts artificial lines with joinchar as marker to make room
+    when a node has >2 parents. 
+    """
+    seen = []
+    for node, parents, char, lines in nodes:
+        if node not in seen:
+            seen.append(node)
+        nodeidx = seen.index(node)
 
-def revision_grapher(repo, start_rev, stop_rev):
-    """incremental revision grapher
+        knownparents = []
+        newparents = []
+        for parent in parents:
+            if parent in seen:
+                knownparents.append(parent)
+            else:
+                newparents.append(parent)
 
+        ncols = len(seen)
+        nextseen = seen[:]
+        nextseen[nodeidx:nodeidx + 1] = newparents
+        edges = [(nodeidx, nextseen.index(p)) for p in knownparents]
+
+        while len(newparents) > 2:
+            edges.append((nodeidx, nodeidx))
+            edges.append((nodeidx, nodeidx + 1))
+            nmorecols = +1
+            yield (char, lines, nodeidx, edges, ncols, nmorecols)
+            char = joinchar
+            lines = []
+            seen = nextseen
+            nodeidx += 1
+            ncols += 1
+            edges = []
+            del newparents[0]
+
+        if len(newparents) > 0:
+            edges.append((nodeidx, nodeidx))
+        if len(newparents) > 1:
+            edges.append((nodeidx, nodeidx + 1))
+        nmorecols = len(nextseen) - ncols
+        seen = nextseen
+        yield (char, lines, nodeidx, edges, ncols, nmorecols)
+
+def debugnodegraph(ui, repo, **opts):
+    dag = [[0, [1, 2, 3, 4], 'o', ['0']],
+           [1, [5], 'o', ['1']],
+           [2, [5], 'o', ['2']],
+           [3, [5], 'o', ['3']],
+           [4, [5], 'o', ['4']],
+           [5, [], 'o', ['5']],
+          ]
+    grapher = nodegrapher(dag)
+    graphlog.asciigraph(ui, grapher)
+
+
+def cset_nodes(repo, start_rev, stop_rev):
+    """cset DAG generator yielding (rev, node, [parents]) tuples
+    
     This generator function walks through the revision history from
     revision start_rev to revision stop_rev (which must be less than
-    or equal to start_rev) and for each revision emits tuples with the
-    following elements:
-
-      - Current revision.
-      - Current node.
-      - Column of the current node in the set of ongoing edges.
-      - Edges; a list of (col, next_col) indicating the edges between
-        the current node and its parents.
-      - Number of columns (ongoing edges) in the current revision.
-      - The difference between the number of columns (ongoing edges)
-        in the next revision and the number of columns (ongoing edges)
-        in the current revision. That is: -1 means one column removed;
-        0 means no columns added or removed; 1 means one column added.
+    or equal to start_rev).
     """
-
     assert start_rev >= stop_rev
     curr_rev = start_rev
-    revs = []
     while curr_rev >= stop_rev:
         node = repo.changelog.node(curr_rev)
-
-        # Compute revs and next_revs.
-        if curr_rev not in revs:
-            # New head.
-            revs.append(curr_rev)
-        rev_index = revs.index(curr_rev)
-        next_revs = revs[:]
-
-        # Add parents to next_revs.
-        parents = get_rev_parents(repo, curr_rev)
-        parents_to_add = []
-        for parent in parents:
-            if parent not in next_revs:
-                parents_to_add.append(parent)
-        next_revs[rev_index:rev_index + 1] = util.sort(parents_to_add)
-
-        edges = []
-        for parent in parents:
-            edges.append((rev_index, next_revs.index(parent)))
-
-        n_columns_diff = len(next_revs) - len(revs)
-        yield (curr_rev, node, rev_index, edges, len(revs), n_columns_diff)
-
-        revs = next_revs
+        parents = [x for x in repo.changelog.parentrevs(curr_rev) if x != nullrev]
+        parents.sort()
+        yield (curr_rev, node, parents)
         curr_rev -= 1
 
-def filelog_grapher(repo, path, start_rev, stop_rev):
-    """incremental file log grapher
-
-    This generator function walks through the revision history of a
-    single file from revision start_rev to revision stop_rev (which must
-    be less than or equal to start_rev) and for each revision emits
-    tuples with the following elements:
-
-      - Current revision.
-      - Current node.
-      - Column of the current node in the set of ongoing edges.
-      - Edges; a list of (col, next_col) indicating the edges between
-        the current node and its parents.
-      - Number of columns (ongoing edges) in the current revision.
-      - The difference between the number of columns (ongoing edges)
-        in the next revision and the number of columns (ongoing edges)
-        in the current revision. That is: -1 means one column removed;
-        0 means no columns added or removed; 1 means one column added.
+def file_nodes(repo, path, start_rev, stop_rev):
+    """file cset DAG generator yielding (rev, node, [parents]) tuples
+    
+    This generator function walks through the revision history of a single file
+    from revision start_rev to revision stop_rev (which must be less than
+    or equal to start_rev).
     """
-
     assert start_rev >= stop_rev
-    revs = []
     filerev = len(repo.file(path)) - 1
     while filerev >= 0:
         fctx = repo.filectx(path, fileid=filerev)
-
-        # Compute revs and next_revs.
-        if filerev not in revs:
-            revs.append(filerev)
-        rev_index = revs.index(filerev)
-        next_revs = revs[:]
-
-        # Add parents to next_revs.
         parents = [f.filerev() for f in fctx.parents() if f.path() == path]
-        parents_to_add = []
-        for parent in parents:
-            if parent not in next_revs:
-                parents_to_add.append(parent)
-        next_revs[rev_index:rev_index + 1] = util.sort(parents_to_add)
-
-        edges = []
-        for parent in parents:
-            edges.append((rev_index, next_revs.index(parent)))
-
+        parents.sort()
         changerev = fctx.linkrev()
         if changerev <= start_rev:
             node = repo.changelog.node(changerev)
-            n_columns_diff = len(next_revs) - len(revs)
-            yield (changerev, node, rev_index, edges, len(revs), n_columns_diff)
+            yield (changerev, node, parents)
         if changerev <= stop_rev:
             break
-        revs = next_revs
         filerev -= 1
 
 
@@ -319,14 +315,14 @@
     if path:
         path = canonpath(repo.root, os.getcwd(), path)
     if path:
-        revgrapher = filelog_grapher(repo, path, start_rev, stop_rev)
+        revdag = file_nodes(repo, path, start_rev, stop_rev)
     else:
-        revgrapher = revision_grapher(repo, start_rev, stop_rev)
+        revdag = cset_nodes(repo, start_rev, stop_rev)
 
     repo_parents = repo.dirstate.parents()
     cs_printer = show_changeset(ui, repo, opts)
-    def grapher():
-        for (rev, node, node_index, edges, n_columns, n_columns_diff) in revgrapher:
+    def graphabledag():
+        for (rev, node, parents) in revdag:
             # log_strings is the list of all log strings to draw alongside
             # the graph.
             ui.pushbuffer()
@@ -336,9 +332,9 @@
                 node_ch = "@"
             else:
                 node_ch = "o"
-            yield (node_ch, log_strings, node_index, edges, n_columns, n_columns_diff)
+            yield (rev, parents, node_ch, log_strings)
 
-    asciigraph(ui, grapher())
+    asciigraph(ui, nodegrapher(graphabledag()))
 
 cmdtable = {
     "glog":
@@ -348,4 +344,5 @@
           ('r', 'rev', [], _('show the specified revision or range')),
          ] + templateopts,
          _('hg glog [OPTION]... [FILE]')),
+    # "debuggraph": (debugnodegraph, [], ''),
 }


More information about the Mercurial-devel mailing list