Avoid allowing --undo if it doesn't change anything
[stgit] / stgit / commands / push.py
CommitLineData
fcee87cf
CM
1
2__copyright__ = """
3Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
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
27help = 'push a patch on top of the series'
26aab5b0
CM
28usage = """%prog [options] [<name>]
29
30Push a patch (defaulting to the first unapplied one) or range of
31patches to the stack. The 'push' operation allows patch reordering by
32commuting them with the three-way merge algorithm. If the result of
33the 'push' operation is not acceptable or if there are too many
34conflicts, the '--undo' option can be used to revert the patch and the
35tree to the state before the operation. Conflicts raised during the
36push operation have to be fixed and the 'resolved' command run.
37
38The 'push' command also notifies when the patch becomes empty after
39the merge operation (i.e. it was fully merged upstream)."""
fcee87cf
CM
40
41options = [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 '
48 'PATCH1 and PATCH2'),
49 make_option('--reverse',
50 help = 'push the patches in reverse order',
51 action = 'store_true'),
52 make_option('--undo',
53 help = 'undo the last push operation',
54 action = 'store_true')]
55
56
57def func(parser, options, args):
58 """Pushes the given patch or all onto the series
59 """
60 # If --undo is passed, do the work and exit
61 if options.undo:
62 patch = crt_series.get_current()
63 if not patch:
64 raise CmdException, 'No patch to undo'
65
66 print 'Undoing the "%s" push...' % patch,
67 sys.stdout.flush()
68 resolved_all()
a5bbc44d
PBG
69 if crt_series.undo_push():
70 print 'done'
71 else:
72 print 'done (patch unchanged)'
fcee87cf
CM
73 print_crt_patch()
74
75 return
76
77 check_local_changes()
78 check_conflicts()
79 check_head_top_equal()
80
81 unapplied = crt_series.get_unapplied()
82 if not unapplied:
83 raise CmdException, 'No more patches to push'
84
85 if options.to:
86 boundaries = options.to.split(':')
87 if len(boundaries) == 1:
88 if boundaries[0] not in unapplied:
89 raise CmdException, 'Patch "%s" not unapplied' % boundaries[0]
90 patches = unapplied[:unapplied.index(boundaries[0])+1]
91 elif len(boundaries) == 2:
92 if boundaries[0] not in unapplied:
93 raise CmdException, 'Patch "%s" not unapplied' % boundaries[0]
94 if boundaries[1] not in unapplied:
95 raise CmdException, 'Patch "%s" not unapplied' % boundaries[1]
96 lb = unapplied.index(boundaries[0])
97 hb = unapplied.index(boundaries[1])
98 if lb > hb:
99 raise CmdException, 'Patch "%s" after "%s"' \
100 % (boundaries[0], boundaries[1])
101 patches = unapplied[lb:hb+1]
102 else:
103 raise CmdException, 'incorrect parameters to "--to"'
104 elif options.number:
105 patches = unapplied[:options.number]
106 elif options.all:
107 patches = unapplied
108 elif len(args) == 0:
109 patches = [unapplied[0]]
110 elif len(args) == 1:
111 patches = [args[0]]
112 else:
113 parser.error('incorrect number of arguments')
114
115 if patches == []:
116 raise CmdException, 'No patches to push'
117
118 if options.reverse:
119 patches.reverse()
120
680e3a32
PBG
121 print 'Trying fast-forward...'
122
123 forwarded = crt_series.forward_patches(patches)
124 if forwarded > 1:
125 print 'Fast-forwarded patches "%s" - "%s"' % (patches[0],
126 patches[forwarded - 1])
127 elif forwarded == 1:
128 print 'Fast-forwarded patch "%s"' % patches[0]
129 else:
130 print 'Fast-forwarding failed, using normal pushing'
131
132 for p in patches[forwarded:]:
f3dd7770
CM
133 if p not in unapplied:
134 raise CmdException, 'Patch "%s" not unapplied' % p
135
fcee87cf
CM
136 print 'Pushing patch "%s"...' % p,
137 sys.stdout.flush()
138
139 crt_series.push_patch(p)
140
141 if crt_series.empty_patch(p):
142 print 'done (empty patch)'
143 else:
144 print 'done'
145 print_crt_patch()