@@ -10,95 +10,14 @@
from mercurial.i18n import _
from mercurial import cmdutil, commands, extensions, hg, patch
from mercurial import util
-from mercurial.patch import header
-from mercurial.patch import recordhunk as hunk
from mercurial.patch import scanpatch
+from mercurial.patch import parsepatch
import copy, cStringIO, errno, os, re, shutil, tempfile
cmdtable = {}
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 = hunk(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 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"""
@@ -1195,6 +1195,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