debugindex: move the logic into its own module
authorPierre-Yves David <pierre-yves.david@octobus.net>
Mon, 30 May 2022 23:24:14 +0200
changeset 49250 61cf3d39fd9e
parent 49249 db19f6be0442
child 49251 ccd76e292be5
debugindex: move the logic into its own module Adding more information will significantly increase the amount of code. So we move the code into its own module before making it more complex.
mercurial/debugcommands.py
mercurial/revlogutils/debug.py
--- a/mercurial/debugcommands.py	Mon May 30 11:30:48 2022 +0200
+++ b/mercurial/debugcommands.py	Mon May 30 23:24:14 2022 +0200
@@ -105,6 +105,7 @@
 
 from .revlogutils import (
     constants as revlog_constants,
+    debug as revlog_debug,
     deltas as deltautil,
     nodemap,
     rewrite,
@@ -1874,36 +1875,16 @@
     opts = pycompat.byteskwargs(opts)
     store = cmdutil.openstorage(repo, b'debugindex', file_, opts)
 
-    if ui.debugflag:
-        shortfn = hex
-    else:
-        shortfn = short
-
-    idlen = 12
-    for i in store:
-        idlen = len(shortfn(store.node(i)))
-        break
-
     fm = ui.formatter(b'debugindex', opts)
-    fm.plain(
-        b'   rev linkrev %s %s p2\n'
-        % (b'nodeid'.ljust(idlen), b'p1'.ljust(idlen))
+
+    return revlog_debug.debug_index(
+        ui,
+        repo,
+        formatter=fm,
+        revlog=store,
+        full_node=ui.debugflag,
     )
 
-    for rev in store:
-        node = store.node(rev)
-        parents = store.parents(node)
-
-        fm.startitem()
-        fm.write(b'rev', b'%6d ', rev)
-        fm.write(b'linkrev', b'%7d ', store.linkrev(rev))
-        fm.write(b'node', b'%s ', shortfn(node))
-        fm.write(b'p1', b'%s ', shortfn(parents[0]))
-        fm.write(b'p2', b'%s', shortfn(parents[1]))
-        fm.plain(b'\n')
-
-    fm.end()
-
 
 @command(
     b'debugindexdot',
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial/revlogutils/debug.py	Mon May 30 23:24:14 2022 +0200
@@ -0,0 +1,51 @@
+# revlogutils/debug.py - utility used for revlog debuging
+#
+# Copyright 2005-2007 Olivia Mackall <olivia@selenic.com>
+# Copyright 2022 Octobus <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 .. import (
+    node as nodemod,
+)
+
+
+def debug_index(
+    ui,
+    repo,
+    formatter,
+    revlog,
+    full_node,
+):
+    """display index data for a revlog"""
+    if full_node:
+        hexfn = nodemod.hex
+    else:
+        hexfn = nodemod.short
+
+    idlen = 12
+    for i in revlog:
+        idlen = len(hexfn(revlog.node(i)))
+        break
+
+    fm = formatter
+
+    fm.plain(
+        b'   rev linkrev %s %s p2\n'
+        % (b'nodeid'.ljust(idlen), b'p1'.ljust(idlen))
+    )
+
+    for rev in revlog:
+        node = revlog.node(rev)
+        parents = revlog.parents(node)
+
+        fm.startitem()
+        fm.write(b'rev', b'%6d ', rev)
+        fm.write(b'linkrev', b'%7d ', revlog.linkrev(rev))
+        fm.write(b'node', b'%s ', hexfn(node))
+        fm.write(b'p1', b'%s ', hexfn(parents[0]))
+        fm.write(b'p2', b'%s', hexfn(parents[1]))
+        fm.plain(b'\n')
+
+    fm.end()