Show More
@@ -69,51 +69,6 b' def scanpatch(fp):' | |||||
69 | else: |
|
69 | else: | |
70 | yield 'other', line |
|
70 | yield 'other', line | |
71 |
|
71 | |||
72 | class hunk(object): |
|
|||
73 | """patch hunk |
|
|||
74 |
|
||||
75 | XXX shouldn't we merge this with patch.hunk ? |
|
|||
76 | """ |
|
|||
77 | maxcontext = 3 |
|
|||
78 |
|
||||
79 | def __init__(self, header, fromline, toline, proc, before, hunk, after): |
|
|||
80 | def trimcontext(number, lines): |
|
|||
81 | delta = len(lines) - self.maxcontext |
|
|||
82 | if False and delta > 0: |
|
|||
83 | return number + delta, lines[:self.maxcontext] |
|
|||
84 | return number, lines |
|
|||
85 |
|
||||
86 | self.header = header |
|
|||
87 | self.fromline, self.before = trimcontext(fromline, before) |
|
|||
88 | self.toline, self.after = trimcontext(toline, after) |
|
|||
89 | self.proc = proc |
|
|||
90 | self.hunk = hunk |
|
|||
91 | self.added, self.removed = self.countchanges(self.hunk) |
|
|||
92 |
|
||||
93 | def countchanges(self, hunk): |
|
|||
94 | """hunk -> (n+,n-)""" |
|
|||
95 | add = len([h for h in hunk if h[0] == '+']) |
|
|||
96 | rem = len([h for h in hunk if h[0] == '-']) |
|
|||
97 | return add, rem |
|
|||
98 |
|
||||
99 | def write(self, fp): |
|
|||
100 | delta = len(self.before) + len(self.after) |
|
|||
101 | if self.after and self.after[-1] == '\\ No newline at end of file\n': |
|
|||
102 | delta -= 1 |
|
|||
103 | fromlen = delta + self.removed |
|
|||
104 | tolen = delta + self.added |
|
|||
105 | fp.write('@@ -%d,%d +%d,%d @@%s\n' % |
|
|||
106 | (self.fromline, fromlen, self.toline, tolen, |
|
|||
107 | self.proc and (' ' + self.proc))) |
|
|||
108 | fp.write(''.join(self.before + self.hunk + self.after)) |
|
|||
109 |
|
||||
110 | pretty = write |
|
|||
111 |
|
||||
112 | def filename(self): |
|
|||
113 | return self.header.filename() |
|
|||
114 |
|
||||
115 | def __repr__(self): |
|
|||
116 | return '<hunk %r@%d>' % (self.filename(), self.fromline) |
|
|||
117 |
|
72 | |||
118 | def parsepatch(fp): |
|
73 | def parsepatch(fp): | |
119 | """patch -> [] of headers -> [] of hunks """ |
|
74 | """patch -> [] of headers -> [] of hunks """ | |
@@ -137,8 +92,8 b' def parsepatch(fp):' | |||||
137 |
|
92 | |||
138 | def addcontext(self, context): |
|
93 | def addcontext(self, context): | |
139 | if self.hunk: |
|
94 | if self.hunk: | |
140 |
h = hunk(self.header, self.fromline, self.toline, |
|
95 | h = patch.recordhunk(self.header, self.fromline, self.toline, | |
141 | self.before, self.hunk, context) |
|
96 | self.proc, self.before, self.hunk, context) | |
142 | self.header.hunks.append(h) |
|
97 | self.header.hunks.append(h) | |
143 | self.fromline += len(self.before) + h.removed |
|
98 | self.fromline += len(self.before) + h.removed | |
144 | self.toline += len(self.before) + h.added |
|
99 | self.toline += len(self.before) + h.added | |
@@ -490,7 +445,7 b' def dorecord(ui, repo, commitfunc, cmdsu' | |||||
490 |
|
445 | |||
491 | newandmodifiedfiles = set() |
|
446 | newandmodifiedfiles = set() | |
492 | for h in chunks: |
|
447 | for h in chunks: | |
493 | ishunk = isinstance(h, hunk) |
|
448 | ishunk = isinstance(h, patch.recordhunk) | |
494 | isnew = h.filename() in status.added |
|
449 | isnew = h.filename() in status.added | |
495 | if ishunk and isnew and not h in originalchunks: |
|
450 | if ishunk and isnew and not h in originalchunks: | |
496 | newandmodifiedfiles.add(h.filename()) |
|
451 | newandmodifiedfiles.add(h.filename()) |
@@ -862,6 +862,52 b' class header(object):' | |||||
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 |
|
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) | |||
|
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 |
General Comments 0
You need to be logged in to leave comments.
Login now