Browse Source

Support reading diff or log for given files/dirs in workspace (usage changed on

Matthew Wang 11 years ago
parent
commit
75b79f0935
5 changed files with 26 additions and 37 deletions
  1. 5
    0
      CHANGES
  2. 9
    11
      README.rst
  3. 10
    20
      cdiff.py
  4. 2
    2
      tests/regression.sh
  5. 0
    4
      tests/test_cdiff.py

+ 5
- 0
CHANGES View File

@@ -2,6 +2,11 @@
2 2
 Change log
3 3
 ==========
4 4
 
5
+Version 0.7 (2013-02-22)
6
+
7
+  - Support reading diff or log for given files/dirs in workspace (usage changed
8
+    on reading a patch file or two given files!)
9
+
5 10
 Version 0.6 (2013-02-20)
6 11
 
7 12
   - A few performance tuning and code clean up

+ 9
- 11
README.rst View File

@@ -71,8 +71,10 @@ Read diff from local modification in a *Git/Mercurial/Svn* workspace:
71 71
     cdiff                       # view colored incremental udiff
72 72
     cdiff -s                    # view side by side
73 73
     cdiff -s -w 90              # use text width 90 other than default 80
74
+    cdiff -s file1 dir2         # view modification of given files/dirs only
74 75
 
75
-Read the log (e.g. ``git log -p``) in a *Git/Mercurial/Svn* workspace:
76
+Read the log with diff (e.g. ``git log -p``, ``svn log --diff``) in a
77
+*Git/Mercurial/Svn* workspace (note *--diff* option is new in svn 1.7.0):
76 78
 
77 79
 .. code:: sh
78 80
 
@@ -80,6 +82,7 @@ Read the log (e.g. ``git log -p``) in a *Git/Mercurial/Svn* workspace:
80 82
     cdiff -l
81 83
     cdiff -ls                   # equivalent to cdiff -l -s
82 84
     cdiff -ls -w90
85
+    cdiff -ls file1 dir2        # view log with diff of given files/dirs only
83 86
 
84 87
 Pipe in a diff:
85 88
 
@@ -88,21 +91,16 @@ Pipe in a diff:
88 91
     git log -p -2 | cdiff -s
89 92
     git show 15bfa5 | cdiff -s
90 93
     svn diff -r PREV | cdiff -s
94
+    diff -u foo bar | cdiff     # note cdiff only takes unified diff
95
+    diff -ur dir1 dir2 | cdiff  # read diff of two dirs
91 96
 
92 97
 View a diff (patch) file:
93 98
 
94 99
 .. code:: sh
95 100
 
96
-    cdiff foo.patch
97
-    cdiff foo.patch -s
98
-    cdiff foo.patch -s -w 90
99
-
100
-View diff between two files (wrapper of ``diff``):
101
-
102
-.. code:: sh
103
-
104
-    cdiff foo foo.new           # equivalent to diff -u foo foo.new | cdiff
105
-    cdiff foo foo.new -s
101
+    cdiff < foo.patch           # or cat foo.patch | cdiff
102
+    cdiff -s < foo.patch
103
+    cdiff -s -w 90 < foo.patch
106 104
 
107 105
 Redirect output to another patch file is safe:
108 106
 

+ 10
- 20
cdiff.py View File

@@ -8,7 +8,7 @@ workspace, given patch or two files, or from stdin, with **side by side** and
8 8
 """
9 9
 
10 10
 META_INFO = {
11
-    'version'     : '0.6',
11
+    'version'     : '0.7',
12 12
     'license'     : 'BSD-3',
13 13
     'author'      : 'Matthew Wang',
14 14
     'email'       : 'mattwyl(@)gmail(.)com',
@@ -588,7 +588,7 @@ def markup_to_pager(stream, opts):
588 588
     color_diff = markup.markup(side_by_side=opts.side_by_side,
589 589
             width=opts.width)
590 590
 
591
-    # args stolen fron git source: github.com/git/git/blob/master/pager.c
591
+    # Args stolen from git source: github.com/git/git/blob/master/pager.c
592 592
     pager = subprocess.Popen(['less', '-FRSX'],
593 593
             stdin=subprocess.PIPE, stdout=sys.stdout)
594 594
     try:
@@ -610,18 +610,20 @@ def check_command_status(arguments):
610 610
         return False
611 611
 
612 612
 
613
-def revision_control_diff():
613
+def revision_control_diff(args):
614 614
     """Return diff from revision control system."""
615 615
     for _, ops in VCS_INFO.items():
616 616
         if check_command_status(ops['probe']):
617
-            return subprocess.Popen(ops['diff'], stdout=subprocess.PIPE).stdout
617
+            return subprocess.Popen(
618
+                    ops['diff'] + args, stdout=subprocess.PIPE).stdout
618 619
 
619 620
 
620
-def revision_control_log():
621
+def revision_control_log(args):
621 622
     """Return log from revision control system."""
622 623
     for _, ops in VCS_INFO.items():
623 624
         if check_command_status(ops['probe']):
624
-            return subprocess.Popen(ops['log'], stdout=subprocess.PIPE).stdout
625
+            return subprocess.Popen(
626
+                    ops['log'] + args, stdout=subprocess.PIPE).stdout
625 627
 
626 628
 
627 629
 def decode(line):
@@ -655,25 +657,13 @@ def main():
655 657
     opts, args = parser.parse_args()
656 658
 
657 659
     if opts.log:
658
-        diff_hdl = revision_control_log()
660
+        diff_hdl = revision_control_log(args)
659 661
         if not diff_hdl:
660 662
             sys.stderr.write(('*** Not in a supported workspace, supported '
661 663
                               'are: %s\n') % ', '.join(supported_vcs))
662 664
             return 1
663
-    elif len(args) > 2:
664
-        parser.print_help()
665
-        return 1
666
-    elif len(args) == 2:
667
-        diff_hdl = subprocess.Popen(['diff', '-u', args[0], args[1]],
668
-                stdout=subprocess.PIPE).stdout
669
-    elif len(args) == 1:
670
-        if IS_PY3:
671
-            # Python3 needs the newline='' to keep '\r' (DOS format)
672
-            diff_hdl = open(args[0], mode='rt', newline='')
673
-        else:
674
-            diff_hdl = open(args[0], mode='rt')
675 665
     elif sys.stdin.isatty():
676
-        diff_hdl = revision_control_diff()
666
+        diff_hdl = revision_control_diff(args)
677 667
         if not diff_hdl:
678 668
             sys.stderr.write(('*** Not in a supported workspace, supported '
679 669
                               'are: %s\n\n') % ', '.join(supported_vcs))

+ 2
- 2
tests/regression.sh View File

@@ -33,9 +33,9 @@ function cmp_output()
33 33
     local cdiff_opt=${3:-""}
34 34
     local cmd
35 35
 
36
-    cmd=$(printf "%-8s $CDIFF %-25s %-20s " $PYTHON "$input" "$cdiff_opt")
36
+    cmd=$(printf "%-8s $CDIFF %-18s < %-25s " $PYTHON "$cdiff_opt" "$input")
37 37
     printf "$cmd"
38
-    if $cmd 2>/dev/null | diff -ubq $expected_out - >& /dev/null; then
38
+    if eval $cmd 2>/dev/null | diff -ubq $expected_out - >& /dev/null; then
39 39
         pass
40 40
         return 0
41 41
     else

+ 0
- 4
tests/test_cdiff.py View File

@@ -447,10 +447,6 @@ class TestMain(unittest.TestCase):
447 447
                'cd %s; git add foo; git commit foo -m update' % self._ws]
448 448
         subprocess.call(cmd, stdout=subprocess.PIPE)
449 449
 
450
-    def test_too_many_args(self):
451
-        sys.argv = [sys.argv[0], 'a', 'b', 'c']
452
-        self.assertNotEqual(cdiff.main(), 0)
453
-
454 450
     def test_read_diff(self):
455 451
         sys.argv = sys.argv[:1]
456 452
         self._change_file('read_diff')