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