Mercurial > hg
annotate hgext/schemes.py @ 29087:ad1bdea43965
dispatch: defer environment variable resolution in alias commands (BC)
Before this patch, if there are environment variables in an alias command,
they will be expanded immediately when we first see the alias.
This will cause issues with chg, because environment variable updates will
not propagate to expanded arguments.
This patch makes "args" of "cmdalias" a property that will be calculated
every time when accessed.
author | Jun Wu <quark@fb.com> |
---|---|
date | Sat, 07 May 2016 00:16:58 +0100 |
parents | 27a9032374a7 |
children | a0939666b836 |
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 """ |
28379
27a9032374a7
schemas: use absolute_import
timeless <timeless@mozdev.org>
parents:
27982
diff
changeset
|
42 from __future__ import absolute_import |
9964 | 43 |
28379
27a9032374a7
schemas: use absolute_import
timeless <timeless@mozdev.org>
parents:
27982
diff
changeset
|
44 import os |
27a9032374a7
schemas: use absolute_import
timeless <timeless@mozdev.org>
parents:
27982
diff
changeset
|
45 import re |
27a9032374a7
schemas: use absolute_import
timeless <timeless@mozdev.org>
parents:
27982
diff
changeset
|
46 from mercurial import ( |
27a9032374a7
schemas: use absolute_import
timeless <timeless@mozdev.org>
parents:
27982
diff
changeset
|
47 cmdutil, |
27a9032374a7
schemas: use absolute_import
timeless <timeless@mozdev.org>
parents:
27982
diff
changeset
|
48 error, |
27a9032374a7
schemas: use absolute_import
timeless <timeless@mozdev.org>
parents:
27982
diff
changeset
|
49 extensions, |
27a9032374a7
schemas: use absolute_import
timeless <timeless@mozdev.org>
parents:
27982
diff
changeset
|
50 hg, |
27a9032374a7
schemas: use absolute_import
timeless <timeless@mozdev.org>
parents:
27982
diff
changeset
|
51 templater, |
27a9032374a7
schemas: use absolute_import
timeless <timeless@mozdev.org>
parents:
27982
diff
changeset
|
52 util, |
27a9032374a7
schemas: use absolute_import
timeless <timeless@mozdev.org>
parents:
27982
diff
changeset
|
53 ) |
13822
fbf32a6c903e
schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents:
10777
diff
changeset
|
54 from mercurial.i18n import _ |
9964 | 55 |
27982
bf1d5c223ac0
schemes: add debugexpandscheme command, resolving a scheme to canonical form
Jason R. Coombs <jaraco@jaraco.com>
parents:
27981
diff
changeset
|
56 cmdtable = {} |
bf1d5c223ac0
schemes: add debugexpandscheme command, resolving a scheme to canonical form
Jason R. Coombs <jaraco@jaraco.com>
parents:
27981
diff
changeset
|
57 command = cmdutil.command(cmdtable) |
25186
80c5b2666a96
extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents:
18910
diff
changeset
|
58 # Note for extension authors: ONLY specify testedwith = 'internal' for |
80c5b2666a96
extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents:
18910
diff
changeset
|
59 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
80c5b2666a96
extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents:
18910
diff
changeset
|
60 # be specifying the version(s) of Mercurial they are tested with, or |
80c5b2666a96
extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents:
18910
diff
changeset
|
61 # leave the attribute unspecified. |
16743
38caf405d010
hgext: mark all first-party extensions as such
Augie Fackler <raf@durin42.com>
parents:
15609
diff
changeset
|
62 testedwith = 'internal' |
38caf405d010
hgext: mark all first-party extensions as such
Augie Fackler <raf@durin42.com>
parents:
15609
diff
changeset
|
63 |
9964 | 64 |
65 class ShortRepository(object): | |
66 def __init__(self, url, scheme, templater): | |
67 self.scheme = scheme | |
68 self.templater = templater | |
69 self.url = url | |
70 try: | |
71 self.parts = max(map(int, re.findall(r'\{(\d+)\}', self.url))) | |
72 except ValueError: | |
73 self.parts = 0 | |
74 | |
75 def __repr__(self): | |
76 return '<ShortRepository: %s>' % self.scheme | |
77 | |
78 def instance(self, ui, url, create): | |
27981
d630eac3a5db
schemes: extract scheme expansion as its own method on ShortRepository
Jason R. Coombs <jaraco@jaraco.com>
parents:
26587
diff
changeset
|
79 url = self.resolve(url) |
d630eac3a5db
schemes: extract scheme expansion as its own method on ShortRepository
Jason R. Coombs <jaraco@jaraco.com>
parents:
26587
diff
changeset
|
80 return hg._peerlookup(url).instance(ui, url, create) |
d630eac3a5db
schemes: extract scheme expansion as its own method on ShortRepository
Jason R. Coombs <jaraco@jaraco.com>
parents:
26587
diff
changeset
|
81 |
d630eac3a5db
schemes: extract scheme expansion as its own method on ShortRepository
Jason R. Coombs <jaraco@jaraco.com>
parents:
26587
diff
changeset
|
82 def resolve(self, url): |
17425
e95ec38f86b0
fix wording and not-completely-trivial spelling errors and bad docstrings
Mads Kiilerich <mads@kiilerich.com>
parents:
16743
diff
changeset
|
83 # Should this use the util.url class, or is manual parsing better? |
18910
b52404a914a9
scheme: don't crash on invalid URLs
Mads Kiilerich <madski@unity3d.com>
parents:
17425
diff
changeset
|
84 try: |
b52404a914a9
scheme: don't crash on invalid URLs
Mads Kiilerich <madski@unity3d.com>
parents:
17425
diff
changeset
|
85 url = url.split('://', 1)[1] |
b52404a914a9
scheme: don't crash on invalid URLs
Mads Kiilerich <madski@unity3d.com>
parents:
17425
diff
changeset
|
86 except IndexError: |
26587
56b2bcea2529
error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25186
diff
changeset
|
87 raise error.Abort(_("no '://' in scheme url '%s'") % url) |
9964 | 88 parts = url.split('/', self.parts) |
89 if len(parts) > self.parts: | |
90 tail = parts[-1] | |
91 parts = parts[:-1] | |
92 else: | |
93 tail = '' | |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
94 context = dict((str(i + 1), v) for i, v in enumerate(parts)) |
27981
d630eac3a5db
schemes: extract scheme expansion as its own method on ShortRepository
Jason R. Coombs <jaraco@jaraco.com>
parents:
26587
diff
changeset
|
95 return ''.join(self.templater.process(self.url, context)) + tail |
9964 | 96 |
13827
f1823b9f073b
url: nuke some newly-introduced underbars in identifiers
Matt Mackall <mpm@selenic.com>
parents:
13822
diff
changeset
|
97 def hasdriveletter(orig, path): |
15609
8f4bad72d8b1
util: fix url.__str__() for windows file URLs
Patrick Mezard <pmezard@gmail.com>
parents:
14606
diff
changeset
|
98 if path: |
8f4bad72d8b1
util: fix url.__str__() for windows file URLs
Patrick Mezard <pmezard@gmail.com>
parents:
14606
diff
changeset
|
99 for scheme in schemes: |
8f4bad72d8b1
util: fix url.__str__() for windows file URLs
Patrick Mezard <pmezard@gmail.com>
parents:
14606
diff
changeset
|
100 if path.startswith(scheme + ':'): |
8f4bad72d8b1
util: fix url.__str__() for windows file URLs
Patrick Mezard <pmezard@gmail.com>
parents:
14606
diff
changeset
|
101 return False |
13822
fbf32a6c903e
schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents:
10777
diff
changeset
|
102 return orig(path) |
fbf32a6c903e
schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents:
10777
diff
changeset
|
103 |
9964 | 104 schemes = { |
105 'py': 'http://hg.python.org/', | |
106 'bb': 'https://bitbucket.org/', | |
107 '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
|
108 'gcode': 'https://{1}.googlecode.com/hg/', |
bdc3256a318e
schemes: add Kiln On Demand to default schemes
Benjamin Pollack <benjamin@bitquabit.com>
parents:
10282
diff
changeset
|
109 'kiln': 'https://{1}.kilnhg.com/Repo/' |
9964 | 110 } |
111 | |
112 def extsetup(ui): | |
113 schemes.update(dict(ui.configitems('schemes'))) | |
114 t = templater.engine(lambda x: x) | |
115 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
|
116 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
|
117 and os.path.exists('%s:\\' % scheme)): |
26587
56b2bcea2529
error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
25186
diff
changeset
|
118 raise error.Abort(_('custom scheme %s:// conflicts with drive ' |
13822
fbf32a6c903e
schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents:
10777
diff
changeset
|
119 'letter %s:\\\n') % (scheme, scheme.upper())) |
14606
6e631c24c6d9
hg: move peerschemes back to schemes
Matt Mackall <mpm@selenic.com>
parents:
14605
diff
changeset
|
120 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
|
121 |
14076
924c82157d46
url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents:
13827
diff
changeset
|
122 extensions.wrapfunction(util, 'hasdriveletter', hasdriveletter) |
27982
bf1d5c223ac0
schemes: add debugexpandscheme command, resolving a scheme to canonical form
Jason R. Coombs <jaraco@jaraco.com>
parents:
27981
diff
changeset
|
123 |
bf1d5c223ac0
schemes: add debugexpandscheme command, resolving a scheme to canonical form
Jason R. Coombs <jaraco@jaraco.com>
parents:
27981
diff
changeset
|
124 @command('debugexpandscheme', norepo=True) |
bf1d5c223ac0
schemes: add debugexpandscheme command, resolving a scheme to canonical form
Jason R. Coombs <jaraco@jaraco.com>
parents:
27981
diff
changeset
|
125 def expandscheme(ui, url, **opts): |
bf1d5c223ac0
schemes: add debugexpandscheme command, resolving a scheme to canonical form
Jason R. Coombs <jaraco@jaraco.com>
parents:
27981
diff
changeset
|
126 """given a repo path, provide the scheme-expanded path |
bf1d5c223ac0
schemes: add debugexpandscheme command, resolving a scheme to canonical form
Jason R. Coombs <jaraco@jaraco.com>
parents:
27981
diff
changeset
|
127 """ |
bf1d5c223ac0
schemes: add debugexpandscheme command, resolving a scheme to canonical form
Jason R. Coombs <jaraco@jaraco.com>
parents:
27981
diff
changeset
|
128 repo = hg._peerlookup(url) |
bf1d5c223ac0
schemes: add debugexpandscheme command, resolving a scheme to canonical form
Jason R. Coombs <jaraco@jaraco.com>
parents:
27981
diff
changeset
|
129 if isinstance(repo, ShortRepository): |
bf1d5c223ac0
schemes: add debugexpandscheme command, resolving a scheme to canonical form
Jason R. Coombs <jaraco@jaraco.com>
parents:
27981
diff
changeset
|
130 url = repo.resolve(url) |
bf1d5c223ac0
schemes: add debugexpandscheme command, resolving a scheme to canonical form
Jason R. Coombs <jaraco@jaraco.com>
parents:
27981
diff
changeset
|
131 ui.write(url + '\n') |