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
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 /usr/share/stgit/templates/patchexport.tmpl') can be used for the
36 patch format. The following variables are supported in the template
39 %(description)s - patch description
40 %(shortdescr)s - the first line of the patch description
41 %(longdescr)s - the rest of the patch description, after the first line
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
49 'export' can also generate a diff for a range of patches."""
51 options
= [make_option('-n', '--numbered',
52 help = 'prefix the patch names with order numbers',
53 action
= 'store_true'),
54 make_option('-d', '--diff',
55 help = 'append .diff to the patch names',
56 action
= 'store_true'),
57 make_option('-p', '--patch',
58 help = 'append .patch to the patch names',
59 action
= 'store_true'),
60 make_option('-t', '--template', metavar
= 'FILE',
61 help = 'Use FILE as a template'),
62 make_option('-r', '--range',
63 metavar
= '[PATCH1][:[PATCH2]]',
64 help = 'export patches between PATCH1 and PATCH2'),
65 make_option('-b', '--branch',
66 help = 'use BRANCH instead of the default one'),
67 make_option('-s', '--stdout',
68 help = 'dump the patches to the standard output',
69 action
= 'store_true')]
72 def func(parser
, options
, args
):
74 dirname
= 'patches-%s' % crt_series
.get_branch()
78 parser
.error('incorrect number of arguments')
80 if not options
.branch
and git
.local_changes():
81 print 'Warning: local changes in the tree. ' \
82 'You might want to commit them first'
84 if not options
.stdout
:
85 if not os
.path
.isdir(dirname
):
87 series
= file(os
.path
.join(dirname
, 'series'), 'w+')
89 applied
= crt_series
.get_applied()
90 unapplied
= crt_series
.get_unapplied()
93 boundaries
= options
.range.split(':')
94 if len(boundaries
) == 1:
97 elif len(boundaries
) == 2:
98 if boundaries
[0] == '':
101 start
= boundaries
[0]
102 if boundaries
[1] == '':
107 raise CmdException
, 'incorrect parameters to "--range"'
110 start_idx
= applied
.index(start
)
112 if start
in unapplied
:
113 raise CmdException
, 'Patch "%s" not applied' % start
115 raise CmdException
, 'Patch "%s" does not exist' % start
118 stop_idx
= applied
.index(stop
) + 1
120 if stop
in unapplied
:
121 raise CmdException
, 'Patch "%s" not applied' % stop
123 raise CmdException
, 'Patch "%s" does not exist' % stop
125 if start_idx
>= stop_idx
:
126 raise CmdException
, 'Incorrect patch range order'
129 stop_idx
= len(applied
)
131 patches
= applied
[start_idx
:stop_idx
]
135 raise CmdException
, 'No patches applied'
137 zpadding
= len(str(num
))
143 patch_tmpl_list
= [options
.template
]
147 patch_tmpl_list
+= [os
.path
.join(git
.get_base_dir(), 'patchexport.tmpl'),
148 os
.path
.join(sys
.prefix
,
149 'share/stgit/templates/patchexport.tmpl')]
151 for patch_tmpl
in patch_tmpl_list
:
152 if os
.path
.isfile(patch_tmpl
):
153 tmpl
= file(patch_tmpl
).read()
156 # note the base commit for this series
157 if not options
.stdout
:
158 base_commit
= crt_series
.get_patch(patches
[0]).get_bottom()
159 print >> series
, '# This series applies on GIT commit %s' % base_commit
165 pname
= '%s.diff' % pname
167 pname
= '%s.patch' % pname
169 pname
= '%s-%s' %
(str(patch_no
).zfill(zpadding
), pname
)
170 pfile
= os
.path
.join(dirname
, pname
)
171 if not options
.stdout
:
172 print >> series
, pname
174 # get the patch description
175 patch
= crt_series
.get_patch(p
)
177 descr
= patch
.get_description().strip()
178 descr_lines
= descr
.split('\n')
180 short_descr
= descr_lines
[0].rstrip()
181 long_descr
= reduce(lambda x
, y
: x
+ '\n' + y
,
182 descr_lines
[1:], '').strip()
184 tmpl_dict
= {'description': patch
.get_description().rstrip(),
185 'shortdescr': short_descr
,
186 'longdescr': long_descr
,
187 'diffstat': git
.diffstat(rev1
= patch
.get_bottom(),
188 rev2
= patch
.get_top()),
189 'authname': patch
.get_authname(),
190 'authemail': patch
.get_authemail(),
191 'authdate': patch
.get_authdate(),
192 'commname': patch
.get_commname(),
193 'commemail': patch
.get_commemail()}
194 for key
in tmpl_dict
:
195 if not tmpl_dict
[key
]:
199 descr
= tmpl % tmpl_dict
200 except KeyError, err
:
201 raise CmdException
, 'Unknown patch template variable: %s' \
204 raise CmdException
, 'Only "%(name)s" variables are ' \
205 'supported in the patch template'
210 f
= open(pfile
, 'w+')
212 if options
.stdout
and num
> 1:
213 print '-------------------------------------------------------------------------------'
214 print patch
.get_name()
215 print '-------------------------------------------------------------------------------'
220 git
.diff(rev1
= patch
.get_bottom(),
221 rev2
= patch
.get_top(),
223 if not options
.stdout
:
227 if not options
.stdout
: