summaryrefslogtreecommitdiffstats
path: root/needs-checking/unitdiff.py
blob: d19d5e7fc39a465e5be60c5ecf819f4356b25a52 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/python

import sys
import re
import string

#TODO
# clean up rest/file
# clean up +6 and like (assumptions). should be turned into 'find'
# make regession tests for all cases (Only in, etc)

try:
        filename = sys.argv[1]
except:
        print 'requires a file name'
        sys.exit(1)

filefd = open(filename)
file = filefd.read()
filefd.close()

rest = file
pat = "(^(?:diff .*\n)?--- .*\n\+\+\+ .*)?\n@@ -(\d+),?(\d+)? \+(\d+),?(\d+)? @@|^(Only in .*)"
startpat = re.compile(pat, re.M)

pos = 0
oldpos = 0
filelen = len(rest)
oldrest = ""
while(1):
        rexp = startpat.search(rest)
        if not rexp:
                break

	if rexp.group(6):
		print rexp.group(6)
		rest = rest[rexp.end(6)+1:]
		continue
		
	header = rexp.group(1)
        orgfile_start = string.atoi(rexp.group(2))
	if rexp.group(3):
		orgfile_len = string.atoi(rexp.group(3))
	else:
		orgfile_len = -1
        newfile_start = string.atoi(rexp.group(4))
	if rexp.group(5):
		newfile_len = string.atoi(rexp.group(5))
	else:
		newfile_len = -1
        rest = rest[rexp.start(2):]
        rest = rest[string.find(rest, "\n")+1:]

	rexp2 = startpat.search(rest)
	if rexp2:
		if rexp2.start(6) != -1:
			oldrest = rest[rexp2.start(6)-1:]
			rest = rest[:rexp2.start(6)]
		elif rexp2.start(1) == -1:
			oldrest = rest[rexp2.start(2)-5:]
			rest = rest[:rexp2.start(2)-4]
		else:
			oldrest = rest[rexp2.start(1)-1:]
			rest = rest[:rexp2.start(1)]
	else:
		oldrest = rest

#	pos = filelen - len(oldrest)
#	if pos - oldpos > 100:
#		sys.stderr.write(`pos`+'/'+`filelen`+'\n')
#		oldpos = pos

	first = 1
	oldminuses = 0
	oldplusses = 0
	oldoffset = 0
	while(1):
		#erstat early line stuff med lookbehind paa {1,2}-dims
		#nedenfor RAA
	        linepat = "^([^-+\n]*)\n?(((^[-+].*\n)|^(.*\n){1,2}(?=^[-+].*\n))+)(.*)\n?"
		compat = re.compile(linepat, re.M)
	        rexp = compat.search(rest)
	        if not rexp:
	                break

		prematch = rexp.group(1)
	        match = rexp.group(2)
		muddle = len(match)

#		print rest
#		print 'prematch ', rexp.start(1), rexp.end(1), prematch
#		print 'match ---------'
#		print match
#		print 'match --------'

		# dump unwanted early lines...
		if match[0] != "+" and match[0] != "-":
			while(1):
				next = string.find(match, '\n')
				if next == -1:
					break
				if match[next+1] == "+" or match[next+1] == "-":
					prematch = match[:next]
					match = match[next+1:]
					break
				match = match[next+1:]


#		print 'prematch ', rexp.start(1), rexp.end(1), len(prematch)
#		print '('+prematch+')'
#		if prematch == ' ':
#			print 'space'
		muddle = muddle - len(match)

	        lines = string.count(match, "\n")
		compat = re.compile("^-", re.M)
	        minuses = len(compat.findall(match))
		compat = re.compile("^\+", re.M)
	        plusses = len(compat.findall(match))
	        orgsize = minuses + 2 + (lines - minuses - plusses)
	        newsize = plusses + 2 + (lines - minuses - plusses)

		noeol = "^(\\\ No newline at end of file)$"
		compnoeol = re.compile(noeol, re.M)
		if compnoeol.search(match) or compnoeol.search(rexp.group(6)):
			orgsize = orgsize - 1
			newsize = newsize - 1
			
		coherent = 0
		if lines - plusses == 0:
			coherent = 1
		elif lines - minuses == 0:
			coherent = 1

		# RAA FIXME
		if not len(prematch):#or len(prematch) == 1 and prematch == ' ':
			orgsize = orgsize -1
			newsize = newsize -1
		if rexp.start(6) == rexp.end(6):
			orgsize = orgsize -1
			newsize = newsize -1

