Add support for merge-friendly branches
[stgit] / stgit / commands / new.py
CommitLineData
fcee87cf
CM
1
2__copyright__ = """
3Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License version 2 as
7published by the Free Software Foundation.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17"""
18
20a52e06 19from stgit import argparse, utils
4ed9cbbc
KH
20from stgit.commands import common
21from stgit.lib import git as gitlib, transaction
199bfcd3 22from stgit.config import config
fcee87cf 23
575bbdae 24help = 'Create a new, empty patch'
33ff9cdd 25kind = 'patch'
575bbdae
KH
26usage = ['[options] [<name>]']
27description = """
28Create a new, empty patch on the current stack. The new patch is
29created on top of the currently applied patches, and is made the new
30top of the stack. Uncommitted changes in the work tree are not
760a7cfc 31included in the patch -- that is handled by linkstg:refresh[].
26aab5b0 32
575bbdae
KH
33The given name must be unique in the stack, and may only contain
34alphanumeric characters, dashes and underscores. If no name is given,
35one is generated from the first line of the patch's commit message.
9060d420 36
575bbdae
KH
37An editor will be launched to edit the commit message to be used for
38the patch, unless the '--message' flag already specified one. The
39'patchdescr.tmpl' template file (if available) is used to pre-fill the
40editor."""
fcee87cf 41
6c8a90e1 42args = []
53388a71 43options = (argparse.author_options()
f9d69fc4 44 + argparse.message_options(save_template = True)
575bbdae
KH
45 + argparse.sign_options())
46
47directory = common.DirectoryHasRepositoryLib()
fcee87cf
CM
48
49def func(parser, options, args):
4ed9cbbc
KH
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.
9060d420 58 if len(args) == 0:
4ed9cbbc 59 name = None
9060d420
KH
60 elif len(args) == 1:
61 name = args[0]
4ed9cbbc
KH
62 if stack.patches.exists(name):
63 raise common.CmdException('%s: patch already exists' % name)
9060d420 64 else:
fcee87cf
CM
65 parser.error('incorrect number of arguments')
66
f5f22afe 67 cd = gitlib.CommitData(
2b8d32ac 68 tree = stack.head.data.tree, parents = [stack.head], message = '',
bea18554 69 author = gitlib.Person.author(), committer = gitlib.Person.committer())
e58f264a 70 cd = common.update_commit_data(cd, options, allow_edit = True)
4ed9cbbc 71
b70ff167
KH
72 if options.save_template:
73 options.save_template(cd.message)
74 return utils.STGIT_SUCCESS
75
4ed9cbbc
KH
76 if name == None:
77 name = utils.make_patch_name(cd.message,
78 lambda name: stack.patches.exists(name))
79
80 # Write the new patch.
81 iw = stack.repository.default_iw
82 trans = transaction.StackTransaction(stack, 'new')
83 trans.patches[name] = stack.repository.commit(cd)
84 trans.applied.append(name)
85 return trans.run()