Add support for initializing a branch for stgit from Emacs.
[stgit] / stgit / commands / clean.py
CommitLineData
de4c9d27
CM
1__copyright__ = """
2Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
3
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License version 2 as
6published by the Free Software Foundation.
7
8This program is distributed in the hope that it will be useful,
9but WITHOUT ANY WARRANTY; without even the implied warranty of
10MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11GNU General Public License for more details.
12
13You should have received a copy of the GNU General Public License
14along with this program; if not, write to the Free Software
15Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16"""
17
fe1cee2e 18from optparse import make_option
5e888f30 19from stgit.out import *
fe1cee2e
KH
20from stgit.commands import common
21from stgit.lib import transaction
de4c9d27
CM
22
23help = 'delete the empty patches in the series'
26aab5b0
CM
24usage = """%prog [options]
25
26Delete the empty patches in the whole series or only those applied or
27unapplied. A patch is considered empty if the two commit objects
28representing its boundaries refer to the same tree object."""
de4c9d27 29
fe1cee2e 30directory = common.DirectoryHasRepositoryLib()
de4c9d27
CM
31options = [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
fe1cee2e 39def _clean(stack, clean_applied, clean_unapplied):
781e549a 40 trans = transaction.StackTransaction(stack, 'clean', allow_conflicts = True)
b75457e8
KH
41 def del_patch(pn):
42 if pn in stack.patchorder.applied:
a639e7bb
KH
43 if pn == stack.patchorder.applied[-1]:
44 # We're about to clean away the topmost patch. Don't
45 # do that if we have conflicts, since that means the
46 # patch is only empty because the conflicts have made
47 # us dump its contents into the index and worktree.
48 if stack.repository.default_index.conflicts():
49 return False
b75457e8
KH
50 return clean_applied and trans.patches[pn].data.is_nochange()
51 elif pn in stack.patchorder.unapplied:
52 return clean_unapplied and trans.patches[pn].data.is_nochange()
53 for pn in trans.delete_patches(del_patch):
54 trans.push_patch(pn)
fe1cee2e 55 trans.run()
de4c9d27
CM
56
57def func(parser, options, args):
58 """Delete the empty patches in the series
59 """
60 if len(args) != 0:
61 parser.error('incorrect number of arguments')
62
de4c9d27
CM
63 if not (options.applied or options.unapplied):
64 options.applied = options.unapplied = True
65
fe1cee2e
KH
66 _clean(directory.repository.current_stack,
67 options.applied, options.unapplied)