#	        print "lines in match: ", lines
#	        print "number of minuses: ", minuses
#	        print "number of plusses: ", plusses
	
	        matchpos = rexp.start(2) + muddle
	        offset =  string.count(rest[:matchpos], "\n")

#		print 'offset/oldoffset: ', offset,oldoffset
#		print 'oldplusses/oldminuses: ', oldplusses, oldminuses
#		print 'orgfile_start/newfile_start: ', orgfile_start, newfile_start

	        orgstart = orgfile_start + offset + oldoffset - oldplusses
	        newstart = newfile_start + offset - oldminuses + oldoffset

		# RAA: Bwadr. Fix antagelse om prematch paa en anden
		# maade
		orgstartmod = 0
		newstartmod = 0
		if orgfile_start == 1 and not len(prematch):
			orgstartmod = 1
		if newfile_start == 1 and not len(prematch):
			newstartmod = 1
		if orgfile_start == 0 and orgfile_len == 0:
			orgstartmod = 1
			# RAA Hack!
			plusses = plusses + 1
			minuses = minuses +1
		if newfile_start == 0 and newfile_len == 0:
			newstartmod = 1
			# RAA Hack!
			plusses = plusses + 1
			minuses = minuses +1
		
		if header and first:
			print header
			first = 0

		# should the start(1) == 0 be orgstart == 1? RAA
	        if orgstart == 1 and newstart == 1 and plusses == 0 and coherent:
	                print "@@ -"+`orgstart`+","+`orgsize`+" +"+`newstart`+" @@"
	                print match[:string.rfind(match, "\n")]
	                print rexp.group(6)
	        elif rexp.start(6) == rexp.end(6) and plusses == 0 and coherent:
			if orgstartmod:
				orgstart = orgstart + 1
			if newstartmod:
				newstart = newstart + 1
	                print "@@ -"+`orgstart-1`+","+`orgsize`+" +"+`newstart-1`+" @@"
	                print prematch
	                print match[:string.rfind(match, "\n")]
	        elif orgstart == 1 and orgstart == 1 and minuses == 0 and coherent:
	                print "@@ -"+`orgstart`+" +"+`newstart`+","+`newsize`+" @@"
	                print match[:string.rfind(match, "\n")]
	                print rexp.group(6)
	        elif rexp.start(6) == rexp.end(6) and minuses == 0 and coherent:
			if orgstartmod:
				orgstart = orgstart + 1
			if newstartmod:
				newstart = newstart + 1
	                print "@@ -"+`orgstart-1`+" +"+`newstart-1`+","+`newsize`+" @@"
	                print prematch
	                print match[:string.rfind(match, "\n")]
	        else:
			if orgstartmod:
				orgstart = orgstart + 1
			if newstartmod:
				newstart = newstart + 1
	                print "@@ -"+`orgstart-1`+","+`orgsize`+" +"+`newstart-1`+","+`newsize`+" @@"
			if len(prematch):
				print prematch
	                print match[:string.rfind(match, "\n")]
			if rexp.start(6) != rexp.end(6):
	                	print rexp.group(6)
	
        	rest = rest[rexp.end(6):]
		oldminuses = minuses + oldminuses
		oldplusses = plusses + oldplusses
		oldoffset = oldoffset + offset + lines #include match()-lines


	rest = oldrest