comparison mercurial/patch.py @ 24263:a45d1c51109e

record: move hunk class from record to patch Part of a series of patches to move record from hgext to core
author Laurent Charignon <lcharignon@fb.com>
date Mon, 09 Mar 2015 13:09:15 -0700
parents 20aac24e2114
children c4205452f1b7
comparison
equal deleted inserted replaced
24262:7f24b2a0581a 24263:a45d1c51109e
859 def __repr__(self): 859 def __repr__(self):
860 return '<header %s>' % (' '.join(map(repr, self.files()))) 860 return '<header %s>' % (' '.join(map(repr, self.files())))
861 861
862 def special(self): 862 def special(self):
863 return util.any(self.special_re.match(h) for h in self.header) 863 return util.any(self.special_re.match(h) for h in self.header)
864
865 class recordhunk(object):
866 """patch hunk
867
868 XXX shouldn't we merge this with the other hunk class?
869 """
870 maxcontext = 3
871
872 def __init__(self, header, fromline, toline, proc, before, hunk, after):
873 def trimcontext(number, lines):
874 delta = len(lines) - self.maxcontext
875 if False and delta > 0:
876 return number + delta, lines[:self.maxcontext]
877 return number, lines
878
879 self.header = header
880 self.fromline, self.before = trimcontext(fromline, before)
881 self.toline, self.after = trimcontext(toline, after)
882 self.proc = proc
883 self.hunk = hunk
884 self.added, self.removed = self.countchanges(self.hunk)
885
886 def countchanges(self, hunk):
887 """hunk -> (n+,n-)"""
888 add = len([h for h in hunk if h[0] == '+'])
889 rem = len([h for h in hunk if h[0] == '-'])
890 return add, rem
891
892 def write(self, fp):
893 delta = len(self.before) + len(self.after)
894 if self.after and self.after[-1] == '\\ No newline at end of file\n':
895 delta -= 1
896 fromlen = delta + self.removed
897 tolen = delta + self.added
898 fp.write('@@ -%d,%d +%d,%d @@%s\n' %
899 (self.fromline, fromlen, self.toline, tolen,
900 self.proc and (' ' + self.proc)))
901 fp.write(''.join(self.before + self.hunk + self.after))
902
903 pretty = write
904
905 def filename(self):
906 return self.header.filename()
907
908 def __repr__(self):
909 return '<hunk %r@%d>' % (self.filename(), self.fromline)
864 910
865 class hunk(object): 911 class hunk(object):
866 def __init__(self, desc, num, lr, context): 912 def __init__(self, desc, num, lr, context):
867 self.number = num 913 self.number = num
868 self.desc = desc 914 self.desc = desc