Use the ".." syntax for patch ranges
[stgit] / stgit / commands / common.py
1 """Function/variables common to all the commands
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, os.path, re
22 from optparse import OptionParser, make_option
23
24 from stgit.utils import *
25 from stgit import stack, git, basedir
26 from stgit.config import config, file_extensions
27
28 crt_series = None
29
30
31 # Command exception class
32 class CmdException(Exception):
33 pass
34
35
36 # Utility functions
37 class RevParseException(Exception):
38 """Revision spec parse error."""
39 pass
40
41 def parse_rev(rev):
42 """Parse a revision specification into its
43 patchname@branchname//patch_id parts. If no branch name has a slash
44 in it, also accept / instead of //."""
45 files, dirs = list_files_and_dirs(os.path.join(basedir.get(),
46 'refs', 'heads'))
47 if len(dirs) != 0:
48 # We have branch names with / in them.
49 branch_chars = r'[^@]'
50 patch_id_mark = r'//'
51 else:
52 # No / in branch names.
53 branch_chars = r'[^@/]'
54 patch_id_mark = r'(/|//)'
55 patch_re = r'(?P<patch>[^@/]+)'
56 branch_re = r'@(?P<branch>%s+)' % branch_chars
57 patch_id_re = r'%s(?P<patch_id>[a-z.]*)' % patch_id_mark
58
59 # Try //patch_id.
60 m = re.match(r'^%s$' % patch_id_re, rev)
61 if m:
62 return None, None, m.group('patch_id')
63
64 # Try path[@branch]//patch_id.
65 m = re.match(r'^%s(%s)?%s$' % (patch_re, branch_re, patch_id_re), rev)
66 if m:
67 return m.group('patch'), m.group('branch'), m.group('patch_id')
68
69 # Try patch[@branch].
70 m = re.match(r'^%s(%s)?$' % (patch_re, branch_re), rev)
71 if m:
72 return m.group('patch'), m.group('branch'), None
73
74 # No, we can't parse that.
75 raise RevParseException
76
77 def git_id(rev):
78 """Return the GIT id
79 """
80 if not rev:
81 return None
82 try:
83 patch, branch, patch_id = parse_rev(rev)
84 if branch == None:
85 series = crt_series
86 else:
87 series = stack.Series(branch)
88 if patch == None:
89 patch = series.get_current()
90 if not patch:
91 raise CmdException, 'No patches applied'
92 if patch in series.get_applied() or patch in series.get_unapplied():
93 if patch_id in ['top', '', None]:
94 return series.get_patch(patch).get_top()
95 elif patch_id == 'bottom':
96 return series.get_patch(patch).get_bottom()
97 elif patch_id == 'top.old':
98 return series.get_patch(patch).get_old_top()
99 elif patch_id == 'bottom.old':
100 return series.get_patch(patch).get_old_bottom()
101 if patch == 'base' and patch_id == None:
102 return read_string(series.get_base_file())
103 except RevParseException:
104 pass
105 return git.rev_parse(rev + '^{commit}')
106
107 def check_local_changes():
108 if git.local_changes():
109 raise CmdException, \
110 'local changes in the tree. Use "refresh" to commit them'
111
112 def check_head_top_equal():
113 if not crt_series.head_top_equal():
114 raise CmdException, \
115 'HEAD and top are not the same. You probably committed\n' \
116 ' changes to the tree outside of StGIT. If you know what you\n' \
117 ' are doing, use the "refresh -f" command'
118
119 def check_conflicts():
120 if os.path.exists(os.path.join(basedir.get(), 'conflicts')):
121 raise CmdException, 'Unsolved conflicts. Please resolve them first'
122
123 def print_crt_patch(branch = None):
124 if not branch:
125 patch = crt_series.get_current()
126 else:
127 patch = stack.Series(branch).get_current()
128
129 if patch:
130 print 'Now at patch "%s"' % patch
131 else:
132 print 'No patches applied'
133
134 def resolved(filename, reset = None):
135 if reset:
136 reset_file = filename + file_extensions()[reset]
137 if os.path.isfile(reset_file):
138 if os.path.isfile(filename):
139 os.remove(filename)
140 os.rename(reset_file, filename)
141
142 git.update_cache([filename], force = True)
143
144 for ext in file_extensions().values():
145 fn = filename + ext
146 if os.path.isfile(fn):
147 os.remove(fn)
148
149 def resolved_all(reset = None):
150 conflicts = git.get_conflicts()
151 if conflicts:
152 for filename in conflicts:
153 resolved(filename, reset)
154 os.remove(os.path.join(basedir.get(), 'conflicts'))
155
156 def push_patches(patches, check_merged = False):
157 """Push multiple patches onto the stack. This function is shared
158 between the push and pull commands
159 """
160 forwarded = crt_series.forward_patches(patches)
161 if forwarded > 1:
162 print 'Fast-forwarded patches "%s" - "%s"' % (patches[0],
163 patches[forwarded - 1])
164 elif forwarded == 1:
165 print 'Fast-forwarded patch "%s"' % patches[0]
166
167 names = patches[forwarded:]
168
169 # check for patches merged upstream
170 if check_merged:
171 print 'Checking for patches merged upstream...',
172 sys.stdout.flush()
173
174 merged = crt_series.merged_patches(names)
175
176 print 'done (%d found)' % len(merged)
177 else:
178 merged = []
179
180 for p in names:
181 print 'Pushing patch "%s"...' % p,
182 sys.stdout.flush()
183
184 if p in merged:
185 crt_series.push_patch(p, empty = True)
186 print 'done (merged upstream)'
187 else:
188 modified = crt_series.push_patch(p)
189
190 if crt_series.empty_patch(p):
191 print 'done (empty patch)'
192 elif modified:
193 print 'done (modified)'
194 else:
195 print 'done'
196
197 def pop_patches(patches):
198 """Pop the patches in the list from the stack. It is assumed that
199 the patches are listed in the stack reverse order.
200 """
201 p = patches[-1]
202 if len(patches) == 1:
203 print 'Popping patch "%s"...' % p,
204 else:
205 print 'Popping "%s" - "%s" patches...' % (patches[0], p),
206 sys.stdout.flush()
207
208 crt_series.pop_patch(p)
209
210 print 'done'
211
212 def parse_patches(patch_args, patch_list):
213 """Parse patch_args list for patch names in patch_list and return
214 a list. The names can be individual patches and/or in the
215 patch1..patch2 format.
216 """
217 patches = []
218
219 for name in patch_args:
220 pair = name.split('..')
221 for p in pair:
222 if p and not p in patch_list:
223 raise CmdException, 'Unknown patch name: %s' % p
224
225 if len(pair) == 1:
226 # single patch name
227 pl = pair
228 elif len(pair) == 2:
229 # patch range [p1]..[p2]
230 # inclusive boundary
231 if pair[0]:
232 first = patch_list.index(pair[0])
233 else:
234 first = 0
235 # exclusive boundary
236 if pair[1]:
237 last = patch_list.index(pair[1]) + 1
238 else:
239 last = len(patch_list)
240
241 if last > first:
242 pl = patch_list[first:last]
243 else:
244 pl = patch_list[(last - 1):(first + 1)]
245 pl.reverse()
246 else:
247 raise CmdException, 'Malformed patch name: %s' % name
248
249 for p in pl:
250 if p in patches:
251 raise CmdException, 'Duplicate patch name: %s' % p
252
253 patches += pl
254
255 return patches
256
257 def name_email(address):
258 """Return a tuple consisting of the name and email parsed from a
259 standard 'name <email>' or 'email (name)' string
260 """
261 address = re.sub('[\\\\"]', '\\\\\g<0>', address)
262 str_list = re.findall('^(.*)\s*<(.*)>\s*$', address)
263 if not str_list:
264 str_list = re.findall('^(.*)\s*\((.*)\)\s*$', address)
265 if not str_list:
266 raise CmdException, 'Incorrect "name <email>"/"email (name)" string: %s' % address
267 return ( str_list[0][1], str_list[0][0] )
268
269 return str_list[0]
270
271 def name_email_date(address):
272 """Return a tuple consisting of the name, email and date parsed
273 from a 'name <email> date' string
274 """
275 address = re.sub('[\\\\"]', '\\\\\g<0>', address)
276 str_list = re.findall('^(.*)\s*<(.*)>\s*(.*)\s*$', address)
277 if not str_list:
278 raise CmdException, 'Incorrect "name <email> date" string: %s' % address
279
280 return str_list[0]
281
282 def make_patch_name(msg):
283 """Return a string to be used as a patch name. This is generated
284 from the top line of the string passed as argument.
285 """
286 if not msg:
287 return None
288
289 subject_line = msg.lstrip().split('\n', 1)[0].lower()
290 return re.sub('[\W]+', '-', subject_line).strip('-')