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