183a1b570cf1cad1e1bcddd27242ca0314782f2d
[stgit] / stgit / commands / export.py
1 """Export command
2 """
3
4 __copyright__ = """
5 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
6
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.
10
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.
15
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
19 """
20
21 import os
22 from optparse import make_option
23
24 from stgit.commands import common
25 from stgit import git, utils, templates
26 from stgit.out import out
27 from stgit.lib import git as gitlib
28
29 help = 'exports patches to a directory'
30 usage = """%prog [options] [<patch1>] [<patch2>] [<patch3>..<patch4>]
31
32 Export a range of applied patches to a given directory (defaults to
33 'patches-<branch>') in a standard unified GNU diff format. A template
34 file (defaulting to '.git/patchexport.tmpl' or
35 '~/.stgit/templates/patchexport.tmpl' or
36 '/usr/share/stgit/templates/patchexport.tmpl') can be used for the
37 patch format. The following variables are supported in the template
38 file:
39
40 %(description)s - patch description
41 %(shortdescr)s - the first line of the patch description
42 %(longdescr)s - the rest of the patch description, after the first line
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
49 """
50
51 directory = common.DirectoryHasRepositoryLib()
52 options = [make_option('-d', '--dir',
53 help = 'export patches to DIR instead of the default'),
54 make_option('-p', '--patch',
55 help = 'append .patch to the patch names',
56 action = 'store_true'),
57 make_option('-e', '--extension',
58 help = 'append .EXTENSION to the patch names'),
59 make_option('-n', '--numbered',
60 help = 'prefix the patch names with order numbers',
61 action = 'store_true'),
62 make_option('-t', '--template', metavar = 'FILE',
63 help = 'Use FILE as a template'),
64 make_option('-b', '--branch',
65 help = 'use BRANCH instead of the default one'),
66 make_option('-s', '--stdout',
67 help = 'dump the patches to the standard output',
68 action = 'store_true')
69 ] + utils.make_diff_opts_option()
70
71 def func(parser, options, args):
72 """Export a range of patches.
73 """
74 stack = directory.repository.get_stack(options.branch)
75
76 if options.dir:
77 dirname = options.dir
78 else:
79 dirname = 'patches-%s' % stack.name
80 directory.cd_to_topdir()
81
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')
85
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+')
90
91 applied = stack.patchorder.applied
92 unapplied = stack.patchorder.unapplied
93 if len(args) != 0:
94 patches = common.parse_patches(args, applied + unapplied, len(applied))
95 else:
96 patches = applied
97
98 num = len(patches)
99 if num == 0:
100 raise common.CmdException, 'No patches applied'
101
102 zpadding = len(str(num))
103 if zpadding < 2:
104 zpadding = 2
105
106 # get the template
107 if options.template:
108 tmpl = file(options.template).read()
109 else:
110 tmpl = templates.get_template('patchexport.tmpl')
111 if not tmpl:
112 tmpl = ''
113
114 # note the base commit for this series
115 if not options.stdout:
116 base_commit = stack.patches.get(patches[0]).commit.sha1
117 print >> series, '# This series applies on GIT commit %s' % base_commit
118
119 patch_no = 1;
120 for p in patches:
121 pname = p
122 if options.patch:
123 pname = '%s.patch' % pname
124 elif options.extension:
125 pname = '%s.%s' % (pname, options.extension)
126 if options.numbered:
127 pname = '%s-%s' % (str(patch_no).zfill(zpadding), pname)
128 pfile = os.path.join(dirname, pname)
129 if not options.stdout:
130 print >> series, pname
131
132 # get the patch description
133 patch = stack.patches.get(p)
134 cd = patch.commit.data
135
136 descr = cd.message.strip()
137 descr_lines = descr.split('\n')
138
139 short_descr = descr_lines[0].rstrip()
140 long_descr = reduce(lambda x, y: x + '\n' + y,
141 descr_lines[1:], '').strip()
142
143 diff = stack.repository.diff_tree(cd.parent.data.tree, cd.tree, options.diff_flags)
144
145 tmpl_dict = {'description': descr,
146 'shortdescr': short_descr,
147 'longdescr': long_descr,
148 'diffstat': git.diffstat(diff).rstrip(),
149 'authname': cd.author.name,
150 'authemail': cd.author.email,
151 'authdate': cd.author.date.isoformat(),
152 'commname': cd.committer.name,
153 'commemail': cd.committer.email}
154 for key in tmpl_dict:
155 if not tmpl_dict[key]:
156 tmpl_dict[key] = ''
157
158 try:
159 descr = tmpl % tmpl_dict
160 except KeyError, err:
161 raise common.CmdException, 'Unknown patch template variable: %s' \
162 % err
163 except TypeError:
164 raise common.CmdException, 'Only "%(name)s" variables are ' \
165 'supported in the patch template'
166
167 if options.stdout:
168 f = sys.stdout
169 else:
170 f = open(pfile, 'w+')
171
172 if options.stdout and num > 1:
173 print '-'*79
174 print patch.name
175 print '-'*79
176
177 f.write(descr)
178 f.write(diff)
179 if not options.stdout:
180 f.close()
181 patch_no += 1
182
183 if not options.stdout:
184 series.close()