2c984310ab73022e08212867b33a6260b444b06e
[stgit] / stgit / commands / new.py
1
2 __copyright__ = """
3 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 """
18
19 from stgit import argparse, utils
20 from stgit.commands import common
21 from stgit.lib import git as gitlib, transaction
22 from stgit.config import config
23
24 help = 'Create a new, empty patch'
25 kind = 'patch'
26 usage = ['[options] [<name>]']
27 description = """
28 Create a new, empty patch on the current stack. The new patch is
29 created on top of the currently applied patches, and is made the new
30 top of the stack. Uncommitted changes in the work tree are not
31 included in the patch -- that is handled by linkstg:refresh[].
32
33 The given name must be unique in the stack, and may only contain
34 alphanumeric characters, dashes and underscores. If no name is given,
35 one is generated from the first line of the patch's commit message.
36
37 An editor will be launched to edit the commit message to be used for
38 the patch, unless the '--message' flag already specified one. The
39 'patchdescr.tmpl' template file (if available) is used to pre-fill the
40 editor."""
41
42 args = []
43 options = (argparse.author_options()
44 + argparse.message_options(save_template = True)
45 + argparse.sign_options())
46
47 directory = common.DirectoryHasRepositoryLib()
48
49 def func(parser, options, args):
50 """Create a new patch."""
51 stack = directory.repository.current_stack
52 if stack.repository.default_index.conflicts():
53 raise common.CmdException(
54 'Cannot create a new patch -- resolve conflicts first')
55
56 # Choose a name for the new patch -- or None, which means make one
57 # up later when we've gotten hold of the commit message.
58 if len(args) == 0:
59 name = None
60 elif len(args) == 1:
61 name = args[0]
62 if stack.patches.exists(name):
63 raise common.CmdException('%s: patch already exists' % name)
64 else:
65 parser.error('incorrect number of arguments')
66
67 cd = gitlib.CommitData(
68 tree = stack.head.data.tree, parents = [stack.head], message = '',
69 author = gitlib.Person.author(), committer = gitlib.Person.committer())
70
71 # Set patch commit message from commandline.
72 if options.message != None:
73 cd = cd.set_message(options.message)
74
75 # Modify author data.
76 cd = cd.set_author(options.author(cd.author))
77
78 # Add Signed-off-by: or similar.
79 if options.sign_str != None:
80 sign_str = options.sign_str
81 else:
82 sign_str = config.get("stgit.autosign")
83
84 if sign_str != None:
85 cd = cd.set_message(
86 utils.add_sign_line(cd.message, sign_str,
87 cd.committer.name, cd.committer.email))
88
89 if options.save_template:
90 options.save_template(cd.message)
91 return utils.STGIT_SUCCESS
92
93 # Let user edit the commit message manually.
94 if not options.message:
95 cd = cd.set_message(utils.edit_string(cd.message, '.stgit-new.txt'))
96 if name == None:
97 name = utils.make_patch_name(cd.message,
98 lambda name: stack.patches.exists(name))
99
100 # Write the new patch.
101 iw = stack.repository.default_iw
102 trans = transaction.StackTransaction(stack, 'new')
103 trans.patches[name] = stack.repository.commit(cd)
104 trans.applied.append(name)
105 return trans.run()