obshistory: refactor debugobshistory
authorBoris Feld <boris.feld@octobus.net>
Wed, 17 May 2017 19:20:43 +0200
changeset 2403 1b348702d79e
parent 2402 d7230f7162b7
child 2404 c07f752137f4
obshistory: refactor debugobshistory Rename the function and extract the heavy work of the command in a separate module named obshistory.py
hgext3rd/evolve/__init__.py
hgext3rd/evolve/obshistory.py
--- a/hgext3rd/evolve/__init__.py	Wed May 17 18:54:48 2017 +0200
+++ b/hgext3rd/evolve/__init__.py	Wed May 17 19:20:43 2017 +0200
@@ -148,6 +148,7 @@
     obsexchange,
     safeguard,
     utility,
+    obshistory
 )
 
 __version__ = metadata.__version__
@@ -3263,104 +3264,11 @@
     '^debugobshistory',
     [] + commands.formatteropts,
     _('hg debugobshistory [OPTION]... [REV]'))
-def debugobshistory(ui, repo, *revs, **opts):
+def cmdobshistory(ui, repo, *revs, **opts):
     revs = scmutil.revrange(repo, revs)
     fm = ui.formatter('debugobshistory', opts)
 
     revs.reverse()
-    _debugobshistorysingle(fm, repo, revs)
+    obshistory._debugobshistorysingle(fm, repo, revs)
 
     fm.end()
