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 options = [make_option('-n', '--numbered',
33 help = 'number the patch names',
34 action = 'store_true'),
35 make_option('-d', '--diff',
36 help = 'append .diff to the patch names',
37 action = 'store_true'),
38 make_option('-t', '--template', metavar = 'FILE',
39 help = 'Use FILE as a template'),
40 make_option('-r', '--range',
41 metavar = '[PATCH1][:[PATCH2]]',
42 help = 'export patches between PATCH1 and PATCH2')]
45 def func(parser, options, args):
51 parser.error('incorrect number of arguments')
53 if git.local_changes():
54 print 'Warning: local changes in the tree. ' \
55 'You might want to commit them first'
57 if not os.path.isdir(dirname):
59 series = file(os.path.join(dirname, 'series'), 'w+')
61 applied = crt_series.get_applied()
64 boundaries = options.range.split(':')
65 if len(boundaries) == 1:
68 if len(boundaries) == 2:
69 if boundaries[0] == '':
73 if boundaries[1] == '':
78 raise MainException, 'incorrect parameters to "--range"'
81 start_idx = applied.index(start)
83 raise MainException, 'Patch "%s" not applied' % start
85 stop_idx = applied.index(stop) + 1
87 raise MainException, 'Patch "%s" not applied' % stop
89 if start_idx >= stop_idx:
90 raise MainException, 'Incorrect patch range order'
93 stop_idx = len(applied)
95 patches = applied[start_idx:stop_idx]
98 zpadding = len(str(num))
106 pname = '%s.diff' % pname
108 pname = '%s-%s' % (str(patch_no).zfill(zpadding), pname)
109 pfile = os.path.join(dirname, pname)
110 print >> series, pname
114 patch_tmpl = options.template
116 patch_tmpl = os.path.join(git.base_dir, 'patchexport.tmpl')
117 if os.path.isfile(patch_tmpl):
118 tmpl = file(patch_tmpl).read()
122 # get the patch description
123 patch = crt_series.get_patch(p)
125 tmpl_dict = {'description': patch.get_description().rstrip(),
126 'diffstat': git.diffstat(rev1 = git_id('%s/bottom' % p),
127 rev2 = git_id('%s/top' % p)),
128 'authname': patch.get_authname(),
129 'authemail': patch.get_authemail(),
130 'authdate': patch.get_authdate(),
131 'commname': patch.get_commname(),
132 'commemail': patch.get_commemail()}
133 for key in tmpl_dict:
134 if not tmpl_dict[key]:
138 descr = tmpl % tmpl_dict
139 except KeyError, err:
140 raise MainException, 'Unknown patch template variable: %s' \
143 raise MainException, 'Only "%(name)s" variables are ' \
144 'supported in the patch template'
145 f = open(pfile, 'w+')
149 git.diff(rev1 = git_id('%s/bottom' % p),
150 rev2 = git_id('%s/top' % p),