annotate hggit/compat.py @ 1061:a2eccdeed26d

config: provide a central place to define and access defaults Provides a place to register the defaults for all config options in one place, and a helper to read them. This will let us eliminate deprecation warnings on hg 4.4 (in a future change) while remaining compatible with older versions.
author Kevin Bullock <kbullock@ringworld.org>
date Tue, 24 Oct 2017 13:56:26 -0500
parents da62ef0569bb
children c249de74742b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
991
92d7702c19da compat: add method for backwards compatible passwordmgr
Sean Farley <sean@farley.io>
parents: 850
diff changeset
1 from mercurial import (
92d7702c19da compat: add method for backwards compatible passwordmgr
Sean Farley <sean@farley.io>
parents: 850
diff changeset
2 url,
92d7702c19da compat: add method for backwards compatible passwordmgr
Sean Farley <sean@farley.io>
parents: 850
diff changeset
3 util as hgutil,
92d7702c19da compat: add method for backwards compatible passwordmgr
Sean Farley <sean@farley.io>
parents: 850
diff changeset
4 )
92d7702c19da compat: add method for backwards compatible passwordmgr
Sean Farley <sean@farley.io>
parents: 850
diff changeset
5
850
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
6 try:
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
7 from mercurial import encoding
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
8 hfsignoreclean = encoding.hfsignoreclean
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
9 except AttributeError:
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
10 # compat with hg 3.2.1 and earlier, which doesn't have
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
11 # hfsignoreclean (This was borrowed wholesale from hg 3.2.2.)
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
12 _ignore = [unichr(int(x, 16)).encode("utf-8") for x in
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
13 "200c 200d 200e 200f 202a 202b 202c 202d 202e "
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
14 "206a 206b 206c 206d 206e 206f feff".split()]
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
15 # verify the next function will work
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
16 assert set([i[0] for i in _ignore]) == set(["\xe2", "\xef"])
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
17
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
18 def hfsignoreclean(s):
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
19 """Remove codepoints ignored by HFS+ from s.
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
20
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
21 >>> hfsignoreclean(u'.h\u200cg'.encode('utf-8'))
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
22 '.hg'
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
23 >>> hfsignoreclean(u'.h\ufeffg'.encode('utf-8'))
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
24 '.hg'
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
25 """
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
26 if "\xe2" in s or "\xef" in s:
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
27 for c in _ignore:
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
28 s = s.replace(c, '')
81c55f8629ba hg2git: audit path components during export (CVE-2014-9390)
Augie Fackler <raf@durin42.com>
parents:
diff changeset
29 return s
991
92d7702c19da compat: add method for backwards compatible passwordmgr
Sean Farley <sean@farley.io>
parents: 850
diff changeset
30
1035
cfcd3032355c compat: remove unused argument
Kevin Bullock <kbullock@ringworld.org>
parents: 1005
diff changeset
31 def passwordmgr(ui):
991
92d7702c19da compat: add method for backwards compatible passwordmgr
Sean Farley <sean@farley.io>
parents: 850
diff changeset
32 try:
1036
da62ef0569bb compat: fix fallback for ui.passwordmgr
Kevin Bullock <kbullock@ringworld.org>
parents: 1035
diff changeset
33 realm = hgutil.urlreq.httppasswordmgrwithdefaultrealm()
da62ef0569bb compat: fix fallback for ui.passwordmgr
Kevin Bullock <kbullock@ringworld.org>
parents: 1035
diff changeset
34 return url.passwordmgr(ui, realm)
da62ef0569bb compat: fix fallback for ui.passwordmgr
Kevin Bullock <kbullock@ringworld.org>
parents: 1035
diff changeset
35 except (TypeError, AttributeError):
991
92d7702c19da compat: add method for backwards compatible passwordmgr
Sean Farley <sean@farley.io>
parents: 850
diff changeset
36 # compat with hg < 3.9
92d7702c19da compat: add method for backwards compatible passwordmgr
Sean Farley <sean@farley.io>
parents: 850
diff changeset
37 return url.passwordmgr(ui)
1005
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
38
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
39 # dulwich doesn't return the symref where remote HEAD points, so we monkey
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
40 # patch it here
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
41 from dulwich.errors import GitProtocolError
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
42 from dulwich.protocol import extract_capabilities
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
43
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
44 def read_pkt_refs(proto):
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
45 server_capabilities = None
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
46 refs = {}
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
47 # Receive refs from server
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
48 for pkt in proto.read_pkt_seq():
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
49 (sha, ref) = pkt.rstrip('\n').split(None, 1)
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
50 if sha == 'ERR':
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
51 raise GitProtocolError(ref)
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
52 if server_capabilities is None:
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
53 (ref, server_capabilities) = extract_capabilities(ref)
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
54 symref = 'symref=HEAD:'
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
55 for cap in server_capabilities:
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
56 if cap.startswith(symref):
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
57 sha = cap.replace(symref, '')
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
58 refs[ref] = sha
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
59
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
60 if len(refs) == 0:
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
61 return None, set([])
31f52d62ae13 compat: add method for dulwich to return the symref
Sean Farley <sean@farley.io>
parents: 991
diff changeset
62 return refs, set(server_capabilities)
1061
a2eccdeed26d config: provide a central place to define and access defaults
Kevin Bullock <kbullock@ringworld.org>
parents: 1036
diff changeset
63
a2eccdeed26d config: provide a central place to define and access defaults
Kevin Bullock <kbullock@ringworld.org>
parents: 1036
diff changeset
64
a2eccdeed26d config: provide a central place to define and access defaults
Kevin Bullock <kbullock@ringworld.org>
parents: 1036
diff changeset
65 CONFIG_DEFAULTS = {
a2eccdeed26d config: provide a central place to define and access defaults
Kevin Bullock <kbullock@ringworld.org>
parents: 1036
diff changeset
66 }
a2eccdeed26d config: provide a central place to define and access defaults
Kevin Bullock <kbullock@ringworld.org>
parents: 1036
diff changeset
67
a2eccdeed26d config: provide a central place to define and access defaults
Kevin Bullock <kbullock@ringworld.org>
parents: 1036
diff changeset
68 def config(ui, subtype, section, item):
a2eccdeed26d config: provide a central place to define and access defaults
Kevin Bullock <kbullock@ringworld.org>
parents: 1036
diff changeset
69 if subtype == 'string':
a2eccdeed26d config: provide a central place to define and access defaults
Kevin Bullock <kbullock@ringworld.org>
parents: 1036
diff changeset
70 subtype = ''
a2eccdeed26d config: provide a central place to define and access defaults
Kevin Bullock <kbullock@ringworld.org>
parents: 1036
diff changeset
71 getconfig = getattr(ui, 'config' + subtype)
a2eccdeed26d config: provide a central place to define and access defaults
Kevin Bullock <kbullock@ringworld.org>
parents: 1036
diff changeset
72 return getconfig(section, item, CONFIG_DEFAULTS[section][item])