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