801 def close(self): |
801 def close(self): |
802 if self.dirty: |
802 if self.dirty: |
803 self.writelines(self.fname, self.lines, self.mode) |
803 self.writelines(self.fname, self.lines, self.mode) |
804 self.write_rej() |
804 self.write_rej() |
805 return len(self.rej) |
805 return len(self.rej) |
|
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) |
806 |
864 |
807 class hunk(object): |
865 class hunk(object): |
808 def __init__(self, desc, num, lr, context): |
866 def __init__(self, desc, num, lr, context): |
809 self.number = num |
867 self.number = num |
810 self.desc = desc |
868 self.desc = desc |