Make the 'mail' command a bit safer
authorCatalin Marinas <catalin.marinas@gmail.com>
Wed, 20 Jul 2005 11:59:10 +0000 (12:59 +0100)
committerCatalin Marinas <catalin.marinas@gmail.com>
Wed, 20 Jul 2005 11:59:10 +0000 (12:59 +0100)
A simple 'mail' command automatically sends all the applied patches.
This patch changes so that the user needs to explicitely add the
'--all' or '--range' options.

Signed-off-by: Catalin Marinas <catalin.marinas@gmail.com>
stgit/commands/mail.py

index 8ecc8a5..8f514e6 100644 (file)
@@ -26,13 +26,16 @@ from stgit.config import config
 
 
 help = 'send a patch or series of patches by e-mail'
-usage = """%prog [options]"""
+usage = """%prog [options] [<patch>]"""
 
-options = [make_option('-t', '--template', metavar = 'FILE',
-                       help = 'use FILE as the message template'),
+options = [make_option('-a', '--all',
+                       help = 'e-mail all the applied patches',
+                       action = 'store_true'),
            make_option('-r', '--range',
                        metavar = '[PATCH1][:[PATCH2]]',
                        help = 'e-mail patches between PATCH1 and PATCH2'),
+           make_option('-t', '--template', metavar = 'FILE',
+                       help = 'use FILE as the message template'),
            make_option('-f', '--first', metavar = 'FILE',
                        help = 'send FILE as the first message'),
            make_option('-s', '--sleep', type = 'int', metavar = 'SECONDS',
@@ -158,7 +161,7 @@ def func(parser, options, args):
     """Send the patches by e-mail using the patchmail.tmpl file as
     a template
     """
-    if len(args) != 0:
+    if len(args) > 1:
         parser.error('incorrect number of arguments')
 
     if not config.has_option('stgit', 'smtpserver'):
@@ -167,7 +170,14 @@ def func(parser, options, args):
 
     applied = crt_series.get_applied()
 
-    if options.range:
+    if len(args) == 1:
+        if args[0] in applied:
+            patches = [args[0]]
+        else:
+            raise CmdException, 'Patch "%s" not applied' % args[0]
+    elif options.all:
+        patches = applied
+    elif options.range:
         boundaries = options.range.split(':')
         if len(boundaries) == 1:
             start = boundaries[0]
@@ -195,12 +205,14 @@ def func(parser, options, args):
 
         if start_idx >= stop_idx:
             raise CmdException, 'Incorrect patch range order'
+
+        patches = applied[start_idx:stop_idx]
     else:
-        start_idx = 0
-        stop_idx = len(applied)
+        raise CmdException, 'Incorrect options. Unknown patches to send'
 
-    patches = applied[start_idx:stop_idx]
     total_nr = len(patches)
+    if total_nr == 0:
+        raise CmdException, 'No patches to send'
 
     ref_id = options.refid