-
-def _debugobshistorysingle(fm, repo, revs):
-    """ Display the obsolescence history for a single revision
-    """
-    precursors = repo.obsstore.precursors
-    successors = repo.obsstore.successors
-    nodec = repo.changelog.node
-    nodes = [nodec(r) for r in revs]
-
-    seen = set(nodes)
-
-    while nodes:
-        ctxnode = nodes.pop()
-
-        _debugobshistorydisplaynode(fm, repo, ctxnode)
-
-        succs = successors.get(ctxnode, ())
-
-        markerfm = fm.nested("debugobshistory.markers")
-        for successor in sorted(succs):
-            _debugobshistorydisplaymarker(markerfm, repo, successor)
-        markerfm.end()
-
-        precs = precursors.get(ctxnode, ())
-        for p in sorted(precs):
-            # Only show nodes once
-            if p[0] not in seen:
-                seen.add(p[0])
-                nodes.append(p[0])
-
-def _debugobshistorydisplaynode(fm, repo, node):
-    if node in repo.unfiltered():
-        _debugobshistorydisplayctx(fm, repo.unfiltered()[node])
-    else:
-        _debugobshistorydisplaymissingctx(fm, node)
-
-def _debugobshistorydisplayctx(fm, ctx):
-    shortdescription = ctx.description().splitlines()[0]
-
-    fm.startitem()
-    fm.write('debugobshistory.node', '%s', str(ctx),
-             label="evolve.short_node")
-    fm.plain(' ')
-
-    fm.write('debugobshistory.rev', '(%d)', int(ctx),
-             label="evolve.rev")
-    fm.plain(' ')
-
-    fm.write('debugobshistory.shortdescription', '%s', shortdescription,
-             label="evolve.short_description")
-    fm.plain('\n')
-
-def _debugobshistorydisplaymissingctx(fm, nodewithoutctx):
-    hexnode = node.short(nodewithoutctx)
-    fm.startitem()
-    fm.write('debugobshistory.node', '%s', hexnode,
-             label="evolve.short_node evolve.missing_change_ctx")
-    fm.plain('\n')
-
-def _debugobshistorydisplaymarker(fm, repo, marker):
-    succnodes = marker[1]
-    date = marker[4]
-    metadata = dict(marker[3])
-
-    fm.startitem()
-    fm.plain('  ')
-
-    # Detect pruned revisions
-    if len(succnodes) == 0:
-        verb = 'pruned'
-    else:
-        verb = 'rewritten'
-
-    fm.write('debugobshistory.verb', '%s', verb,
-             label="evolve.verb")
-    fm.plain(' by ')
-
-    fm.write('debugobshistory.marker_user', '%s', metadata['user'],
-             label="evolve.user")
-    fm.plain(' ')
-
-    fm.write('debugobshistory.marker_date', '(%s)', fm.formatdate(date),
-             label="evolve.date")
-
-    if len(succnodes) > 0:
-        fm.plain(' as ')
-
-        shortsnodes = (node.short(succnode) for succnode in sorted(succnodes))
-        nodes = fm.formatlist(shortsnodes, 'debugobshistory.succnodes', sep=', ')
-        fm.write('debugobshistory.succnodes', '%s', nodes,
-                 label="evolve.short_node")
-
-    fm.plain("\n")
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hgext3rd/evolve/obshistory.py	Wed May 17 19:20:43 2017 +0200
@@ -0,0 +1,105 @@
+# Code dedicated to display and exploration of the obsolescence history
+#
+# This module content aims at being upstreamed enventually.
+#
+# Copyright 2017 Octobus SAS <contact@octobus.net>
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+from mercurial import (
+    node
+)
+
+def _debugobshistorysingle(fm, repo, revs):
+    """ Display the obsolescence history for a single revision
+    """
+    precursors = repo.obsstore.precursors
+    successors = repo.obsstore.successors
+    nodec = repo.changelog.node
+    nodes = [nodec(r) for r in revs]
+
+    seen = set(nodes)
+
+    while nodes:
+        ctxnode = nodes.pop()
+
+        _debugobshistorydisplaynode(fm, repo, ctxnode)
+
+        succs = successors.get(ctxnode, ())
+
+        markerfm = fm.nested("debugobshistory.markers")
+        for successor in sorted(succs):
+            _debugobshistorydisplaymarker(markerfm, repo, successor)
+        markerfm.end()
+
+        precs = precursors.get(ctxnode, ())
+        for p in sorted(precs):
+            # Only show nodes once
+            if p[0] not in seen:
+                seen.add(p[0])
+                nodes.append(p[0])
+
+def _debugobshistorydisplaynode(fm, repo, node):
+    if node in repo.unfiltered():
+        _debugobshistorydisplayctx(fm, repo.unfiltered()[node])
+    else:
+        _debugobshistorydisplaymissingctx(fm, node)
+
+def _debugobshistorydisplayctx(fm, ctx):
+    shortdescription = ctx.description().splitlines()[0]
+
+    fm.startitem()
+    fm.write('debugobshistory.node', '%s', str(ctx),
+             label="evolve.short_node")
+    fm.plain(' ')
+
+    fm.write('debugobshistory.rev', '(%d)', int(ctx),
+             label="evolve.rev")
+    fm.plain(' ')
+
+    fm.write('debugobshistory.shortdescription', '%s', shortdescription,
+             label="evolve.short_description")
+    fm.plain('\n')
+
+def _debugobshistorydisplaymissingctx(fm, nodewithoutctx):
+    hexnode = node.short(nodewithoutctx)
+    fm.startitem()
+    fm.write('debugobshistory.node', '%s', hexnode,
+             label="evolve.short_node evolve.missing_change_ctx")
+    fm.plain('\n')
+
+def _debugobshistorydisplaymarker(fm, repo, marker):
+    succnodes = marker[1]
+    date = marker[4]
+    metadata = dict(marker[3])
+
+    fm.startitem()
+    fm.plain('  ')
+
+    # Detect pruned revisions
+    if len(succnodes) == 0:
+        verb = 'pruned'
+    else:
+        verb = 'rewritten'
+
+    fm.write('debugobshistory.verb', '%s', verb,
+             label="evolve.verb")
+    fm.plain(' by ')
+
+    fm.write('debugobshistory.marker_user', '%s', metadata['user'],
+             label="evolve.user")
+    fm.plain(' ')
+
+    fm.write('debugobshistory.marker_date', '(%s)', fm.formatdate(date),
+             label="evolve.date")
+
+    if len(succnodes) > 0:
+        fm.plain(' as ')
+
+        shortsnodes = (node.short(succnode) for succnode in sorted(succnodes))
+        nodes = fm.formatlist(shortsnodes, 'debugobshistory.succnodes', sep=', ')
+        fm.write('debugobshistory.succnodes', '%s', nodes,
+                 label="evolve.short_node")
+
+    fm.plain("\n")