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 * | |
1f3bb017 | 26 | from stgit import stack, git, templates |
fcee87cf CM |
27 | |
28 | ||
29 | help = 'exports a series of patches to <dir> (or patches)' | |
8560c67b | 30 | usage = """%prog [options] [<patch1>] [<patch2>] [<patch3>..<patch4>] |
26aab5b0 | 31 | |
8560c67b CM |
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 | |
94d18868 YD |
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 | |
8560c67b | 49 | """ |
26aab5b0 | 50 | |
8560c67b CM |
51 | options = [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 CM |
64 | help = 'use BRANCH instead of the default one'), |
65 | make_option('-s', '--stdout', | |
66 | help = 'dump the patches to the standard output', | |
67 | action = 'store_true')] | |
fcee87cf CM |
68 | |
69 | ||
70 | def func(parser, options, args): | |
8560c67b CM |
71 | """Export a range of patches. |
72 | """ | |
73 | if options.dir: | |
74 | dirname = options.dir | |
fcee87cf | 75 | else: |
8560c67b | 76 | dirname = 'patches-%s' % crt_series.get_branch() |
fcee87cf | 77 | |
2f7c8b0b | 78 | if not options.branch and git.local_changes(): |
fcee87cf CM |
79 | print 'Warning: local changes in the tree. ' \ |
80 | 'You might want to commit them first' | |
81 | ||
1fceece7 CM |
82 | if not options.stdout: |
83 | if not os.path.isdir(dirname): | |
84 | os.makedirs(dirname) | |
85 | series = file(os.path.join(dirname, 'series'), 'w+') | |
fcee87cf CM |
86 | |
87 | applied = crt_series.get_applied() | |
8560c67b CM |
88 | if len(args) != 0: |
89 | patches = parse_patches(args, applied) | |
fcee87cf | 90 | else: |
6b1e0111 | 91 | patches = applied |
fcee87cf CM |
92 | |
93 | num = len(patches) | |
16ad223e PR |
94 | if num == 0: |
95 | raise CmdException, 'No patches applied' | |
96 | ||
fcee87cf CM |
97 | zpadding = len(str(num)) |
98 | if zpadding < 2: | |
99 | zpadding = 2 | |
100 | ||
23a88c7d CM |
101 | # get the template |
102 | if options.template: | |
1f3bb017 | 103 | tmpl = file(options.template).read() |
23a88c7d | 104 | else: |
1f3bb017 CM |
105 | tmpl = templates.get_template('patchexport.tmpl') |
106 | if not tmpl: | |
107 | tmpl = '' | |
23a88c7d | 108 | |
6c4e4b68 | 109 | # note the base commit for this series |
1fceece7 CM |
110 | if not options.stdout: |
111 | base_commit = crt_series.get_patch(patches[0]).get_bottom() | |
112 | print >> series, '# This series applies on GIT commit %s' % base_commit | |
6c4e4b68 | 113 | |
fcee87cf CM |
114 | patch_no = 1; |
115 | for p in patches: | |
116 | pname = p | |
8560c67b | 117 | if options.patch: |
099ff6cd | 118 | pname = '%s.patch' % pname |
8560c67b CM |
119 | elif options.extension: |
120 | pname = '%s.%s' % (pname, options.extension) | |
fcee87cf CM |
121 | if options.numbered: |
122 | pname = '%s-%s' % (str(patch_no).zfill(zpadding), pname) | |
123 | pfile = os.path.join(dirname, pname) | |
1fceece7 CM |
124 | if not options.stdout: |
125 | print >> series, pname | |
fcee87cf | 126 | |
fcee87cf CM |
127 | # get the patch description |
128 | patch = crt_series.get_patch(p) | |
129 | ||
99e73103 CM |
130 | descr = patch.get_description().strip() |
131 | descr_lines = descr.split('\n') | |
132 | ||
133 | short_descr = descr_lines[0].rstrip() | |
134 | long_descr = reduce(lambda x, y: x + '\n' + y, | |
135 | descr_lines[1:], '').strip() | |
136 | ||
fcee87cf | 137 | tmpl_dict = {'description': patch.get_description().rstrip(), |
99e73103 CM |
138 | 'shortdescr': short_descr, |
139 | 'longdescr': long_descr, | |
ed7ec17a CM |
140 | 'diffstat': git.diffstat(rev1 = patch.get_bottom(), |
141 | rev2 = patch.get_top()), | |
fcee87cf CM |
142 | 'authname': patch.get_authname(), |
143 | 'authemail': patch.get_authemail(), | |
144 | 'authdate': patch.get_authdate(), | |
145 | 'commname': patch.get_commname(), | |
146 | 'commemail': patch.get_commemail()} | |
147 | for key in tmpl_dict: | |
148 | if not tmpl_dict[key]: | |
149 | tmpl_dict[key] = '' | |
150 | ||
151 | try: | |
152 | descr = tmpl % tmpl_dict | |
153 | except KeyError, err: | |
8f4d71da | 154 | raise CmdException, 'Unknown patch template variable: %s' \ |
fcee87cf CM |
155 | % err |
156 | except TypeError: | |
8f4d71da | 157 | raise CmdException, 'Only "%(name)s" variables are ' \ |
fcee87cf | 158 | 'supported in the patch template' |
fcee87cf | 159 | |
1fceece7 CM |
160 | if options.stdout: |
161 | f = sys.stdout | |
162 | else: | |
163 | f = open(pfile, 'w+') | |
164 | ||
165 | if options.stdout and num > 1: | |
166 | print '-------------------------------------------------------------------------------' | |
167 | print patch.get_name() | |
168 | print '-------------------------------------------------------------------------------' | |
169 | ||
170 | # write description | |
171 | f.write(descr) | |
fcee87cf | 172 | # write the diff |
ed7ec17a CM |
173 | git.diff(rev1 = patch.get_bottom(), |
174 | rev2 = patch.get_top(), | |
26dba451 | 175 | out_fd = f) |
1fceece7 CM |
176 | if not options.stdout: |
177 | f.close() | |
fcee87cf CM |
178 | patch_no += 1 |
179 | ||
1fceece7 CM |
180 | if not options.stdout: |
181 | series.close() |