Remove unneeded import
[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()
b70ff167
KH
40options = (utils.make_author_committer_options()
41 + utils.make_message_options() + utils.make_sign_options())
fcee87cf
CM
42
43def func(parser, options, args):
4ed9cbbc
KH
44 """Create a new patch."""
45 stack = directory.repository.current_stack
46 if stack.repository.default_index.conflicts():
47 raise common.CmdException(
48 'Cannot create a new patch -- resolve conflicts first')
49
50 # Choose a name for the new patch -- or None, which means make one
51 # up later when we've gotten hold of the commit message.
9060d420 52 if len(args) == 0:
4ed9cbbc 53 name = None
9060d420
KH
54 elif len(args) == 1:
55 name = args[0]
4ed9cbbc
KH
56 if stack.patches.exists(name):
57 raise common.CmdException('%s: patch already exists' % name)
9060d420 58 else:
fcee87cf
CM
59 parser.error('incorrect number of arguments')
60
f5f22afe 61 cd = gitlib.CommitData(
2b8d32ac 62 tree = stack.head.data.tree, parents = [stack.head], message = '',
bea18554 63 author = gitlib.Person.author(), committer = gitlib.Person.committer())
4ed9cbbc
KH
64
65 # Set patch commit message from commandline.
66 if options.message != None:
67 cd = cd.set_message(options.message)
68
bea18554
KH
69 # Modify author and committer data.
70 cd = (cd.set_author(options.author(cd.author))
71 .set_committer(options.committer(cd.committer)))
4ed9cbbc
KH
72
73 # Add Signed-off-by: or similar.
74 if options.sign_str != None:
bea18554
KH
75 cd = cd.set_message(
76 utils.add_sign_line(cd.message, options.sign_str,
77 cd.committer.name, cd.committer.email))
4ed9cbbc 78
b70ff167
KH
79 if options.save_template:
80 options.save_template(cd.message)
81 return utils.STGIT_SUCCESS
82
4ed9cbbc
KH
83 # Let user edit the commit message manually.
84 if not options.message:
85 cd = cd.set_message(utils.edit_string(cd.message, '.stgit-new.txt'))
86 if name == None:
87 name = utils.make_patch_name(cd.message,
88 lambda name: stack.patches.exists(name))
89
90 # Write the new patch.
91 iw = stack.repository.default_iw
92 trans = transaction.StackTransaction(stack, 'new')
93 trans.patches[name] = stack.repository.commit(cd)
94 trans.applied.append(name)
95 return trans.run()