Set the .txt extension to the StGIT commit message file
[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'
2c3a1655 28usage = """%prog [options] [<patch1> [<patch2>...]]
26aab5b0
CM
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'),
1777d8cd
CM
52 make_option('-m', '--merged',
53 help = 'check for patches merged upstream',
54 action = 'store_true'),
fcee87cf
CM
55 make_option('--undo',
56 help = 'undo the last push operation',
57 action = 'store_true')]
58
59
bca12bd1
CL
60def is_patch_appliable(p):
61 """See if patch exists, or is already applied.
62 """
63 if p in applied:
1777d8cd 64 raise CmdException, 'Patch "%s" is already applied' % p
bca12bd1 65 if p not in unapplied:
1777d8cd 66 raise CmdException, 'Patch "%s" does not exist' % p
bca12bd1 67
fcee87cf
CM
68def func(parser, options, args):
69 """Pushes the given patch or all onto the series
70 """
bca12bd1
CL
71 global applied, unapplied
72
fcee87cf
CM
73 # If --undo is passed, do the work and exit
74 if options.undo:
75 patch = crt_series.get_current()
76 if not patch:
77 raise CmdException, 'No patch to undo'
78
79 print 'Undoing the "%s" push...' % patch,
80 sys.stdout.flush()
81 resolved_all()
a5bbc44d
PBG
82 if crt_series.undo_push():
83 print 'done'
84 else:
85 print 'done (patch unchanged)'
fcee87cf
CM
86 print_crt_patch()
87
88 return
89
90 check_local_changes()
91 check_conflicts()
92 check_head_top_equal()
93
bca12bd1 94 applied = crt_series.get_applied()
fcee87cf
CM
95 unapplied = crt_series.get_unapplied()
96 if not unapplied:
97 raise CmdException, 'No more patches to push'
98
99 if options.to:
100 boundaries = options.to.split(':')
101 if len(boundaries) == 1:
bca12bd1 102 is_patch_appliable(boundaries[0])
fcee87cf
CM
103 patches = unapplied[:unapplied.index(boundaries[0])+1]
104 elif len(boundaries) == 2:
bca12bd1
CL
105 is_patch_appliable(boundaries[0])
106 is_patch_appliable(boundaries[1])
fcee87cf
CM
107 lb = unapplied.index(boundaries[0])
108 hb = unapplied.index(boundaries[1])
109 if lb > hb:
110 raise CmdException, 'Patch "%s" after "%s"' \
111 % (boundaries[0], boundaries[1])
112 patches = unapplied[lb:hb+1]
113 else:
114 raise CmdException, 'incorrect parameters to "--to"'
115 elif options.number:
116 patches = unapplied[:options.number]
117 elif options.all:
118 patches = unapplied
119 elif len(args) == 0:
120 patches = [unapplied[0]]
fcee87cf 121 else:
2c3a1655
YD
122 patches = args
123 map(is_patch_appliable, patches)
fcee87cf
CM
124
125 if patches == []:
126 raise CmdException, 'No patches to push'
127
128 if options.reverse:
129 patches.reverse()
130
1777d8cd 131 push_patches(patches, options.merged)
fcee87cf 132
fcee87cf 133 print_crt_patch()