f67cc0a7b047206610076cc694f6e9a0276ac823
[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', long = '''
43 Pop the specified number of patches.
44
45 With a negative number, pop all but that many patches.'''),
46 ] + argparse.keep_option()
47
48 directory = common.DirectoryHasRepositoryLib()
49
50 def func(parser, options, args):
51 """Pop the given patches or the topmost one from the stack."""
52 stack = directory.repository.current_stack
53 iw = stack.repository.default_iw
54 clean_iw = (not options.keep and iw) or None
55 trans = transaction.StackTransaction(stack, 'pop',
56 check_clean_iw = clean_iw)
57
58 if options.number == 0:
59 # explicitly allow this without any warning/error message
60 return
61
62 if not trans.applied:
63 raise common.CmdException('No patches applied')
64
65 if options.all:
66 patches = trans.applied
67 elif options.number is not None:
68 # reverse it twice to also work with negative or bigger than
69 # the length numbers
70 patches = trans.applied[::-1][:options.number][::-1]
71 elif not args:
72 patches = [trans.applied[-1]]
73 else:
74 patches = common.parse_patches(args, trans.applied, ordered = True)
75
76 if not patches:
77 raise common.CmdException('No patches to pop')
78
79 applied = [p for p in trans.applied if not p in set(patches)]
80 unapplied = patches + trans.unapplied
81 try:
82 trans.reorder_patches(applied, unapplied, iw = iw)
83 except transaction.TransactionException:
84 pass
85 return trans.run(iw)