changeset 24263:a45d1c51109e

record: move hunk class from record to patch Part of a series of patches to move record from hgext to core
author Laurent Charignon <lcharignon@fb.com>
date Mon, 09 Mar 2015 13:09:15 -0700
parents 7f24b2a0581a
children c4205452f1b7
files hgext/record.py mercurial/patch.py
diffstat 2 files changed, 49 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/record.py	Mon Mar 09 13:08:19 2015 -0700
+++ b/hgext/record.py	Mon Mar 09 13:09:15 2015 -0700
@@ -69,51 +69,6 @@
             else:
                 yield 'other', line
 
-class hunk(object):
-    """patch hunk
-
-    XXX shouldn't we merge this with patch.hunk ?
-    """
-    maxcontext = 3
-
-    def __init__(self, header, fromline, toline, proc, before, hunk, after):
-        def trimcontext(number, lines):
-            delta = len(lines) - self.maxcontext
-            if False and delta > 0:
-                return number + delta, lines[:self.maxcontext]
-            return number, lines
-
-        self.header = header
-        self.fromline, self.before = trimcontext(fromline, before)
-        self.toline, self.after = trimcontext(toline, after)
-        self.proc = proc
-        self.hunk = hunk
-        self.added, self.removed = self.countchanges(self.hunk)
-
-    def countchanges(self, hunk):
-        """hunk -> (n+,n-)"""
-        add = len([h for h in hunk if h[0] == '+'])
-        rem = len([h for h in hunk if h[0] == '-'])
-        return add, rem
-
-    def write(self, fp):
-        delta = len(self.before) + len(self.after)
-        if self.after and self.after[-1] == '\\ No newline at end of file\n':
-            delta -= 1
-        fromlen = delta + self.removed
-        tolen = delta + self.added
-        fp.write('@@ -%d,%d +%d,%d @@%s\n' %
-                 (self.fromline, fromlen, self.toline, tolen,
-                  self.proc and (' ' + self.proc)))
-        fp.write(''.join(self.before + self.hunk + self.after))
-
-    pretty = write
-
-    def filename(self):
-        return self.header.filename()
-
-    def __repr__(self):
-        return '<hunk %r@%d>' % (self.filename(), self.fromline)
 
 def parsepatch(fp):
     """patch -> [] of headers -> [] of hunks """
@@ -137,8 +92,8 @@
 
         def addcontext(self, context):
             if self.hunk:
-                h = hunk(self.header, self.fromline, self.toline, self.proc,
-                         self.before, self.hunk, context)
+                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
@@ -490,7 +445,7 @@
 
         newandmodifiedfiles = set()
         for h in chunks:
-            ishunk = isinstance(h, hunk)
+            ishunk = isinstance(h, patch.recordhunk)
             isnew = h.filename() in status.added
             if ishunk and isnew and not h in originalchunks:
                 newandmodifiedfiles.add(h.filename())
--- a/mercurial/patch.py	Mon Mar 09 13:08:19 2015 -0700
+++ b/mercurial/patch.py	Mon Mar 09 13:09:15 2015 -0700
@@ -862,6 +862,52 @@
     def special(self):
         return util.any(self.special_re.match(h) for h in self.header)
 
+class recordhunk(object):
+    """patch hunk
+
+    XXX shouldn't we merge this with the other hunk class?
+    """
+    maxcontext = 3
+
+    def __init__(self, header, fromline, toline, proc, before, hunk, after):
+        def trimcontext(number, lines):
+            delta = len(lines) - self.maxcontext
+            if False and delta > 0:
+                return number + delta, lines[:self.maxcontext]
+            return number, lines
+
+        self.header = header
+        self.fromline, self.before = trimcontext(fromline, before)
+        self.toline, self.after = trimcontext(toline, after)
+        self.proc = proc
+        self.hunk = hunk
+        self.added, self.removed = self.countchanges(self.hunk)
+
+    def countchanges(self, hunk):
+        """hunk -> (n+,n-)"""
+        add = len([h for h in hunk if h[0] == '+'])
+        rem = len([h for h in hunk if h[0] == '-'])
+        return add, rem
+
+    def write(self, fp):
+        delta = len(self.before) + len(self.after)
+        if self.after and self.after[-1] == '\\ No newline at end of file\n':
+            delta -= 1
+        fromlen = delta + self.removed
+        tolen = delta + self.added
+        fp.write('@@ -%d,%d +%d,%d @@%s\n' %
+                 (self.fromline, fromlen, self.toline, tolen,
+                  self.proc and (' ' + self.proc)))
+        fp.write(''.join(self.before + self.hunk + self.after))
+
+    pretty = write
+
+    def filename(self):
+        return self.header.filename()
+
+    def __repr__(self):
+        return '<hunk %r@%d>' % (self.filename(), self.fromline)
+
 class hunk(object):
     def __init__(self, desc, num, lr, context):
         self.number = num