Allow patch ranges for the 'pop' command
[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 import sys, os
20 from optparse import OptionParser, make_option
21
22 from stgit.commands.common import *
23 from stgit.utils import *
24 from stgit import stack, git
25
26
27 help = 'pop one or more patches from the stack'
28 usage = """%prog [options] [<patch1>] [<patch2>] [<patch3>..<patch4>]
29
30 Pop the topmost patch or a range of patches from the stack. The
31 command fails if there are conflicts or local changes (and --keep was
32 not specified).
33
34 A series of pop and push operations are performed so that only the
35 patches passed on the command line are popped from the stack. Some of
36 the push operations may fail because of conflicts (push --undo would
37 revert the last push operation)."""
38
39 options = [make_option('-a', '--all',
40 help = 'pop all the applied patches',
41 action = 'store_true'),
42 make_option('-n', '--number', type = 'int',
43 help = 'pop the specified number of patches'),
44 make_option('-k', '--keep',
45 help = 'keep the local changes',
46 action = 'store_true')]
47
48
49 def func(parser, options, args):
50 """Pop the topmost patch from the stack
51 """
52 check_conflicts()
53 check_head_top_equal()
54
55 if not options.keep:
56 check_local_changes()
57
58 applied = crt_series.get_applied()
59 if not applied:
60 raise CmdException, 'No patches applied'
61
62 if options.all:
63 patches = applied
64 elif options.number:
65 # reverse it twice to also work with negative or bigger than
66 # the length numbers
67 patches = applied[::-1][:options.number][::-1]
68 elif len(args) == 0:
69 patches = [applied[-1]]
70 else:
71 patches = parse_patches(args, applied, ordered = True)
72
73 if not patches:
74 raise CmdException, 'No patches to pop'
75
76 # pop to the most distant popped patch
77 topop = applied[applied.index(patches[0]):]
78 # push those not in the popped range
79 topush = [p for p in topop if p not in patches]
80
81 if options.keep and topush:
82 raise CmdException, 'Cannot pop arbitrary patches with --keep'
83
84 topop.reverse()
85 pop_patches(topop, options.keep)
86 if topush:
87 push_patches(topush)
88
89 print_crt_patch()