Run commit-msg hook on new, edit, refresh -e, squash
[stgit.git] / stgit / commands / new.py
blobf89cf45564fa345de96fbacf0906944df7b4434e
2 __copyright__ = """
3 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see http://www.gnu.org/licenses/.
16 """
18 from stgit import argparse, utils
19 from stgit.commands import common
20 from stgit.lib import git as gitlib, transaction
21 from stgit.config import config
23 help = 'Create a new, empty patch'
24 kind = 'patch'
25 usage = ['[options] [--] [<name>]']
26 description = """
27 Create a new, empty patch on the current stack. The new patch is
28 created on top of the currently applied patches, and is made the new
29 top of the stack. Uncommitted changes in the work tree are not
30 included in the patch -- that is handled by linkstg:refresh[].
32 The given name must be unique in the stack, and may only contain
33 alphanumeric characters, dashes and underscores. If no name is given,
34 one is generated from the first line of the patch's commit message.
36 An editor will be launched to edit the commit message to be used for
37 the patch, unless the '--message' flag already specified one. The
38 'patchdescr.tmpl' template file (if available) is used to pre-fill the
39 editor."""
41 args = []
42 options = (argparse.author_options()
43 + argparse.message_options(save_template = True)
44 + argparse.sign_options()
45 + argparse.hook_options())
47 directory = common.DirectoryHasRepositoryLib()
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')
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')
67 cd = gitlib.CommitData(
68 tree = stack.head.data.tree, parents = [stack.head], message = '',
69 author = gitlib.Person.author(), committer = gitlib.Person.committer())
70 cd = common.update_commit_data(cd, options)
72 if options.save_template:
73 options.save_template(cd.message)
74 return utils.STGIT_SUCCESS
76 if not options.no_verify:
77 cd = common.run_commit_msg_hook(stack.repository, cd)
79 if name == None:
80 name = utils.make_patch_name(cd.message,
81 lambda name: stack.patches.exists(name))
83 # Write the new patch.
84 iw = stack.repository.default_iw
85 trans = transaction.StackTransaction(stack, 'new')
86 trans.patches[name] = stack.repository.commit(cd)
87 trans.applied.append(name)
88 return trans.run()