stg import now extracts Message-ID header
[stgit.git] / stgit / commands / new.py
blobf56df3d89960d8e5974758fd817b2f861ac4d8a6
1 from stgit import argparse, utils
2 from stgit.argparse import opt
3 from stgit.commands.common import (
4 CmdException,
5 DirectoryHasRepository,
6 run_commit_msg_hook,
7 update_commit_data,
9 from stgit.config import config
10 from stgit.lib.git import CommitData, Person
11 from stgit.lib.transaction import StackTransaction
13 __copyright__ = """
14 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License version 2 as
18 published by the Free Software Foundation.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, see http://www.gnu.org/licenses/.
27 """
29 help = 'Create a new, empty patch'
30 kind = 'patch'
31 usage = ['[options] [--] [<name>]']
32 description = """
33 Create a new, empty patch on the current stack. The new patch is
34 created on top of the currently applied patches, and is made the new
35 top of the stack. Uncommitted changes in the work tree are not
36 included in the patch -- that is handled by linkstg:refresh[].
38 The given name must be unique in the stack, and may only contain
39 alphanumeric characters, dashes and underscores. If no name is given,
40 one is generated from the first line of the patch's commit message.
42 An editor will be launched to edit the commit message to be used for
43 the patch, unless the '--message' flag already specified one. The
44 'patchdescr.tmpl' template file (if available) is used to pre-fill the
45 editor."""
47 args = []
48 options = [
49 opt(
50 '-v',
51 '--verbose',
52 action='store_true',
53 short='show diff in patch commit message template',
54 long='''
55 In addition to the names of files that have been changed, also show a
56 diff of staged and unstaged changes.''',
59 options.extend(argparse.author_options())
60 options.extend(argparse.message_options(save_template=True))
61 options.extend(argparse.trailer_options())
62 options.extend(argparse.hook_options())
65 directory = DirectoryHasRepository()
68 def func(parser, options, args):
69 """Create a new patch."""
70 stack = directory.repository.current_stack
71 if stack.repository.default_index.conflicts():
72 raise CmdException('Cannot create a new patch -- resolve conflicts first')
74 # Choose a name for the new patch -- or None, which means make one
75 # up later when we've gotten hold of the commit message.
76 if len(args) == 0:
77 name = None
78 elif len(args) == 1:
79 name = args[0]
80 if not stack.patches.is_name_valid(name):
81 raise CmdException('Invalid patch name: "%s"' % name)
82 elif name in stack.patches:
83 raise CmdException('%s: patch already exists' % name)
84 else:
85 parser.error('incorrect number of arguments')
87 if options.verbose:
88 verbose = options.verbose
89 else:
90 verbose = config.getbool('commit.verbose') or False
92 cd = CommitData(
93 tree=stack.head.data.tree,
94 parents=[stack.head],
95 message='',
96 author=Person.author(),
97 committer=Person.committer(),
99 cd = update_commit_data(
100 stack.repository,
102 message=options.message,
103 author=options.author(cd.author),
104 trailers=options.trailers,
105 edit=(not options.save_template and options.message is None),
106 verbose=verbose,
109 if options.save_template:
110 options.save_template(cd.message)
111 return utils.STGIT_SUCCESS
113 if not options.no_verify:
114 cd = run_commit_msg_hook(stack.repository, cd)
116 if name is None:
117 name = stack.patches.make_name(cd.message_str)
119 # Write the new patch.
120 stack.repository.default_iw
121 trans = StackTransaction(stack, 'new: %s' % name)
122 trans.patches[name] = stack.repository.commit(cd)
123 trans.applied.append(name)
124 return trans.run()