Selaa lähdekoodia

Support compare two files (wrapper of diff)

Matthew Wang 11 vuotta sitten
vanhempi
commit
abe1787c4b
3 muutettua tiedostoa jossa 34 lisäystä ja 20 poistoa
  1. 4
    0
      CHANGES
  2. 8
    4
      README.rst
  3. 22
    16
      cdiff.py

+ 4
- 0
CHANGES Näytä tiedosto

2
 Change log
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
 Version 0.2 (2013-02-06)
9
 Version 0.2 (2013-02-06)
6
 
10
 
7
   - Move cdiff.py to top dir for better meta info management
11
   - Move cdiff.py to top dir for better meta info management

+ 8
- 4
README.rst Näytä tiedosto

2
 =====
2
 =====
3
 
3
 
4
 Term based tool to view **colored**, **incremental** diff in *git/svn/hg*
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
 .. image:: http://ymattw.github.com/cdiff/img/default.png
8
 .. image:: http://ymattw.github.com/cdiff/img/default.png
9
    :alt: default
9
    :alt: default
78
 .. code:: sh
78
 .. code:: sh
79
 
79
 
80
     git log -p -2 | cdiff -s
80
     git log -p -2 | cdiff -s
81
-    git show 15bfa56 | cdiff -s
81
+    git show 15bfa5 | cdiff -s
82
     svn diff -r PREV | cdiff -s
82
     svn diff -r PREV | cdiff -s
83
-    diff -u foo foo.new | cdiff -s
84
 
83
 
85
 View a diff (patch) file:
84
 View a diff (patch) file:
86
 
85
 
90
     cdiff foo.patch -s
89
     cdiff foo.patch -s
91
     cdiff foo.patch -s -w 90
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
 Redirect output to another patch file is safe:
97
 Redirect output to another patch file is safe:
94
 
98
 
95
 .. code:: sh
99
 .. code:: sh

+ 22
- 16
cdiff.py Näytä tiedosto

3
 
3
 
4
 """
4
 """
5
 Term based tool to view **colored**, **incremental** diff in *git/svn/hg*
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
 META_INFO = {
10
 META_INFO = {
11
-    'version'     : '0.2',
11
+    'version'     : '0.3',
12
     'license'     : 'BSD-3',
12
     'license'     : 'BSD-3',
13
     'author'      : 'Matthew Wang',
13
     'author'      : 'Matthew Wang',
14
     'email'       : 'mattwyl(@)gmail(.)com',
14
     'email'       : 'mattwyl(@)gmail(.)com',
15
     'url'         : 'https://github.com/ymattw/cdiff',
15
     'url'         : 'https://github.com/ymattw/cdiff',
16
     'keywords'    : 'colored incremental side-by-side diff',
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
 import sys
22
 import sys
32
 import difflib
33
 import difflib
33
 
34
 
34
 
35
 
35
-
36
 COLORS = {
36
 COLORS = {
37
     'reset'         : '\x1b[0m',
37
     'reset'         : '\x1b[0m',
38
     'underline'     : '\x1b[4m',
38
     'underline'     : '\x1b[4m',
564
 
564
 
565
     supported_vcs = [check[0] for check, _ in REVISION_CONTROL]
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
             version='%%prog %s' % META_INFO['version'])
573
             version='%%prog %s' % META_INFO['version'])
574
     parser.add_option('-s', '--side-by-side', action='store_true',
574
     parser.add_option('-s', '--side-by-side', action='store_true',
575
             help=('show in side-by-side mode'))
575
             help=('show in side-by-side mode'))
577
             help='set text width (side-by-side mode only), default is 80')
577
             help='set text width (side-by-side mode only), default is 80')
578
     opts, args = parser.parse_args()
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
         if IS_PY3:
587
         if IS_PY3:
582
             # Python3 needs the newline='' to keep '\r' (DOS format)
588
             # Python3 needs the newline='' to keep '\r' (DOS format)
583
             diff_hdl = open(args[0], mode='rt', newline='')
589
             diff_hdl = open(args[0], mode='rt', newline='')
596
     # FIXME: can't use generator for now due to current implementation in parser
602
     # FIXME: can't use generator for now due to current implementation in parser
597
     stream = [decode(line) for line in diff_hdl.readlines()]
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
     # Don't let empty diff pass thru
608
     # Don't let empty diff pass thru
600
     if not stream:
609
     if not stream:
601
         return 0
610
         return 0
602
 
611
 
603
-    if diff_hdl is not sys.stdin:
604
-        diff_hdl.close()
605
-
606
     if sys.stdout.isatty():
612
     if sys.stdout.isatty():
607
         try:
613
         try:
608
             markup_to_pager(stream, opts)
614
             markup_to_pager(stream, opts)