stg import now extracts Message-ID header
[stgit.git] / stgit / commands / clean.py
blobf6521807fa4c3dbd69494c7193987416f8c87af1
1 from stgit.argparse import opt
2 from stgit.commands.common import DirectoryHasRepository
3 from stgit.lib import transaction
5 __copyright__ = """
6 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see http://www.gnu.org/licenses/.
19 """
21 help = 'Delete the empty patches in the series'
22 kind = 'stack'
23 usage = ['']
24 description = """
25 Delete the empty patches in the whole series or only those applied or
26 unapplied. A patch is considered empty if the two commit objects
27 representing its boundaries refer to the same tree object."""
29 args = []
30 options = [
31 opt(
32 '-a',
33 '--applied',
34 action='store_true',
35 short='Delete the empty applied patches',
37 opt(
38 '-u',
39 '--unapplied',
40 action='store_true',
41 short='Delete the empty unapplied patches',
45 directory = DirectoryHasRepository()
48 def _clean(stack, clean_applied, clean_unapplied):
49 trans = transaction.StackTransaction(stack, 'clean', allow_conflicts=True)
51 def del_patch(pn):
52 if pn in stack.patchorder.applied:
53 if pn == stack.patchorder.applied[-1]:
54 # We're about to clean away the topmost patch. Don't
55 # do that if we have conflicts, since that means the
56 # patch is only empty because the conflicts have made
57 # us dump its contents into the index and worktree.
58 if stack.repository.default_index.conflicts():
59 return False
60 return clean_applied and trans.patches[pn].data.is_nochange()
61 elif pn in stack.patchorder.unapplied:
62 return clean_unapplied and trans.patches[pn].data.is_nochange()
64 for pn in trans.delete_patches(del_patch):
65 trans.push_patch(pn)
66 trans.run()
69 def func(parser, options, args):
70 """Delete the empty patches in the series"""
71 if len(args) != 0:
72 parser.error('incorrect number of arguments')
74 if not (options.applied or options.unapplied):
75 options.applied = options.unapplied = True
77 _clean(directory.repository.current_stack, options.applied, options.unapplied)