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 | |
40 | %(diffstat)s - the diff statistics | |
41 | %(authname)s - author's name | |
42 | %(authemail)s - author's e-mail | |
43 | %(authdate)s - patch creation date | |
44 | %(commname)s - committer's name | |
45 | %(commemail)s - committer's e-mail | |
46 | ||
47 | 'export' can also generate a diff for a range of patches.""" | |
fcee87cf CM |
48 | |
49 | options = [make_option('-n', '--numbered', | |
26aab5b0 | 50 | help = 'prefix the patch names with order numbers', |
fcee87cf CM |
51 | action = 'store_true'), |
52 | make_option('-d', '--diff', | |
53 | help = 'append .diff to the patch names', | |
54 | action = 'store_true'), | |
55 | make_option('-t', '--template', metavar = 'FILE', | |
56 | help = 'Use FILE as a template'), | |
57 | make_option('-r', '--range', | |
58 | metavar = '[PATCH1][:[PATCH2]]', | |
59 | help = 'export patches between PATCH1 and PATCH2')] | |
60 | ||
61 | ||
62 | def func(parser, options, args): | |
63 | if len(args) == 0: | |
64 | dirname = 'patches' | |
65 | elif len(args) == 1: | |
66 | dirname = args[0] | |
67 | else: | |
68 | parser.error('incorrect number of arguments') | |
69 | ||
70 | if git.local_changes(): | |
71 | print 'Warning: local changes in the tree. ' \ | |
72 | 'You might want to commit them first' | |
73 | ||
74 | if not os.path.isdir(dirname): | |
75 | os.makedirs(dirname) | |
76 | series = file(os.path.join(dirname, 'series'), 'w+') | |
77 | ||
78 | applied = crt_series.get_applied() | |
79 | ||
80 | if options.range: | |
81 | boundaries = options.range.split(':') | |
82 | if len(boundaries) == 1: | |
83 | start = boundaries[0] | |
84 | stop = boundaries[0] | |
8f4d71da | 85 | elif len(boundaries) == 2: |
fcee87cf CM |
86 | if boundaries[0] == '': |
87 | start = applied[0] | |
88 | else: | |
89 | start = boundaries[0] | |
90 | if boundaries[1] == '': | |
91 | stop = applied[-1] | |
92 | else: | |
93 | stop = boundaries[1] | |
94 | else: | |
8f4d71da | 95 | raise CmdException, 'incorrect parameters to "--range"' |
fcee87cf CM |
96 | |
97 | if start in applied: | |
98 | start_idx = applied.index(start) | |
99 | else: | |
8f4d71da | 100 | raise CmdException, 'Patch "%s" not applied' % start |
fcee87cf CM |
101 | if stop in applied: |
102 | stop_idx = applied.index(stop) + 1 | |
103 | else: | |
8f4d71da | 104 | raise CmdException, 'Patch "%s" not applied' % stop |
fcee87cf CM |
105 | |
106 | if start_idx >= stop_idx: | |
8f4d71da | 107 | raise CmdException, 'Incorrect patch range order' |
fcee87cf CM |
108 | else: |
109 | start_idx = 0 | |
b054c8bd | 110 | stop_idx = len(applied) |
fcee87cf CM |
111 | |
112 | patches = applied[start_idx:stop_idx] | |
113 | ||
114 | num = len(patches) | |
115 | zpadding = len(str(num)) | |
116 | if zpadding < 2: | |
117 | zpadding = 2 | |
118 | ||
23a88c7d CM |
119 | # get the template |
120 | if options.template: | |
121 | patch_tmpl_list = [options.template] | |
122 | else: | |
123 | patch_tmpl_list = [] | |
124 | ||
125 | patch_tmpl_list += [os.path.join(git.base_dir, 'patchexport.tmpl'), | |
126 | os.path.join(sys.prefix, | |
127 | 'share/stgit/templates/patchexport.tmpl')] | |
128 | tmpl = '' | |
129 | for patch_tmpl in patch_tmpl_list: | |
130 | if os.path.isfile(patch_tmpl): | |
131 | tmpl = file(patch_tmpl).read() | |
132 | break | |
133 | ||
fcee87cf CM |
134 | patch_no = 1; |
135 | for p in patches: | |
136 | pname = p | |
137 | if options.diff: | |
138 | pname = '%s.diff' % pname | |
139 | if options.numbered: | |
140 | pname = '%s-%s' % (str(patch_no).zfill(zpadding), pname) | |
141 | pfile = os.path.join(dirname, pname) | |
142 | print >> series, pname | |
143 | ||
fcee87cf CM |
144 | # get the patch description |
145 | patch = crt_series.get_patch(p) | |
146 | ||
147 | tmpl_dict = {'description': patch.get_description().rstrip(), | |
148 | 'diffstat': git.diffstat(rev1 = git_id('%s/bottom' % p), | |
149 | rev2 = git_id('%s/top' % p)), | |
150 | 'authname': patch.get_authname(), | |
151 | 'authemail': patch.get_authemail(), | |
152 | 'authdate': patch.get_authdate(), | |
153 | 'commname': patch.get_commname(), | |
154 | 'commemail': patch.get_commemail()} | |
155 | for key in tmpl_dict: | |
156 | if not tmpl_dict[key]: | |
157 | tmpl_dict[key] = '' | |
158 | ||
159 | try: | |
160 | descr = tmpl % tmpl_dict | |
161 | except KeyError, err: | |
8f4d71da | 162 | raise CmdException, 'Unknown patch template variable: %s' \ |
fcee87cf CM |
163 | % err |
164 | except TypeError: | |
8f4d71da | 165 | raise CmdException, 'Only "%(name)s" variables are ' \ |
fcee87cf CM |
166 | 'supported in the patch template' |
167 | f = open(pfile, 'w+') | |
168 | f.write(descr) | |
fcee87cf CM |
169 | |
170 | # write the diff | |
171 | git.diff(rev1 = git_id('%s/bottom' % p), | |
172 | rev2 = git_id('%s/top' % p), | |
26dba451 BL |
173 | out_fd = f) |
174 | f.close() | |
fcee87cf CM |
175 | patch_no += 1 |
176 | ||
177 | series.close() |