df7fa67857330658e61b9349520458775f9d3434
[stgit] / stgit / commands / commit.py
1 __copyright__ = """
2 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License version 2 as
6 published by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 """
17
18 from optparse import make_option
19 from stgit.commands import common
20 from stgit.lib import transaction
21 from stgit.out import *
22
23 help = 'permanently store the applied patches into stack base'
24 usage = """%prog [<patchnames>] | -n NUM | --all
25
26 Merge one or more patches into the base of the current stack and
27 remove them from the series while advancing the base. This is the
28 opposite of 'stg uncommit'. Use this command if you no longer want to
29 manage a patch with StGIT.
30
31 By default, the bottommost patch is committed. If patch names are
32 given, the stack is rearranged so that those patches are at the
33 bottom, and then they are committed.
34
35 The -n/--number option specifies the number of applied patches to
36 commit (counting from the bottom of the stack). If -a/--all is given,
37 all applied patches are committed."""
38
39 directory = common.DirectoryHasRepositoryLib()
40 options = [make_option('-n', '--number', type = 'int',
41 help = 'commit the specified number of patches'),
42 make_option('-a', '--all', action = 'store_true',
43 help = 'commit all applied patches')]
44
45 def func(parser, options, args):
46 """Commit a number of patches."""
47 stack = directory.repository.current_stack
48 args = common.parse_patches(args, list(stack.patchorder.all_visible))
49 if len([x for x in [args, options.number != None, options.all] if x]) > 1:
50 parser.error('too many options')
51 if args:
52 patches = [pn for pn in stack.patchorder.all_visible if pn in args]
53 bad = set(args) - set(patches)
54 if bad:
55 raise common.CmdException('Nonexistent or hidden patch names: %s'
56 % ', '.join(sorted(bad)))
57 elif options.number != None:
58 if options.number <= len(stack.patchorder.applied):
59 patches = stack.patchorder.applied[:options.number]
60 else:
61 raise common.CmdException('There are not that many applied patches')
62 elif options.all:
63 patches = stack.patchorder.applied
64 else:
65 patches = stack.patchorder.applied[:1]
66 if not patches:
67 raise common.CmdException('No patches to commit')
68
69 iw = stack.repository.default_iw
70 def allow_conflicts(trans):
71 # As long as the topmost patch stays where it is, it's OK to
72 # run "stg commit" with conflicts in the index.
73 return len(trans.applied) >= 1
74 trans = transaction.StackTransaction(stack, 'commit',
75 allow_conflicts = allow_conflicts)
76 try:
77 common_prefix = 0
78 for i in xrange(min(len(stack.patchorder.applied), len(patches))):
79 if stack.patchorder.applied[i] == patches[i]:
80 common_prefix += 1
81 if common_prefix < len(patches):
82 to_push = trans.pop_patches(
83 lambda pn: pn in stack.patchorder.applied[common_prefix:])
84 for pn in patches[common_prefix:]:
85 trans.push_patch(pn, iw)
86 else:
87 to_push = []
88 new_base = trans.patches[patches[-1]]
89 for pn in patches:
90 trans.patches[pn] = None
91 trans.applied = [pn for pn in trans.applied if pn not in patches]
92 trans.base = new_base
93 out.info('Committed %d patch%s' % (len(patches),
94 ['es', ''][len(patches) == 1]))
95 for pn in to_push:
96 trans.push_patch(pn, iw)
97 except transaction.TransactionHalted:
98 pass
99 return trans.run(iw)