Add support for merge-friendly branches
[stgit] / stgit / commands / new.py
index 9bcab03..9fd51c3 100644 (file)
@@ -16,42 +16,35 @@ along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 """
 
-from optparse import make_option
-
-from stgit import utils
+from stgit import argparse, utils
 from stgit.commands import common
 from stgit.lib import git as gitlib, transaction
-
-help = 'create a new patch and make it the topmost one'
-usage = """%prog [options] [name]
-
-Create a new, empty patch and make it the topmost one. If the
-'--message' option is not passed, an editor is invoked with the
-.git/patchdescr.tmpl, ~/.stgit/templates/patchdescr.tmpl or
-/usr/share/stgit/templates/patchdescr.tmpl file used a as template,
-together with generated lines. The local changes in the working tree
-are not included in the patch; an "stg refresh" command is needed for
-this.
-
-If no name is given for the new patch, one is generated from the first
-line of the commit message."""
+from stgit.config import config
+
+help = 'Create a new, empty patch'
+kind = 'patch'
+usage = ['[options] [<name>]']
+description = """
+Create a new, empty patch on the current stack. The new patch is
+created on top of the currently applied patches, and is made the new
+top of the stack. Uncommitted changes in the work tree are not
+included in the patch -- that is handled by linkstg:refresh[].
+
+The given name must be unique in the stack, and may only contain
+alphanumeric characters, dashes and underscores. If no name is given,
+one is generated from the first line of the patch's commit message.
+
+An editor will be launched to edit the commit message to be used for
+the patch, unless the '--message' flag already specified one. The
+'patchdescr.tmpl' template file (if available) is used to pre-fill the
+editor."""
+
+args = []
+options = (argparse.author_options()
+           + argparse.message_options(save_template = True)
+           + argparse.sign_options())
 
 directory = common.DirectoryHasRepositoryLib()
-options = [make_option('-m', '--message',
-                       help = 'use MESSAGE as the patch description'),
-           make_option('-a', '--author', metavar = '"NAME <EMAIL>"',
-                       help = 'use "NAME <EMAIL>" as the author details'),
-           make_option('--authname',
-                       help = 'use AUTHNAME as the author name'),
-           make_option('--authemail',
-                       help = 'use AUTHEMAIL as the author e-mail'),
-           make_option('--authdate',
-                       help = 'use AUTHDATE as the author date'),
-           make_option('--commname',
-                       help = 'use COMMNAME as the committer name'),
-           make_option('--commemail',
-                       help = 'use COMMEMAIL as the committer e-mail')
-           ] + utils.make_sign_options()
 
 def func(parser, options, args):
     """Create a new patch."""
@@ -71,35 +64,15 @@ def func(parser, options, args):
     else:
         parser.error('incorrect number of arguments')
 
-    head = directory.repository.refs.get(directory.repository.head)
-    cd = gitlib.Commitdata(tree = head.data.tree, parents = [head],
-                           message = '')
-
-    # Set patch commit message from commandline.
-    if options.message != None:
-        cd = cd.set_message(options.message)
-
-    # Specify author and committer data.
-    if options.author != None:
-        options.authname, options.authemail = common.name_email(options.author)
-    for p, f, val in [('author', 'name', options.authname),
-                      ('author', 'email', options.authemail),
-                      ('author', 'date', gitlib.Date.maybe(options.authdate)),
-                      ('committer', 'name', options.commname),
-                      ('committer', 'email', options.commemail)]:
-        if val != None:
-            cd = getattr(cd, 'set_' + p)(
-                getattr(getattr(cd, p), 'set_' + f)(val))
-
-    # Add Signed-off-by: or similar.
-    if options.sign_str != None:
-        cd = cd.set_message(utils.add_sign_line(
-                cd.message, options.sign_str, gitlib.Person.committer().name,
-                gitlib.Person.committer().email))
-
-    # Let user edit the commit message manually.
-    if not options.message:
-        cd = cd.set_message(utils.edit_string(cd.message, '.stgit-new.txt'))
+    cd = gitlib.CommitData(
+        tree = stack.head.data.tree, parents = [stack.head], message = '',
+        author = gitlib.Person.author(), committer = gitlib.Person.committer())
+    cd = common.update_commit_data(cd, options, allow_edit = True)
+
+    if options.save_template:
+        options.save_template(cd.message)
+        return utils.STGIT_SUCCESS
+
     if name == None:
         name = utils.make_patch_name(cd.message,
                                      lambda name: stack.patches.exists(name))