Show More
@@ -69,66 +69,6 b' def scanpatch(fp):' | |||
|
69 | 69 | else: |
|
70 | 70 | yield 'other', line |
|
71 | 71 | |
|
72 | class header(object): | |
|
73 | """patch header | |
|
74 | ||
|
75 | XXX shouldn't we move this to mercurial/patch.py ? | |
|
76 | """ | |
|
77 | diffgit_re = re.compile('diff --git a/(.*) b/(.*)$') | |
|
78 | diff_re = re.compile('diff -r .* (.*)$') | |
|
79 | allhunks_re = re.compile('(?:index|deleted file) ') | |
|
80 | pretty_re = re.compile('(?:new file|deleted file) ') | |
|
81 | special_re = re.compile('(?:index|new|deleted|copy|rename) ') | |
|
82 | ||
|
83 | def __init__(self, header): | |
|
84 | self.header = header | |
|
85 | self.hunks = [] | |
|
86 | ||
|
87 | def binary(self): | |
|
88 | return util.any(h.startswith('index ') for h in self.header) | |
|
89 | ||
|
90 | def pretty(self, fp): | |
|
91 | for h in self.header: | |
|
92 | if h.startswith('index '): | |
|
93 | fp.write(_('this modifies a binary file (all or nothing)\n')) | |
|
94 | break | |
|
95 | if self.pretty_re.match(h): | |
|
96 | fp.write(h) | |
|
97 | if self.binary(): | |
|
98 | fp.write(_('this is a binary file\n')) | |
|
99 | break | |
|
100 | if h.startswith('---'): | |
|
101 | fp.write(_('%d hunks, %d lines changed\n') % | |
|
102 | (len(self.hunks), | |
|
103 | sum([max(h.added, h.removed) for h in self.hunks]))) | |
|
104 | break | |
|
105 | fp.write(h) | |
|
106 | ||
|
107 | def write(self, fp): | |
|
108 | fp.write(''.join(self.header)) | |
|
109 | ||
|
110 | def allhunks(self): | |
|
111 | return util.any(self.allhunks_re.match(h) for h in self.header) | |
|
112 | ||
|
113 | def files(self): | |
|
114 | match = self.diffgit_re.match(self.header[0]) | |
|
115 | if match: | |
|
116 | fromfile, tofile = match.groups() | |
|
117 | if fromfile == tofile: | |
|
118 | return [fromfile] | |
|
119 | return [fromfile, tofile] | |
|
120 | else: | |
|
121 | return self.diff_re.match(self.header[0]).groups() | |
|
122 | ||
|
123 | def filename(self): | |
|
124 | return self.files()[-1] | |
|
125 | ||
|
126 | def __repr__(self): | |
|
127 | return '<header %s>' % (' '.join(map(repr, self.files()))) | |
|
128 | ||
|
129 | def special(self): | |
|
130 | return util.any(self.special_re.match(h) for h in self.header) | |
|
131 | ||
|
132 | 72 | def countchanges(hunk): |
|
133 | 73 | """hunk -> (n+,n-)""" |
|
134 | 74 | add = len([h for h in hunk if h[0] == '+']) |
@@ -215,7 +155,7 b' def parsepatch(fp):' | |||
|
215 | 155 | |
|
216 | 156 | def newfile(self, hdr): |
|
217 | 157 | self.addcontext([]) |
|
218 | h = header(hdr) | |
|
158 | h = patch.header(hdr) | |
|
219 | 159 | self.headers.append(h) |
|
220 | 160 | self.header = h |
|
221 | 161 |
@@ -804,6 +804,64 b' class patchfile(object):' | |||
|
804 | 804 | self.write_rej() |
|
805 | 805 | return len(self.rej) |
|
806 | 806 | |
|
807 | class header(object): | |
|
808 | """patch header | |
|
809 | """ | |
|
810 | diffgit_re = re.compile('diff --git a/(.*) b/(.*)$') | |
|
811 | diff_re = re.compile('diff -r .* (.*)$') | |
|
812 | allhunks_re = re.compile('(?:index|deleted file) ') | |
|
813 | pretty_re = re.compile('(?:new file|deleted file) ') | |
|
814 | special_re = re.compile('(?:index|new|deleted|copy|rename) ') | |
|
815 | ||
|
816 | def __init__(self, header): | |
|
817 | self.header = header | |
|
818 | self.hunks = [] | |
|
819 | ||
|
820 | def binary(self): | |
|
821 | return util.any(h.startswith('index ') for h in self.header) | |
|
822 | ||
|
823 | def pretty(self, fp): | |
|
824 | for h in self.header: | |
|
825 | if h.startswith('index '): | |
|
826 | fp.write(_('this modifies a binary file (all or nothing)\n')) | |
|
827 | break | |
|
828 | if self.pretty_re.match(h): | |
|
829 | fp.write(h) | |
|
830 | if self.binary(): | |
|
831 | fp.write(_('this is a binary file\n')) | |
|
832 | break | |
|
833 | if h.startswith('---'): | |
|
834 | fp.write(_('%d hunks, %d lines changed\n') % | |
|
835 | (len(self.hunks), | |
|
836 | sum([max(h.added, h.removed) for h in self.hunks]))) | |
|
837 | break | |
|
838 | fp.write(h) | |
|
839 | ||
|
840 | def write(self, fp): | |
|
841 | fp.write(''.join(self.header)) | |
|
842 | ||
|
843 | def allhunks(self): | |
|
844 | return util.any(self.allhunks_re.match(h) for h in self.header) | |
|
845 | ||
|
846 | def files(self): | |
|
847 | match = self.diffgit_re.match(self.header[0]) | |
|
848 | if match: | |
|
849 | fromfile, tofile = match.groups() | |
|
850 | if fromfile == tofile: | |
|
851 | return [fromfile] | |
|
852 | return [fromfile, tofile] | |
|
853 | else: | |
|
854 | return self.diff_re.match(self.header[0]).groups() | |
|
855 | ||
|
856 | def filename(self): | |
|
857 | return self.files()[-1] | |
|
858 | ||
|
859 | def __repr__(self): | |
|
860 | return '<header %s>' % (' '.join(map(repr, self.files()))) | |
|
861 | ||
|
862 | def special(self): | |
|
863 | return util.any(self.special_re.match(h) for h in self.header) | |
|
864 | ||
|
807 | 865 | class hunk(object): |
|
808 | 866 | def __init__(self, desc, num, lr, context): |
|
809 | 867 | self.number = num |
General Comments 0
You need to be logged in to leave comments.
Login now