Patchwork [5,of,5,V2] record: move parsepatch from record to patch

login
register
mail settings
Submitter Laurent Charignon
Date March 10, 2015, 7:16 p.m.
Message ID <8e8e752e242ed8f178e3.1426014977@dev919.prn2.facebook.com>
Download mbox | patch
Permalink /patch/7976/
State Accepted
Headers show

Comments

Laurent Charignon - March 10, 2015, 7:16 p.m.
# HG changeset patch
# User Laurent Charignon <lcharignon@fb.com>
# Date 1425932185 25200
#      Mon Mar 09 13:16:25 2015 -0700
# Node ID 8e8e752e242ed8f178e31a2bb64100ab06a0e6a3
# Parent  c9396bdb303113f68e5f7c5072a9a8a78cb67294
record: move parsepatch from record to patch

Part of a serie of patches to move record from hgext to core
Pierre-Yves David - March 10, 2015, 8:08 p.m.
On 03/10/2015 12:16 PM, Laurent Charignon wrote:
> # HG changeset patch
> # User Laurent Charignon <lcharignon@fb.com>
> # Date 1425932185 25200
> #      Mon Mar 09 13:16:25 2015 -0700
> # Node ID 8e8e752e242ed8f178e31a2bb64100ab06a0e6a3
> # Parent  c9396bdb303113f68e5f7c5072a9a8a78cb67294
> record: move parsepatch from record to patch

This series looks good to me but need a rebase atop the other patches in 
the clowncopter. I'm getting such rebase directly from the author and 
will push the result to the clowcopter.

Patch

diff --git a/hgext/record.py b/hgext/record.py
--- a/hgext/record.py
+++ b/hgext/record.py
@@ -16,86 +16,6 @@ 
 command = cmdutil.command(cmdtable)
 testedwith = 'internal'
 
-def parsepatch(fp):
-    """patch -> [] of headers -> [] of hunks """
-    class parser(object):
-        """patch parsing state machine"""
-        def __init__(self):
-            self.fromline = 0
-            self.toline = 0
-            self.proc = ''
-            self.header = None
-            self.context = []
-            self.before = []
-            self.hunk = []
-            self.headers = []
-
-        def addrange(self, limits):
-            fromstart, fromend, tostart, toend, proc = limits
-            self.fromline = int(fromstart)
-            self.toline = int(tostart)
-            self.proc = proc
-
-        def addcontext(self, context):
-            if self.hunk:
-                h = patch.recordhunk(self.header, self.fromline, self.toline,
-                         self.proc, self.before, self.hunk, context)
-                self.header.hunks.append(h)
-                self.fromline += len(self.before) + h.removed
-                self.toline += len(self.before) + h.added
-                self.before = []
-                self.hunk = []
-                self.proc = ''
-            self.context = context
-
-        def addhunk(self, hunk):
-            if self.context:
-                self.before = self.context
-                self.context = []
-            self.hunk = hunk
-
-        def newfile(self, hdr):
-            self.addcontext([])
-            h = patch.header(hdr)
-            self.headers.append(h)
-            self.header = h
-
-        def addother(self, line):
-            pass # 'other' lines are ignored
-
-        def finished(self):
-            self.addcontext([])
-            return self.headers
-
-        transitions = {
-            'file': {'context': addcontext,
-                     'file': newfile,
-                     'hunk': addhunk,
-                     'range': addrange},
-            'context': {'file': newfile,
-                        'hunk': addhunk,
-                        'range': addrange,
-                        'other': addother},
-            'hunk': {'context': addcontext,
-                     'file': newfile,
-                     'range': addrange},
-            'range': {'context': addcontext,
-                      'hunk': addhunk},
-            'other': {'other': addother},
-            }
-
-    p = parser()
-
-    state = 'context'
-    for newstate, data in patch.scanpatch(fp):
-        try:
-            p.transitions[state][newstate](p, data)
-        except KeyError:
-            raise patch.PatchError('unhandled transition: %s -> %s' %
-                                   (state, newstate))
-        state = newstate
-    return p.finished()
-
 def filterpatch(ui, headers):
     """Interactively filter patch chunks into applied-only chunks"""
 
@@ -180,7 +100,7 @@ 
                             ncpatchfp.write(line)
                     patchfp.close()
                     ncpatchfp.seek(0)
-                    newpatches = parsepatch(ncpatchfp)
+                    newpatches = patch.parsepatch(ncpatchfp)
                 finally:
                     os.unlink(patchfn)
                     del ncpatchfp
@@ -370,7 +290,7 @@ 
 
         # 1. filter patch, so we have intending-to apply subset of it
         try:
-            chunks = filterpatch(ui, parsepatch(fp))
+            chunks = filterpatch(ui, patch.parsepatch(fp))
         except patch.PatchError, err:
             raise util.Abort(_('error parsing patch: %s') % err)
 
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -1193,6 +1193,86 @@ 
             return s
     return s[:i]
 
+def parsepatch(fp):
+    """patch -> [] of headers -> [] of hunks """
+    class parser(object):
+        """patch parsing state machine"""
+        def __init__(self):
+            self.fromline = 0
+            self.toline = 0
+            self.proc = ''
+            self.header = None
+            self.context = []
+            self.before = []
+            self.hunk = []
+            self.headers = []
+
+        def addrange(self, limits):
+            fromstart, fromend, tostart, toend, proc = limits
+            self.fromline = int(fromstart)
+            self.toline = int(tostart)
+            self.proc = proc
+
+        def addcontext(self, context):
+            if self.hunk:
+                h = recordhunk(self.header, self.fromline, self.toline,
+                        self.proc, self.before, self.hunk, context)
+                self.header.hunks.append(h)
+                self.fromline += len(self.before) + h.removed
+                self.toline += len(self.before) + h.added
+                self.before = []
+                self.hunk = []
+                self.proc = ''
+            self.context = context
+
+        def addhunk(self, hunk):
+            if self.context:
+                self.before = self.context
+                self.context = []
+            self.hunk = hunk
+
+        def newfile(self, hdr):
+            self.addcontext([])
+            h = header(hdr)
+            self.headers.append(h)
+            self.header = h
+
+        def addother(self, line):
+            pass # 'other' lines are ignored
+
+        def finished(self):
+            self.addcontext([])
+            return self.headers
+
+        transitions = {
+            'file': {'context': addcontext,
+                     'file': newfile,
+                     'hunk': addhunk,
+                     'range': addrange},
+            'context': {'file': newfile,
+                        'hunk': addhunk,
+                        'range': addrange,
+                        'other': addother},
+            'hunk': {'context': addcontext,
+                     'file': newfile,
+                     'range': addrange},
+            'range': {'context': addcontext,
+                      'hunk': addhunk},
+            'other': {'other': addother},
+            }
+
+    p = parser()
+
+    state = 'context'
+    for newstate, data in scanpatch(fp):
+        try:
+            p.transitions[state][newstate](p, data)
+        except KeyError:
+            raise PatchError('unhandled transition: %s -> %s' %
+                                   (state, newstate))
+        state = newstate
+    return p.finished()
+
 def pathstrip(path, strip):
     pathlen = len(path)
     i = 0