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