5 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 from optparse
import OptionParser
, make_option
24 from stgit
.commands
.common
import *
25 from stgit
.utils
import *
26 from stgit
import stack
, git
, templates
29 help = 'exports a series of patches to <dir> (or patches)'
30 usage
= """%prog [options] [<dir>]
32 Export the applied patches into a given directory (defaults to
33 'patches') in a standard unified GNU diff format. A template file
34 (defaulting to '.git/patchexport.tmpl' or
35 '~/.stgit/templates/patchexport.tmpl' or
36 '/usr/share/stgit/templates/patchexport.tmpl') can be used for the
37 patch format. The following variables are supported in the template
40 %(description)s - patch description
41 %(shortdescr)s - the first line of the patch description
42 %(longdescr)s - the rest of the patch description, after the first line
43 %(diffstat)s - the diff statistics
44 %(authname)s - author's name
45 %(authemail)s - author's e-mail
46 %(authdate)s - patch creation date
47 %(commname)s - committer's name
48 %(commemail)s - committer's e-mail
50 'export' can also generate a diff for a range of patches."""
52 options
= [make_option('-n', '--numbered',
53 help = 'prefix the patch names with order numbers',
54 action
= 'store_true'),
55 make_option('-d', '--diff',
56 help = 'append .diff to the patch names',
57 action
= 'store_true'),
58 make_option('-p', '--patch',
59 help = 'append .patch to the patch names',
60 action
= 'store_true'),
61 make_option('-t', '--template', metavar
= 'FILE',
62 help = 'Use FILE as a template'),
63 make_option('-r', '--range',
64 metavar
= '[PATCH1][:[PATCH2]]',
65 help = 'export patches between PATCH1 and PATCH2'),
66 make_option('-b', '--branch',
67 help = 'use BRANCH instead of the default one'),
68 make_option('-s', '--stdout',
69 help = 'dump the patches to the standard output',
70 action
= 'store_true')]
73 def func(parser
, options
, args
):
75 dirname
= 'patches-%s' % crt_series
.get_branch()
79 parser
.error('incorrect number of arguments')
81 if not options
.branch
and git
.local_changes():
82 print 'Warning: local changes in the tree. ' \
83 'You might want to commit them first'
85 if not options
.stdout
:
86 if not os
.path
.isdir(dirname
):
88 series
= file(os
.path
.join(dirname
, 'series'), 'w+')
90 applied
= crt_series
.get_applied()
91 unapplied
= crt_series
.get_unapplied()
94 boundaries
= options
.range.split(':')
95 if len(boundaries
) == 1:
98 elif len(boundaries
) == 2:
99 if boundaries
[0] == '':
102 start
= boundaries
[0]
103 if boundaries
[1] == '':
108 raise CmdException
, 'incorrect parameters to "--range"'
111 start_idx
= applied
.index(start
)
113 if start
in unapplied
:
114 raise CmdException
, 'Patch "%s" not applied' % start
116 raise CmdException
, 'Patch "%s" does not exist' % start
119 stop_idx
= applied
.index(stop
) + 1
121 if stop
in unapplied
:
122 raise CmdException
, 'Patch "%s" not applied' % stop
124 raise CmdException
, 'Patch "%s" does not exist' % stop
126 if start_idx
>= stop_idx
:
127 raise CmdException
, 'Incorrect patch range order'
130 stop_idx
= len(applied
)
132 patches
= applied
[start_idx
:stop_idx
]
136 raise CmdException
, 'No patches applied'
138 zpadding
= len(str(num
))
144 tmpl
= file(options
.template
).read()
146 tmpl
= templates
.get_template('patchexport.tmpl')
150 # note the base commit for this series
151 if not options
.stdout
:
152 base_commit
= crt_series
.get_patch(patches
[0]).get_bottom()
153 print >> series
, '# This series applies on GIT commit %s' % base_commit
159 pname
= '%s.diff' % pname
161 pname
= '%s.patch' % pname
163 pname
= '%s-%s' %
(str(patch_no
).zfill(zpadding
), pname
)
164 pfile
= os
.path
.join(dirname
, pname
)
165 if not options
.stdout
:
166 print >> series
, pname
168 # get the patch description
169 patch
= crt_series
.get_patch(p
)
171 descr
= patch
.get_description().strip()
172 descr_lines
= descr
.split('\n')
174 short_descr
= descr_lines
[0].rstrip()
175 long_descr
= reduce(lambda x
, y
: x
+ '\n' + y
,
176 descr_lines
[1:], '').strip()
178 tmpl_dict
= {'description': patch
.get_description().rstrip(),
179 'shortdescr': short_descr
,
180 'longdescr': long_descr
,
181 'diffstat': git
.diffstat(rev1
= patch
.get_bottom(),
182 rev2
= patch
.get_top()),
183 'authname': patch
.get_authname(),
184 'authemail': patch
.get_authemail(),
185 'authdate': patch
.get_authdate(),
186 'commname': patch
.get_commname(),
187 'commemail': patch
.get_commemail()}
188 for key
in tmpl_dict
:
189 if not tmpl_dict
[key
]:
193 descr
= tmpl % tmpl_dict
194 except KeyError, err
:
195 raise CmdException
, 'Unknown patch template variable: %s' \
198 raise CmdException
, 'Only "%(name)s" variables are ' \
199 'supported in the patch template'
204 f
= open(pfile
, 'w+')
206 if options
.stdout
and num
> 1:
207 print '-------------------------------------------------------------------------------'
208 print patch
.get_name()
209 print '-------------------------------------------------------------------------------'
214 git
.diff(rev1
= patch
.get_bottom(),
215 rev2
= patch
.get_top(),
217 if not options
.stdout
:
221 if not options
.stdout
: