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