Infrastructure for current directory handling
[stgit] / stgit / commands / patches.py
index c0f2ba8..0b618fe 100644 (file)
@@ -17,34 +17,54 @@ 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.commands.common import *
 from stgit.utils import *
+from stgit.out import *
 from stgit import stack, git
 
 
-help = 'show the patches modifying a file'
-usage = """%prog [options] <file> [<file>...]
+help = 'show the applied patches modifying a file'
+usage = """%prog [options] [<files...>]
 
-Show the applied patches modifying the given files. The '--diff'
-option also lists the patch log and the diff for the given files."""
+Show the applied patches modifying the given files. Without arguments,
+it shows the patches affected by the local tree modifications. The
+'--diff' option also lists the patch log and the diff for the given
+files."""
 
+directory = DirectoryHasRepository()
 options = [make_option('-d', '--diff',
                        help = 'show the diff for the given files',
-                       action = 'store_true')]
+                       action = 'store_true'),
+           make_option('-b', '--branch',
+                       help = 'use BRANCH instead of the default one')]
 
+diff_tmpl = \
+          '-------------------------------------------------------------------------------\n' \
+          '%s\n' \
+          '-------------------------------------------------------------------------------\n' \
+          '%s' \
+          '---\n\n' \
+          '%s'
 
 def func(parser, options, args):
     """Show the patches modifying a file
     """
-    if len(args) < 1:
-        parser.error('incorrect number of arguments')
+    if not args:
+        files = [path for (stat,path) in git.tree_status(verbose = True)]
+    else:
+        files = args
+
+    if not files:
+        raise CmdException, 'No files specified or no local changes'
 
     applied = crt_series.get_applied()
     if not applied:
         raise CmdException, 'No patches applied'
 
-    revs = git.modifying_revs(args, git_id('base'))
+    revs = git.modifying_revs(files, crt_series.get_base(),
+                              crt_series.get_head())
     revs.reverse()
 
     # build the patch/revision mapping
@@ -54,16 +74,17 @@ def func(parser, options, args):
         rev_patch[patch.get_top()] = patch
 
     # print the patch names
+    diff_output = ''
     for rev in revs:
         if rev in rev_patch:
             patch = rev_patch[rev]
             if options.diff:
-                print '-------------------------------------------------------------------------------'
-                print patch.get_name()
-                print '-------------------------------------------------------------------------------'
-                print patch.get_description(),
-                print '---'
-                print
-                print git.diff(args, patch.get_bottom(), patch.get_top())
+                diff_output += diff_tmpl \
+                               % (patch.get_name(), patch.get_description(),
+                                  git.diff(files, patch.get_bottom(),
+                                           patch.get_top()))
             else:
-                print patch.get_name()
+                out.stdout(patch.get_name())
+
+    if options.diff:
+        pager(diff_output)