[PATCH 1 of 5 V2] record: move header class from record to patch

Laurent Charignon lcharignon at fb.com
Tue Mar 10 14:16:13 CDT 2015


# HG changeset patch
# User Laurent Charignon <lcharignon at fb.com>
# Date 1425931490 25200
#      Mon Mar 09 13:04:50 2015 -0700
# Node ID 9ec826349460426e40b8f08ea6644a77ea133db5
# Parent  4e865115566e75f938cbff9dcf081da39008a161
record: move header class from record to patch

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

diff --git a/hgext/record.py b/hgext/record.py
--- a/hgext/record.py
+++ b/hgext/record.py
@@ -69,66 +69,6 @@
             else:
                 yield 'other', line
 
-class header(object):
-    """patch header
-
-    XXX shouldn't we move this to mercurial/patch.py ?
-    """
-    diffgit_re = re.compile('diff --git a/(.*) b/(.*)$')
-    diff_re = re.compile('diff -r .* (.*)$')
-    allhunks_re = re.compile('(?:index|new file|deleted file) ')
-    pretty_re = re.compile('(?:new file|deleted file) ')
-    special_re = re.compile('(?:index|new|deleted|copy|rename) ')
-
-    def __init__(self, header):
-        self.header = header
-        self.hunks = []
-
-    def binary(self):
-        return util.any(h.startswith('index ') for h in self.header)
-
-    def pretty(self, fp):
-        for h in self.header:
-            if h.startswith('index '):
-                fp.write(_('this modifies a binary file (all or nothing)\n'))
-                break
-            if self.pretty_re.match(h):
-                fp.write(h)
-                if self.binary():
-                    fp.write(_('this is a binary file\n'))
-                break
-            if h.startswith('---'):
-                fp.write(_('%d hunks, %d lines changed\n') %
-                         (len(self.hunks),
-                          sum([max(h.added, h.removed) for h in self.hunks])))
-                break
-            fp.write(h)
-
-    def write(self, fp):
-        fp.write(''.join(self.header))
-
-    def allhunks(self):
-        return util.any(self.allhunks_re.match(h) for h in self.header)
-
-    def files(self):
-        match = self.diffgit_re.match(self.header[0])
-        if match:
-            fromfile, tofile = match.groups()
-            if fromfile == tofile:
-                return [fromfile]
-            return [fromfile, tofile]
-        else:
-            return self.diff_re.match(self.header[0]).groups()
-
-    def filename(self):
-        return self.files()[-1]
-
-    def __repr__(self):
-        return '<header %s>' % (' '.join(map(repr, self.files())))
-
-    def special(self):
-        return util.any(self.special_re.match(h) for h in self.header)
-
 def countchanges(hunk):
     """hunk -> (n+,n-)"""
     add = len([h for h in hunk if h[0] == '+'])
@@ -215,7 +155,7 @@
 
         def newfile(self, hdr):
             self.addcontext([])
-            h = header(hdr)
+            h = patch.header(hdr)
             self.headers.append(h)
             self.header = h
 
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -804,6 +804,64 @@
         self.write_rej()
         return len(self.rej)
 
+class header(object):
+    """patch header
+    """
+    diffgit_re = re.compile('diff --git a/(.*) b/(.*)$')
+    diff_re = re.compile('diff -r .* (.*)$')
+    allhunks_re = re.compile('(?:index|new file|deleted file) ')
+    pretty_re = re.compile('(?:new file|deleted file) ')
+    special_re = re.compile('(?:index|new|deleted|copy|rename) ')
+
+    def __init__(self, header):
+        self.header = header
+        self.hunks = []
+
+    def binary(self):
+        return util.any(h.startswith('index ') for h in self.header)
+
+    def pretty(self, fp):
+        for h in self.header:
+            if h.startswith('index '):
+                fp.write(_('this modifies a binary file (all or nothing)\n'))
+                break
+            if self.pretty_re.match(h):
+                fp.write(h)
+                if self.binary():
+                    fp.write(_('this is a binary file\n'))
+                break
+            if h.startswith('---'):
+                fp.write(_('%d hunks, %d lines changed\n') %
+                         (len(self.hunks),
+                          sum([max(h.added, h.removed) for h in self.hunks])))
+                break
+            fp.write(h)
+
+    def write(self, fp):
+        fp.write(''.join(self.header))
+
+    def allhunks(self):
+        return util.any(self.allhunks_re.match(h) for h in self.header)
+
+    def files(self):
+        match = self.diffgit_re.match(self.header[0])
+        if match:
+            fromfile, tofile = match.groups()
+            if fromfile == tofile:
+                return [fromfile]
+            return [fromfile, tofile]
+        else:
+            return self.diff_re.match(self.header[0]).groups()
+
+    def filename(self):
+        return self.files()[-1]
+
+    def __repr__(self):
+        return '<header %s>' % (' '.join(map(repr, self.files())))
+
+    def special(self):
+        return util.any(self.special_re.match(h) for h in self.header)
+
 class hunk(object):
     def __init__(self, desc, num, lr, context):
         self.number = num


More information about the Mercurial-devel mailing list