chiark / gitweb /
4a58d52ef1fdab23f4fe456d01e3313c56bb57c6
[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 optparse import make_option
20
21 from stgit import argparse, utils
22 from stgit.commands import common
23 from stgit.lib import git as gitlib, transaction
24 from stgit.config import config
25
26 help = 'create a new patch and make it the topmost one'
27 usage = """%prog [options] [name]
28
29 Create a new, empty patch and make it the topmost one. If the
30 '--message' option is not passed, an editor is invoked with the
31 .git/patchdescr.tmpl, ~/.stgit/templates/patchdescr.tmpl or
32 /usr/share/stgit/templates/patchdescr.tmpl file used a as template,
33 together with generated lines. The local changes in the working tree
34 are not included in the patch; an "stg refresh" command is needed for
35 this.
36
37 If no name is given for the new patch, one is generated from the first
38 line of the commit message."""
39
40 directory = common.DirectoryHasRepositoryLib()
41 options = (argparse.author_committer_options()
42            + argparse.message_options() + argparse.sign_options())
43
44 def func(parser, options, args):
45     """Create a new patch."""
46     stack = directory.repository.current_stack
47     if stack.repository.default_index.conflicts():
48         raise common.CmdException(
49             'Cannot create a new patch -- resolve conflicts first')
50
51     # Choose a name for the new patch -- or None, which means make one
52     # up later when we've gotten hold of the commit message.
53     if len(args) == 0:
54         name = None
55     elif len(args) == 1:
56         name = args[0]
57         if stack.patches.exists(name):
58             raise common.CmdException('%s: patch already exists' % name)
59     else:
60         parser.error('incorrect number of arguments')
61
62     cd = gitlib.CommitData(
63         tree = stack.head.data.tree, parents = [stack.head], message = '',
64         author = gitlib.Person.author(), committer = gitlib.Person.committer())
65
66     # Set patch commit message from commandline.
67     if options.message != None:
68         cd = cd.set_message(options.message)
69
70     # Modify author and committer data.
71     cd = (cd.set_author(options.author(cd.author))
72             .set_committer(options.committer(cd.committer)))
73
74     # Add Signed-off-by: or similar.
75     if options.sign_str != None:
76         sign_str = options.sign_str
77     else:
78         sign_str = config.get("stgit.autosign")
79
80     if sign_str != None:
81         cd = cd.set_message(
82             utils.add_sign_line(cd.message, sign_str,
83                                 cd.committer.name, cd.committer.email))
84
85     if options.save_template:
86         options.save_template(cd.message)
87         return utils.STGIT_SUCCESS
88
89     # Let user edit the commit message manually.
90     if not options.message:
91         cd = cd.set_message(utils.edit_string(cd.message, '.stgit-new.txt'))
92     if name == None:
93         name = utils.make_patch_name(cd.message,
94                                      lambda name: stack.patches.exists(name))
95
96     # Write the new patch.
97     iw = stack.repository.default_iw
98     trans = transaction.StackTransaction(stack, 'new')
99     trans.patches[name] = stack.repository.commit(cd)
100     trans.applied.append(name)
101     return trans.run()