Discard exitcode of subprocess in a better way
[stgit] / stgit / commands / log.py
index 4d6b022..c5f71a2 100644 (file)
@@ -21,6 +21,7 @@ from pydoc import pager
 from stgit.commands.common import *
 from stgit import stack, git
 from stgit.out import *
+from stgit.run import Run
 
 help = 'display the patch changelog'
 usage = """%prog [options] [patch]
@@ -43,11 +44,14 @@ represent the changes to the entire base of the current
 patch. Conflicts reset the patch content and a subsequent 'refresh'
 will show the entire patch."""
 
+directory = DirectoryHasRepository()
 options = [make_option('-b', '--branch',
                        help = 'use BRANCH instead of the default one'),
            make_option('-p', '--patch',
                        help = 'show the refresh diffs',
                        action = 'store_true'),
+           make_option('-n', '--number', type = 'int',
+                       help = 'limit the output to NUMBER commits'),
            make_option('-f', '--full',
                        help = 'show the full commit ids',
                        action = 'store_true'),
@@ -59,8 +63,16 @@ def show_log(log, options):
     """List the patch changelog
     """
     commit = git.get_commit(log)
-    diff_str = ''
+    if options.number != None:
+        n = options.number
+    else:
+        n = -1
+    diff_list = []
     while commit:
+        if n == 0:
+            # limit the output
+            break
+
         log = commit.get_log().split('\n')
 
         cmd_rev = log[0].split()
@@ -74,9 +86,11 @@ def show_log(log, options):
             cmd = rev = ''
 
         if options.patch:
-            if cmd in ['refresh', 'undo', 'sync']:
-                diff_str = '%s%s\n' % (diff_str,
-                                       git.pretty_commit(commit.get_id_hash()))
+            if cmd in ['refresh', 'undo', 'sync', 'edit']:
+                diff_list.append(git.pretty_commit(commit.get_id_hash()))
+
+                # limiter decrement
+                n -= 1
         else:
             if len(log) >= 3:
                 notes = log[2]
@@ -93,14 +107,17 @@ def show_log(log, options):
                 out.stdout('%-8s [%-7s] %-28s  %s' % \
                            (rev[:8], cmd[:7], notes[:28], date))
 
+            # limiter decrement
+            n -= 1
+
         parent = commit.get_parent()
         if parent:
             commit = git.get_commit(parent)
         else:
             commit = None
 
-    if options.patch and diff_str:
-        pager(diff_str.rstrip())
+    if options.patch and diff_list:
+        pager('\n'.join(diff_list).rstrip())
 
 def func(parser, options, args):
     """Show the patch changelog
@@ -124,7 +141,6 @@ def func(parser, options, args):
         raise CmdException, 'No changelog for patch "%s"' % name
 
     if options.graphical:
-        if os.system('gitk %s' % log) != 0:
-            raise CmdException, 'gitk execution failed'
+        Run('gitk', log).discard_exitcode().run()
     else:
         show_log(log, options)