3 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
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.
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.
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
20 from optparse import OptionParser, make_option
22 from stgit.commands.common import *
23 from stgit.utils import *
24 from stgit import stack, git
27 help = 'push a patch on top of the series'
28 usage = """%prog [options] [<name>]
30 Push a patch (defaulting to the first unapplied one) or range of
31 patches to the stack. The 'push' operation allows patch reordering by
32 commuting them with the three-way merge algorithm. If the result of
33 the 'push' operation is not acceptable or if there are too many
34 conflicts, the '--undo' option can be used to revert the patch and the
35 tree to the state before the operation. Conflicts raised during the
36 push operation have to be fixed and the 'resolved' command run.
38 The command also notifies when the patch becomes empty (fully merged
39 upstream) or is modified (three-way merged) by the 'push' operation."""
41 options = [make_option('-a', '--all',
42 help = 'push all the unapplied patches',
43 action = 'store_true'),
44 make_option('-n', '--number', type = 'int',
45 help = 'push the specified number of patches'),
46 make_option('-t', '--to', metavar = 'PATCH1[:PATCH2]',
47 help = 'push all patches to PATCH1 or between '
49 make_option('--reverse',
50 help = 'push the patches in reverse order',
51 action = 'store_true'),
53 help = 'undo the last push operation',
54 action = 'store_true')]
57 def is_patch_appliable(p):
58 """See if patch exists, or is already applied.
61 raise CmdException, 'Patch "%s" is already applied.' % p
62 if p not in unapplied:
63 raise CmdException, 'Patch "%s" does not exist.' % p
65 def func(parser, options, args):
66 """Pushes the given patch or all onto the series
68 global applied, unapplied
70 # If --undo is passed, do the work and exit
72 patch = crt_series.get_current()
74 raise CmdException, 'No patch to undo'
76 print 'Undoing the "%s" push...' % patch,
79 if crt_series.undo_push():
82 print 'done (patch unchanged)'
89 check_head_top_equal()
91 applied = crt_series.get_applied()
92 unapplied = crt_series.get_unapplied()
94 raise CmdException, 'No more patches to push'
97 boundaries = options.to.split(':')
98 if len(boundaries) == 1:
99 is_patch_appliable(boundaries[0])
100 patches = unapplied[:unapplied.index(boundaries[0])+1]
101 elif len(boundaries) == 2:
102 is_patch_appliable(boundaries[0])
103 is_patch_appliable(boundaries[1])
104 lb = unapplied.index(boundaries[0])
105 hb = unapplied.index(boundaries[1])
107 raise CmdException, 'Patch "%s" after "%s"' \
108 % (boundaries[0], boundaries[1])
109 patches = unapplied[lb:hb+1]
111 raise CmdException, 'incorrect parameters to "--to"'
113 patches = unapplied[:options.number]
117 patches = [unapplied[0]]
120 is_patch_appliable(patches[0])
122 parser.error('incorrect number of arguments')
125 raise CmdException, 'No patches to push'
130 forwarded = crt_series.forward_patches(patches)
132 print 'Fast-forwarded patches "%s" - "%s"' % (patches[0],
133 patches[forwarded - 1])
135 print 'Fast-forwarded patch "%s"' % patches[0]
137 for p in patches[forwarded:]:
138 is_patch_appliable(p)
140 print 'Pushing patch "%s"...' % p,
143 modified = crt_series.push_patch(p)
145 if crt_series.empty_patch(p):
146 print 'done (empty patch)'
148 print 'done (modified)'