Add "stg id" support for "{public}" ref
[stgit] / stgit / commands / diff.py
index 6036a18..568651c 100644 (file)
@@ -17,51 +17,62 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 """
 
 import sys, os
-from optparse import OptionParser, make_option
-
+from pydoc import pager
+from stgit.argparse import opt
 from stgit.commands.common import *
 from stgit.utils import *
-from stgit import stack, git
-
-
-help = 'show the tree diff'
-usage = """%prog [options] [<files...>]
+from stgit.out import *
+from stgit import argparse, stack, git
+from stgit.lib import git as gitlib
 
-The revision format is '([patch]/[bottom | top]) | <tree-ish>'"""
+help = 'Show the tree diff'
+kind = 'wc'
+usage = ['[options] [<files or dirs>]']
+description = """
+Show the diff (default) or diffstat between the current working copy
+or a tree-ish object and another tree-ish object (defaulting to HEAD).
+File names can also be given to restrict the diff output. The
+tree-ish object has the format accepted by the linkstg:id[] command."""
 
-options = [make_option('-r', metavar = 'rev1[:[rev2]]', dest = 'revs',
-                       help = 'show the diff between revisions'),
-           make_option('-s', '--stat',
-                       help = 'show the stat instead of the diff',
-                       action = 'store_true')]
+args = [argparse.known_files, argparse.dirty_files]
+options = [
+    opt('-r', '--range', metavar = 'rev1[..[rev2]]', dest = 'revs',
+        args = [argparse.patch_range(argparse.applied_patches,
+                                     argparse.unapplied_patches,
+                                     argparse.hidden_patches)],
+        short = 'Show the diff between revisions'),
+    opt('-s', '--stat', action = 'store_true',
+        short = 'Show the stat instead of the diff'),
+    ] + argparse.diff_opts_option()
 
+directory = DirectoryHasRepository(log = False)
 
 def func(parser, options, args):
     """Show the tree diff
     """
+    args = git.ls_files(args)
+    directory.cd_to_topdir()
+
     if options.revs:
-        rev_list = options.revs.split(':')
+        rev_list = options.revs.split('..')
         rev_list_len = len(rev_list)
         if rev_list_len == 1:
-            if rev_list[0][-1] == '/':
-                # the whole patch
-                rev1 = rev_list[0] + 'bottom'
-                rev2 = rev_list[0] + 'top'
-            else:
-                rev1 = rev_list[0]
-                rev2 = None
+            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
 
+    diff_str = git.diff(args, git_id(crt_series, rev1),
+                        rev2 and git_id(crt_series, rev2),
+                        diff_flags = options.diff_flags)
     if options.stat:
-        print git.diffstat(args, git_id(rev1), git_id(rev2))
+        out.stdout_raw(gitlib.diffstat(diff_str) + '\n')
     else:
-        git.diff(args, git_id(rev1), git_id(rev2))
+        if diff_str:
+            pager(diff_str)