..\latest\diff.py
..\patched\diff.py
1""" Command line interface to difflib.py providing diffs in three formats: n 1""" Command line interface to difflib.py providing diffs in four formats:
2 2
3* ndiff:    lists every line and highlights interline changes. 3* ndiff:    lists every line and highlights interline changes.
4* context:  highlights clusters of changes in a before/after format n 4* context:  highlights clusters of changes in a before/after format.
5* unified:  highlights clusters of changes in an inline format. 5* unified:  highlights clusters of changes in an inline format.
n 6* html:     generates side by side comparison with change highlights.
6 7
7""" 8"""
8 9
9import sys, os, time, difflib, optparse 10import sys, os, time, difflib, optparse
10 11
11usage = "usage: %prog [options] fromfile tofile" 12usage = "usage: %prog [options] fromfile tofile"
12parser = optparse.OptionParser(usage) 13parser = optparse.OptionParser(usage)
13parser.add_option("-c", action="store_true", default=False, help='Produce a context format diff (default)') 14parser.add_option("-c", action="store_true", default=False, help='Produce a context format diff (default)')
14parser.add_option("-u", action="store_true", default=False, help='Produce a unified format diff') 15parser.add_option("-u", action="store_true", default=False, help='Produce a unified format diff')
n 16parser.add_option("-m", action="store_true", default=False, help='Produce HTML side by side diff (can use -c and -l in conjunction)')
15parser.add_option("-n", action="store_true", default=False, help='Produce a ndiff format diff') 17parser.add_option("-n", action="store_true", default=False, help='Produce a ndiff format diff')
16parser.add_option("-l", "--lines", type="int", default=3, help='Set number of context lines (default 3)') 18parser.add_option("-l", "--lines", type="int", default=3, help='Set number of context lines (default 3)')
17(options, args) = parser.parse_args() 19(options, args) = parser.parse_args()
18 20
19if len(args) == 0: 21if len(args) == 0:
20    parser.print_help() 22    parser.print_help()
21    sys.exit(1) 23    sys.exit(1)
22if len(args) != 2: 24if len(args) != 2:
23    parser.error("need to specify both a fromfile and tofile") 25    parser.error("need to specify both a fromfile and tofile")
24 26
25n = options.lines 27n = options.lines
26fromfile, tofile = args 28fromfile, tofile = args
27 29
28fromdate = time.ctime(os.stat(fromfile).st_mtime) 30fromdate = time.ctime(os.stat(fromfile).st_mtime)
29todate = time.ctime(os.stat(tofile).st_mtime) 31todate = time.ctime(os.stat(tofile).st_mtime)
30fromlines = open(fromfile).readlines() 32fromlines = open(fromfile).readlines()
31tolines = open(tofile).readlines() 33tolines = open(tofile).readlines()
32 34
33if options.u: 35if options.u:
34    diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n) 36    diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
35elif options.n: 37elif options.n:
36    diff = difflib.ndiff(fromlines, tolines) 38    diff = difflib.ndiff(fromlines, tolines)
t 39elif options.m:
40    diff = difflib.HtmlDiff().make_file(fromlines,tolines,fromfile,tofile,context=options.c,numlines=n)
37else: 41else:
38    diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n) 42    diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
39 43
40sys.stdout.writelines(diff) 44sys.stdout.writelines(diff)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op