stg import now extracts Message-ID header
[stgit.git] / stgit / commands / float.py
blobb87eda0b0aac779b29ccfede6dbd1d0e504983dc
1 import io
2 import re
3 import sys
5 from stgit.argparse import keep_option, opt, patch_range
6 from stgit.commands.common import CmdException, DirectoryHasRepository, parse_patches
7 from stgit.lib import transaction
9 __copyright__ = """
10 Copyright (C) 2006, Robin Rosenberg <robin.rosenberg@dewire.com>
11 Modified by Catalin Marinas
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License version 2 as
15 published by the Free Software Foundation.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, see http://www.gnu.org/licenses/.
24 """
26 help = 'Push patches to the top, even if applied'
27 kind = 'stack'
28 usage = ['[--] <patches>', '-s <series>']
29 description = """
30 Float a patch or range of patches to be the top-most applied patches.
31 The patches to be floated may currently be either applied or unapplied.
32 The necessary pop and push operations will be performed to float the
33 named patches. Patches not specified will remain applied or unapplied
34 as they were prior to the float operation."""
36 args = [patch_range('applied_patches', 'unapplied_patches')]
37 options = [
38 opt(
39 '--noapply',
40 action='store_true',
41 short='Reorder patches by floating without applying.',
43 opt(
44 '-s',
45 '--series',
46 metavar='FILE',
47 short='Rearrange according to the series FILE',
50 options.extend(keep_option())
52 directory = DirectoryHasRepository()
55 def func(parser, options, args):
56 """Reorder patches to make the named patch the topmost one."""
57 if options.series and args:
58 parser.error('<patches> cannot be used with --series')
59 elif not options.series and not args:
60 parser.error('incorrect number of arguments')
62 stack = directory.repository.current_stack
64 if options.series:
65 if options.series == '-':
66 f = io.open(sys.stdin.fileno())
67 else:
68 f = io.open(options.series)
70 patches = []
71 for line in f:
72 patch = re.sub('#.*$', '', line).strip()
73 if patch:
74 patches.append(patch)
75 patches = parse_patches(patches, stack.patchorder.all)
76 else:
77 patches = parse_patches(args, stack.patchorder.all)
79 if not patches:
80 raise CmdException('No patches to float')
82 iw = stack.repository.default_iw
83 if options.keep or (
84 options.noapply and not any(pn in stack.patchorder.applied for pn in patches)
86 clean_iw = None
87 else:
88 clean_iw = iw
89 trans = transaction.StackTransaction(stack, 'float', check_clean_iw=clean_iw)
91 if options.noapply:
92 applied = [p for p in trans.applied if p not in patches]
93 unapplied = patches + [p for p in trans.unapplied if p not in patches]
94 else:
95 applied = [p for p in trans.applied if p not in patches] + patches
96 unapplied = [p for p in trans.unapplied if p not in patches]
98 try:
99 trans.reorder_patches(applied, unapplied, iw=iw)
100 except transaction.TransactionHalted:
101 pass
102 return trans.run(iw)