stg import now extracts Message-ID header
[stgit.git] / stgit / commands / delete.py
blobd5371b21932bd4f97dac692a84feb5629a41f926
1 from stgit.argparse import opt, patch_range
2 from stgit.commands.common import (
3 CmdException,
4 DirectoryHasRepository,
5 delete_patches,
6 parse_patches,
9 __copyright__ = """
10 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License version 2 as
14 published by the Free Software Foundation.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, see http://www.gnu.org/licenses/.
23 """
25 help = 'Delete patches'
26 kind = 'patch'
27 usage = ['[options] [--] <patch1> [<patch2>] [<patch3>..<patch4>]']
28 description = """
29 Delete the patches passed as arguments."""
31 args = [patch_range('applied_patches', 'unapplied_patches')]
32 options = [
33 opt(
34 '--spill',
35 action='store_true',
36 short='Spill patch contents to worktree and index',
37 long="""
38 Delete the patches, but do not touch the index and worktree.
39 This only works with applied patches at the top of the stack.
40 The effect is to "spill" the patch contents into the index and
41 worktree. This can be useful e.g. if you want to split a patch
42 into several smaller pieces.""",
44 opt(
45 '-b',
46 '--branch',
47 args=['stg_branches'],
48 short='Use BRANCH instead of the default branch',
50 opt('-t', '--top', action='store_true', short='Delete top patch'),
53 directory = DirectoryHasRepository()
56 def func(parser, options, args):
57 """Delete one or more patches."""
58 stack = directory.repository.get_stack(options.branch)
59 if options.branch:
60 iw = None # can't use index/workdir to manipulate another branch
61 else:
62 iw = stack.repository.default_iw
63 if args and options.top:
64 parser.error('Either --top or patches must be specified')
65 elif args:
66 patches = set(
67 parse_patches(
68 args, list(stack.patchorder.all), len(stack.patchorder.applied)
71 elif options.top:
72 applied = stack.patchorder.applied
73 if applied:
74 patches = set([applied[-1]])
75 else:
76 raise CmdException('No patches applied')
77 else:
78 parser.error('No patches specified')
80 if options.spill:
81 if set(stack.patchorder.applied[-len(patches) :]) != patches:
82 parser.error('Can only spill topmost applied patches')
83 iw = None # don't touch index+worktree
85 return delete_patches(stack, iw, patches)