Patchwork [3,of,5] record: move hunk class from record to patch

login
register
mail settings
Submitter Laurent Charignon
Date March 10, 2015, 5:13 p.m.
Message ID <e2e14d9901e914f7e010.1426007581@lcharignon-mbp.local>
Download mbox | patch
Permalink /patch/7968/
State Superseded
Headers show

Comments

Laurent Charignon - March 10, 2015, 5:13 p.m.
# HG changeset patch
# User Laurent Charignon <lcharignon@fb.com>
# Date 1425931755 25200
#      Mon Mar 09 13:09:15 2015 -0700
# Node ID e2e14d9901e914f7e0102c4fcdf928f65f745864
# Parent  84edda1593c613e864e652ce42cdcaa069d4adb5
record: move hunk class from record to patch

Part of a serie of patches to move record from hgext to core

Patch

diff --git a/hgext/record.py b/hgext/record.py
--- a/hgext/record.py
+++ b/hgext/record.py
@@ -11,6 +11,7 @@ 
 from mercurial import cmdutil, commands, extensions, hg, patch
 from mercurial import util
 from mercurial.patch import header
+from mercurial.patch import recordhunk as hunk
 import copy, cStringIO, errno, os, re, shutil, tempfile
 
 cmdtable = {}
@@ -70,53 +71,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 """
     class parser(object):
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -864,6 +864,54 @@ 
     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 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)
+
+
 class hunk(object):
     def __init__(self, desc, num, lr, context):
         self.number = num