Simplify editor selection logic
[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 *
5e888f30 26from stgit.out import *
1f3bb017 27from stgit import stack, git, templates
fcee87cf
CM
28
29
4ec67741 30help = 'exports patches to a directory'
8560c67b 31usage = """%prog [options] [<patch1>] [<patch2>] [<patch3>..<patch4>]
26aab5b0 32
8560c67b
CM
33Export a range of applied patches to a given directory (defaults to
34'patches-<branch>') in a standard unified GNU diff format. A template
35file (defaulting to '.git/patchexport.tmpl' or
94d18868
YD
36'~/.stgit/templates/patchexport.tmpl' or
37'/usr/share/stgit/templates/patchexport.tmpl') can be used for the
23a88c7d
CM
38patch format. The following variables are supported in the template
39file:
26aab5b0
CM
40
41 %(description)s - patch description
99e73103
CM
42 %(shortdescr)s - the first line of the patch description
43 %(longdescr)s - the rest of the patch description, after the first line
26aab5b0
CM
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
8560c67b 50"""
26aab5b0 51
6dd8fafa 52directory = DirectoryHasRepository()
8560c67b
CM
53options = [make_option('-d', '--dir',
54 help = 'export patches to DIR instead of the default'),
099ff6cd
CM
55 make_option('-p', '--patch',
56 help = 'append .patch to the patch names',
57 action = 'store_true'),
8560c67b
CM
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'),
fcee87cf
CM
63 make_option('-t', '--template', metavar = 'FILE',
64 help = 'Use FILE as a template'),
2f7c8b0b 65 make_option('-b', '--branch',
1fceece7
CM
66 help = 'use BRANCH instead of the default one'),
67 make_option('-s', '--stdout',
68 help = 'dump the patches to the standard output',
0b635fe6
KH
69 action = 'store_true')
70 ] + make_diff_opts_option()
fcee87cf
CM
71
72
73def func(parser, options, args):
8560c67b
CM
74 """Export a range of patches.
75 """
76 if options.dir:
77 dirname = options.dir
fcee87cf 78 else:
d37ff079 79 dirname = 'patches-%s' % crt_series.get_name()
7b601c9e 80 directory.cd_to_topdir()
fcee87cf 81
2f7c8b0b 82 if not options.branch and git.local_changes():
27ac2b7e
KH
83 out.warn('Local changes in the tree;'
84 ' you might want to commit them first')
fcee87cf 85
1fceece7
CM
86 if not options.stdout:
87 if not os.path.isdir(dirname):
88 os.makedirs(dirname)
89 series = file(os.path.join(dirname, 'series'), 'w+')
fcee87cf
CM
90
91 applied = crt_series.get_applied()
8560c67b
CM
92 if len(args) != 0:
93 patches = parse_patches(args, applied)
fcee87cf 94 else:
6b1e0111 95 patches = applied
fcee87cf
CM
96
97 num = len(patches)
16ad223e
PR
98 if num == 0:
99 raise CmdException, 'No patches applied'
100
fcee87cf
CM
101 zpadding = len(str(num))
102 if zpadding < 2:
103 zpadding = 2
104
23a88c7d
CM
105 # get the template
106 if options.template:
1f3bb017 107 tmpl = file(options.template).read()
23a88c7d 108 else:
1f3bb017
CM
109 tmpl = templates.get_template('patchexport.tmpl')
110 if not tmpl:
111 tmpl = ''
23a88c7d 112
6c4e4b68 113 # note the base commit for this series
1fceece7
CM
114 if not options.stdout:
115 base_commit = crt_series.get_patch(patches[0]).get_bottom()
116 print >> series, '# This series applies on GIT commit %s' % base_commit
6c4e4b68 117
fcee87cf
CM
118 patch_no = 1;
119 for p in patches:
120 pname = p
8560c67b 121 if options.patch:
099ff6cd 122 pname = '%s.patch' % pname
8560c67b
CM
123 elif options.extension:
124 pname = '%s.%s' % (pname, options.extension)
fcee87cf
CM
125 if options.numbered:
126 pname = '%s-%s' % (str(patch_no).zfill(zpadding), pname)
127 pfile = os.path.join(dirname, pname)
1fceece7
CM
128 if not options.stdout:
129 print >> series, pname
fcee87cf 130
fcee87cf
CM
131 # get the patch description
132 patch = crt_series.get_patch(p)
133
99e73103
CM
134 descr = patch.get_description().strip()
135 descr_lines = descr.split('\n')
136
137 short_descr = descr_lines[0].rstrip()
138 long_descr = reduce(lambda x, y: x + '\n' + y,
139 descr_lines[1:], '').strip()
140
fcee87cf 141 tmpl_dict = {'description': patch.get_description().rstrip(),
99e73103
CM
142 'shortdescr': short_descr,
143 'longdescr': long_descr,
ed7ec17a
CM
144 'diffstat': git.diffstat(rev1 = patch.get_bottom(),
145 rev2 = patch.get_top()),
fcee87cf
CM
146 'authname': patch.get_authname(),
147 'authemail': patch.get_authemail(),
148 'authdate': patch.get_authdate(),
149 'commname': patch.get_commname(),
150 'commemail': patch.get_commemail()}
151 for key in tmpl_dict:
152 if not tmpl_dict[key]:
153 tmpl_dict[key] = ''
154
155 try:
156 descr = tmpl % tmpl_dict
157 except KeyError, err:
8f4d71da 158 raise CmdException, 'Unknown patch template variable: %s' \
fcee87cf
CM
159 % err
160 except TypeError:
8f4d71da 161 raise CmdException, 'Only "%(name)s" variables are ' \
fcee87cf 162 'supported in the patch template'
fcee87cf 163
1fceece7
CM
164 if options.stdout:
165 f = sys.stdout
166 else:
167 f = open(pfile, 'w+')
168
169 if options.stdout and num > 1:
27ac2b7e 170 print '-'*79
1fceece7 171 print patch.get_name()
27ac2b7e 172 print '-'*79
1fceece7 173
1fceece7 174 f.write(descr)
f0de3f92
KH
175 f.write(git.diff(rev1 = patch.get_bottom(),
176 rev2 = patch.get_top(),
0b635fe6 177 diff_flags = options.diff_flags))
1fceece7
CM
178 if not options.stdout:
179 f.close()
fcee87cf
CM
180 patch_no += 1
181
1fceece7
CM
182 if not options.stdout:
183 series.close()