Refactor --author/--committer options
[stgit] / stgit / commands / new.py
CommitLineData
fcee87cf
CM
1
2__copyright__ = """
3Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License version 2 as
7published by the Free Software Foundation.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17"""
18
4ed9cbbc 19from optparse import make_option
fcee87cf 20
4ed9cbbc
KH
21from stgit import utils
22from stgit.commands import common
23from stgit.lib import git as gitlib, transaction
fcee87cf
CM
24
25help = 'create a new patch and make it the topmost one'
9060d420 26usage = """%prog [options] [name]
26aab5b0
CM
27
28Create a new, empty patch and make it the topmost one. If the
29'--message' option is not passed, an editor is invoked with the
1f3bb017
CM
30.git/patchdescr.tmpl, ~/.stgit/templates/patchdescr.tmpl or
31/usr/share/stgit/templates/patchdescr.tmpl file used a as template,
ed20728d
KH
32together with generated lines. The local changes in the working tree
33are not included in the patch; an "stg refresh" command is needed for
34this.
9060d420
KH
35
36If no name is given for the new patch, one is generated from the first
37line of the commit message."""
fcee87cf 38
4ed9cbbc 39directory = common.DirectoryHasRepositoryLib()
fcee87cf
CM
40options = [make_option('-m', '--message',
41 help = 'use MESSAGE as the patch description'),
bea18554
KH
42 ] + (utils.make_author_committer_options()
43 + utils.make_sign_options())
fcee87cf
CM
44
45def func(parser, options, args):
4ed9cbbc
KH
46 """Create a new patch."""
47 stack = directory.repository.current_stack
48 if stack.repository.default_index.conflicts():
49 raise common.CmdException(
50 'Cannot create a new patch -- resolve conflicts first')
51
52 # Choose a name for the new patch -- or None, which means make one
53 # up later when we've gotten hold of the commit message.
9060d420 54 if len(args) == 0:
4ed9cbbc 55 name = None
9060d420
KH
56 elif len(args) == 1:
57 name = args[0]
4ed9cbbc
KH
58 if stack.patches.exists(name):
59 raise common.CmdException('%s: patch already exists' % name)
9060d420 60 else:
fcee87cf
CM
61 parser.error('incorrect number of arguments')
62
4ed9cbbc 63 head = directory.repository.refs.get(directory.repository.head)
bea18554
KH
64 cd = gitlib.Commitdata(
65 tree = head.data.tree, parents = [head], message = '',
66 author = gitlib.Person.author(), committer = gitlib.Person.committer())
4ed9cbbc
KH
67
68 # Set patch commit message from commandline.
69 if options.message != None:
70 cd = cd.set_message(options.message)
71
bea18554
KH
72 # Modify author and committer data.
73 cd = (cd.set_author(options.author(cd.author))
74 .set_committer(options.committer(cd.committer)))
4ed9cbbc
KH
75
76 # Add Signed-off-by: or similar.
77 if options.sign_str != None:
bea18554
KH
78 cd = cd.set_message(
79 utils.add_sign_line(cd.message, options.sign_str,
80 cd.committer.name, cd.committer.email))
4ed9cbbc
KH
81
82 # Let user edit the commit message manually.
83 if not options.message:
84 cd = cd.set_message(utils.edit_string(cd.message, '.stgit-new.txt'))
85 if name == None:
86 name = utils.make_patch_name(cd.message,
87 lambda name: stack.patches.exists(name))
88
89 # Write the new patch.
90 iw = stack.repository.default_iw
91 trans = transaction.StackTransaction(stack, 'new')
92 trans.patches[name] = stack.repository.commit(cd)
93 trans.applied.append(name)
94 return trans.run()