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