Convert "pop" to the lib infrastructure
[stgit] / stgit / commands / pop.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
1056440b
CM
19from stgit.commands import common
20from stgit.lib import transaction
21from stgit import argparse
575bbdae 22from stgit.argparse import opt
fcee87cf 23
575bbdae 24help = 'Pop one or more patches from the stack'
33ff9cdd 25kind = 'stack'
575bbdae
KH
26usage = ['[options] [<patch1>] [<patch2>] [<patch3>..<patch4>]']
27description = """
d1368d81
CM
28Pop the topmost patch or a range of patches from the stack. The
29command fails if there are conflicts or local changes (and --keep was
30not specified).
31
32A series of pop and push operations are performed so that only the
33patches passed on the command line are popped from the stack. Some of
625e8de7 34the push operations may fail because of conflicts ("stg undo" would
d1368d81 35revert the last push operation)."""
fcee87cf 36
6c8a90e1 37args = [argparse.patch_range(argparse.applied_patches)]
575bbdae
KH
38options = [
39 opt('-a', '--all', action = 'store_true',
40 short = 'Pop all the applied patches'),
41 opt('-n', '--number', type = 'int',
1056440b
CM
42 short = 'Pop the specified number of patches')
43 ] + argparse.keep_option()
fcee87cf 44
1056440b 45directory = common.DirectoryHasRepositoryLib()
fcee87cf
CM
46
47def func(parser, options, args):
1056440b
CM
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)
e45af3d4 54
1056440b
CM
55 if not trans.applied:
56 raise common.CmdException('No patches applied')
fcee87cf 57
6b1e0111 58 if options.all:
1056440b 59 patches = trans.applied
fcee87cf 60 elif options.number:
d1368d81
CM
61 # reverse it twice to also work with negative or bigger than
62 # the length numbers
1056440b
CM
63 patches = trans.applied[::-1][:options.number][::-1]
64 elif not args:
65 patches = [trans.applied[-1]]
fcee87cf 66 else:
1056440b 67 patches = common.parse_patches(args, trans.applied, ordered = True)
fcee87cf 68
d1368d81 69 if not patches:
1056440b
CM
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)