Show More
@@ -1,104 +1,105 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 |
|
2 | |||
3 | import os, sys, struct, stat |
|
3 | import os, sys, struct, stat | |
4 | import difflib |
|
4 | import difflib | |
5 | import re |
|
5 | import re | |
6 | from optparse import OptionParser |
|
6 | from optparse import OptionParser | |
7 | from mercurial.bdiff import bdiff, blocks |
|
7 | from mercurial.bdiff import bdiff, blocks | |
8 | from mercurial.mdiff import bunidiff |
|
8 | from mercurial.mdiff import bunidiff, diffopts | |
9 |
|
9 | |||
10 |
VERSION="0. |
|
10 | VERSION="0.3" | |
11 | usage = "usage: %prog [options] file1 file2" |
|
11 | usage = "usage: %prog [options] file1 file2" | |
12 | parser = OptionParser(usage=usage) |
|
12 | parser = OptionParser(usage=usage) | |
13 |
|
13 | |||
14 | parser.add_option("-d", "--difflib", action="store_true", default=False) |
|
14 | parser.add_option("-d", "--difflib", action="store_true", default=False) | |
15 | parser.add_option('-x', '--count', default=1) |
|
15 | parser.add_option('-x', '--count', default=1) | |
16 | parser.add_option('-c', '--context', type="int", default=3) |
|
16 | parser.add_option('-c', '--context', type="int", default=3) | |
17 | parser.add_option('-p', '--show-c-function', action="store_true", default=False) |
|
17 | parser.add_option('-p', '--show-c-function', action="store_true", default=False) | |
18 | parser.add_option('-w', '--ignore-all-space', action="store_true", |
|
18 | parser.add_option('-w', '--ignore-all-space', action="store_true", | |
19 | default=False) |
|
19 | default=False) | |
20 |
|
20 | |||
21 | (options, args) = parser.parse_args() |
|
21 | (options, args) = parser.parse_args() | |
22 |
|
22 | |||
23 | if not args: |
|
23 | if not args: | |
24 | parser.print_help() |
|
24 | parser.print_help() | |
25 | sys.exit(1) |
|
25 | sys.exit(1) | |
26 |
|
26 | |||
27 | # simple utility function to put all the |
|
27 | # simple utility function to put all the | |
28 | # files from a directory tree into a dict |
|
28 | # files from a directory tree into a dict | |
29 | def buildlist(names, top): |
|
29 | def buildlist(names, top): | |
30 | tlen = len(top) |
|
30 | tlen = len(top) | |
31 | for root, dirs, files in os.walk(top): |
|
31 | for root, dirs, files in os.walk(top): | |
32 | l = root[tlen + 1:] |
|
32 | l = root[tlen + 1:] | |
33 | for x in files: |
|
33 | for x in files: | |
34 | p = os.path.join(root, x) |
|
34 | p = os.path.join(root, x) | |
35 | st = os.lstat(p) |
|
35 | st = os.lstat(p) | |
36 | if stat.S_ISREG(st.st_mode): |
|
36 | if stat.S_ISREG(st.st_mode): | |
37 | names[os.path.join(l, x)] = (st.st_dev, st.st_ino) |
|
37 | names[os.path.join(l, x)] = (st.st_dev, st.st_ino) | |
38 |
|
38 | |||
39 | def diff_files(file1, file2): |
|
39 | def diff_files(file1, file2): | |
40 | if file1 == None: |
|
40 | if file1 == None: | |
41 | b = file(file2).read().splitlines(1) |
|
41 | b = file(file2).read().splitlines(1) | |
42 | l1 = "--- %s\n" % (file2) |
|
42 | l1 = "--- %s\n" % (file2) | |
43 | l2 = "+++ %s\n" % (file2) |
|
43 | l2 = "+++ %s\n" % (file2) | |
44 | l3 = "@@ -0,0 +1,%d @@\n" % len(b) |
|
44 | l3 = "@@ -0,0 +1,%d @@\n" % len(b) | |
45 | l = [l1, l2, l3] + ["+" + e for e in b] |
|
45 | l = [l1, l2, l3] + ["+" + e for e in b] | |
46 | elif file2 == None: |
|
46 | elif file2 == None: | |
47 | a = file(file1).read().splitlines(1) |
|
47 | a = file(file1).read().splitlines(1) | |
48 | l1 = "--- %s\n" % (file1) |
|
48 | l1 = "--- %s\n" % (file1) | |
49 | l2 = "+++ %s\n" % (file1) |
|
49 | l2 = "+++ %s\n" % (file1) | |
50 | l3 = "@@ -1,%d +0,0 @@\n" % len(a) |
|
50 | l3 = "@@ -1,%d +0,0 @@\n" % len(a) | |
51 | l = [l1, l2, l3] + ["-" + e for e in a] |
|
51 | l = [l1, l2, l3] + ["-" + e for e in a] | |
52 | else: |
|
52 | else: | |
53 | t1 = file(file1).read() |
|
53 | t1 = file(file1).read() | |
54 | t2 = file(file2).read() |
|
54 | t2 = file(file2).read() | |
55 | l1 = t1.splitlines(1) |
|
55 | l1 = t1.splitlines(1) | |
56 | l2 = t2.splitlines(1) |
|
56 | l2 = t2.splitlines(1) | |
57 | if options.difflib: |
|
57 | if options.difflib: | |
58 | l = difflib.unified_diff(l1, l2, file1, file2) |
|
58 | l = difflib.unified_diff(l1, l2, file1, file2) | |
59 | else: |
|
59 | else: | |
60 |
l = bunidiff(t1, t2, l1, l2, file1, file2, |
|
60 | l = bunidiff(t1, t2, l1, l2, file1, file2, | |
|
61 | diffopts(context=options.context, | |||
61 | showfunc=options.show_c_function, |
|
62 | showfunc=options.show_c_function, | |
62 | ignorews=options.ignore_all_space) |
|
63 | ignorews=options.ignore_all_space)) | |
63 | for x in l: |
|
64 | for x in l: | |
64 | if x[-1] != '\n': |
|
65 | if x[-1] != '\n': | |
65 | x += "\n\ No newline at end of file\n" |
|
66 | x += "\n\ No newline at end of file\n" | |
66 | print x, |
|
67 | print x, | |
67 |
|
68 | |||
68 | file1 = args[0] |
|
69 | file1 = args[0] | |
69 | file2 = args[1] |
|
70 | file2 = args[1] | |
70 |
|
71 | |||
71 | if os.path.isfile(file1) and os.path.isfile(file2): |
|
72 | if os.path.isfile(file1) and os.path.isfile(file2): | |
72 | diff_files(file1, file2) |
|
73 | diff_files(file1, file2) | |
73 | elif os.path.isdir(file1): |
|
74 | elif os.path.isdir(file1): | |
74 | if not os.path.isdir(file2): |
|
75 | if not os.path.isdir(file2): | |
75 | sys.stderr.write("file types don't match\n") |
|
76 | sys.stderr.write("file types don't match\n") | |
76 | sys.exit(1) |
|
77 | sys.exit(1) | |
77 |
|
78 | |||
78 | d1 = {} |
|
79 | d1 = {} | |
79 | d2 = {} |
|
80 | d2 = {} | |
80 |
|
81 | |||
81 | buildlist(d1, file1) |
|
82 | buildlist(d1, file1) | |
82 | buildlist(d2, file2) |
|
83 | buildlist(d2, file2) | |
83 | keys = d1.keys() |
|
84 | keys = d1.keys() | |
84 | keys.sort() |
|
85 | keys.sort() | |
85 | for x in keys: |
|
86 | for x in keys: | |
86 | if x not in d2: |
|
87 | if x not in d2: | |
87 | f2 = None |
|
88 | f2 = None | |
88 | else: |
|
89 | else: | |
89 | f2 = os.path.join(file2, x) |
|
90 | f2 = os.path.join(file2, x) | |
90 | st1 = d1[x] |
|
91 | st1 = d1[x] | |
91 | st2 = d2[x] |
|
92 | st2 = d2[x] | |
92 | del d2[x] |
|
93 | del d2[x] | |
93 | if st1[0] == st2[0] and st1[1] == st2[1]: |
|
94 | if st1[0] == st2[0] and st1[1] == st2[1]: | |
94 | sys.stderr.write("%s is a hard link\n" % x) |
|
95 | sys.stderr.write("%s is a hard link\n" % x) | |
95 | continue |
|
96 | continue | |
96 | x = os.path.join(file1, x) |
|
97 | x = os.path.join(file1, x) | |
97 | diff_files(x, f2) |
|
98 | diff_files(x, f2) | |
98 | keys = d2.keys() |
|
99 | keys = d2.keys() | |
99 | keys.sort() |
|
100 | keys.sort() | |
100 | for x in keys: |
|
101 | for x in keys: | |
101 | f1 = None |
|
102 | f1 = None | |
102 | x = os.path.join(file2, x) |
|
103 | x = os.path.join(file2, x) | |
103 | diff_files(f1, x) |
|
104 | diff_files(f1, x) | |
104 |
|
105 |
General Comments 0
You need to be logged in to leave comments.
Login now