Add support for initializing a branch for stgit from Emacs.
[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
19import sys, os
20from optparse import OptionParser, make_option
21
22from stgit.commands.common import *
23from stgit.utils import *
24from stgit import stack, git
25
26
6f1c5e3c 27help = 'send patches deeper down the stack'
de7a79c4
YD
28usage = """%prog [-t <target patch>] [-n] [<patches>]
29
30Pop all patches (or all patches including <target patch>), then
31push the specified <patches> (the current patch by default), and
32then push back into place the formerly-applied patches (unless -n
33is also given)."""
34
7b601c9e 35directory = DirectoryGotoToplevel()
de7a79c4
YD
36options = [make_option('-n', '--nopush',
37 help = 'do not push the patches back after sinking',
38 action = 'store_true'),
39 make_option('-t', '--to', metavar = 'TARGET',
6f1c5e3c 40 help = 'sink patches below TARGET patch')]
de7a79c4
YD
41
42def func(parser, options, args):
6f1c5e3c 43 """Sink patches down the stack.
de7a79c4
YD
44 """
45
46 check_local_changes()
47 check_conflicts()
6972fd6b 48 check_head_top_equal(crt_series)
de7a79c4
YD
49
50 oldapplied = crt_series.get_applied()
51 unapplied = crt_series.get_unapplied()
8bcdcdc9 52 all = oldapplied + unapplied
de7a79c4 53
74477393
KH
54 if options.to and not options.to in oldapplied:
55 raise CmdException('Cannot sink below %s, since it is not applied'
56 % options.to)
57
de7a79c4
YD
58 if len(args) > 0:
59 patches = parse_patches(args, all)
60 else:
cd65a83b
KH
61 current = crt_series.get_current()
62 if not current:
63 raise CmdException('No patch applied')
64 patches = [current]
de7a79c4 65
8bcdcdc9
CM
66 before_patches = after_patches = []
67
68 # pop necessary patches
cd65a83b 69 if oldapplied:
8bcdcdc9
CM
70 if options.to:
71 pop_idx = oldapplied.index(options.to)
72 else:
73 pop_idx = 0
74 after_patches = [p for p in oldapplied[pop_idx:] if p not in patches]
75
76 # find the deepest patch to pop
77 sink_applied = [p for p in oldapplied if p in patches]
78 if sink_applied:
79 sinked_idx = oldapplied.index(sink_applied[0])
80 if sinked_idx < pop_idx:
81 # this is the case where sink brings patches forward
82 before_patches = [p for p in oldapplied[sinked_idx:pop_idx]
83 if p not in patches]
84 pop_idx = sinked_idx
85
86 crt_series.pop_patch(oldapplied[pop_idx])
87
88 push_patches(crt_series, before_patches)
6972fd6b 89 push_patches(crt_series, patches)
de7a79c4 90 if not options.nopush:
8bcdcdc9 91 push_patches(crt_series, after_patches)