Patchwork [1,of,5] record: move header class from record to patch

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

Comments

Laurent Charignon - March 10, 2015, 5:12 p.m.
# HG changeset patch
# User Laurent Charignon <lcharignon@fb.com>
# Date 1425931490 25200
#      Mon Mar 09 13:04:50 2015 -0700
# Node ID 6f6b0d8c18fd84b70009fcc6f4a88b6a883116c6
# Parent  4e865115566e75f938cbff9dcf081da39008a161
record: move header class from record to patch

Part of a serie of patches to move record from hgext to core
Jordi GutiƩrrez Hermoso - March 10, 2015, 5:22 p.m.
On Tue, 2015-03-10 at 10:12 -0700, Laurent Charignon wrote:
> # HG changeset patch
> # User Laurent Charignon <lcharignon@fb.com>
> # Date 1425931490 25200
> #      Mon Mar 09 13:04:50 2015 -0700
> # Node ID 6f6b0d8c18fd84b70009fcc6f4a88b6a883116c6
> # Parent  4e865115566e75f938cbff9dcf081da39008a161
> record: move header class from record to patch

I think that in order for this patch series to be useful, it requires
more work than just moving code from one file to another.

In particular,

> -class header(object):
> -    """patch header
> -
> -    XXX shouldn't we move this to mercurial/patch.py ?
> -    """

XXX didn't we just do precisely this?

At least update the comments. :-)
Laurent Charignon - March 10, 2015, 5:31 p.m.
Hi,

I was thinking about doing this in a later steps, first moving the code
and then consolidating with what is in patch.py.
Would you rather do it the other way around?


On 3/10/15, 10:22 AM, "Jordi GutiƩrrez Hermoso" <jordigh@octave.org> wrote:

>On Tue, 2015-03-10 at 10:12 -0700, Laurent Charignon wrote:
>> # HG changeset patch
>> # User Laurent Charignon <lcharignon@fb.com>
>> # Date 1425931490 25200
>> #      Mon Mar 09 13:04:50 2015 -0700
>> # Node ID 6f6b0d8c18fd84b70009fcc6f4a88b6a883116c6
>> # Parent  4e865115566e75f938cbff9dcf081da39008a161
>> record: move header class from record to patch
>
>I think that in order for this patch series to be useful, it requires
>more work than just moving code from one file to another.
>
>In particular,
>
>> -class header(object):
>> -    """patch header
>> -
>> -    XXX shouldn't we move this to mercurial/patch.py ?
>> -    """
>
>XXX didn't we just do precisely this?
>
>At least update the comments. :-)

I will, thanks!
>
>

Patch

diff --git a/hgext/record.py b/hgext/record.py
--- a/hgext/record.py
+++ b/hgext/record.py
@@ -10,6 +10,7 @@ 
 from mercurial.i18n import _
 from mercurial import cmdutil, commands, extensions, hg, patch
 from mercurial import util
+from mercurial.patch import header
 import copy, cStringIO, errno, os, re, shutil, tempfile
 
 cmdtable = {}
@@ -69,66 +70,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] == '+'])
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -804,6 +804,66 @@ 
         self.write_rej()
         return len(self.rej)
 
+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)
+
 class hunk(object):
     def __init__(self, desc, num, lr, context):
         self.number = num