chiark / gitweb /
Make a common superclass for all StGit exceptions
[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 import stack, git, basedir
28 from stgit.config import config, file_extensions
29
30 crt_series = None
31
32
33 # Command exception class
34 class CmdException(StgException):
35     pass
36
37 # Utility functions
38 class RevParseException(StgException):
39     """Revision spec parse error."""
40     pass
41
42 def parse_rev(rev):
43     """Parse a revision specification into its
44     patchname@branchname//patch_id parts. If no branch name has a slash
45     in it, also accept / instead of //."""
46     if '/' in ''.join(git.get_heads()):
47         # We have branch names with / in them.
48         branch_chars = r'[^@]'
49         patch_id_mark = r'//'
50     else:
51         # No / in branch names.
52         branch_chars = r'[^@/]'
53         patch_id_mark = r'(/|//)'
54     patch_re = r'(?P<patch>[^@/]+)'
55     branch_re = r'@(?P<branch>%s+)' % branch_chars
56     patch_id_re = r'%s(?P<patch_id>[a-z.]*)' % patch_id_mark
57
58     # Try //patch_id.
59     m = re.match(r'^%s$' % patch_id_re, rev)
60     if m:
61         return None, None, m.group('patch_id')
62
63     # Try path[@branch]//patch_id.
64     m = re.match(r'^%s(%s)?%s$' % (patch_re, branch_re, patch_id_re), rev)
65     if m:
66         return m.group('patch'), m.group('branch'), m.group('patch_id')
67
68     # Try patch[@branch].
69     m = re.match(r'^%s(%s)?$' % (patch_re, branch_re), rev)
70     if m:
71         return m.group('patch'), m.group('branch'), None
72
73     # No, we can't parse that.
74     raise RevParseException
75
76 def git_id(rev):
77     """Return the GIT id
78     """
79     if not rev:
80         return None
81     try:
82         patch, branch, patch_id = parse_rev(rev)
83         if branch == None:
84             series = crt_series
85         else:
86             series = stack.Series(branch)
87         if patch == None:
88             patch = series.get_current()
89             if not patch:
90                 raise CmdException, 'No patches applied'
91         if patch in series.get_applied() or patch in series.get_unapplied() or \
92                patch in series.get_hidden():
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             elif patch_id == 'log':
102                 return series.get_patch(patch).get_log()
103         if patch == 'base' and patch_id == None:
104             return series.get_base()
105     except RevParseException:
106         pass
107     return git.rev_parse(rev + '^{commit}')
108
109 def check_local_changes():
110     if git.local_changes():
111         raise CmdException, \
112               'local changes in the tree. Use "refresh" or "status --reset"'
113
114 def check_head_top_equal():
115     if not crt_series.head_top_equal():
116         raise CmdException(
117 """HEAD and top are not the same. This can happen if you
118    modify a branch with git. The "assimilate" command can
119    fix this situation.""")
120
121 def check_conflicts():
122     if os.path.exists(os.path.join(basedir.get(), 'conflicts')):
123         raise CmdException, \
124               'Unsolved conflicts. Please resolve them first or\n' \
125               '  revert the changes with "status --reset"'
126
127 def print_crt_patch(branch = None):
128     if not branch:
129         patch = crt_series.get_current()
130     else:
131         patch = stack.Series(branch).get_current()
132
133     if patch:
134         out.info('Now at patch "%s"' % patch)
135     else:
136         out.info('No patches applied')
137
138 def resolved(filename, reset = None):
139     if reset:
140         reset_file = filename + file_extensions()[reset]
141         if os.path.isfile(reset_file):
142             if os.path.isfile(filename):
143                 os.remove(filename)
144             os.rename(reset_file, filename)
145
146     git.update_cache([filename], force = True)
147
148     for ext in file_extensions().values():
149         fn = filename + ext
150         if os.path.isfile(fn):
151             os.remove(fn)
152
153 def resolved_all(reset = None):
154     conflicts = git.get_conflicts()
155     if conflicts:
156         for filename in conflicts:
157             resolved(filename, reset)
158         os.remove(os.path.join(basedir.get(), 'conflicts'))
159
160 def push_patches(patches, check_merged = False):
161     """Push multiple patches onto the stack. This function is shared
162     between the push and pull commands
163     """
164     forwarded = crt_series.forward_patches(patches)
165     if forwarded > 1:
166         out.info('Fast-forwarded patches "%s" - "%s"'
167                  % (patches[0], patches[forwarded - 1]))
168     elif forwarded == 1:
169         out.info('Fast-forwarded patch "%s"' % patches[0])
170
171     names = patches[forwarded:]
172
173     # check for patches merged upstream
174     if names and check_merged:
175         out.start('Checking for patches merged upstream')
176
177         merged = crt_series.merged_patches(names)
178
179         out.done('%d found' % len(merged))
180     else:
181         merged = []
182
183     for p in names:
184         out.start('Pushing patch "%s"' % p)
185
186         if p in merged:
187             crt_series.push_empty_patch(p)
188             out.done('merged upstream')
189         else:
190             modified = crt_series.push_patch(p)
191
192             if crt_series.empty_patch(p):
193                 out.done('empty patch')
194             elif modified:
195                 out.done('modified')
196             else:
197                 out.done()
198
199 def pop_patches(patches, keep = False):
200     """Pop the patches in the list from the stack. It is assumed that
201     the patches are listed in the stack reverse order.
202     """
203     if len(patches) == 0:
204         out.info('Nothing to push/pop')
205     else:
206         p = patches[-1]
207         if len(patches) == 1:
208             out.start('Popping patch "%s"' % p)
209         else:
210             out.start('Popping patches "%s" - "%s"' % (patches[0], p))
211         crt_series.pop_patch(p, keep)
212         out.done()
213
214 def parse_patches(patch_args, patch_list, boundary = 0, ordered = False):
215     """Parse patch_args list for patch names in patch_list and return
216     a list. The names can be individual patches and/or in the
217     patch1..patch2 format.
218     """
219     patches = []
220
221     for name in patch_args:
222         pair = name.split('..')
223         for p in pair:
224             if p and not p in patch_list:
225                 raise CmdException, 'Unknown patch name: %s' % p
226
227         if len(pair) == 1:
228             # single patch name
229             pl = pair
230         elif len(pair) == 2:
231             # patch range [p1]..[p2]
232             # inclusive boundary
233             if pair[0]:
234                 first = patch_list.index(pair[0])
235             else:
236                 first = -1
237             # exclusive boundary
238             if pair[1]:
239                 last = patch_list.index(pair[1]) + 1
240             else:
241                 last = -1
242
243             # only cross the boundary if explicitly asked
244             if not boundary:
245                 boundary = len(patch_list)
246             if first < 0:
247                 if last <= boundary:
248                     first = 0
249                 else:
250                     first = boundary
251             if last < 0:
252                 if first < boundary:
253                     last = boundary
254                 else:
255                     last = len(patch_list)
256
257             if last > first:
258                 pl = patch_list[first:last]
259             else:
260                 pl = patch_list[(last - 1):(first + 1)]
261                 pl.reverse()
262         else:
263             raise CmdException, 'Malformed patch name: %s' % name
264
265         for p in pl:
266             if p in patches:
267                 raise CmdException, 'Duplicate patch name: %s' % p
268
269         patches += pl
270
271     if ordered:
272         patches = [p for p in patch_list if p in patches]
273
274     return patches
275
276 def name_email(address):
277     """Return a tuple consisting of the name and email parsed from a
278     standard 'name <email>' or 'email (name)' string
279     """
280     address = re.sub('[\\\\"]', '\\\\\g<0>', address)
281     str_list = re.findall('^(.*)\s*<(.*)>\s*$', address)
282     if not str_list:
283         str_list = re.findall('^(.*)\s*\((.*)\)\s*$', address)
284         if not str_list:
285             raise CmdException, 'Incorrect "name <email>"/"email (name)" string: %s' % address
286         return ( str_list[0][1], str_list[0][0] )
287
288     return str_list[0]
289
290 def name_email_date(address):
291     """Return a tuple consisting of the name, email and date parsed
292     from a 'name <email> date' string
293     """
294     address = re.sub('[\\\\"]', '\\\\\g<0>', address)
295     str_list = re.findall('^(.*)\s*<(.*)>\s*(.*)\s*$', address)
296     if not str_list:
297         raise CmdException, 'Incorrect "name <email> date" string: %s' % address
298
299     return str_list[0]
300
301 def address_or_alias(addr_str):
302     """Return the address if it contains an e-mail address or look up
303     the aliases in the config files.
304     """
305     def __address_or_alias(addr):
306         if not addr:
307             return None
308         if addr.find('@') >= 0:
309             # it's an e-mail address
310             return addr
311         alias = config.get('mail.alias.'+addr)
312         if alias:
313             # it's an alias
314             return alias
315         raise CmdException, 'unknown e-mail alias: %s' % addr
316
317     addr_list = [__address_or_alias(addr.strip())
318                  for addr in addr_str.split(',')]
319     return ', '.join([addr for addr in addr_list if addr])
320
321 def prepare_rebase(force=None):
322     if not force:
323         # Be sure we won't loose results of stg-(un)commit by error.
324         # Do not require an existing orig-base for compatibility with 0.12 and earlier.
325         origbase = crt_series._get_field('orig-base')
326         if origbase and crt_series.get_base() != origbase:
327             raise CmdException, 'Rebasing would possibly lose data'
328
329     # pop all patches
330     applied = crt_series.get_applied()
331     if len(applied) > 0:
332         out.start('Popping all applied patches')
333         crt_series.pop_patch(applied[0])
334         out.done()
335     return applied
336
337 def rebase(target):
338     try:
339         tree_id = git_id(target)
340     except:
341         # it might be that we use a custom rebase command with its own
342         # target type
343         tree_id = target
344     if tree_id == git.get_head():
345         out.info('Already at "%s", no need for rebasing.' % target)
346         return
347     if target:
348         out.start('Rebasing to "%s"' % target)
349     else:
350         out.start('Rebasing to the default target')
351     git.rebase(tree_id = tree_id)
352     out.done()
353
354 def post_rebase(applied, nopush, merged):
355     # memorize that we rebased to here
356     crt_series._set_field('orig-base', git.get_head())
357     # push the patches back
358     if not nopush:
359         push_patches(applied, merged)
360
361 #
362 # Patch description/e-mail/diff parsing
363 #
364 def __end_descr(line):
365     return re.match('---\s*$', line) or re.match('diff -', line) or \
366             re.match('Index: ', line)
367
368 def __split_descr_diff(string):
369     """Return the description and the diff from the given string
370     """
371     descr = diff = ''
372     top = True
373
374     for line in string.split('\n'):
375         if top:
376             if not __end_descr(line):
377                 descr += line + '\n'
378                 continue
379             else:
380                 top = False
381         diff += line + '\n'
382
383     return (descr.rstrip(), diff)
384
385 def __parse_description(descr):
386     """Parse the patch description and return the new description and
387     author information (if any).
388     """
389     subject = body = ''
390     authname = authemail = authdate = None
391
392     descr_lines = [line.rstrip() for line in  descr.split('\n')]
393     if not descr_lines:
394         raise CmdException, "Empty patch description"
395
396     lasthdr = 0
397     end = len(descr_lines)
398
399     # Parse the patch header
400     for pos in range(0, end):
401         if not descr_lines[pos]:
402            continue
403         # check for a "From|Author:" line
404         if re.match('\s*(?:from|author):\s+', descr_lines[pos], re.I):
405             auth = re.findall('^.*?:\s+(.*)$', descr_lines[pos])[0]
406             authname, authemail = name_email(auth)
407             lasthdr = pos + 1
408             continue
409         # check for a "Date:" line
410         if re.match('\s*date:\s+', descr_lines[pos], re.I):
411             authdate = re.findall('^.*?:\s+(.*)$', descr_lines[pos])[0]
412             lasthdr = pos + 1
413             continue
414         if subject:
415             break
416         # get the subject
417         subject = descr_lines[pos]
418         lasthdr = pos + 1
419
420     # get the body
421     if lasthdr < end:
422         body = reduce(lambda x, y: x + '\n' + y, descr_lines[lasthdr:], '')
423
424     return (subject + body, authname, authemail, authdate)
425
426 def parse_mail(msg):
427     """Parse the message object and return (description, authname,
428     authemail, authdate, diff)
429     """
430     from email.Header import decode_header, make_header
431
432     def __decode_header(header):
433         """Decode a qp-encoded e-mail header as per rfc2047"""
434         try:
435             words_enc = decode_header(header)
436             hobj = make_header(words_enc)
437         except Exception, ex:
438             raise CmdException, 'header decoding error: %s' % str(ex)
439         return unicode(hobj).encode('utf-8')
440
441     # parse the headers
442     if msg.has_key('from'):
443         authname, authemail = name_email(__decode_header(msg['from']))
444     else:
445         authname = authemail = None
446
447     # '\n\t' can be found on multi-line headers
448     descr = __decode_header(msg['subject']).replace('\n\t', ' ')
449     authdate = msg['date']
450
451     # remove the '[*PATCH*]' expression in the subject
452     if descr:
453         descr = re.findall('^(\[.*?[Pp][Aa][Tt][Cc][Hh].*?\])?\s*(.*)$',
454                            descr)[0][1]
455     else:
456         raise CmdException, 'Subject: line not found'
457
458     # the rest of the message
459     msg_text = ''
460     for part in msg.walk():
461         if part.get_content_type() == 'text/plain':
462             msg_text += part.get_payload(decode = True)
463
464     rem_descr, diff = __split_descr_diff(msg_text)
465     if rem_descr:
466         descr += '\n\n' + rem_descr
467
468     # parse the description for author information
469     descr, descr_authname, descr_authemail, descr_authdate = \
470            __parse_description(descr)
471     if descr_authname:
472         authname = descr_authname
473     if descr_authemail:
474         authemail = descr_authemail
475     if descr_authdate:
476        authdate = descr_authdate
477
478     return (descr, authname, authemail, authdate, diff)
479
480 def parse_patch(fobj):
481     """Parse the input file and return (description, authname,
482     authemail, authdate, diff)
483     """
484     descr, diff = __split_descr_diff(fobj.read())
485     descr, authname, authemail, authdate = __parse_description(descr)
486
487     # we don't yet have an agreed place for the creation date.
488     # Just return None
489     return (descr, authname, authemail, authdate, diff)