Merge branch 'stable'
[stgit] / stgit / commands / float.py
CommitLineData
d98a499c
RR
1__copyright__ = """
2Copyright (C) 2006, Robin Rosenberg <robin.rosenberg@dewire.com>
3Modified by Catalin Marinas
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
575bbdae 20from stgit.argparse import opt
d98a499c
RR
21from stgit.commands.common import *
22from stgit.utils import *
6c8a90e1 23from stgit import argparse, stack, git
d98a499c 24
575bbdae 25help = 'Push patches to the top, even if applied'
33ff9cdd 26kind = 'stack'
575bbdae
KH
27usage = ['<patches>',
28 '-s <series>']
29description = """
d98a499c
RR
30Push a patch or a range of patches to the top even if applied. The
31necessary pop and push operations will be performed to accomplish
1ab20f6c
CM
32this. The '--series' option can be used to rearrange the (top) patches
33as specified by the given series file (or the standard input)."""
d98a499c 34
6c8a90e1
KH
35args = [argparse.patch_range(argparse.applied_patches,
36 argparse.unapplied_patches)]
575bbdae
KH
37options = [
38 opt('-s', '--series', action = 'store_true',
39 short = 'Rearrange according to a series file')]
40
117ed129 41directory = DirectoryGotoToplevel(log = True)
d98a499c
RR
42
43def func(parser, options, args):
44 """Pops and pushed to make the named patch the topmost patch
45 """
1ab20f6c
CM
46 args_nr = len(args)
47 if (options.series and args_nr > 1) \
48 or (not options.series and args_nr == 0):
d98a499c
RR
49 parser.error('incorrect number of arguments')
50
51 check_local_changes()
52 check_conflicts()
6972fd6b 53 check_head_top_equal(crt_series)
d98a499c
RR
54
55 unapplied = crt_series.get_unapplied()
56 applied = crt_series.get_applied()
57 all = unapplied + applied
58
1ab20f6c
CM
59 if options.series:
60 if args_nr:
61 f = file(args[0])
62 else:
63 f = sys.stdin
64
65 patches = []
66 for line in f:
67 patch = re.sub('#.*$', '', line).strip()
68 if patch:
69 patches.append(patch)
70 else:
71 patches = parse_patches(args, all)
72
d98a499c
RR
73 # working with "topush" patches in reverse order might be a bit
74 # more efficient for large series but the main reason is for the
75 # "topop != topush" comparison to work
76 patches.reverse()
77
78 topush = []
79 topop = []
80
81 for p in patches:
82 while p in applied:
83 top = applied.pop()
84 if not top in patches:
85 topush.append(top)
86 topop.append(top)
87 topush = patches + topush
88
ee8aedfa
CM
89 # remove common patches to avoid unnecessary pop/push
90 while topush and topop:
91 if topush[-1] != topop[-1]:
92 break
93 topush.pop()
94 topop.pop()
95
d98a499c
RR
96 # check whether the operation is really needed
97 if topop != topush:
98 if topop:
6972fd6b 99 pop_patches(crt_series, topop)
d98a499c
RR
100 if topush:
101 topush.reverse()
6972fd6b 102 push_patches(crt_series, topush)