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