Commit | Line | Data |
---|---|---|
d98a499c RR |
1 | __copyright__ = """ |
2 | Copyright (C) 2006, Robin Rosenberg <robin.rosenberg@dewire.com> | |
3 | Modified by Catalin Marinas | |
4 | ||
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License version 2 as | |
7 | published by the Free Software Foundation. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program; if not, write to the Free Software | |
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 | """ | |
18 | ||
19 | import sys, os | |
575bbdae | 20 | from stgit.argparse import opt |
d98a499c RR |
21 | from stgit.commands.common import * |
22 | from stgit.utils import * | |
23 | from stgit import stack, git | |
24 | ||
575bbdae | 25 | help = 'Push patches to the top, even if applied' |
33ff9cdd | 26 | kind = 'stack' |
575bbdae KH |
27 | usage = ['<patches>', |
28 | '-s <series>'] | |
29 | description = """ | |
d98a499c RR |
30 | Push a patch or a range of patches to the top even if applied. The |
31 | necessary pop and push operations will be performed to accomplish | |
1ab20f6c CM |
32 | this. The '--series' option can be used to rearrange the (top) patches |
33 | as specified by the given series file (or the standard input).""" | |
d98a499c | 34 | |
575bbdae KH |
35 | options = [ |
36 | opt('-s', '--series', action = 'store_true', | |
37 | short = 'Rearrange according to a series file')] | |
38 | ||
7b601c9e | 39 | directory = DirectoryGotoToplevel() |
d98a499c RR |
40 | |
41 | def func(parser, options, args): | |
42 | """Pops and pushed to make the named patch the topmost patch | |
43 | """ | |
1ab20f6c CM |
44 | args_nr = len(args) |
45 | if (options.series and args_nr > 1) \ | |
46 | or (not options.series and args_nr == 0): | |
d98a499c RR |
47 | parser.error('incorrect number of arguments') |
48 | ||
49 | check_local_changes() | |
50 | check_conflicts() | |
6972fd6b | 51 | check_head_top_equal(crt_series) |
d98a499c RR |
52 | |
53 | unapplied = crt_series.get_unapplied() | |
54 | applied = crt_series.get_applied() | |
55 | all = unapplied + applied | |
56 | ||
1ab20f6c CM |
57 | if options.series: |
58 | if args_nr: | |
59 | f = file(args[0]) | |
60 | else: | |
61 | f = sys.stdin | |
62 | ||
63 | patches = [] | |
64 | for line in f: | |
65 | patch = re.sub('#.*$', '', line).strip() | |
66 | if patch: | |
67 | patches.append(patch) | |
68 | else: | |
69 | patches = parse_patches(args, all) | |
70 | ||
d98a499c RR |
71 | # working with "topush" patches in reverse order might be a bit |
72 | # more efficient for large series but the main reason is for the | |
73 | # "topop != topush" comparison to work | |
74 | patches.reverse() | |
75 | ||
76 | topush = [] | |
77 | topop = [] | |
78 | ||
79 | for p in patches: | |
80 | while p in applied: | |
81 | top = applied.pop() | |
82 | if not top in patches: | |
83 | topush.append(top) | |
84 | topop.append(top) | |
85 | topush = patches + topush | |
86 | ||
ee8aedfa CM |
87 | # remove common patches to avoid unnecessary pop/push |
88 | while topush and topop: | |
89 | if topush[-1] != topop[-1]: | |
90 | break | |
91 | topush.pop() | |
92 | topop.pop() | |
93 | ||
d98a499c RR |
94 | # check whether the operation is really needed |
95 | if topop != topush: | |
96 | if topop: | |
6972fd6b | 97 | pop_patches(crt_series, topop) |
d98a499c RR |
98 | if topush: |
99 | topush.reverse() | |
6972fd6b | 100 | push_patches(crt_series, topush) |