blob: ddca07d38ff3cdb2065947e1aadc41051988efa1 [file] [log] [blame]
Guido van Rossum83b85181998-05-06 17:43:30 +00001#! /usr/bin/env python
2
Tim Peters2f1aeb92000-12-09 05:03:22 +00003# Module ndiff version 1.6.0
4# Released to the public domain 08-Dec-2000,
5# by Tim Peters (tim.one@home.com).
Guido van Rossum83b85181998-05-06 17:43:30 +00006
Guido van Rossuma3433e81999-03-27 13:34:01 +00007# Provided as-is; use at your own risk; no warranty; no promises; enjoy!
Guido van Rossum83b85181998-05-06 17:43:30 +00008
Guido van Rossuma3433e81999-03-27 13:34:01 +00009"""ndiff [-q] file1 file2
Guido van Rossum02ef28b1999-03-28 17:55:32 +000010 or
11ndiff (-r1 | -r2) < ndiff_output > file1_or_file2
Guido van Rossuma3433e81999-03-27 13:34:01 +000012
13Print a human-friendly file difference report to stdout. Both inter-
Guido van Rossum02ef28b1999-03-28 17:55:32 +000014and intra-line differences are noted. In the second form, recreate file1
15(-r1) or file2 (-r2) on stdout, from an ndiff report on stdin.
Guido van Rossuma3433e81999-03-27 13:34:01 +000016
Guido van Rossum02ef28b1999-03-28 17:55:32 +000017In the first form, if -q ("quiet") is not specified, the first two lines
18of output are
Guido van Rossuma3433e81999-03-27 13:34:01 +000019
20-: file1
21+: file2
22
23Each remaining line begins with a two-letter code:
24
25 "- " line unique to file1
26 "+ " line unique to file2
27 " " line common to both files
28 "? " line not present in either input file
29
30Lines beginning with "? " attempt to guide the eye to intraline
Tim Peters0d430e22000-11-01 02:51:27 +000031differences, and were not present in either input file. These lines can be
32confusing if the source files contain tab characters.
Guido van Rossuma3433e81999-03-27 13:34:01 +000033
34The first file can be recovered by retaining only lines that begin with
Guido van Rossum02ef28b1999-03-28 17:55:32 +000035" " or "- ", and deleting those 2-character prefixes; use ndiff with -r1.
Guido van Rossuma3433e81999-03-27 13:34:01 +000036
Tim Peters0d430e22000-11-01 02:51:27 +000037The second file can be recovered similarly, but by retaining only " " and
38"+ " lines; use ndiff with -r2; or, on Unix, the second file can be
Guido van Rossum02ef28b1999-03-28 17:55:32 +000039recovered by piping the output through
40
Guido van Rossuma3433e81999-03-27 13:34:01 +000041 sed -n '/^[+ ] /s/^..//p'
Guido van Rossuma3433e81999-03-27 13:34:01 +000042
43See module comments for details and programmatic interface.
44"""
45
Tim Peters0d430e22000-11-01 02:51:27 +000046__version__ = 1, 5, 0
Guido van Rossum83b85181998-05-06 17:43:30 +000047
48# SequenceMatcher tries to compute a "human-friendly diff" between
49# two sequences (chiefly picturing a file as a sequence of lines,
Guido van Rossuma3433e81999-03-27 13:34:01 +000050# and a line as a sequence of characters, here). Unlike e.g. UNIX(tm)
51# diff, the fundamental notion is the longest *contiguous* & junk-free
Guido van Rossum83b85181998-05-06 17:43:30 +000052# matching subsequence. That's what catches peoples' eyes. The
53# Windows(tm) windiff has another interesting notion, pairing up elements
54# that appear uniquely in each sequence. That, and the method here,
55# appear to yield more intuitive difference reports than does diff. This
56# method appears to be the least vulnerable to synching up on blocks
57# of "junk lines", though (like blank lines in ordinary text files,
58# or maybe "<P>" lines in HTML files). That may be because this is
59# the only method of the 3 that has a *concept* of "junk" <wink>.
60#
61# Note that ndiff makes no claim to produce a *minimal* diff. To the
62# contrary, minimal diffs are often counter-intuitive, because they
63# synch up anywhere possible, sometimes accidental matches 100 pages
64# apart. Restricting synch points to contiguous matches preserves some
65# notion of locality, at the occasional cost of producing a longer diff.
66#
Guido van Rossuma3433e81999-03-27 13:34:01 +000067# With respect to junk, an earlier version of ndiff simply refused to
Guido van Rossum83b85181998-05-06 17:43:30 +000068# *start* a match with a junk element. The result was cases like this:
69# before: private Thread currentThread;
70# after: private volatile Thread currentThread;
Guido van Rossuma3433e81999-03-27 13:34:01 +000071# If you consider whitespace to be junk, the longest contiguous match
Guido van Rossum83b85181998-05-06 17:43:30 +000072# not starting with junk is "e Thread currentThread". So ndiff reported
73# that "e volatil" was inserted between the 't' and the 'e' in "private".
74# While an accurate view, to people that's absurd. The current version
75# looks for matching blocks that are entirely junk-free, then extends the
76# longest one of those as far as possible but only with matching junk.
77# So now "currentThread" is matched, then extended to suck up the
78# preceding blank; then "private" is matched, and extended to suck up the
79# following blank; then "Thread" is matched; and finally ndiff reports
80# that "volatile " was inserted before "Thread". The only quibble
Guido van Rossuma3433e81999-03-27 13:34:01 +000081# remaining is that perhaps it was really the case that " volatile"
Guido van Rossum83b85181998-05-06 17:43:30 +000082# was inserted after "private". I can live with that <wink>.
83#
Guido van Rossum83b85181998-05-06 17:43:30 +000084# NOTE on junk: the module-level names
85# IS_LINE_JUNK
86# IS_CHARACTER_JUNK
87# can be set to any functions you like. The first one should accept
88# a single string argument, and return true iff the string is junk.
89# The default is whether the regexp r"\s*#?\s*$" matches (i.e., a
90# line without visible characters, except for at most one splat).
91# The second should accept a string of length 1 etc. The default is
92# whether the character is a blank or tab (note: bad idea to include
93# newline in this!).
94#
95# After setting those, you can call fcompare(f1name, f2name) with the
96# names of the files you want to compare. The difference report
Guido van Rossuma3433e81999-03-27 13:34:01 +000097# is sent to stdout. Or you can call main(args), passing what would
98# have been in sys.argv[1:] had the cmd-line form been used.
Guido van Rossum83b85181998-05-06 17:43:30 +000099
Tim Peters9ae21482001-02-10 08:00:53 +0000100from difflib import SequenceMatcher
101
Guido van Rossum83b85181998-05-06 17:43:30 +0000102import string
103TRACE = 0
104
105# define what "junk" means
106import re
107
108def IS_LINE_JUNK(line, pat=re.compile(r"\s*#?\s*$").match):
109 return pat(line) is not None
110
111def IS_CHARACTER_JUNK(ch, ws=" \t"):
112 return ch in ws
113
114del re
115
Guido van Rossum83b85181998-05-06 17:43:30 +0000116# meant for dumping lines
117def dump(tag, x, lo, hi):
118 for i in xrange(lo, hi):
119 print tag, x[i],
120
Guido van Rossum83b85181998-05-06 17:43:30 +0000121def plain_replace(a, alo, ahi, b, blo, bhi):
122 assert alo < ahi and blo < bhi
123 # dump the shorter block first -- reduces the burden on short-term
124 # memory if the blocks are of very different sizes
125 if bhi - blo < ahi - alo:
126 dump('+', b, blo, bhi)
127 dump('-', a, alo, ahi)
128 else:
129 dump('-', a, alo, ahi)
130 dump('+', b, blo, bhi)
131
132# When replacing one block of lines with another, this guy searches
133# the blocks for *similar* lines; the best-matching pair (if any) is
134# used as a synch point, and intraline difference marking is done on
135# the similar pair. Lots of work, but often worth it.
136
137def fancy_replace(a, alo, ahi, b, blo, bhi):
138 if TRACE:
139 print '*** fancy_replace', alo, ahi, blo, bhi
140 dump('>', a, alo, ahi)
141 dump('<', b, blo, bhi)
142
143 # don't synch up unless the lines have a similarity score of at
144 # least cutoff; best_ratio tracks the best score seen so far
145 best_ratio, cutoff = 0.74, 0.75
146 cruncher = SequenceMatcher(IS_CHARACTER_JUNK)
147 eqi, eqj = None, None # 1st indices of equal lines (if any)
148
149 # search for the pair that matches best without being identical
150 # (identical lines must be junk lines, & we don't want to synch up
151 # on junk -- unless we have to)
152 for j in xrange(blo, bhi):
153 bj = b[j]
154 cruncher.set_seq2(bj)
155 for i in xrange(alo, ahi):
156 ai = a[i]
157 if ai == bj:
158 if eqi is None:
159 eqi, eqj = i, j
160 continue
161 cruncher.set_seq1(ai)
162 # computing similarity is expensive, so use the quick
163 # upper bounds first -- have seen this speed up messy
164 # compares by a factor of 3.
165 # note that ratio() is only expensive to compute the first
166 # time it's called on a sequence pair; the expensive part
167 # of the computation is cached by cruncher
168 if cruncher.real_quick_ratio() > best_ratio and \
169 cruncher.quick_ratio() > best_ratio and \
170 cruncher.ratio() > best_ratio:
171 best_ratio, best_i, best_j = cruncher.ratio(), i, j
172 if best_ratio < cutoff:
173 # no non-identical "pretty close" pair
174 if eqi is None:
175 # no identical pair either -- treat it as a straight replace
176 plain_replace(a, alo, ahi, b, blo, bhi)
177 return
178 # no close pair, but an identical pair -- synch up on that
179 best_i, best_j, best_ratio = eqi, eqj, 1.0
180 else:
181 # there's a close pair, so forget the identical pair (if any)
182 eqi = None
183
184 # a[best_i] very similar to b[best_j]; eqi is None iff they're not
185 # identical
186 if TRACE:
187 print '*** best_ratio', best_ratio, best_i, best_j
188 dump('>', a, best_i, best_i+1)
189 dump('<', b, best_j, best_j+1)
190
191 # pump out diffs from before the synch point
192 fancy_helper(a, alo, best_i, b, blo, best_j)
193
194 # do intraline marking on the synch pair
195 aelt, belt = a[best_i], b[best_j]
196 if eqi is None:
Tim Peters2f1aeb92000-12-09 05:03:22 +0000197 # pump out a '-', '?', '+', '?' quad for the synched lines
Guido van Rossum83b85181998-05-06 17:43:30 +0000198 atags = btags = ""
199 cruncher.set_seqs(aelt, belt)
200 for tag, ai1, ai2, bj1, bj2 in cruncher.get_opcodes():
201 la, lb = ai2 - ai1, bj2 - bj1
202 if tag == 'replace':
Tim Peters2f1aeb92000-12-09 05:03:22 +0000203 atags += '^' * la
204 btags += '^' * lb
Guido van Rossum83b85181998-05-06 17:43:30 +0000205 elif tag == 'delete':
Tim Peters2f1aeb92000-12-09 05:03:22 +0000206 atags += '-' * la
Guido van Rossum83b85181998-05-06 17:43:30 +0000207 elif tag == 'insert':
Tim Peters2f1aeb92000-12-09 05:03:22 +0000208 btags += '+' * lb
Guido van Rossum83b85181998-05-06 17:43:30 +0000209 elif tag == 'equal':
Tim Peters2f1aeb92000-12-09 05:03:22 +0000210 atags += ' ' * la
211 btags += ' ' * lb
Guido van Rossum83b85181998-05-06 17:43:30 +0000212 else:
213 raise ValueError, 'unknown tag ' + `tag`
Tim Peters2f1aeb92000-12-09 05:03:22 +0000214 printq(aelt, belt, atags, btags)
Guido van Rossum83b85181998-05-06 17:43:30 +0000215 else:
216 # the synch pair is identical
217 print ' ', aelt,
218
219 # pump out diffs from after the synch point
220 fancy_helper(a, best_i+1, ahi, b, best_j+1, bhi)
221
222def fancy_helper(a, alo, ahi, b, blo, bhi):
223 if alo < ahi:
224 if blo < bhi:
225 fancy_replace(a, alo, ahi, b, blo, bhi)
226 else:
227 dump('-', a, alo, ahi)
228 elif blo < bhi:
229 dump('+', b, blo, bhi)
230
Tim Peters0d430e22000-11-01 02:51:27 +0000231# Crap to deal with leading tabs in "?" output. Can hurt, but will
232# probably help most of the time.
233
Tim Peters2f1aeb92000-12-09 05:03:22 +0000234def printq(aline, bline, atags, btags):
Tim Peters0d430e22000-11-01 02:51:27 +0000235 common = min(count_leading(aline, "\t"),
236 count_leading(bline, "\t"))
Tim Peters2f1aeb92000-12-09 05:03:22 +0000237 common = min(common, count_leading(atags[:common], " "))
238 print "-", aline,
239 if count_leading(atags, " ") < len(atags):
240 print "?", "\t" * common + atags[common:]
241 print "+", bline,
242 if count_leading(btags, " ") < len(btags):
243 print "?", "\t" * common + btags[common:]
Tim Peters0d430e22000-11-01 02:51:27 +0000244
245def count_leading(line, ch):
246 i, n = 0, len(line)
247 while i < n and line[i] == ch:
248 i += 1
249 return i
250
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000251def fail(msg):
252 import sys
253 out = sys.stderr.write
254 out(msg + "\n\n")
255 out(__doc__)
256 return 0
257
Guido van Rossum83b85181998-05-06 17:43:30 +0000258# open a file & return the file object; gripe and return 0 if it
259# couldn't be opened
260def fopen(fname):
261 try:
262 return open(fname, 'r')
263 except IOError, detail:
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000264 return fail("couldn't open " + fname + ": " + str(detail))
Guido van Rossum83b85181998-05-06 17:43:30 +0000265
266# open two files & spray the diff to stdout; return false iff a problem
267def fcompare(f1name, f2name):
268 f1 = fopen(f1name)
269 f2 = fopen(f2name)
270 if not f1 or not f2:
271 return 0
272
273 a = f1.readlines(); f1.close()
274 b = f2.readlines(); f2.close()
275
276 cruncher = SequenceMatcher(IS_LINE_JUNK, a, b)
277 for tag, alo, ahi, blo, bhi in cruncher.get_opcodes():
278 if tag == 'replace':
279 fancy_replace(a, alo, ahi, b, blo, bhi)
280 elif tag == 'delete':
281 dump('-', a, alo, ahi)
282 elif tag == 'insert':
283 dump('+', b, blo, bhi)
284 elif tag == 'equal':
285 dump(' ', a, alo, ahi)
286 else:
287 raise ValueError, 'unknown tag ' + `tag`
288
289 return 1
290
Guido van Rossuma3433e81999-03-27 13:34:01 +0000291# crack args (sys.argv[1:] is normal) & compare;
292# return false iff a problem
293
294def main(args):
295 import getopt
296 try:
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000297 opts, args = getopt.getopt(args, "qr:")
Guido van Rossuma3433e81999-03-27 13:34:01 +0000298 except getopt.error, detail:
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000299 return fail(str(detail))
Guido van Rossuma3433e81999-03-27 13:34:01 +0000300 noisy = 1
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000301 qseen = rseen = 0
Guido van Rossuma3433e81999-03-27 13:34:01 +0000302 for opt, val in opts:
303 if opt == "-q":
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000304 qseen = 1
Guido van Rossuma3433e81999-03-27 13:34:01 +0000305 noisy = 0
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000306 elif opt == "-r":
307 rseen = 1
308 whichfile = val
309 if qseen and rseen:
310 return fail("can't specify both -q and -r")
311 if rseen:
312 if args:
313 return fail("no args allowed with -r option")
314 if whichfile in "12":
315 restore(whichfile)
316 return 1
317 return fail("-r value must be 1 or 2")
Guido van Rossuma3433e81999-03-27 13:34:01 +0000318 if len(args) != 2:
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000319 return fail("need 2 filename args")
Guido van Rossuma3433e81999-03-27 13:34:01 +0000320 f1name, f2name = args
321 if noisy:
322 print '-:', f1name
323 print '+:', f2name
Guido van Rossum83b85181998-05-06 17:43:30 +0000324 return fcompare(f1name, f2name)
325
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000326def restore(which):
327 import sys
328 tag = {"1": "- ", "2": "+ "}[which]
329 prefixes = (" ", tag)
330 for line in sys.stdin.readlines():
331 if line[:2] in prefixes:
332 print line[2:],
333
Guido van Rossum83b85181998-05-06 17:43:30 +0000334if __name__ == '__main__':
Guido van Rossuma3433e81999-03-27 13:34:01 +0000335 import sys
336 args = sys.argv[1:]
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000337 if "-profile" in args:
Guido van Rossum83b85181998-05-06 17:43:30 +0000338 import profile, pstats
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000339 args.remove("-profile")
Guido van Rossum83b85181998-05-06 17:43:30 +0000340 statf = "ndiff.pro"
Guido van Rossuma3433e81999-03-27 13:34:01 +0000341 profile.run("main(args)", statf)
Guido van Rossum83b85181998-05-06 17:43:30 +0000342 stats = pstats.Stats(statf)
343 stats.strip_dirs().sort_stats('time').print_stats()
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000344 else:
345 main(args)