chiark / gitweb /
Refactor output handling to break circular dependency
[stgit] / stgit / commands / diff.py
index 6a730ee4f4041482969f1d046ee86fe63ac03d3e..aeca4ab72a48bf1765d7d33ed9e4f5f0470ad2e1 100644 (file)
@@ -18,9 +18,11 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 import sys, os
 from optparse import OptionParser, make_option
 
 import sys, os
 from optparse import OptionParser, make_option
+from pydoc import pager
 
 from stgit.commands.common import *
 from stgit.utils import *
 
 from stgit.commands.common import *
 from stgit.utils import *
+from stgit.out import *
 from stgit import stack, git
 
 
 from stgit import stack, git
 
 
@@ -32,16 +34,19 @@ or a tree-ish object and another tree-ish object. File names can also
 be given to restrict the diff output. The tree-ish object can be a
 standard git commit, tag or tree. In addition to these, the command
 also supports 'base', representing the bottom of the current stack,
 be given to restrict the diff output. The tree-ish object can be a
 standard git commit, tag or tree. In addition to these, the command
 also supports 'base', representing the bottom of the current stack,
-and '[patch]/[bottom | top]' for the patch boundaries (defaulting to
+and '[patch][//[bottom | top]]' for the patch boundaries (defaulting to
 the current one):
 
 the current one):
 
-rev = '([patch]/[bottom | top]) | <tree-ish> | base'
+rev = '([patch][//[bottom | top]]) | <tree-ish> | base'
 
 
-If neither bottom or top are given but a '/' is present, the command
+If neither bottom nor top are given but a '//' is present, the command
 shows the specified patch (defaulting to the current one)."""
 
 shows the specified patch (defaulting to the current one)."""
 
-options = [make_option('-r', metavar = 'rev1[:[rev2]]', dest = 'revs',
+options = [make_option('-r', '--range',
+                       metavar = 'rev1[..[rev2]]', dest = 'revs',
                        help = 'show the diff between revisions'),
                        help = 'show the diff between revisions'),
+           make_option('-O', '--diff-opts',
+                       help = 'options to pass to git-diff'),
            make_option('-s', '--stat',
                        help = 'show the stat instead of the diff',
                        action = 'store_true')]
            make_option('-s', '--stat',
                        help = 'show the stat instead of the diff',
                        action = 'store_true')]
@@ -51,28 +56,38 @@ def func(parser, options, args):
     """Show the tree diff
     """
     if options.revs:
     """Show the tree diff
     """
     if options.revs:
-        rev_list = options.revs.split(':')
+        rev_list = options.revs.split('..')
         rev_list_len = len(rev_list)
         if rev_list_len == 1:
         rev_list_len = len(rev_list)
         if rev_list_len == 1:
-            if rev_list[0][-1] == '/':
+            rev = rev_list[0]
+            if rev.endswith('/'):
                 # the whole patch
                 # the whole patch
-                rev1 = rev_list[0] + 'bottom'
-                rev2 = rev_list[0] + 'top'
+                rev = strip_suffix('/', rev)
+                if rev.endswith('/'):
+                    rev = strip_suffix('/', rev)
+                rev1 = rev + '//bottom'
+                rev2 = rev + '//top'
             else:
                 rev1 = rev_list[0]
                 rev2 = None
         elif rev_list_len == 2:
             rev1 = rev_list[0]
             rev2 = rev_list[1]
             else:
                 rev1 = rev_list[0]
                 rev2 = None
         elif rev_list_len == 2:
             rev1 = rev_list[0]
             rev2 = rev_list[1]
-            if rev2 == '':
-                rev2 = 'HEAD'
         else:
             parser.error('incorrect parameters to -r')
     else:
         rev1 = 'HEAD'
         rev2 = None
 
         else:
             parser.error('incorrect parameters to -r')
     else:
         rev1 = 'HEAD'
         rev2 = None
 
+    if options.diff_opts:
+        diff_flags = options.diff_opts.split()
+    else:
+        diff_flags = []
+
     if options.stat:
     if options.stat:
-        print git.diffstat(args, git_id(rev1), git_id(rev2))
+        out.stdout_raw(git.diffstat(args, git_id(rev1), git_id(rev2)) + '\n')
     else:
     else:
-        git.diff(args, git_id(rev1), git_id(rev2), sys.stdout)
+        diff_str = git.diff(args, git_id(rev1), git_id(rev2),
+                            diff_flags = diff_flags )
+        if diff_str:
+            pager(diff_str)