[PATCH] Allow fast-forward pushing.
[stgit] / stgit / commands / push.py
1
2 __copyright__ = """
3 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
4
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.
8
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.
13
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
17 """
18
19 import sys, os
20 from optparse import OptionParser, make_option
21
22 from stgit.commands.common import *
23 from stgit.utils import *
24 from stgit import stack, git
25
26
27 help = 'push a patch on top of the series'
28 usage = """%prog [options] [<name>]
29
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.
37
38 The 'push' command also notifies when the patch becomes empty after
39 the merge operation (i.e. it was fully merged upstream)."""
40
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 '
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
57 def 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()
69 crt_series.undo_push()
70 print 'done'
71 print_crt_patch()
72
73 return
74
75 check_local_changes()
76 check_conflicts()
77 check_head_top_equal()
78
79 unapplied = crt_series.get_unapplied()
80 if not unapplied:
81 raise CmdException, 'No more patches to push'
82
83 if options.to:
84 boundaries = options.to.split(':')
85 if len(boundaries) == 1:
86 if boundaries[0] not in unapplied:
87 raise CmdException, 'Patch "%s" not unapplied' % boundaries[0]
88 patches = unapplied[:unapplied.index(boundaries[0])+1]
89 elif len(boundaries) == 2:
90 if boundaries[0] not in unapplied:
91 raise CmdException, 'Patch "%s" not unapplied' % boundaries[0]
92 if boundaries[1] not in unapplied:
93 raise CmdException, 'Patch "%s" not unapplied' % boundaries[1]
94 lb = unapplied.index(boundaries[0])
95 hb = unapplied.index(boundaries[1])
96 if lb > hb:
97 raise CmdException, 'Patch "%s" after "%s"' \
98 % (boundaries[0], boundaries[1])
99 patches = unapplied[lb:hb+1]
100 else:
101 raise CmdException, 'incorrect parameters to "--to"'
102 elif options.number:
103 patches = unapplied[:options.number]
104 elif options.all:
105 patches = unapplied
106 elif len(args) == 0:
107 patches = [unapplied[0]]
108 elif len(args) == 1:
109 patches = [args[0]]
110 else:
111 parser.error('incorrect number of arguments')
112
113 if patches == []:
114 raise CmdException, 'No patches to push'
115
116 if options.reverse:
117 patches.reverse()
118
119 print 'Trying fast-forward...'
120
121 forwarded = crt_series.forward_patches(patches)
122 if forwarded > 1:
123 print 'Fast-forwarded patches "%s" - "%s"' % (patches[0],
124 patches[forwarded - 1])
125 elif forwarded == 1:
126 print 'Fast-forwarded patch "%s"' % patches[0]
127 else:
128 print 'Fast-forwarding failed, using normal pushing'
129
130 for p in patches[forwarded:]:
131 if p not in unapplied:
132 raise CmdException, 'Patch "%s" not unapplied' % p
133
134 print 'Pushing patch "%s"...' % p,
135 sys.stdout.flush()
136
137 crt_series.push_patch(p)
138
139 if crt_series.empty_patch(p):
140 print 'done (empty patch)'
141 else:
142 print 'done'
143 print_crt_patch()