Print a shorter usage message with the --help option
[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
718a5e51
CM
38The command also notifies when the patch becomes empty (fully merged
39upstream) or is modified (three-way merged) by the 'push' operation."""
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
bca12bd1
CL
57def is_patch_appliable(p):
58 """See if patch exists, or is already applied.
59 """
60 if p in 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
64
fcee87cf
CM
65def func(parser, options, args):
66 """Pushes the given patch or all onto the series
67 """
bca12bd1
CL
68 global applied, unapplied
69
fcee87cf
CM
70 # If --undo is passed, do the work and exit
71 if options.undo:
72 patch = crt_series.get_current()
73 if not patch:
74 raise CmdException, 'No patch to undo'
75
76 print 'Undoing the "%s" push...' % patch,
77 sys.stdout.flush()
78 resolved_all()
a5bbc44d
PBG
79 if crt_series.undo_push():
80 print 'done'
81 else:
82 print 'done (patch unchanged)'
fcee87cf
CM
83 print_crt_patch()
84
85 return
86
87 check_local_changes()
88 check_conflicts()
89 check_head_top_equal()
90
bca12bd1 91 applied = crt_series.get_applied()
fcee87cf
CM
92 unapplied = crt_series.get_unapplied()
93 if not unapplied:
94 raise CmdException, 'No more patches to push'
95
96 if options.to:
97 boundaries = options.to.split(':')
98 if len(boundaries) == 1:
bca12bd1 99 is_patch_appliable(boundaries[0])
fcee87cf
CM
100 patches = unapplied[:unapplied.index(boundaries[0])+1]
101 elif len(boundaries) == 2:
bca12bd1
CL
102 is_patch_appliable(boundaries[0])
103 is_patch_appliable(boundaries[1])
fcee87cf
CM
104 lb = unapplied.index(boundaries[0])
105 hb = unapplied.index(boundaries[1])
106 if lb > hb:
107 raise CmdException, 'Patch "%s" after "%s"' \
108 % (boundaries[0], boundaries[1])
109 patches = unapplied[lb:hb+1]
110 else:
111 raise CmdException, 'incorrect parameters to "--to"'
112 elif options.number:
113 patches = unapplied[:options.number]
114 elif options.all:
115 patches = unapplied
116 elif len(args) == 0:
117 patches = [unapplied[0]]
118 elif len(args) == 1:
b70e43eb 119 patches = args
bca12bd1 120 is_patch_appliable(patches[0])
fcee87cf
CM
121 else:
122 parser.error('incorrect number of arguments')
123
124 if patches == []:
125 raise CmdException, 'No patches to push'
126
127 if options.reverse:
128 patches.reverse()
129
680e3a32
PBG
130 forwarded = crt_series.forward_patches(patches)
131 if forwarded > 1:
132 print 'Fast-forwarded patches "%s" - "%s"' % (patches[0],
133 patches[forwarded - 1])
134 elif forwarded == 1:
135 print 'Fast-forwarded patch "%s"' % patches[0]
680e3a32
PBG
136
137 for p in patches[forwarded:]:
bca12bd1 138 is_patch_appliable(p)
f3dd7770 139
fcee87cf
CM
140 print 'Pushing patch "%s"...' % p,
141 sys.stdout.flush()
142
718a5e51 143 modified = crt_series.push_patch(p)
fcee87cf
CM
144
145 if crt_series.empty_patch(p):
146 print 'done (empty patch)'
718a5e51
CM
147 elif modified:
148 print 'done (modified)'
fcee87cf
CM
149 else:
150 print 'done'
151 print_crt_patch()