Commit | Line | Data |
---|---|---|
fcee87cf CM |
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 * | |
170f576b | 26 | from stgit import stack, git, basedir |
fcee87cf CM |
27 | |
28 | ||
29 | help = 'exports a series of patches to <dir> (or patches)' | |
26aab5b0 CM |
30 | usage = """%prog [options] [<dir>] |
31 | ||
32 | Export the applied patches into a given directory (defaults to | |
23a88c7d | 33 | 'patches') in a standard unified GNU diff format. A template file |
94d18868 YD |
34 | (defaulting to '.git/patchexport.tmpl' or |
35 | '~/.stgit/templates/patchexport.tmpl' or | |
36 | '/usr/share/stgit/templates/patchexport.tmpl') can be used for the | |
23a88c7d CM |
37 | patch format. The following variables are supported in the template |
38 | file: | |
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 | |
49 | ||
50 | 'export' can also generate a diff for a range of patches.""" | |
fcee87cf CM |
51 | |
52 | options = [make_option('-n', '--numbered', | |
26aab5b0 | 53 | help = 'prefix the patch names with order numbers', |
fcee87cf CM |
54 | action = 'store_true'), |
55 | make_option('-d', '--diff', | |
56 | help = 'append .diff to the patch names', | |
57 | action = 'store_true'), | |
099ff6cd CM |
58 | make_option('-p', '--patch', |
59 | help = 'append .patch to the patch names', | |
60 | action = 'store_true'), | |
fcee87cf CM |
61 | make_option('-t', '--template', metavar = 'FILE', |
62 | help = 'Use FILE as a template'), | |
63 | make_option('-r', '--range', | |
64 | metavar = '[PATCH1][:[PATCH2]]', | |
2f7c8b0b CM |
65 | help = 'export patches between PATCH1 and PATCH2'), |
66 | make_option('-b', '--branch', | |
1fceece7 CM |
67 | help = 'use BRANCH instead of the default one'), |
68 | make_option('-s', '--stdout', | |
69 | help = 'dump the patches to the standard output', | |
70 | action = 'store_true')] | |
fcee87cf CM |
71 | |
72 | ||
73 | def func(parser, options, args): | |
74 | if len(args) == 0: | |
629ddd02 | 75 | dirname = 'patches-%s' % crt_series.get_branch() |
fcee87cf CM |
76 | elif len(args) == 1: |
77 | dirname = args[0] | |
78 | else: | |
79 | parser.error('incorrect number of arguments') | |
80 | ||
2f7c8b0b | 81 | if not options.branch and git.local_changes(): |
fcee87cf CM |
82 | print 'Warning: local changes in the tree. ' \ |
83 | 'You might want to commit them first' | |
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() | |
3888eb9a | 91 | unapplied = crt_series.get_unapplied() |
fcee87cf CM |
92 | |
93 | if options.range: | |
94 | boundaries = options.range.split(':') | |
95 | if len(boundaries) == 1: | |
96 | start = boundaries[0] | |
97 | stop = boundaries[0] | |
8f4d71da | 98 | elif len(boundaries) == 2: |
fcee87cf CM |
99 | if boundaries[0] == '': |
100 | start = applied[0] | |
101 | else: | |
102 | start = boundaries[0] | |
103 | if boundaries[1] == '': | |
104 | stop = applied[-1] | |
105 | else: | |
106 | stop = boundaries[1] | |
107 | else: | |
8f4d71da | 108 | raise CmdException, 'incorrect parameters to "--range"' |
fcee87cf CM |
109 | |
110 | if start in applied: | |
111 | start_idx = applied.index(start) | |
112 | else: | |
4dcb1859 CL |
113 | if start in unapplied: |
114 | raise CmdException, 'Patch "%s" not applied' % start | |
115 | else: | |
116 | raise CmdException, 'Patch "%s" does not exist' % start | |
117 | ||
fcee87cf CM |
118 | if stop in applied: |
119 | stop_idx = applied.index(stop) + 1 | |
120 | else: | |
4dcb1859 CL |
121 | if stop in unapplied: |
122 | raise CmdException, 'Patch "%s" not applied' % stop | |
123 | else: | |
124 | raise CmdException, 'Patch "%s" does not exist' % stop | |
fcee87cf CM |
125 | |
126 | if start_idx >= stop_idx: | |
8f4d71da | 127 | raise CmdException, 'Incorrect patch range order' |
fcee87cf CM |
128 | else: |
129 | start_idx = 0 | |
b054c8bd | 130 | stop_idx = len(applied) |
fcee87cf CM |
131 | |
132 | patches = applied[start_idx:stop_idx] | |
133 | ||
134 | num = len(patches) | |
16ad223e PR |
135 | if num == 0: |
136 | raise CmdException, 'No patches applied' | |
137 | ||
fcee87cf CM |
138 | zpadding = len(str(num)) |
139 | if zpadding < 2: | |
140 | zpadding = 2 | |
141 | ||
23a88c7d CM |
142 | # get the template |
143 | if options.template: | |
144 | patch_tmpl_list = [options.template] | |
145 | else: | |
146 | patch_tmpl_list = [] | |
147 | ||
170f576b | 148 | patch_tmpl_list += [os.path.join(basedir.get(), 'patchexport.tmpl'), |
94d18868 YD |
149 | os.path.join(os.path.expanduser('~'), '.stgit', 'templates', |
150 | 'patchexport.tmpl'), | |
23a88c7d | 151 | os.path.join(sys.prefix, |
94d18868 | 152 | 'share', 'stgit', 'templates', 'patchexport.tmpl')] |
23a88c7d CM |
153 | tmpl = '' |
154 | for patch_tmpl in patch_tmpl_list: | |
155 | if os.path.isfile(patch_tmpl): | |
156 | tmpl = file(patch_tmpl).read() | |
157 | break | |
158 | ||
6c4e4b68 | 159 | # note the base commit for this series |
1fceece7 CM |
160 | if not options.stdout: |
161 | base_commit = crt_series.get_patch(patches[0]).get_bottom() | |
162 | print >> series, '# This series applies on GIT commit %s' % base_commit | |
6c4e4b68 | 163 | |
fcee87cf CM |
164 | patch_no = 1; |
165 | for p in patches: | |
166 | pname = p | |
167 | if options.diff: | |
168 | pname = '%s.diff' % pname | |
099ff6cd CM |
169 | elif options.patch: |
170 | pname = '%s.patch' % pname | |
fcee87cf CM |
171 | if options.numbered: |
172 | pname = '%s-%s' % (str(patch_no).zfill(zpadding), pname) | |
173 | pfile = os.path.join(dirname, pname) | |
1fceece7 CM |
174 | if not options.stdout: |
175 | print >> series, pname | |
fcee87cf | 176 | |
fcee87cf CM |
177 | # get the patch description |
178 | patch = crt_series.get_patch(p) | |
179 | ||
99e73103 CM |
180 | descr = patch.get_description().strip() |
181 | descr_lines = descr.split('\n') | |
182 | ||
183 | short_descr = descr_lines[0].rstrip() | |
184 | long_descr = reduce(lambda x, y: x + '\n' + y, | |
185 | descr_lines[1:], '').strip() | |
186 | ||
fcee87cf | 187 | tmpl_dict = {'description': patch.get_description().rstrip(), |
99e73103 CM |
188 | 'shortdescr': short_descr, |
189 | 'longdescr': long_descr, | |
ed7ec17a CM |
190 | 'diffstat': git.diffstat(rev1 = patch.get_bottom(), |
191 | rev2 = patch.get_top()), | |
fcee87cf CM |
192 | 'authname': patch.get_authname(), |
193 | 'authemail': patch.get_authemail(), | |
194 | 'authdate': patch.get_authdate(), | |
195 | 'commname': patch.get_commname(), | |
196 | 'commemail': patch.get_commemail()} | |
197 | for key in tmpl_dict: | |
198 | if not tmpl_dict[key]: | |
199 | tmpl_dict[key] = '' | |
200 | ||
201 | try: | |
202 | descr = tmpl % tmpl_dict | |
203 | except KeyError, err: | |
8f4d71da | 204 | raise CmdException, 'Unknown patch template variable: %s' \ |
fcee87cf CM |
205 | % err |
206 | except TypeError: | |
8f4d71da | 207 | raise CmdException, 'Only "%(name)s" variables are ' \ |
fcee87cf | 208 | 'supported in the patch template' |
fcee87cf | 209 | |
1fceece7 CM |
210 | if options.stdout: |
211 | f = sys.stdout | |
212 | else: | |
213 | f = open(pfile, 'w+') | |
214 | ||
215 | if options.stdout and num > 1: | |
216 | print '-------------------------------------------------------------------------------' | |
217 | print patch.get_name() | |
218 | print '-------------------------------------------------------------------------------' | |
219 | ||
220 | # write description | |
221 | f.write(descr) | |
fcee87cf | 222 | # write the diff |
ed7ec17a CM |
223 | git.diff(rev1 = patch.get_bottom(), |
224 | rev2 = patch.get_top(), | |
26dba451 | 225 | out_fd = f) |
1fceece7 CM |
226 | if not options.stdout: |
227 | f.close() | |
fcee87cf CM |
228 | patch_no += 1 |
229 | ||
1fceece7 CM |
230 | if not options.stdout: |
231 | series.close() |