Mercurial > hg
annotate hgext/schemes.py @ 13899:a7cd0eee396b
commit: note when files are missing
Before, you could experience the following strange interaction:
$ hg commit
nothing changed
$ hg merge
abort: outstanding uncommitted changes
which confused at least one user in #mercurial.
author | Martin Geisler <mg@aragost.com> |
---|---|
date | Wed, 06 Apr 2011 16:21:12 +0200 |
parents | f1823b9f073b |
children | 924c82157d46 |
rev | line source |
---|---|
9964 | 1 # Copyright 2009, Alexander Solovyov <piranha@piranha.org.ua> |
2 # | |
3 # This software may be used and distributed according to the terms of the | |
10263 | 4 # GNU General Public License version 2 or any later version. |
9964 | 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/ | |
10777
bdc3256a318e
schemes: add Kiln On Demand to default schemes
Benjamin Pollack <benjamin@bitquabit.com>
parents:
10282
diff
changeset
|
37 kiln = https://{1}.kilnhg.com/Repo/ |
9964 | 38 |
9965
963ed04a8fde
schemes: fixed typos in module docstring
Martin Geisler <mg@lazybytes.net>
parents:
9964
diff
changeset
|
39 You can override a predefined scheme by defining a new scheme with the |
963ed04a8fde
schemes: fixed typos in module docstring
Martin Geisler <mg@lazybytes.net>
parents:
9964
diff
changeset
|
40 same name. |
9964 | 41 """ |
42 | |
13822
fbf32a6c903e
schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents:
10777
diff
changeset
|
43 import os, re |
fbf32a6c903e
schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents:
10777
diff
changeset
|
44 from mercurial import extensions, hg, templater, url as urlmod, util |
fbf32a6c903e
schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents:
10777
diff
changeset
|
45 from mercurial.i18n import _ |
9964 | 46 |
47 | |
48 class ShortRepository(object): | |
49 def __init__(self, url, scheme, templater): | |
50 self.scheme = scheme | |
51 self.templater = templater | |
52 self.url = url | |
53 try: | |
54 self.parts = max(map(int, re.findall(r'\{(\d+)\}', self.url))) | |
55 except ValueError: | |
56 self.parts = 0 | |
57 | |
58 def __repr__(self): | |
59 return '<ShortRepository: %s>' % self.scheme | |
60 | |
61 def instance(self, ui, url, create): | |
13822
fbf32a6c903e
schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents:
10777
diff
changeset
|
62 # Should this use urlmod.url(), or is manual parsing better? |
9964 | 63 url = url.split('://', 1)[1] |
64 parts = url.split('/', self.parts) | |
65 if len(parts) > self.parts: | |
66 tail = parts[-1] | |
67 parts = parts[:-1] | |
68 else: | |
69 tail = '' | |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
70 context = dict((str(i + 1), v) for i, v in enumerate(parts)) |
9964 | 71 url = ''.join(self.templater.process(self.url, context)) + tail |
72 return hg._lookup(url).instance(ui, url, create) | |
73 | |
13827
f1823b9f073b
url: nuke some newly-introduced underbars in identifiers
Matt Mackall <mpm@selenic.com>
parents:
13822
diff
changeset
|
74 def hasdriveletter(orig, path): |
13822
fbf32a6c903e
schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents:
10777
diff
changeset
|
75 for scheme in schemes: |
fbf32a6c903e
schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents:
10777
diff
changeset
|
76 if path.startswith(scheme + ':'): |
fbf32a6c903e
schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents:
10777
diff
changeset
|
77 return False |
fbf32a6c903e
schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents:
10777
diff
changeset
|
78 return orig(path) |
fbf32a6c903e
schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents:
10777
diff
changeset
|
79 |
9964 | 80 schemes = { |
81 'py': 'http://hg.python.org/', | |
82 'bb': 'https://bitbucket.org/', | |
83 'bb+ssh': 'ssh://hg@bitbucket.org/', | |
10777
bdc3256a318e
schemes: add Kiln On Demand to default schemes
Benjamin Pollack <benjamin@bitquabit.com>
parents:
10282
diff
changeset
|
84 'gcode': 'https://{1}.googlecode.com/hg/', |
bdc3256a318e
schemes: add Kiln On Demand to default schemes
Benjamin Pollack <benjamin@bitquabit.com>
parents:
10282
diff
changeset
|
85 'kiln': 'https://{1}.kilnhg.com/Repo/' |
9964 | 86 } |
87 | |
88 def extsetup(ui): | |
89 schemes.update(dict(ui.configitems('schemes'))) | |
90 t = templater.engine(lambda x: x) | |
91 for scheme, url in schemes.items(): | |
13822
fbf32a6c903e
schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents:
10777
diff
changeset
|
92 if (os.name == 'nt' and len(scheme) == 1 and scheme.isalpha() |
fbf32a6c903e
schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents:
10777
diff
changeset
|
93 and os.path.exists('%s:\\' % scheme)): |
fbf32a6c903e
schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents:
10777
diff
changeset
|
94 raise util.Abort(_('custom scheme %s:// conflicts with drive ' |
fbf32a6c903e
schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents:
10777
diff
changeset
|
95 'letter %s:\\\n') % (scheme, scheme.upper())) |
9964 | 96 hg.schemes[scheme] = ShortRepository(url, scheme, t) |
13822
fbf32a6c903e
schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents:
10777
diff
changeset
|
97 |
13827
f1823b9f073b
url: nuke some newly-introduced underbars in identifiers
Matt Mackall <mpm@selenic.com>
parents:
13822
diff
changeset
|
98 extensions.wrapfunction(urlmod, 'hasdriveletter', hasdriveletter) |