Patchwork D2946: histedit: factor out the logic to read oldstate file in separate fn

login
register
mail settings
Submitter phabricator
Date March 26, 2018, 5:11 p.m.
Message ID <differential-rev-PHID-DREV-5uxk2sa6hfhjqdz6az4g-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/29878/
State New
Headers show

Comments

phabricator - March 26, 2018, 5:11 p.m.
pulkit created this revision.
Herald added a reviewer: durin42.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  This patch moves out logic to read old histedit-state file to a separate
  function as we will need to register that function as a function which reads old
  state formats when we start using cbor format here.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D2946

AFFECTED FILES
  hgext/histedit.py

CHANGE DETAILS




To: pulkit, durin42, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/hgext/histedit.py b/hgext/histedit.py
--- a/hgext/histedit.py
+++ b/hgext/histedit.py
@@ -362,58 +362,65 @@ 
 
     def _load(self):
         fp = self.repo.vfs('histedit-state', 'r')
-        lines = [l[:-1] for l in fp.readlines()]
-
-        index = 0
-        lines[index] # version number
-        index += 1
-
-        parentctxnode = node.bin(lines[index])
-        index += 1
-
-        topmost = node.bin(lines[index])
-        index += 1
-
-        keep = lines[index] == 'True'
-        index += 1
-
-        # Rules
-        rules = []
-        rulelen = int(lines[index])
-        index += 1
-        for i in xrange(rulelen):
-            ruleaction = lines[index]
-            index += 1
-            rule = lines[index]
-            index += 1
-            rules.append((ruleaction, rule))
-
-        # Replacements
-        replacements = []
-        replacementlen = int(lines[index])
-        index += 1
-        for i in xrange(replacementlen):
-            replacement = lines[index]
-            original = node.bin(replacement[:40])
-            succ = [node.bin(replacement[i:i + 40]) for i in
-                    range(40, len(replacement), 40)]
-            replacements.append((original, succ))
-            index += 1
-
-        backupfile = lines[index]
-        index += 1
-
+        statedict = oldhisteditstate(fp)
         fp.close()
 
-        return parentctxnode, rules, keep, topmost, replacements, backupfile
+        return (statedict['parentctxnode'], statedict['rules'],
+                statedict['keep'], statedict['topmost'],
+                statedict['replacements'], statedict['backupfile'])
 
     def clear(self):
         if self.inprogress():
             self.repo.vfs.unlink('histedit-state')
 
     def inprogress(self):
         return self.repo.vfs.exists('histedit-state')
 
+def oldhisteditstate(fp):
+    statedict = {}
+    lines = [l[:-1] for l in fp.readlines()]
+
+    index = 0
+    statedict['version'] = lines[index] # version number
+    index += 1
+
+    statedict['parentctxnode'] = node.bin(lines[index])
+    index += 1
+
+    statedict['topmost'] = node.bin(lines[index])
+    index += 1
+
+    statedict['keep'] = lines[index] == 'True'
+    index += 1
+
+    # Rules
+    rules = []
+    rulelen = int(lines[index])
+    index += 1
+    for i in xrange(rulelen):
+        ruleaction = lines[index]
+        index += 1
+        rule = lines[index]
+        index += 1
+        rules.append((ruleaction, rule))
+
+    statedict['rules'] = rules
+
+    # Replacements
+    replacements = []
+    replacementlen = int(lines[index])
+    index += 1
+    for i in xrange(replacementlen):
+        replacement = lines[index]
+        original = node.bin(replacement[:40])
+        succ = [node.bin(replacement[i:i + 40]) for i in
+                range(40, len(replacement), 40)]
+        replacements.append((original, succ))
+        index += 1
+
+    statedict['replacements'] = replacements
+    statedict['backupfile'] = lines[index]
+    return statedict
 
 class histeditaction(object):
     def __init__(self, state, node):