Convert 'hide' to the lib infrastructure
[stgit] / stgit / commands / sink.py
CommitLineData
de7a79c4
YD
1
2__copyright__ = """
3Copyright (C) 2007, Yann Dirson <ydirson@altern.org>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License version 2 as
7published by the Free Software Foundation.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17"""
18
575bbdae 19from stgit.argparse import opt
54ab5bde
CM
20from stgit.commands import common
21from stgit.lib import transaction
22from stgit import argparse
de7a79c4 23
575bbdae 24help = 'Send patches deeper down the stack'
33ff9cdd 25kind = 'stack'
575bbdae
KH
26usage = ['[-t <target patch>] [-n] [<patches>]']
27description = """
760a7cfc 28This is the opposite operation of linkstg:float[]: move the specified
575bbdae
KH
29patches down the stack. It is for example useful to group stable
30patches near the bottom of the stack, where they are less likely to be
31impacted by the push of another patch, and from where they can be more
32easily committed or pushed.
33
34If no patch is specified on command-line, the current patch gets sunk.
35By default patches are sunk to the bottom of the stack, but the '--to'
36option allows to place them under any applied patch.
de7a79c4 37
575bbdae
KH
38Sinking internally involves popping all patches (or all patches
39including <target patch>), then pushing the patches to sink, and then
40(unless '--nopush' is also given) pushing back into place the
41formerly-applied patches."""
de7a79c4 42
6c8a90e1
KH
43args = [argparse.patch_range(argparse.applied_patches,
44 argparse.unapplied_patches)]
575bbdae
KH
45options = [
46 opt('-n', '--nopush', action = 'store_true',
47 short = 'Do not push the patches back after sinking', long = """
48 Do not push back on the stack the formerly-applied patches.
49 Only the patches to sink are pushed."""),
6c8a90e1 50 opt('-t', '--to', metavar = 'TARGET', args = [argparse.applied_patches],
575bbdae
KH
51 short = 'Sink patches below the TARGET patch', long = """
52 Specify a target patch to place the patches below, instead of
54ab5bde
CM
53 sinking them to the bottom of the stack.""")
54 ] + argparse.keep_option()
de7a79c4 55
54ab5bde 56directory = common.DirectoryHasRepositoryLib()
de7a79c4
YD
57
58def func(parser, options, args):
6f1c5e3c 59 """Sink patches down the stack.
de7a79c4 60 """
54ab5bde 61 stack = directory.repository.current_stack
de7a79c4 62
54ab5bde
CM
63 if options.to and not options.to in stack.patchorder.applied:
64 raise common.CmdException('Cannot sink below %s since it is not applied'
65 % options.to)
de7a79c4 66
54ab5bde
CM
67 if len(args) > 0:
68 patches = common.parse_patches(args, stack.patchorder.all)
69 else:
70 # current patch
71 patches = list(stack.patchorder.applied[-1:])
de7a79c4 72
54ab5bde
CM
73 if not patches:
74 raise common.CmdException('No patches to sink')
75 if options.to and options.to in patches:
76 raise common.CmdException('Cannot have a sinked patch as target')
74477393 77
54ab5bde
CM
78 applied = [p for p in stack.patchorder.applied if p not in patches]
79 if options.to:
80 insert_idx = applied.index(options.to)
de7a79c4 81 else:
54ab5bde
CM
82 insert_idx = 0
83 applied = applied[:insert_idx] + patches + applied[insert_idx:]
54ab5bde 84 unapplied = [p for p in stack.patchorder.unapplied if p not in patches]
54ab5bde
CM
85
86 iw = stack.repository.default_iw
87 clean_iw = (not options.keep and iw) or None
88 trans = transaction.StackTransaction(stack, 'sink',
89 check_clean_iw = clean_iw)
90
91 try:
d44708ef 92 trans.reorder_patches(applied, unapplied, iw = iw)
54ab5bde
CM
93 except transaction.TransactionHalted:
94 pass
95 return trans.run(iw)