chiark / gitweb /
5334704d40758013341f6cc7e7e8fb3249b6b7b5
[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 stgit.argparse import opt
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 = ['']
25 description = """
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 options = [
31     opt('-a', '--applied', action = 'store_true',
32         short = 'Delete the empty applied patches'),
33     opt('-u', '--unapplied', action = 'store_true',
34         short = 'Delete the empty unapplied patches')]
35
36 directory = common.DirectoryHasRepositoryLib()
37
38 def _clean(stack, clean_applied, clean_unapplied):
39     trans = transaction.StackTransaction(stack, 'clean', allow_conflicts = True)
40     def del_patch(pn):
41         if pn in stack.patchorder.applied:
42             if pn == stack.patchorder.applied[-1]:
43                 # We're about to clean away the topmost patch. Don't
44                 # do that if we have conflicts, since that means the
45                 # patch is only empty because the conflicts have made
46                 # us dump its contents into the index and worktree.
47                 if stack.repository.default_index.conflicts():
48                     return False
49             return clean_applied and trans.patches[pn].data.is_nochange()
50         elif pn in stack.patchorder.unapplied:
51             return clean_unapplied and trans.patches[pn].data.is_nochange()
52     for pn in trans.delete_patches(del_patch):
53         trans.push_patch(pn)
54     trans.run()
55
56 def func(parser, options, args):
57     """Delete the empty patches in the series
58     """
59     if len(args) != 0:
60         parser.error('incorrect number of arguments')
61
62     if not (options.applied or options.unapplied):
63         options.applied = options.unapplied = True
64
65     _clean(directory.repository.current_stack,
66            options.applied, options.unapplied)