Use git-rev-parse to find the local GIT repository
[stgit] / stgit / commands / export.py
CommitLineData
fcee87cf
CM
1"""Export command
2"""
3
4__copyright__ = """
5Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License version 2 as
9published by the Free Software Foundation.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19"""
20
21import sys, os
22from optparse import OptionParser, make_option
23
24from stgit.commands.common import *
25from stgit.utils import *
26from stgit import stack, git
27
28
29help = 'exports a series of patches to <dir> (or patches)'
26aab5b0
CM
30usage = """%prog [options] [<dir>]
31
32Export 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
36patch format. The following variables are supported in the template
37file:
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
49options = [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]]',
2f7c8b0b
CM
59 help = 'export patches between PATCH1 and PATCH2'),
60 make_option('-b', '--branch',
61 help = 'use BRANCH instead of the default one')]
fcee87cf
CM
62
63
64def func(parser, options, args):
65 if len(args) == 0:
629ddd02 66 dirname = 'patches-%s' % crt_series.get_branch()
fcee87cf
CM
67 elif len(args) == 1:
68 dirname = args[0]
69 else:
70 parser.error('incorrect number of arguments')
71
2f7c8b0b 72 if not options.branch and git.local_changes():
fcee87cf
CM
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()
3888eb9a 81 unapplied = crt_series.get_unapplied()
fcee87cf
CM
82
83 if options.range:
84 boundaries = options.range.split(':')
85 if len(boundaries) == 1:
86 start = boundaries[0]
87 stop = boundaries[0]
8f4d71da 88 elif len(boundaries) == 2:
fcee87cf
CM
89 if boundaries[0] == '':
90 start = applied[0]
91 else:
92 start = boundaries[0]
93 if boundaries[1] == '':
94 stop = applied[-1]
95 else:
96 stop = boundaries[1]
97 else:
8f4d71da 98 raise CmdException, 'incorrect parameters to "--range"'
fcee87cf
CM
99
100 if start in applied:
101 start_idx = applied.index(start)
102 else:
4dcb1859
CL
103 if start in unapplied:
104 raise CmdException, 'Patch "%s" not applied' % start
105 else:
106 raise CmdException, 'Patch "%s" does not exist' % start
107
fcee87cf
CM
108 if stop in applied:
109 stop_idx = applied.index(stop) + 1
110 else:
4dcb1859
CL
111 if stop in unapplied:
112 raise CmdException, 'Patch "%s" not applied' % stop
113 else:
114 raise CmdException, 'Patch "%s" does not exist' % stop
fcee87cf
CM
115
116 if start_idx >= stop_idx:
8f4d71da 117 raise CmdException, 'Incorrect patch range order'
fcee87cf
CM
118 else:
119 start_idx = 0
b054c8bd 120 stop_idx = len(applied)
fcee87cf
CM
121
122 patches = applied[start_idx:stop_idx]
123
124 num = len(patches)
125 zpadding = len(str(num))
126 if zpadding < 2:
127 zpadding = 2
128
23a88c7d
CM
129 # get the template
130 if options.template:
131 patch_tmpl_list = [options.template]
132 else:
133 patch_tmpl_list = []
134
bae29ddd 135 patch_tmpl_list += [os.path.join(git.get_base_dir(), 'patchexport.tmpl'),
23a88c7d
CM
136 os.path.join(sys.prefix,
137 'share/stgit/templates/patchexport.tmpl')]
138 tmpl = ''
139 for patch_tmpl in patch_tmpl_list:
140 if os.path.isfile(patch_tmpl):
141 tmpl = file(patch_tmpl).read()
142 break
143
fcee87cf
CM
144 patch_no = 1;
145 for p in patches:
146 pname = p
147 if options.diff:
148 pname = '%s.diff' % pname
149 if options.numbered:
150 pname = '%s-%s' % (str(patch_no).zfill(zpadding), pname)
151 pfile = os.path.join(dirname, pname)
152 print >> series, pname
153
fcee87cf
CM
154 # get the patch description
155 patch = crt_series.get_patch(p)
156
157 tmpl_dict = {'description': patch.get_description().rstrip(),
ed7ec17a
CM
158 'diffstat': git.diffstat(rev1 = patch.get_bottom(),
159 rev2 = patch.get_top()),
fcee87cf
CM
160 'authname': patch.get_authname(),
161 'authemail': patch.get_authemail(),
162 'authdate': patch.get_authdate(),
163 'commname': patch.get_commname(),
164 'commemail': patch.get_commemail()}
165 for key in tmpl_dict:
166 if not tmpl_dict[key]:
167 tmpl_dict[key] = ''
168
169 try:
170 descr = tmpl % tmpl_dict
171 except KeyError, err:
8f4d71da 172 raise CmdException, 'Unknown patch template variable: %s' \
fcee87cf
CM
173 % err
174 except TypeError:
8f4d71da 175 raise CmdException, 'Only "%(name)s" variables are ' \
fcee87cf
CM
176 'supported in the patch template'
177 f = open(pfile, 'w+')
178 f.write(descr)
fcee87cf
CM
179
180 # write the diff
ed7ec17a
CM
181 git.diff(rev1 = patch.get_bottom(),
182 rev2 = patch.get_top(),
26dba451
BL
183 out_fd = f)
184 f.close()
fcee87cf
CM
185 patch_no += 1
186
187 series.close()