|
@@ -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
|
|
583
|
589
|
diff_hdl = open(args[0], mode='rt', newline='')
|
|
@@ -596,13 +602,13 @@ def main():
|
596
|
602
|
|
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
|
|
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)
|