mercurial/patch.py
changeset 28861 86db5cb55d46
parent 28341 8286f551b7ee
child 29154 9d38a2061fd8
equal deleted inserted replaced
28860:50d11dd8ac02 28861:86db5cb55d46
     6 # This software may be used and distributed according to the terms of the
     6 # This software may be used and distributed according to the terms of the
     7 # GNU General Public License version 2 or any later version.
     7 # GNU General Public License version 2 or any later version.
     8 
     8 
     9 from __future__ import absolute_import
     9 from __future__ import absolute_import
    10 
    10 
    11 import cStringIO
       
    12 import collections
    11 import collections
    13 import copy
    12 import copy
    14 import email
    13 import email
    15 import errno
    14 import errno
    16 import os
    15 import os
    35     mdiff,
    34     mdiff,
    36     pathutil,
    35     pathutil,
    37     scmutil,
    36     scmutil,
    38     util,
    37     util,
    39 )
    38 )
       
    39 stringio = util.stringio
    40 
    40 
    41 gitre = re.compile('diff --git a/(.*) b/(.*)')
    41 gitre = re.compile('diff --git a/(.*) b/(.*)')
    42 tabsplitter = re.compile(r'(\t+|[^\t]+)')
    42 tabsplitter = re.compile(r'(\t+|[^\t]+)')
    43 
    43 
    44 class PatchError(Exception):
    44 class PatchError(Exception):
    58             return False
    58             return False
    59         l = line.split(': ', 1)
    59         l = line.split(': ', 1)
    60         return len(l) == 2 and ' ' not in l[0]
    60         return len(l) == 2 and ' ' not in l[0]
    61 
    61 
    62     def chunk(lines):
    62     def chunk(lines):
    63         return cStringIO.StringIO(''.join(lines))
    63         return stringio(''.join(lines))
    64 
    64 
    65     def hgsplit(stream, cur):
    65     def hgsplit(stream, cur):
    66         inheader = True
    66         inheader = True
    67 
    67 
    68         for line in stream:
    68         for line in stream:
    91             for c in split(chunk(cur[1:])):
    91             for c in split(chunk(cur[1:])):
    92                 yield c
    92                 yield c
    93 
    93 
    94     def mimesplit(stream, cur):
    94     def mimesplit(stream, cur):
    95         def msgfp(m):
    95         def msgfp(m):
    96             fp = cStringIO.StringIO()
    96             fp = stringio()
    97             g = email.Generator.Generator(fp, mangle_from_=False)
    97             g = email.Generator.Generator(fp, mangle_from_=False)
    98             g.flatten(m)
    98             g.flatten(m)
    99             fp.seek(0)
    99             fp.seek(0)
   100             return fp
   100             return fp
   101 
   101 
   244                 hgpatchheader = False
   244                 hgpatchheader = False
   245                 ignoretext = False
   245                 ignoretext = False
   246 
   246 
   247                 ui.debug('found patch at byte %d\n' % m.start(0))
   247                 ui.debug('found patch at byte %d\n' % m.start(0))
   248                 diffs_seen += 1
   248                 diffs_seen += 1
   249                 cfp = cStringIO.StringIO()
   249                 cfp = stringio()
   250                 for line in payload[:m.start(0)].splitlines():
   250                 for line in payload[:m.start(0)].splitlines():
   251                     if line.startswith('# HG changeset patch') and not hgpatch:
   251                     if line.startswith('# HG changeset patch') and not hgpatch:
   252                         ui.debug('patch generated by hg export\n')
   252                         ui.debug('patch generated by hg export\n')
   253                         hgpatch = True
   253                         hgpatch = True
   254                         hgpatchheader = True
   254                         hgpatchheader = True
  1055                     if ret != 0:
  1055                     if ret != 0:
  1056                         ui.warn(_("editor exited with exit code %d\n") % ret)
  1056                         ui.warn(_("editor exited with exit code %d\n") % ret)
  1057                         continue
  1057                         continue
  1058                     # Remove comment lines
  1058                     # Remove comment lines
  1059                     patchfp = open(patchfn)
  1059                     patchfp = open(patchfn)
  1060                     ncpatchfp = cStringIO.StringIO()
  1060                     ncpatchfp = stringio()
  1061                     for line in patchfp:
  1061                     for line in patchfp:
  1062                         if not line.startswith('#'):
  1062                         if not line.startswith('#'):
  1063                             ncpatchfp.write(line)
  1063                             ncpatchfp.write(line)
  1064                     patchfp.close()
  1064                     patchfp.close()
  1065                     ncpatchfp.seek(0)
  1065                     ncpatchfp.seek(0)
  1438     >>> for h in hunks:
  1438     >>> for h in hunks:
  1439     ...     hunkscomingfromfilterpatch.append(h)
  1439     ...     hunkscomingfromfilterpatch.append(h)
  1440     ...     hunkscomingfromfilterpatch.extend(h.hunks)
  1440     ...     hunkscomingfromfilterpatch.extend(h.hunks)
  1441 
  1441 
  1442     >>> reversedhunks = reversehunks(hunkscomingfromfilterpatch)
  1442     >>> reversedhunks = reversehunks(hunkscomingfromfilterpatch)
  1443     >>> fp = cStringIO.StringIO()
  1443     >>> from . import util
       
  1444     >>> fp = util.stringio()
  1444     >>> for c in reversedhunks:
  1445     >>> for c in reversedhunks:
  1445     ...      c.write(fp)
  1446     ...      c.write(fp)
  1446     >>> fp.seek(0)
  1447     >>> fp.seek(0)
  1447     >>> reversedpatch = fp.read()
  1448     >>> reversedpatch = fp.read()
  1448     >>> print reversedpatch
  1449     >>> print reversedpatch
  1551                       'hunk': addhunk},
  1552                       'hunk': addhunk},
  1552             'other': {'other': addother},
  1553             'other': {'other': addother},
  1553             }
  1554             }
  1554 
  1555 
  1555     p = parser()
  1556     p = parser()
  1556     fp = cStringIO.StringIO()
  1557     fp = stringio()
  1557     fp.write(''.join(originalchunks))
  1558     fp.write(''.join(originalchunks))
  1558     fp.seek(0)
  1559     fp.seek(0)
  1559 
  1560 
  1560     state = 'context'
  1561     state = 'context'
  1561     for newstate, data in scanpatch(fp):
  1562     for newstate, data in scanpatch(fp):
  1730     pos = 0
  1731     pos = 0
  1731     try:
  1732     try:
  1732         pos = lr.fp.tell()
  1733         pos = lr.fp.tell()
  1733         fp = lr.fp
  1734         fp = lr.fp
  1734     except IOError:
  1735     except IOError:
  1735         fp = cStringIO.StringIO(lr.fp.read())
  1736         fp = stringio(lr.fp.read())
  1736     gitlr = linereader(fp)
  1737     gitlr = linereader(fp)
  1737     gitlr.push(firstline)
  1738     gitlr.push(firstline)
  1738     gitpatches = readgitpatch(gitlr)
  1739     gitpatches = readgitpatch(gitlr)
  1739     fp.seek(pos)
  1740     fp.seek(pos)
  1740     return gitpatches
  1741     return gitpatches