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