chiark / gitweb /
Convert "pop" to the lib infrastructure
[stgit] / stgit / commands / pop.py
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
19 from stgit.commands import common
20 from stgit.lib import transaction
21 from stgit import argparse
22 from stgit.argparse import opt
23
24 help = 'Pop one or more patches from the stack'
25 kind = 'stack'
26 usage = ['[options] [<patch1>] [<patch2>] [<patch3>..<patch4>]']
27 description = """
28 Pop the topmost patch or a range of patches from the stack. The
29 command fails if there are conflicts or local changes (and --keep was
30 not specified).
31
32 A series of pop and push operations are performed so that only the
33 patches passed on the command line are popped from the stack. Some of
34 the push operations may fail because of conflicts ("stg undo" would
35 revert the last push operation)."""
36
37 args = [argparse.patch_range(argparse.applied_patches)]
38 options = [
39     opt('-a', '--all', action = 'store_true',
40         short = 'Pop all the applied patches'),
41     opt('-n', '--number', type = 'int',
42         short = 'Pop the specified number of patches')
43     ] + argparse.keep_option()
44
45 directory = common.DirectoryHasRepositoryLib()
46
47 def func(parser, options, args):
48     """Pop the given patches or the topmost one from the stack."""
49     stack = directory.repository.current_stack
50     iw = stack.repository.default_iw
51     clean_iw = (not options.keep and iw) or None
52     trans = transaction.StackTransaction(stack, 'pop',
53                                          check_clean_iw = clean_iw)
54
55     if not trans.applied:
56         raise common.CmdException('No patches applied')
57
58     if options.all:
59         patches = trans.applied
60     elif options.number:
61         # reverse it twice to also work with negative or bigger than
62         # the length numbers
63         patches = trans.applied[::-1][:options.number][::-1]
64     elif not args:
65         patches = [trans.applied[-1]]
66     else:
67         patches = common.parse_patches(args, trans.applied, ordered = True)
68
69     if not patches:
70         raise common.CmdException('No patches to pop')
71
72     applied = [p for p in trans.applied if not p in set(patches)]
73     unapplied = patches + trans.unapplied
74     try:
75         trans.reorder_patches(applied, unapplied, iw = iw)
76     except transaction.TransactionException:
77         pass
78     return trans.run(iw)