eggs/mercurial-1.7.3-py2.6-linux-x86_64.egg/hgext/schemes.py
changeset 69 c6bca38c1cbf
equal deleted inserted replaced
68:5ff1fc726848 69:c6bca38c1cbf
       
     1 # Copyright 2009, Alexander Solovyov <piranha@piranha.org.ua>
       
     2 #
       
     3 # This software may be used and distributed according to the terms of the
       
     4 # GNU General Public License version 2 or any later version.
       
     5 
       
     6 """extend schemes with shortcuts to repository swarms
       
     7 
       
     8 This extension allows you to specify shortcuts for parent URLs with a
       
     9 lot of repositories to act like a scheme, for example::
       
    10 
       
    11   [schemes]
       
    12   py = http://code.python.org/hg/
       
    13 
       
    14 After that you can use it like::
       
    15 
       
    16   hg clone py://trunk/
       
    17 
       
    18 Additionally there is support for some more complex schemas, for
       
    19 example used by Google Code::
       
    20 
       
    21   [schemes]
       
    22   gcode = http://{1}.googlecode.com/hg/
       
    23 
       
    24 The syntax is taken from Mercurial templates, and you have unlimited
       
    25 number of variables, starting with ``{1}`` and continuing with
       
    26 ``{2}``, ``{3}`` and so on. This variables will receive parts of URL
       
    27 supplied, split by ``/``. Anything not specified as ``{part}`` will be
       
    28 just appended to an URL.
       
    29 
       
    30 For convenience, the extension adds these schemes by default::
       
    31 
       
    32   [schemes]
       
    33   py = http://hg.python.org/
       
    34   bb = https://bitbucket.org/
       
    35   bb+ssh = ssh://hg@bitbucket.org/
       
    36   gcode = https://{1}.googlecode.com/hg/
       
    37   kiln = https://{1}.kilnhg.com/Repo/
       
    38 
       
    39 You can override a predefined scheme by defining a new scheme with the
       
    40 same name.
       
    41 """
       
    42 
       
    43 import re
       
    44 from mercurial import hg, templater
       
    45 
       
    46 
       
    47 class ShortRepository(object):
       
    48     def __init__(self, url, scheme, templater):
       
    49         self.scheme = scheme
       
    50         self.templater = templater
       
    51         self.url = url
       
    52         try:
       
    53             self.parts = max(map(int, re.findall(r'\{(\d+)\}', self.url)))
       
    54         except ValueError:
       
    55             self.parts = 0
       
    56 
       
    57     def __repr__(self):
       
    58         return '<ShortRepository: %s>' % self.scheme
       
    59 
       
    60     def instance(self, ui, url, create):
       
    61         url = url.split('://', 1)[1]
       
    62         parts = url.split('/', self.parts)
       
    63         if len(parts) > self.parts:
       
    64             tail = parts[-1]
       
    65             parts = parts[:-1]
       
    66         else:
       
    67             tail = ''
       
    68         context = dict((str(i + 1), v) for i, v in enumerate(parts))
       
    69         url = ''.join(self.templater.process(self.url, context)) + tail
       
    70         return hg._lookup(url).instance(ui, url, create)
       
    71 
       
    72 schemes = {
       
    73     'py': 'http://hg.python.org/',
       
    74     'bb': 'https://bitbucket.org/',
       
    75     'bb+ssh': 'ssh://hg@bitbucket.org/',
       
    76     'gcode': 'https://{1}.googlecode.com/hg/',
       
    77     'kiln': 'https://{1}.kilnhg.com/Repo/'
       
    78     }
       
    79 
       
    80 def extsetup(ui):
       
    81     schemes.update(dict(ui.configitems('schemes')))
       
    82     t = templater.engine(lambda x: x)
       
    83     for scheme, url in schemes.items():
       
    84         hg.schemes[scheme] = ShortRepository(url, scheme, t)