Make 'push --undo' safer
[stgit] / stgit / commands / push.py
... / ...
CommitLineData
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 one or more patches onto of the stack'
28usage = """%prog [options] [<patch1>] [<patch2>] [<patch3>..<patch4>]
29
30Push one or more patches (defaulting to the first unapplied one) onto
31the stack. The 'push' operation allows patch reordering by commuting
32them with the three-way merge algorithm. If the result of the 'push'
33operation is not acceptable or if there are too many conflicts, the
34'--undo' option can be used to revert the last pushed patch. Conflicts
35raised during the push operation have to be fixed and the 'resolved'
36command run.
37
38The command also notifies when the patch becomes empty (fully merged
39upstream) or is modified (three-way merged) by the 'push' operation."""
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('--reverse',
47 help = 'push the patches in reverse order',
48 action = 'store_true'),
49 make_option('-m', '--merged',
50 help = 'check for patches merged upstream',
51 action = 'store_true'),
52 make_option('--undo',
53 help = 'undo the last patch pushing',
54 action = 'store_true')]
55
56
57def func(parser, options, args):
58 """Pushes the given patch or all onto the series
59 """
60 check_conflicts()
61 check_local_changes()
62 check_head_top_equal()
63
64 # If --undo is passed, do the work and exit
65 if options.undo:
66 patch = crt_series.get_current()
67 if not patch:
68 raise CmdException, 'No patch to undo'
69
70 print 'Undoing the "%s" push...' % patch,
71 sys.stdout.flush()
72 if crt_series.undo_push():
73 print 'done'
74 else:
75 print 'done (patch unchanged)'
76 print_crt_patch()
77
78 return
79
80 unapplied = crt_series.get_unapplied()
81 if not unapplied:
82 raise CmdException, 'No more patches to push'
83
84 if options.number:
85 patches = unapplied[:options.number]
86 elif options.all:
87 patches = unapplied
88 elif len(args) == 0:
89 patches = [unapplied[0]]
90 else:
91 patches = parse_patches(args, unapplied)
92
93 if patches == []:
94 raise CmdException, 'No patches to push'
95
96 if options.reverse:
97 patches.reverse()
98
99 push_patches(patches, options.merged)
100
101 print_crt_patch()