Infrastructure for current directory handling
[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.exception import *
25 from stgit.utils import *
26 from stgit.out import *
27 from stgit.run import *
28 from stgit import stack, git, basedir
29 from stgit.config import config, file_extensions
30
31 crt_series = None
32
33
34 # Command exception class
35 class CmdException(StgException):
36 pass
37
38 # Utility functions
39 class RevParseException(StgException):
40 """Revision spec parse error."""
41 pass
42
43 def parse_rev(rev):
44 """Parse a revision specification into its
45 patchname@branchname//patch_id parts. If no branch name has a slash
46 in it, also accept / instead of //."""
47 if '/' in ''.join(git.get_heads()):
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() or \
93 patch in series.get_hidden():
94 if patch_id in ['top', '', None]:
95 return series.get_patch(patch).get_top()
96 elif patch_id == 'bottom':
97 return series.get_patch(patch).get_bottom()
98 elif patch_id == 'top.old':
99 return series.get_patch(patch).get_old_top()
100 elif patch_id == 'bottom.old':
101 return series.get_patch(patch).get_old_bottom()
102 elif patch_id == 'log':
103 return series.get_patch(patch).get_log()
104 if patch == 'base' and patch_id == None:
105 return series.get_base()
106 except RevParseException:
107 pass
108 return git.rev_parse(rev + '^{commit}')
109
110 def check_local_changes():
111 if git.local_changes():
112 raise CmdException, \
113 'local changes in the tree. Use "refresh" or "status --reset"'
114
115 def check_head_top_equal():
116 if not crt_series.head_top_equal():
117 raise CmdException(
118 """HEAD and top are not the same. This can happen if you
119 modify a branch with git. The "assimilate" command can
120 fix this situation.""")
121
122 def check_conflicts():
123 if os.path.exists(os.path.join(basedir.get(), 'conflicts')):
124 raise CmdException, \
125 'Unsolved conflicts. Please resolve them first or\n' \
126 ' revert the changes with "status --reset"'
127
128 def print_crt_patch(branch = None):
129 if not branch:
130 patch = crt_series.get_current()
131 else:
132 patch = stack.Series(branch).get_current()
133
134 if patch:
135 out.info('Now at patch "%s"' % patch)
136 else:
137 out.info('No patches applied')
138
139 def resolved(filename, reset = None):
140 if reset:
141 reset_file = filename + file_extensions()[reset]
142 if os.path.isfile(reset_file):
143 if os.path.isfile(filename):
144 os.remove(filename)
145 os.rename(reset_file, filename)
146
147 git.update_cache([filename], force = True)
148
149 for ext in file_extensions().values():
150 fn = filename + ext
151 if os.path.isfile(fn):
152 os.remove(fn)
153
154 def resolved_all(reset = None):
155 conflicts = git.get_conflicts()
156 if conflicts:
157 for filename in conflicts:
158 resolved(filename, reset)
159 os.remove(os.path.join(basedir.get(), 'conflicts'))
160
161 def push_patches(patches, check_merged = False):
162 """Push multiple patches onto the stack. This function is shared
163 between the push and pull commands
164 """
165 forwarded = crt_series.forward_patches(patches)
166 if forwarded > 1:
167 out.info('Fast-forwarded patches "%s" - "%s"'
168 % (patches[0], patches[forwarded - 1]))
169 elif forwarded == 1:
170 out.info('Fast-forwarded patch "%s"' % patches[0])
171
172 names = patches[forwarded:]
173
174 # check for patches merged upstream
175 if names and check_merged:
176 out.start('Checking for patches merged upstream')
177
178 merged = crt_series.merged_patches(names)
179
180 out.done('%d found' % len(merged))
181 else:
182 merged = []
183
184 for p in names:
185 out.start('Pushing patch "%s"' % p)
186
187 if p in merged:
188 crt_series.push_empty_patch(p)
189 out.done('merged upstream')
190 else:
191 modified = crt_series.push_patch(p)
192
193 if crt_series.empty_patch(p):
194 out.done('empty patch')
195 elif modified:
196 out.done('modified')
197 else:
198 out.done()
199
200 def pop_patches(patches, keep = False):
201 """Pop the patches in the list from the stack. It is assumed that
202 the patches are listed in the stack reverse order.
203 """
204 if len(patches) == 0:
205 out.info('Nothing to push/pop')
206 else:
207 p = patches[-1]
208 if len(patches) == 1:
209 out.start('Popping patch "%s"' % p)
210 else:
211 out.start('Popping patches "%s" - "%s"' % (patches[0], p))
212 crt_series.pop_patch(p, keep)
213 out.done()
214
215 def parse_patches(patch_args, patch_list, boundary = 0, ordered = False):
216 """Parse patch_args list for patch names in patch_list and return
217 a list. The names can be individual patches and/or in the
218 patch1..patch2 format.
219 """
220 patches = []
221
222 for name in patch_args:
223 pair = name.split('..')
224 for p in pair:
225 if p and not p in patch_list:
226 raise CmdException, 'Unknown patch name: %s' % p
227
228 if len(pair) == 1:
229 # single patch name
230 pl = pair
231 elif len(pair) == 2:
232 # patch range [p1]..[p2]
233 # inclusive boundary
234 if pair[0]:
235 first = patch_list.index(pair[0])
236 else:
237 first = -1
238 # exclusive boundary
239 if pair[1]:
240 last = patch_list.index(pair[1]) + 1
241 else:
242 last = -1
243
244 # only cross the boundary if explicitly asked
245 if not boundary:
246 boundary = len(patch_list)
247 if first < 0:
248 if last <= boundary:
249 first = 0
250 else:
251 first = boundary
252 if last < 0:
253 if first < boundary:
254 last = boundary
255 else:
256 last = len(patch_list)
257
258 if last > first:
259 pl = patch_list[first:last]
260 else:
261 pl = patch_list[(last - 1):(first + 1)]
262 pl.reverse()
263 else:
264 raise CmdException, 'Malformed patch name: %s' % name
265
266 for p in pl:
267 if p in patches:
268 raise CmdException, 'Duplicate patch name: %s' % p
269
270 patches += pl
271
272 if ordered:
273 patches = [p for p in patch_list if p in patches]
274
275 return patches
276
277 def name_email(address):
278 """Return a tuple consisting of the name and email parsed from a
279 standard 'name <email>' or 'email (name)' string
280 """
281 address = re.sub('[\\\\"]', '\\\\\g<0>', address)
282 str_list = re.findall('^(.*)\s*<(.*)>\s*$', address)
283 if not str_list:
284 str_list = re.findall('^(.*)\s*\((.*)\)\s*$', address)
285 if not str_list:
286 raise CmdException, 'Incorrect "name <email>"/"email (name)" string: %s' % address
287 return ( str_list[0][1], str_list[0][0] )
288
289 return str_list[0]
290
291 def name_email_date(address):
292 """Return a tuple consisting of the name, email and date parsed
293 from a 'name <email> date' string
294 """
295 address = re.sub('[\\\\"]', '\\\\\g<0>', address)
296 str_list = re.findall('^(.*)\s*<(.*)>\s*(.*)\s*$', address)
297 if not str_list:
298 raise CmdException, 'Incorrect "name <email> date" string: %s' % address
299
300 return str_list[0]
301
302 def address_or_alias(addr_str):
303 """Return the address if it contains an e-mail address or look up
304 the aliases in the config files.
305 """
306 def __address_or_alias(addr):
307 if not addr:
308 return None
309 if addr.find('@') >= 0:
310 # it's an e-mail address
311 return addr
312 alias = config.get('mail.alias.'+addr)
313 if alias:
314 # it's an alias
315 return alias
316 raise CmdException, 'unknown e-mail alias: %s' % addr
317
318 addr_list = [__address_or_alias(addr.strip())
319 for addr in addr_str.split(',')]
320 return ', '.join([addr for addr in addr_list if addr])
321
322 def prepare_rebase():
323 # pop all patches
324 applied = crt_series.get_applied()
325 if len(applied) > 0:
326 out.start('Popping all applied patches')
327 crt_series.pop_patch(applied[0])
328 out.done()
329 return applied
330
331 def rebase(target):
332 try:
333 tree_id = git_id(target)
334 except:
335 # it might be that we use a custom rebase command with its own
336 # target type
337 tree_id = target
338 if tree_id == git.get_head():
339 out.info('Already at "%s", no need for rebasing.' % target)
340 return
341 if target:
342 out.start('Rebasing to "%s"' % target)
343 else:
344 out.start('Rebasing to the default target')
345 git.rebase(tree_id = tree_id)
346 out.done()
347
348 def post_rebase(applied, nopush, merged):
349 # memorize that we rebased to here
350 crt_series._set_field('orig-base', git.get_head())
351 # push the patches back
352 if not nopush:
353 push_patches(applied, merged)
354
355 #
356 # Patch description/e-mail/diff parsing
357 #
358 def __end_descr(line):
359 return re.match('---\s*$', line) or re.match('diff -', line) or \
360 re.match('Index: ', line)
361
362 def __split_descr_diff(string):
363 """Return the description and the diff from the given string
364 """
365 descr = diff = ''
366 top = True
367
368 for line in string.split('\n'):
369 if top:
370 if not __end_descr(line):
371 descr += line + '\n'
372 continue
373 else:
374 top = False
375 diff += line + '\n'
376
377 return (descr.rstrip(), diff)
378
379 def __parse_description(descr):
380 """Parse the patch description and return the new description and
381 author information (if any).
382 """
383 subject = body = ''
384 authname = authemail = authdate = None
385
386 descr_lines = [line.rstrip() for line in descr.split('\n')]
387 if not descr_lines:
388 raise CmdException, "Empty patch description"
389
390 lasthdr = 0
391 end = len(descr_lines)
392
393 # Parse the patch header
394 for pos in range(0, end):
395 if not descr_lines[pos]:
396 continue
397 # check for a "From|Author:" line
398 if re.match('\s*(?:from|author):\s+', descr_lines[pos], re.I):
399 auth = re.findall('^.*?:\s+(.*)$', descr_lines[pos])[0]
400 authname, authemail = name_email(auth)
401 lasthdr = pos + 1
402 continue
403 # check for a "Date:" line
404 if re.match('\s*date:\s+', descr_lines[pos], re.I):
405 authdate = re.findall('^.*?:\s+(.*)$', descr_lines[pos])[0]
406 lasthdr = pos + 1
407 continue
408 if subject:
409 break
410 # get the subject
411 subject = descr_lines[pos]
412 lasthdr = pos + 1
413
414 # get the body
415 if lasthdr < end:
416 body = reduce(lambda x, y: x + '\n' + y, descr_lines[lasthdr:], '')
417
418 return (subject + body, authname, authemail, authdate)
419
420 def parse_mail(msg):
421 """Parse the message object and return (description, authname,
422 authemail, authdate, diff)
423 """
424 from email.Header import decode_header, make_header
425
426 def __decode_header(header):
427 """Decode a qp-encoded e-mail header as per rfc2047"""
428 try:
429 words_enc = decode_header(header)
430 hobj = make_header(words_enc)
431 except Exception, ex:
432 raise CmdException, 'header decoding error: %s' % str(ex)
433 return unicode(hobj).encode('utf-8')
434
435 # parse the headers
436 if msg.has_key('from'):
437 authname, authemail = name_email(__decode_header(msg['from']))
438 else:
439 authname = authemail = None
440
441 # '\n\t' can be found on multi-line headers
442 descr = __decode_header(msg['subject']).replace('\n\t', ' ')
443 authdate = msg['date']
444
445 # remove the '[*PATCH*]' expression in the subject
446 if descr:
447 descr = re.findall('^(\[.*?[Pp][Aa][Tt][Cc][Hh].*?\])?\s*(.*)$',
448 descr)[0][1]
449 else:
450 raise CmdException, 'Subject: line not found'
451
452 # the rest of the message
453 msg_text = ''
454 for part in msg.walk():
455 if part.get_content_type() == 'text/plain':
456 msg_text += part.get_payload(decode = True)
457
458 rem_descr, diff = __split_descr_diff(msg_text)
459 if rem_descr:
460 descr += '\n\n' + rem_descr
461
462 # parse the description for author information
463 descr, descr_authname, descr_authemail, descr_authdate = \
464 __parse_description(descr)
465 if descr_authname:
466 authname = descr_authname
467 if descr_authemail:
468 authemail = descr_authemail
469 if descr_authdate:
470 authdate = descr_authdate
471
472 return (descr, authname, authemail, authdate, diff)
473
474 def parse_patch(fobj):
475 """Parse the input file and return (description, authname,
476 authemail, authdate, diff)
477 """
478 descr, diff = __split_descr_diff(fobj.read())
479 descr, authname, authemail, authdate = __parse_description(descr)
480
481 # we don't yet have an agreed place for the creation date.
482 # Just return None
483 return (descr, authname, authemail, authdate, diff)
484
485 def readonly_constant_property(f):
486 """Decorator that converts a function that computes a value to an
487 attribute that returns the value. The value is computed only once,
488 the first time it is accessed."""
489 def new_f(self):
490 n = '__' + f.__name__
491 if not hasattr(self, n):
492 setattr(self, n, f(self))
493 return getattr(self, n)
494 return property(new_f)
495
496 class DirectoryException(StgException):
497 pass
498
499 class _Directory(object):
500 @readonly_constant_property
501 def git_dir(self):
502 try:
503 return Run('git-rev-parse', '--git-dir'
504 ).discard_stderr().output_one_line()
505 except RunException:
506 raise DirectoryException('No git repository found')
507 @readonly_constant_property
508 def __topdir_path(self):
509 try:
510 lines = Run('git-rev-parse', '--show-cdup'
511 ).discard_stderr().output_lines()
512 if len(lines) == 0:
513 return '.'
514 elif len(lines) == 1:
515 return lines[0]
516 else:
517 raise RunException('Too much output')
518 except RunException:
519 raise DirectoryException('No git repository found')
520 @readonly_constant_property
521 def is_inside_git_dir(self):
522 return { 'true': True, 'false': False
523 }[Run('git-rev-parse', '--is-inside-git-dir'
524 ).output_one_line()]
525 @readonly_constant_property
526 def is_inside_worktree(self):
527 return { 'true': True, 'false': False
528 }[Run('git-rev-parse', '--is-inside-work-tree'
529 ).output_one_line()]
530 def cd_to_topdir(self):
531 os.chdir(self.__topdir_path)
532
533 class DirectoryAnywhere(_Directory):
534 def setup(self):
535 pass
536
537 class DirectoryHasRepository(_Directory):
538 def setup(self):
539 self.git_dir # might throw an exception
540
541 class DirectoryInWorktree(DirectoryHasRepository):
542 def setup(self):
543 DirectoryHasRepository.setup(self)
544 if not self.is_inside_worktree:
545 raise DirectoryException('Not inside a git worktree')
546
547 class DirectoryGotoToplevel(DirectoryInWorktree):
548 def setup(self):
549 DirectoryInWorktree.setup(self)
550 self.cd_to_topdir()