1 """Function/variables common to all the commands
5 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
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.
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.
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
21 import sys
, os
, os
.path
, re
22 from optparse
import OptionParser
, make_option
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 from stgit
.lib
import git
as libgit
33 # Command exception class
34 class CmdException(StgException
):
38 class RevParseException(StgException
):
39 """Revision spec parse error."""
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
'[^@]'
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
59 m
= re
.match(r
'^%s$' % patch_id_re
, rev
)
61 return None, None, m
.group('patch_id')
63 # Try path[@branch]//patch_id.
64 m
= re
.match(r
'^%s(%s)?%s$' %
(patch_re
, branch_re
, patch_id_re
), rev
)
66 return m
.group('patch'), m
.group('branch'), m
.group('patch_id')
69 m
= re
.match(r
'^%s(%s)?$' %
(patch_re
, branch_re
), rev
)
71 return m
.group('patch'), m
.group('branch'), None
73 # No, we can't parse that.
74 raise RevParseException
76 def git_id(crt_series
, rev
):
82 # try a GIT revision first
84 return git
.rev_parse(rev
+ '^{commit}')
85 except git
.GitException
:
88 # try an StGIT patch name
90 patch
, branch
, patch_id
= parse_rev(rev
)
94 series
= stack
.Series(branch
)
96 patch
= series
.get_current()
98 raise CmdException
, 'No patches applied'
99 if patch
in series
.get_applied() or patch
in series
.get_unapplied() or \
100 patch
in series
.get_hidden():
101 if patch_id
in ['top', '', None]:
102 return series
.get_patch(patch
).get_top()
103 elif patch_id
== 'bottom':
104 return series
.get_patch(patch
).get_bottom()
105 elif patch_id
== 'top.old':
106 return series
.get_patch(patch
).get_old_top()
107 elif patch_id
== 'bottom.old':
108 return series
.get_patch(patch
).get_old_bottom()
109 elif patch_id
== 'log':
110 return series
.get_patch(patch
).get_log()
111 if patch
== 'base' and patch_id
== None:
112 return series
.get_base()
113 except RevParseException
:
115 except stack
.StackException
:
118 raise CmdException
, 'Unknown patch or revision: %s' % rev
120 def git_commit(name
, repository
, branch
= None):
121 """Return the a Commit object if 'name' is a patch name or Git commit.
122 The patch names allowed are in the form '<branch>:<patch>' and can
123 be followed by standard symbols used by git-rev-parse. If <patch>
124 is '{base}', it represents the bottom of the stack.
126 # Try a [branch:]patch name first
128 branch
, patch
= name
.split(':', 1)
132 branch
= repository
.current_branch_name
135 if patch
.startswith('{base}'):
136 base_id
= repository
.get_stack(branch
).base
.sha1
137 return repository
.rev_parse(base_id
+
138 strip_prefix('{base}', patch
))
140 # Other combination of branch and patch
142 return repository
.rev_parse('patches/%s/%s' %
(branch
, patch
),
143 discard_stderr
= True)
144 except libgit
.RepositoryException
:
149 return repository
.rev_parse(name
, discard_stderr
= True)
150 except libgit
.RepositoryException
:
151 raise CmdException('%s: Unknown patch or revision name' % name
)
153 def check_local_changes():
154 if git
.local_changes():
155 raise CmdException('local changes in the tree. Use "refresh" or'
158 def check_head_top_equal(crt_series
):
159 if not crt_series
.head_top_equal():
160 raise CmdException('HEAD and top are not the same. This can happen'
161 ' if you modify a branch with git. "stg repair'
162 ' --help" explains more about what to do next.')
164 def check_conflicts():
165 if git
.get_conflicts():
166 raise CmdException('Unsolved conflicts. Please resolve them first'
167 ' or revert the changes with "status --reset"')
169 def print_crt_patch(crt_series
, branch
= None):
171 patch
= crt_series
.get_current()
173 patch
= stack
.Series(branch
).get_current()
176 out
.info('Now at patch "%s"' % patch
)
178 out
.info('No patches applied')
180 def resolved_all(reset
= None):
181 conflicts
= git
.get_conflicts()
182 git
.resolved(conflicts
, reset
)
184 def push_patches(crt_series
, patches
, check_merged
= False):
185 """Push multiple patches onto the stack. This function is shared
186 between the push and pull commands
188 forwarded
= crt_series
.forward_patches(patches
)
190 out
.info('Fast-forwarded patches "%s" - "%s"'
191 %
(patches
[0], patches
[forwarded
- 1]))
193 out
.info('Fast-forwarded patch "%s"' % patches
[0])
195 names
= patches
[forwarded
:]
197 # check for patches merged upstream
198 if names
and check_merged
:
199 out
.start('Checking for patches merged upstream')
201 merged
= crt_series
.merged_patches(names
)
203 out
.done('%d found' %
len(merged
))
208 out
.start('Pushing patch "%s"' % p
)
211 crt_series
.push_empty_patch(p
)
212 out
.done('merged upstream')
214 modified
= crt_series
.push_patch(p
)
216 if crt_series
.empty_patch(p
):
217 out
.done('empty patch')
223 def pop_patches(crt_series
, patches
, keep
= False):
224 """Pop the patches in the list from the stack. It is assumed that
225 the patches are listed in the stack reverse order.
227 if len(patches
) == 0:
228 out
.info('Nothing to push/pop')
231 if len(patches
) == 1:
232 out
.start('Popping patch "%s"' % p
)
234 out
.start('Popping patches "%s" - "%s"' %
(patches
[0], p
))
235 crt_series
.pop_patch(p
, keep
)
238 def parse_patches(patch_args
, patch_list
, boundary
= 0, ordered
= False):
239 """Parse patch_args list for patch names in patch_list and return
240 a list. The names can be individual patches and/or in the
241 patch1..patch2 format.
243 # in case it receives a tuple
244 patch_list
= list(patch_list
)
247 for name
in patch_args
:
248 pair
= name
.split('..')
250 if p
and not p
in patch_list
:
251 raise CmdException
, 'Unknown patch name: %s' % p
257 # patch range [p1]..[p2]
260 first
= patch_list
.index(pair
[0])
265 last
= patch_list
.index(pair
[1]) + 1
269 # only cross the boundary if explicitly asked
271 boundary
= len(patch_list
)
281 last
= len(patch_list
)
284 pl
= patch_list
[first
:last
]
286 pl
= patch_list
[(last
- 1):(first
+ 1)]
289 raise CmdException
, 'Malformed patch name: %s' % name
293 raise CmdException
, 'Duplicate patch name: %s' % p
298 patches
= [p
for p
in patch_list
if p
in patches
]
302 def name_email(address
):
303 p
= parse_name_email(address
)
307 raise CmdException('Incorrect "name <email>"/"email (name)" string: %s'
310 def name_email_date(address
):
311 p
= parse_name_email_date(address
)
315 raise CmdException('Incorrect "name <email> date" string: %s' % address
)
317 def address_or_alias(addr_str
):
318 """Return the address if it contains an e-mail address or look up
319 the aliases in the config files.
321 def __address_or_alias(addr
):
324 if addr
.find('@') >= 0:
325 # it's an e-mail address
327 alias
= config
.get('mail.alias.'+addr
)
331 raise CmdException
, 'unknown e-mail alias: %s' % addr
333 addr_list
= [__address_or_alias(addr
.strip())
334 for addr
in addr_str
.split(',')]
335 return ', '.join([addr
for addr
in addr_list
if addr
])
337 def prepare_rebase(crt_series
):
339 applied
= crt_series
.get_applied()
341 out
.start('Popping all applied patches')
342 crt_series
.pop_patch(applied
[0])
346 def rebase(crt_series
, target
):
348 tree_id
= git_id(crt_series
, target
)
350 # it might be that we use a custom rebase command with its own
353 if tree_id
== git
.get_head():
354 out
.info('Already at "%s", no need for rebasing.' % target
)
357 out
.start('Rebasing to "%s"' % target
)
359 out
.start('Rebasing to the default target')
360 git
.rebase(tree_id
= tree_id
)
363 def post_rebase(crt_series
, applied
, nopush
, merged
):
364 # memorize that we rebased to here
365 crt_series
._set_field('orig-base', git
.get_head())
366 # push the patches back
368 push_patches(crt_series
, applied
, merged
)
371 # Patch description/e-mail/diff parsing
373 def __end_descr(line
):
374 return re
.match('---\s*$', line
) or re
.match('diff -', line
) or \
375 re
.match('Index: ', line
)
377 def __split_descr_diff(string
):
378 """Return the description and the diff from the given string
383 for line
in string
.split('\n'):
385 if not __end_descr(line
):
392 return (descr
.rstrip(), diff
)
394 def __parse_description(descr
):
395 """Parse the patch description and return the new description and
396 author information (if any).
399 authname
= authemail
= authdate
= None
401 descr_lines
= [line
.rstrip() for line
in descr
.split('\n')]
403 raise CmdException
, "Empty patch description"
406 end
= len(descr_lines
)
408 # Parse the patch header
409 for pos
in range(0, end
):
410 if not descr_lines
[pos
]:
412 # check for a "From|Author:" line
413 if re
.match('\s*(?:from|author):\s+', descr_lines
[pos
], re
.I
):
414 auth
= re
.findall('^.*?:\s+(.*)$', descr_lines
[pos
])[0]
415 authname
, authemail
= name_email(auth
)
418 # check for a "Date:" line
419 if re
.match('\s*date:\s+', descr_lines
[pos
], re
.I
):
420 authdate
= re
.findall('^.*?:\s+(.*)$', descr_lines
[pos
])[0]
426 subject
= descr_lines
[pos
]
431 body
= reduce(lambda x
, y
: x
+ '\n' + y
, descr_lines
[lasthdr
:], '')
433 return (subject
+ body
, authname
, authemail
, authdate
)
436 """Parse the message object and return (description, authname,
437 authemail, authdate, diff)
439 from email
.Header
import decode_header
, make_header
441 def __decode_header(header
):
442 """Decode a qp-encoded e-mail header as per rfc2047"""
444 words_enc
= decode_header(header
)
445 hobj
= make_header(words_enc
)
446 except Exception, ex
:
447 raise CmdException
, 'header decoding error: %s' % str
(ex
)
448 return unicode(hobj
).encode('utf-8')
451 if msg
.has_key('from'):
452 authname
, authemail
= name_email(__decode_header(msg
['from']))
454 authname
= authemail
= None
456 # '\n\t' can be found on multi-line headers
457 descr
= __decode_header(msg
['subject']).replace('\n\t', ' ')
458 authdate
= msg
['date']
460 # remove the '[*PATCH*]' expression in the subject
462 descr
= re
.findall('^(\[.*?[Pp][Aa][Tt][Cc][Hh].*?\])?\s*(.*)$',
465 raise CmdException
, 'Subject: line not found'
467 # the rest of the message
469 for part
in msg
.walk():
470 if part
.get_content_type() == 'text/plain':
471 msg_text
+= part
.get_payload(decode
= True)
473 rem_descr
, diff
= __split_descr_diff(msg_text
)
475 descr
+= '\n\n' + rem_descr
477 # parse the description for author information
478 descr
, descr_authname
, descr_authemail
, descr_authdate
= \
479 __parse_description(descr
)
481 authname
= descr_authname
483 authemail
= descr_authemail
485 authdate
= descr_authdate
487 return (descr
, authname
, authemail
, authdate
, diff
)
489 def parse_patch(text
):
490 """Parse the input text and return (description, authname,
491 authemail, authdate, diff)
493 descr
, diff
= __split_descr_diff(text
)
494 descr
, authname
, authemail
, authdate
= __parse_description(descr
)
496 # we don't yet have an agreed place for the creation date.
498 return (descr
, authname
, authemail
, authdate
, diff
)
500 def readonly_constant_property(f
):
501 """Decorator that converts a function that computes a value to an
502 attribute that returns the value. The value is computed only once,
503 the first time it is accessed."""
505 n
= '__' + f
.__name__
506 if not hasattr(self
, n
):
507 setattr(self
, n
, f(self
))
508 return getattr(self
, n
)
509 return property(new_f
)
511 class DirectoryException(StgException
):
514 class _Directory(object):
515 def __init__(self
, needs_current_series
= True):
516 self
.needs_current_series
= needs_current_series
517 @readonly_constant_property
520 return Run('git', 'rev-parse', '--git-dir'
521 ).discard_stderr().output_one_line()
523 raise DirectoryException('No git repository found')
524 @readonly_constant_property
525 def __topdir_path(self
):
527 lines
= Run('git', 'rev-parse', '--show-cdup'
528 ).discard_stderr().output_lines()
531 elif len(lines
) == 1:
534 raise RunException('Too much output')
536 raise DirectoryException('No git repository found')
537 @readonly_constant_property
538 def is_inside_git_dir(self
):
539 return { 'true': True, 'false': False
540 }[Run('git', 'rev-parse', '--is-inside-git-dir'
542 @readonly_constant_property
543 def is_inside_worktree(self
):
544 return { 'true': True, 'false': False
545 }[Run('git', 'rev-parse', '--is-inside-work-tree'
547 def cd_to_topdir(self
):
548 os
.chdir(self
.__topdir_path
)
550 class DirectoryAnywhere(_Directory
):
554 class DirectoryHasRepository(_Directory
):
556 self
.git_dir
# might throw an exception
558 class DirectoryInWorktree(DirectoryHasRepository
):
560 DirectoryHasRepository
.setup(self
)
561 if not self
.is_inside_worktree
:
562 raise DirectoryException('Not inside a git worktree')
564 class DirectoryGotoToplevel(DirectoryInWorktree
):
566 DirectoryInWorktree
.setup(self
)
569 class DirectoryHasRepositoryLib(_Directory
):
570 """For commands that use the new infrastructure in stgit.lib.*."""
572 self
.needs_current_series
= False
574 # This will throw an exception if we don't have a repository.
575 self
.repository
= libstack
.Repository
.default()