Emacs mode: automatically cd up to root of worktree
[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 sys, os
22 from optparse import OptionParser, make_option
23
24 from stgit.commands.common import *
25 from stgit.utils import *
26 from stgit.out import *
27 from stgit import stack, git, templates
28
29
30 help = 'exports patches to a directory'
31 usage = """%prog [options] [<patch1>] [<patch2>] [<patch3>..<patch4>]
32
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
39 file:
40
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
50 """
51
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('-s', '--stdout',
68 help = 'dump the patches to the standard output',
69 action = 'store_true')
70 ] + make_diff_opts_option()
71
72
73 def func(parser, options, args):
74 """Export a range of patches.
75 """
76 if options.dir:
77 dirname = options.dir
78 else:
79 dirname = 'patches-%s' % crt_series.get_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 = crt_series.get_applied()
92 if len(args) != 0:
93 patches = parse_patches(args, applied)
94 else:
95 patches = applied
96
97 num = len(patches)
98 if num == 0:
99 raise CmdException, 'No patches applied'
100
101 zpadding = len(str(num))
102 if zpadding < 2:
103 zpadding = 2
104
105 # get the template
106 if options.template:
107 tmpl = file(options.template).read()
108 else:
109 tmpl = templates.get_template('patchexport.tmpl')
110 if not tmpl:
111 tmpl = ''
112
113 # note the base commit for this series
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
117
118 patch_no = 1;
119 for p in patches:
120 pname = p
121 if options.patch:
122 pname = '%s.patch' % pname
123 elif options.extension:
124 pname = '%s.%s' % (pname, options.extension)
125 if options.numbered:
126 pname = '%s-%s' % (str(patch_no).zfill(zpadding), pname)
127 pfile = os.path.join(dirname, pname)
128 if not options.stdout:
129 print >> series, pname
130
131 # get the patch description
132 patch = crt_series.get_patch(p)
133
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
141 diff = git.diff(rev1 = patch.get_bottom(),
142 rev2 = patch.get_top(),
143 diff_flags = options.diff_flags)
144 tmpl_dict = {'description': patch.get_description().rstrip(),
145 'shortdescr': short_descr,
146 'longdescr': long_descr,
147 'diffstat': git.diffstat(diff),
148 'authname': patch.get_authname(),
149 'authemail': patch.get_authemail(),
150 'authdate': patch.get_authdate(),
151 'commname': patch.get_commname(),
152 'commemail': patch.get_commemail()}
153 for key in tmpl_dict:
154 if not tmpl_dict[key]:
155 tmpl_dict[key] = ''
156
157 try:
158 descr = tmpl % tmpl_dict
159 except KeyError, err:
160 raise CmdException, 'Unknown patch template variable: %s' \
161 % err
162 except TypeError:
163 raise CmdException, 'Only "%(name)s" variables are ' \
164 'supported in the patch template'
165
166 if options.stdout:
167 f = sys.stdout
168 else:
169 f = open(pfile, 'w+')
170
171 if options.stdout and num > 1:
172 print '-'*79
173 print patch.get_name()
174 print '-'*79
175
176 f.write(descr)
177 f.write(diff)
178 if not options.stdout:
179 f.close()
180 patch_no += 1
181
182 if not options.stdout:
183 series.close()