"stg export" error message could be more specific
[stgit] / stgit / commands / export.py
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)'
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 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:
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."""
48
49 options = [make_option('-n', '--numbered',
50 help = 'prefix the patch names with order numbers',
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 make_option('-b', '--branch',
61 help = 'use BRANCH instead of the default one')]
62
63
64 def func(parser, options, args):
65 if len(args) == 0:
66 dirname = 'patches-%s' % crt_series.get_branch()
67 elif len(args) == 1:
68 dirname = args[0]
69 else:
70 parser.error('incorrect number of arguments')
71
72 if not options.branch and git.local_changes():
73 print 'Warning: local changes in the tree. ' \
74 'You might want to commit them first'
75
76 if not os.path.isdir(dirname):
77 os.makedirs(dirname)
78 series = file(os.path.join(dirname, 'series'), 'w+')
79
80 applied = crt_series.get_applied()
81
82 if options.range:
83 boundaries = options.range.split(':')
84 if len(boundaries) == 1:
85 start = boundaries[0]
86 stop = boundaries[0]
87 elif len(boundaries) == 2:
88 if boundaries[0] == '':
89 start = applied[0]
90 else:
91 start = boundaries[0]
92 if boundaries[1] == '':
93 stop = applied[-1]
94 else:
95 stop = boundaries[1]
96 else:
97 raise CmdException, 'incorrect parameters to "--range"'
98
99 if start in applied:
100 start_idx = applied.index(start)
101 else:
102 if start in unapplied:
103 raise CmdException, 'Patch "%s" not applied' % start
104 else:
105 raise CmdException, 'Patch "%s" does not exist' % start
106
107 if stop in applied:
108 stop_idx = applied.index(stop) + 1
109 else:
110 if stop in unapplied:
111 raise CmdException, 'Patch "%s" not applied' % stop
112 else:
113 raise CmdException, 'Patch "%s" does not exist' % stop
114
115 if start_idx >= stop_idx:
116 raise CmdException, 'Incorrect patch range order'
117 else:
118 start_idx = 0
119 stop_idx = len(applied)
120
121 patches = applied[start_idx:stop_idx]
122
123 num = len(patches)
124 zpadding = len(str(num))
125 if zpadding < 2:
126 zpadding = 2
127
128 # get the template
129 if options.template:
130 patch_tmpl_list = [options.template]
131 else:
132 patch_tmpl_list = []
133
134 patch_tmpl_list += [os.path.join(git.base_dir, 'patchexport.tmpl'),
135 os.path.join(sys.prefix,
136 'share/stgit/templates/patchexport.tmpl')]
137 tmpl = ''
138 for patch_tmpl in patch_tmpl_list:
139 if os.path.isfile(patch_tmpl):
140 tmpl = file(patch_tmpl).read()
141 break
142
143 patch_no = 1;
144 for p in patches:
145 pname = p
146 if options.diff:
147 pname = '%s.diff' % pname
148 if options.numbered:
149 pname = '%s-%s' % (str(patch_no).zfill(zpadding), pname)
150 pfile = os.path.join(dirname, pname)
151 print >> series, pname
152
153 # get the patch description
154 patch = crt_series.get_patch(p)
155
156 tmpl_dict = {'description': patch.get_description().rstrip(),
157 'diffstat': git.diffstat(rev1 = patch.get_bottom(),
158 rev2 = patch.get_top()),
159 'authname': patch.get_authname(),
160 'authemail': patch.get_authemail(),
161 'authdate': patch.get_authdate(),
162 'commname': patch.get_commname(),
163 'commemail': patch.get_commemail()}
164 for key in tmpl_dict:
165 if not tmpl_dict[key]:
166 tmpl_dict[key] = ''
167
168 try:
169 descr = tmpl % tmpl_dict
170 except KeyError, err:
171 raise CmdException, 'Unknown patch template variable: %s' \
172 % err
173 except TypeError:
174 raise CmdException, 'Only "%(name)s" variables are ' \
175 'supported in the patch template'
176 f = open(pfile, 'w+')
177 f.write(descr)
178
179 # write the diff
180 git.diff(rev1 = patch.get_bottom(),
181 rev2 = patch.get_top(),
182 out_fd = f)
183 f.close()
184 patch_no += 1
185
186 series.close()