ソースを参照

Enable use as a revision control diff

Steven Myint 12 年 前
コミット
ed0ce1cbf3
共有1 個のファイルを変更した20 個の追加4 個の削除を含む
  1. 20
    4
      src/cdiff.py

+ 20
- 4
src/cdiff.py ファイルの表示

17
 
17
 
18
 import os
18
 import os
19
 import re
19
 import re
20
+import subprocess
20
 import errno
21
 import errno
21
 import difflib
22
 import difflib
22
 
23
 
471
     pager.wait()
472
     pager.wait()
472
 
473
 
473
 
474
 
475
+def revision_control_diff(path):
476
+    """Return diff from revision control system."""
477
+    if subprocess.call(['git', 'rev-parse'], stderr=subprocess.PIPE) == 0:
478
+        return subprocess.Popen(['git', 'diff'], stdout=subprocess.PIPE).stdout
479
+
480
+
481
+def decode(line):
482
+    """Decode UTF-8 if necessary."""
483
+    try:
484
+        return line.decode('utf-8')
485
+    except AttributeError:
486
+        return line
487
+
488
+
474
 if __name__ == '__main__':
489
 if __name__ == '__main__':
475
     import optparse
490
     import optparse
476
-    import subprocess
477
 
491
 
478
     usage = '''%s [options] [diff]''' % os.path.basename(sys.argv[0])
492
     usage = '''%s [options] [diff]''' % os.path.basename(sys.argv[0])
479
     description= ('''View incremental, colored diff in unified format or '''
493
     description= ('''View incremental, colored diff in unified format or '''
494
         else:
508
         else:
495
             diff_hdl = open(args[0], mode='rt')
509
             diff_hdl = open(args[0], mode='rt')
496
     elif sys.stdin.isatty():
510
     elif sys.stdin.isatty():
497
-        parser.print_help()
498
-        sys.exit(1)
511
+        diff_hdl = revision_control_diff(os.getcwd())
512
+        if not diff_hdl:
513
+            parser.print_help()
514
+            sys.exit(1)
499
     else:
515
     else:
500
         diff_hdl = sys.stdin
516
         diff_hdl = sys.stdin
501
 
517
 
502
     # FIXME: can't use generator for now due to current implementation in parser
518
     # FIXME: can't use generator for now due to current implementation in parser
503
-    stream = diff_hdl.readlines()
519
+    stream = [decode(l) for l in diff_hdl.readlines()]
504
 
520
 
505
     # Don't let empty diff pass thru
521
     # Don't let empty diff pass thru
506
     if not stream:
522
     if not stream: