9 # clean up +6 and like (assumptions). should be turned into 'find'
10 # make regession tests for all cases (Only in, etc)
13 filename = sys.argv[1]
15 print 'requires a file name'
18 filefd = open(filename)
23 pat = "(^(?:diff .*\n)?--- .*\n\+\+\+ .*)?\n@@ -(\d+),?(\d+)? \+(\d+),?(\d+)? @@|^(Only in .*)"
24 startpat = re.compile(pat, re.M)
31 rexp = startpat.search(rest)
37 rest = rest[rexp.end(6)+1:]
40 header = rexp.group(1)
41 orgfile_start = string.atoi(rexp.group(2))
43 orgfile_len = string.atoi(rexp.group(3))
46 newfile_start = string.atoi(rexp.group(4))
48 newfile_len = string.atoi(rexp.group(5))
51 rest = rest[rexp.start(2):]
52 rest = rest[string.find(rest, "\n")+1:]
54 rexp2 = startpat.search(rest)
56 if rexp2.start(6) != -1:
57 oldrest = rest[rexp2.start(6)-1:]
58 rest = rest[:rexp2.start(6)]
59 elif rexp2.start(1) == -1:
60 oldrest = rest[rexp2.start(2)-5:]
61 rest = rest[:rexp2.start(2)-4]
63 oldrest = rest[rexp2.start(1)-1:]
64 rest = rest[:rexp2.start(1)]
68 # pos = filelen - len(oldrest)
69 # if pos - oldpos > 100:
70 # sys.stderr.write(`pos`+'/'+`filelen`+'\n')
78 #erstat early line stuff med lookbehind paa {1,2}-dims
80 linepat = "^([^-+\n]*)\n?(((^[-+].*\n)|^(.*\n){1,2}(?=^[-+].*\n))+)(.*)\n?"
81 compat = re.compile(linepat, re.M)
82 rexp = compat.search(rest)
86 prematch = rexp.group(1)
91 # print 'prematch ', rexp.start(1), rexp.end(1), prematch
92 # print 'match ---------'
94 # print 'match --------'
96 # dump unwanted early lines...
97 if match[0] != "+" and match[0] != "-":
99 next = string.find(match, '\n')
102 if match[next+1] == "+" or match[next+1] == "-":
103 prematch = match[:next]
104 match = match[next+1:]
106 match = match[next+1:]
109 # print 'prematch ', rexp.start(1), rexp.end(1), len(prematch)
110 # print '('+prematch+')'
111 # if prematch == ' ':
113 muddle = muddle - len(match)
115 lines = string.count(match, "\n")
116 compat = re.compile("^-", re.M)
117 minuses = len(compat.findall(match))
118 compat = re.compile("^\+", re.M)
119 plusses = len(compat.findall(match))
120 orgsize = minuses + 2 + (lines - minuses - plusses)
121 newsize = plusses + 2 + (lines - minuses - plusses)
123 noeol = "^(\\\ No newline at end of file)$"
124 compnoeol = re.compile(noeol, re.M)
125 if compnoeol.search(match) or compnoeol.search(rexp.group(6)):
126 orgsize = orgsize - 1
127 newsize = newsize - 1
130 if lines - plusses == 0:
132 elif lines - minuses == 0:
136 if not len(prematch):#or len(prematch) == 1 and prematch == ' ':
139 if rexp.start(6) == rexp.end(6):
143 # print "lines in match: ", lines
144 # print "number of minuses: ", minuses
145 # print "number of plusses: ", plusses
147 matchpos = rexp.start(2) + muddle
148 offset = string.count(rest[:matchpos], "\n")
150 # print 'offset/oldoffset: ', offset,oldoffset
151 # print 'oldplusses/oldminuses: ', oldplusses, oldminuses
152 # print 'orgfile_start/newfile_start: ', orgfile_start, newfile_start
154 orgstart = orgfile_start + offset + oldoffset - oldplusses
155 newstart = newfile_start + offset - oldminuses + oldoffset
157 # RAA: Bwadr. Fix antagelse om prematch paa en anden
161 if orgfile_start == 1 and not len(prematch):
163 if newfile_start == 1 and not len(prematch):
165 if orgfile_start == 0 and orgfile_len == 0:
168 plusses = plusses + 1
170 if newfile_start == 0 and newfile_len == 0:
173 plusses = plusses + 1
180 # should the start(1) == 0 be orgstart == 1? RAA
181 if orgstart == 1 and newstart == 1 and plusses == 0 and coherent:
182 print "@@ -"+`orgstart`+","+`orgsize`+" +"+`newstart`+" @@"
183 print match[:string.rfind(match, "\n")]
185 elif rexp.start(6) == rexp.end(6) and plusses == 0 and coherent:
187 orgstart = orgstart + 1
189 newstart = newstart + 1
190 print "@@ -"+`orgstart-1`+","+`orgsize`+" +"+`newstart-1`+" @@"
192 print match[:string.rfind(match, "\n")]
193 elif orgstart == 1 and orgstart == 1 and minuses == 0 and coherent:
194 print "@@ -"+`orgstart`+" +"+`newstart`+","+`newsize`+" @@"
195 print match[:string.rfind(match, "\n")]
197 elif rexp.start(6) == rexp.end(6) and minuses == 0 and coherent:
199 orgstart = orgstart + 1
201 newstart = newstart + 1
202 print "@@ -"+`orgstart-1`+" +"+`newstart-1`+","+`newsize`+" @@"
204 print match[:string.rfind(match, "\n")]
207 orgstart = orgstart + 1
209 newstart = newstart + 1
210 print "@@ -"+`orgstart-1`+","+`orgsize`+" +"+`newstart-1`+","+`newsize`+" @@"
213 print match[:string.rfind(match, "\n")]
214 if rexp.start(6) != rexp.end(6):
217 rest = rest[rexp.end(6):]
218 oldminuses = minuses + oldminuses
219 oldplusses = plusses + oldplusses
220 oldoffset = oldoffset + offset + lines #include match()-lines