Browse Source

Support compare two files (wrapper of diff)

Matthew Wang 11 years ago
parent
commit
abe1787c4b
3 changed files with 34 additions and 20 deletions
  1. 4
    0
      CHANGES
  2. 8
    4
      README.rst
  3. 22
    16
      cdiff.py

+ 4
- 0
CHANGES View File

@@ -2,6 +2,10 @@
2 2
 Change log
3 3
 ==========
4 4
 
5
+Version 0.3 (2013-02-07)
6
+
7
+  - Support compare two files (wrapper of diff)
8
+
5 9
 Version 0.2 (2013-02-06)
6 10
 
7 11
   - Move cdiff.py to top dir for better meta info management

+ 8
- 4
README.rst View File

@@ -2,8 +2,8 @@ Cdiff
2 2
 =====
3 3
 
4 4
 Term based tool to view **colored**, **incremental** diff in *git/svn/hg*
5
-workspace, or diff from given file or stdin, with **side by side** and **auto
6
-pager** support.  Requires python (>= 2.5.0) and ``less``.
5
+workspace, given patch or two files, or from stdin, with **side by side** and
6
+**auto pager** support.  Requires python (>= 2.5.0) and ``less``.
7 7
 
8 8
 .. image:: http://ymattw.github.com/cdiff/img/default.png
9 9
    :alt: default
@@ -78,9 +78,8 @@ Pipe in a diff:
78 78
 .. code:: sh
79 79
 
80 80
     git log -p -2 | cdiff -s
81
-    git show 15bfa56 | cdiff -s
81
+    git show 15bfa5 | cdiff -s
82 82
     svn diff -r PREV | cdiff -s
83
-    diff -u foo foo.new | cdiff -s
84 83
 
85 84
 View a diff (patch) file:
86 85
 
@@ -90,6 +89,11 @@ View a diff (patch) file:
90 89
     cdiff foo.patch -s
91 90
     cdiff foo.patch -s -w 90
92 91
 
92
+View diff between two files (wrapper of ``diff``)::
93
+
94
+    cdiff foo foo.new       # equivalent to diff -u foo foo.new | cdiff
95
+    cdiff foo foo.new -s
96
+
93 97
 Redirect output to another patch file is safe:
94 98
 
95 99
 .. code:: sh

+ 22
- 16
cdiff.py View File

@@ -3,19 +3,20 @@
3 3
 
4 4
 """
5 5
 Term based tool to view **colored**, **incremental** diff in *git/svn/hg*
6
-workspace, given file or stdin, with **side by side** and **auto pager**
7
-support.  Requires python (>= 2.5.0) and ``less``.
6
+workspace, given patch or two files, or from stdin, with **side by side** and
7
+**auto pager** support.  Requires python (>= 2.5.0) and ``less``.
8 8
 """
9 9
 
10 10
 META_INFO = {
11
-    'version'     : '0.2',
11
+    'version'     : '0.3',
12 12
     'license'     : 'BSD-3',
13 13
     'author'      : 'Matthew Wang',
14 14
     'email'       : 'mattwyl(@)gmail(.)com',
15 15
     'url'         : 'https://github.com/ymattw/cdiff',
16 16
     'keywords'    : 'colored incremental side-by-side diff',
17
-    'description' : ('View colored, incremental diff in workspace, given file '
18
-                     'or from stdin, with side by side and auto pager support')
17
+    'description' : ('View colored, incremental diff in workspace, given patch '
18
+                     'or two files, or from stdin, with side by side and  auto '
19
+                     'pager support')
19 20
 }
20 21
 
21 22
 import sys
@@ -32,7 +33,6 @@ import errno
32 33
 import difflib
33 34
 
34 35
 
35
-
36 36
 COLORS = {
37 37
     'reset'         : '\x1b[0m',
38 38
     'underline'     : '\x1b[4m',
@@ -564,12 +564,12 @@ def main():
564 564
 
565 565
     supported_vcs = [check[0] for check, _ in REVISION_CONTROL]
566 566
 
567
-    usage = '%prog [options] [diff]'
568
-    description= ('View colored, incremental diff in %s workspace, or diff '
569
-                  'from given file or stdin, with side by side and auto '
570
-                  'pager support') % '/'.join(supported_vcs)
571
-
572
-    parser = optparse.OptionParser(usage=usage, description=description,
567
+    usage = """
568
+  %prog [options]
569
+  %prog [options] <patch>
570
+  %prog [options] <file1> <file2>"""
571
+    parser = optparse.OptionParser(usage=usage,
572
+            description=META_INFO['description'],
573 573
             version='%%prog %s' % META_INFO['version'])
574 574
     parser.add_option('-s', '--side-by-side', action='store_true',
575 575
             help=('show in side-by-side mode'))
@@ -577,7 +577,13 @@ def main():
577 577
             help='set text width (side-by-side mode only), default is 80')
578 578
     opts, args = parser.parse_args()
579 579
 
580
-    if len(args) >= 1:
580
+    if len(args) > 2:
581
+        parser.print_help()
582
+        return 1
583
+    elif len(args) == 2:
584
+        diff_hdl = subprocess.Popen(['diff', '-u', args[0], args[1]],
585
+                stdout=subprocess.PIPE).stdout
586
+    elif len(args) == 1:
581 587
         if IS_PY3:
582 588
             # Python3 needs the newline='' to keep '\r' (DOS format)
583 589
             diff_hdl = open(args[0], mode='rt', newline='')
@@ -596,13 +602,13 @@ def main():
596 602
     # FIXME: can't use generator for now due to current implementation in parser
597 603
     stream = [decode(line) for line in diff_hdl.readlines()]
598 604
 
605
+    if diff_hdl is not sys.stdin:
606
+        diff_hdl.close()
607
+
599 608
     # Don't let empty diff pass thru
600 609
     if not stream:
601 610
         return 0
602 611
 
603
-    if diff_hdl is not sys.stdin:
604
-        diff_hdl.close()
605
-
606 612
     if sys.stdout.isatty():
607 613
         try:
608 614
             markup_to_pager(stream, opts)