Check bad head and clean iw before StackTransaction
[stgit.git] / stgit / commands / clean.py
blob6c2443a9f56ae9c76d847c91c269733078dbe14d
1 from stgit.argparse import opt
2 from stgit.commands.common import DirectoryHasRepository, check_head_top_equal
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 check_head_top_equal(stack)
50 trans = transaction.StackTransaction(stack, allow_conflicts=True)
52 def del_patch(pn):
53 if pn in stack.patchorder.applied:
54 if pn == stack.patchorder.applied[-1]:
55 # We're about to clean away the topmost patch. Don't
56 # do that if we have conflicts, since that means the
57 # patch is only empty because the conflicts have made
58 # us dump its contents into the index and worktree.
59 if stack.repository.default_index.conflicts():
60 return False
61 return clean_applied and trans.patches[pn].data.is_nochange()
62 elif pn in stack.patchorder.unapplied:
63 return clean_unapplied and trans.patches[pn].data.is_nochange()
65 for pn in trans.delete_patches(del_patch):
66 trans.push_patch(pn)
67 trans.run('clean')
70 def func(parser, options, args):
71 """Delete the empty patches in the series"""
72 if len(args) != 0:
73 parser.error('incorrect number of arguments')
75 if not (options.applied or options.unapplied):
76 options.applied = options.unapplied = True
78 _clean(directory.repository.current_stack, options.applied, options.unapplied)