chiark / gitweb /
bbea253bc29a2e1927b37ebcb22233f2572ac029
[stgit] / stgit / commands / clean.py
1 __copyright__ = """
2 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License version 2 as
6 published by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 """
17
18 from optparse import make_option
19 from stgit.out import *
20 from stgit.commands import common
21 from stgit.lib import transaction
22
23 help = 'delete the empty patches in the series'
24 usage = """%prog [options]
25
26 Delete the empty patches in the whole series or only those applied or
27 unapplied. A patch is considered empty if the two commit objects
28 representing its boundaries refer to the same tree object."""
29
30 directory = common.DirectoryHasRepositoryLib()
31 options = [make_option('-a', '--applied',
32                        help = 'delete the empty applied patches',
33                        action = 'store_true'),
34            make_option('-u', '--unapplied',
35                        help = 'delete the empty unapplied patches',
36                        action = 'store_true')]
37
38
39 def _clean(stack, clean_applied, clean_unapplied):
40     def deleting(pn):
41         out.info('Deleting empty patch %s' % pn)
42     trans = transaction.StackTransaction(stack, 'clean')
43     if clean_unapplied:
44         trans.unapplied = []
45         for pn in stack.patchorder.unapplied:
46             p = stack.patches.get(pn)
47             if p.is_empty():
48                 trans.patches[pn] = None
49                 deleting(pn)
50             else:
51                 trans.unapplied.append(pn)
52     if clean_applied:
53         trans.applied = []
54         parent = stack.base
55         for pn in stack.patchorder.applied:
56             p = stack.patches.get(pn)
57             if p.is_empty():
58                 trans.patches[pn] = None
59                 deleting(pn)
60             else:
61                 if parent != p.commit.data.parent:
62                     parent = trans.patches[pn] = stack.repository.commit(
63                         p.commit.data.set_parent(parent))
64                 else:
65                     parent = p.commit
66                 trans.applied.append(pn)
67     trans.run()
68
69 def func(parser, options, args):
70     """Delete the empty patches in the series
71     """
72     if len(args) != 0:
73         parser.error('incorrect number of arguments')
74
75     if not (options.applied or options.unapplied):
76         options.applied = options.unapplied = True
77
78     _clean(directory.repository.current_stack,
79            options.applied, options.unapplied)