Update .git/refs/heads/base after patch deletion
[stgit] / stgit / commands / export.py
CommitLineData
fcee87cf
CM
1"""Export command
2"""
3
4__copyright__ = """
5Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License version 2 as
9published by the Free Software Foundation.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19"""
20
21import sys, os
22from optparse import OptionParser, make_option
23
24from stgit.commands.common import *
25from stgit.utils import *
26from stgit import stack, git
27
28
29help = 'exports a series of patches to <dir> (or patches)'
26aab5b0
CM
30usage = """%prog [options] [<dir>]
31
32Export the applied patches into a given directory (defaults to
23a88c7d
CM
33'patches') in a standard unified GNU diff format. A template file
34(defaulting to '.git/patchexport.tmpl or
35/usr/share/stgit/templates/patchexport.tmpl') can be used for the
36patch format. The following variables are supported in the template
37file:
26aab5b0
CM
38
39 %(description)s - patch description
99e73103
CM
40 %(shortdescr)s - the first line of the patch description
41 %(longdescr)s - the rest of the patch description, after the first line
26aab5b0
CM
42 %(diffstat)s - the diff statistics
43 %(authname)s - author's name
44 %(authemail)s - author's e-mail
45 %(authdate)s - patch creation date
46 %(commname)s - committer's name
47 %(commemail)s - committer's e-mail
48
49'export' can also generate a diff for a range of patches."""
fcee87cf
CM
50
51options = [make_option('-n', '--numbered',
26aab5b0 52 help = 'prefix the patch names with order numbers',
fcee87cf
CM
53 action = 'store_true'),
54 make_option('-d', '--diff',
55 help = 'append .diff to the patch names',
56 action = 'store_true'),
57 make_option('-t', '--template', metavar = 'FILE',
58 help = 'Use FILE as a template'),
59 make_option('-r', '--range',
60 metavar = '[PATCH1][:[PATCH2]]',
2f7c8b0b
CM
61 help = 'export patches between PATCH1 and PATCH2'),
62 make_option('-b', '--branch',
63 help = 'use BRANCH instead of the default one')]
fcee87cf
CM
64
65
66def func(parser, options, args):
67 if len(args) == 0:
629ddd02 68 dirname = 'patches-%s' % crt_series.get_branch()
fcee87cf
CM
69 elif len(args) == 1:
70 dirname = args[0]
71 else:
72 parser.error('incorrect number of arguments')
73
2f7c8b0b 74 if not options.branch and git.local_changes():
fcee87cf
CM
75 print 'Warning: local changes in the tree. ' \
76 'You might want to commit them first'
77
78 if not os.path.isdir(dirname):
79 os.makedirs(dirname)
80 series = file(os.path.join(dirname, 'series'), 'w+')
81
82 applied = crt_series.get_applied()
3888eb9a 83 unapplied = crt_series.get_unapplied()
fcee87cf
CM
84
85 if options.range:
86 boundaries = options.range.split(':')
87 if len(boundaries) == 1:
88 start = boundaries[0]
89 stop = boundaries[0]
8f4d71da 90 elif len(boundaries) == 2:
fcee87cf
CM
91 if boundaries[0] == '':
92 start = applied[0]
93 else:
94 start = boundaries[0]
95 if boundaries[1] == '':
96 stop = applied[-1]
97 else:
98 stop = boundaries[1]
99 else:
8f4d71da 100 raise CmdException, 'incorrect parameters to "--range"'
fcee87cf
CM
101
102 if start in applied:
103 start_idx = applied.index(start)
104 else:
4dcb1859
CL
105 if start in unapplied:
106 raise CmdException, 'Patch "%s" not applied' % start
107 else:
108 raise CmdException, 'Patch "%s" does not exist' % start
109
fcee87cf
CM
110 if stop in applied:
111 stop_idx = applied.index(stop) + 1
112 else:
4dcb1859
CL
113 if stop in unapplied:
114 raise CmdException, 'Patch "%s" not applied' % stop
115 else:
116 raise CmdException, 'Patch "%s" does not exist' % stop
fcee87cf
CM
117
118 if start_idx >= stop_idx:
8f4d71da 119 raise CmdException, 'Incorrect patch range order'
fcee87cf
CM
120 else:
121 start_idx = 0
b054c8bd 122 stop_idx = len(applied)
fcee87cf
CM
123
124 patches = applied[start_idx:stop_idx]
125
126 num = len(patches)
16ad223e
PR
127 if num == 0:
128 raise CmdException, 'No patches applied'
129
fcee87cf
CM
130 zpadding = len(str(num))
131 if zpadding < 2:
132 zpadding = 2
133
23a88c7d
CM
134 # get the template
135 if options.template:
136 patch_tmpl_list = [options.template]
137 else:
138 patch_tmpl_list = []
139
bae29ddd 140 patch_tmpl_list += [os.path.join(git.get_base_dir(), 'patchexport.tmpl'),
23a88c7d
CM
141 os.path.join(sys.prefix,
142 'share/stgit/templates/patchexport.tmpl')]
143 tmpl = ''
144 for patch_tmpl in patch_tmpl_list:
145 if os.path.isfile(patch_tmpl):
146 tmpl = file(patch_tmpl).read()
147 break
148
6c4e4b68
CL
149 # note the base commit for this series
150 base_commit = crt_series.get_patch(patches[0]).get_bottom()
151 print >> series, '# This series applies on GIT commit %s' % base_commit
152
fcee87cf
CM
153 patch_no = 1;
154 for p in patches:
155 pname = p
156 if options.diff:
157 pname = '%s.diff' % pname
158 if options.numbered:
159 pname = '%s-%s' % (str(patch_no).zfill(zpadding), pname)
160 pfile = os.path.join(dirname, pname)
161 print >> series, pname
162
fcee87cf
CM
163 # get the patch description
164 patch = crt_series.get_patch(p)
165
99e73103
CM
166 descr = patch.get_description().strip()
167 descr_lines = descr.split('\n')
168
169 short_descr = descr_lines[0].rstrip()
170 long_descr = reduce(lambda x, y: x + '\n' + y,
171 descr_lines[1:], '').strip()
172
fcee87cf 173 tmpl_dict = {'description': patch.get_description().rstrip(),
99e73103
CM
174 'shortdescr': short_descr,
175 'longdescr': long_descr,
ed7ec17a
CM
176 'diffstat': git.diffstat(rev1 = patch.get_bottom(),
177 rev2 = patch.get_top()),
fcee87cf
CM
178 'authname': patch.get_authname(),
179 'authemail': patch.get_authemail(),
180 'authdate': patch.get_authdate(),
181 'commname': patch.get_commname(),
182 'commemail': patch.get_commemail()}
183 for key in tmpl_dict:
184 if not tmpl_dict[key]:
185 tmpl_dict[key] = ''
186
187 try:
188 descr = tmpl % tmpl_dict
189 except KeyError, err:
8f4d71da 190 raise CmdException, 'Unknown patch template variable: %s' \
fcee87cf
CM
191 % err
192 except TypeError:
8f4d71da 193 raise CmdException, 'Only "%(name)s" variables are ' \
fcee87cf
CM
194 'supported in the patch template'
195 f = open(pfile, 'w+')
196 f.write(descr)
fcee87cf
CM
197
198 # write the diff
ed7ec17a
CM
199 git.diff(rev1 = patch.get_bottom(),
200 rev2 = patch.get_top(),
26dba451
BL
201 out_fd = f)
202 f.close()
fcee87cf
CM
203 patch_no += 1
204
205 series.close()