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
.out
import *
27 from stgit
import stack
, git
, templates
30 help = 'exports patches to a directory'
31 usage
= """%prog [options] [<patch1>] [<patch2>] [<patch3>..<patch4>]
33 Export a range of applied patches to a given directory (defaults to
34 'patches-<branch>') in a standard unified GNU diff format. A template
35 file (defaulting to '.git/patchexport.tmpl' or
36 '~/.stgit/templates/patchexport.tmpl' or
37 '/usr/share/stgit/templates/patchexport.tmpl') can be used for the
38 patch format. The following variables are supported in the template
41 %(description)s - patch description
42 %(shortdescr)s - the first line of the patch description
43 %(longdescr)s - the rest of the patch description, after the first line
44 %(diffstat)s - the diff statistics
45 %(authname)s - author's name
46 %(authemail)s - author's e-mail
47 %(authdate)s - patch creation date
48 %(commname)s - committer's name
49 %(commemail)s - committer's e-mail
52 directory
= DirectoryHasRepository()
53 options
= [make_option('-d', '--dir',
54 help = 'export patches to DIR instead of the default'),
55 make_option('-p', '--patch',
56 help = 'append .patch to the patch names',
57 action
= 'store_true'),
58 make_option('-e', '--extension',
59 help = 'append .EXTENSION to the patch names'),
60 make_option('-n', '--numbered',
61 help = 'prefix the patch names with order numbers',
62 action
= 'store_true'),
63 make_option('-t', '--template', metavar
= 'FILE',
64 help = 'Use FILE as a template'),
65 make_option('-b', '--branch',
66 help = 'use BRANCH instead of the default one'),
67 make_option('-O', '--diff-opts',
68 help = 'options to pass to git-diff'),
69 make_option('-s', '--stdout',
70 help = 'dump the patches to the standard output',
71 action
= 'store_true')]
74 def func(parser
, options
, args
):
75 """Export a range of patches.
80 dirname
= 'patches-%s' % crt_series
.get_name()
82 if not options
.branch
and git
.local_changes():
83 out
.warn('Local changes in the tree;'
84 ' you might want to commit them first')
86 if not options
.stdout
:
87 if not os
.path
.isdir(dirname
):
89 series
= file(os
.path
.join(dirname
, 'series'), 'w+')
92 diff_flags
= options
.diff_opts
.split()
96 applied
= crt_series
.get_applied()
98 patches
= parse_patches(args
, applied
)
104 raise CmdException
, 'No patches applied'
106 zpadding
= len(str(num
))
112 tmpl
= file(options
.template
).read()
114 tmpl
= templates
.get_template('patchexport.tmpl')
118 # note the base commit for this series
119 if not options
.stdout
:
120 base_commit
= crt_series
.get_patch(patches
[0]).get_bottom()
121 print >> series
, '# This series applies on GIT commit %s' % base_commit
127 pname
= '%s.patch' % pname
128 elif options
.extension
:
129 pname
= '%s.%s' %
(pname
, options
.extension
)
131 pname
= '%s-%s' %
(str(patch_no
).zfill(zpadding
), pname
)
132 pfile
= os
.path
.join(dirname
, pname
)
133 if not options
.stdout
:
134 print >> series
, pname
136 # get the patch description
137 patch
= crt_series
.get_patch(p
)
139 descr
= patch
.get_description().strip()
140 descr_lines
= descr
.split('\n')
142 short_descr
= descr_lines
[0].rstrip()
143 long_descr
= reduce(lambda x
, y
: x
+ '\n' + y
,
144 descr_lines
[1:], '').strip()
146 tmpl_dict
= {'description': patch
.get_description().rstrip(),
147 'shortdescr': short_descr
,
148 'longdescr': long_descr
,
149 'diffstat': git
.diffstat(rev1
= patch
.get_bottom(),
150 rev2
= patch
.get_top()),
151 'authname': patch
.get_authname(),
152 'authemail': patch
.get_authemail(),
153 'authdate': patch
.get_authdate(),
154 'commname': patch
.get_commname(),
155 'commemail': patch
.get_commemail()}
156 for key
in tmpl_dict
:
157 if not tmpl_dict
[key
]:
161 descr
= tmpl % tmpl_dict
162 except KeyError, err
:
163 raise CmdException
, 'Unknown patch template variable: %s' \
166 raise CmdException
, 'Only "%(name)s" variables are ' \
167 'supported in the patch template'
172 f
= open(pfile
, 'w+')
174 if options
.stdout
and num
> 1:
176 print patch
.get_name()
180 f
.write(git
.diff(rev1
= patch
.get_bottom(),
181 rev2
= patch
.get_top(),
182 diff_flags
= diff_flags
))
183 if not options
.stdout
:
187 if not options
.stdout
: