##// END OF EJS Templates
hgrc: search XDG_CONFIG_HOME on mac...
Hraban Luyat -
r52500:a4b3b8de default
parent child Browse files
Show More
@@ -1,103 +1,101 b''
1 import array
1 import array
2 import errno
2 import errno
3 import fcntl
3 import fcntl
4 import os
4 import os
5 import sys
5 import sys
6 import typing
6 import typing
7
7
8 from typing import (
8 from typing import (
9 List,
9 List,
10 Tuple,
10 Tuple,
11 )
11 )
12
12
13 from . import (
13 from . import (
14 encoding,
14 encoding,
15 pycompat,
15 pycompat,
16 util,
16 util,
17 )
17 )
18
18
19 if typing.TYPE_CHECKING:
19 if typing.TYPE_CHECKING:
20 from . import ui as uimod
20 from . import ui as uimod
21
21
22 # BSD 'more' escapes ANSI color sequences by default. This can be disabled by
22 # BSD 'more' escapes ANSI color sequences by default. This can be disabled by
23 # $MORE variable, but there's no compatible option with Linux 'more'. Given
23 # $MORE variable, but there's no compatible option with Linux 'more'. Given
24 # OS X is widely used and most modern Unix systems would have 'less', setting
24 # OS X is widely used and most modern Unix systems would have 'less', setting
25 # 'less' as the default seems reasonable.
25 # 'less' as the default seems reasonable.
26 fallbackpager = b'less'
26 fallbackpager = b'less'
27
27
28
28
29 def _rcfiles(path: bytes) -> List[bytes]:
29 def _rcfiles(path: bytes) -> List[bytes]:
30 rcs = [os.path.join(path, b'hgrc')]
30 rcs = [os.path.join(path, b'hgrc')]
31 rcdir = os.path.join(path, b'hgrc.d')
31 rcdir = os.path.join(path, b'hgrc.d')
32 try:
32 try:
33 rcs.extend(
33 rcs.extend(
34 [
34 [
35 os.path.join(rcdir, f)
35 os.path.join(rcdir, f)
36 for f, kind in sorted(util.listdir(rcdir))
36 for f, kind in sorted(util.listdir(rcdir))
37 if f.endswith(b".rc")
37 if f.endswith(b".rc")
38 ]
38 ]
39 )
39 )
40 except OSError:
40 except OSError:
41 pass
41 pass
42 return rcs
42 return rcs
43
43
44
44
45 def systemrcpath() -> List[bytes]:
45 def systemrcpath() -> List[bytes]:
46 path = []
46 path = []
47 if pycompat.sysplatform == b'plan9':
47 if pycompat.sysplatform == b'plan9':
48 root = b'lib/mercurial'
48 root = b'lib/mercurial'
49 else:
49 else:
50 root = b'etc/mercurial'
50 root = b'etc/mercurial'
51 # old mod_python does not set sys.argv
51 # old mod_python does not set sys.argv
52 if len(getattr(sys, 'argv', [])) > 0:
52 if len(getattr(sys, 'argv', [])) > 0:
53 p = os.path.dirname(os.path.dirname(pycompat.sysargv[0]))
53 p = os.path.dirname(os.path.dirname(pycompat.sysargv[0]))
54 if p != b'/':
54 if p != b'/':
55 path.extend(_rcfiles(os.path.join(p, root)))
55 path.extend(_rcfiles(os.path.join(p, root)))
56 path.extend(_rcfiles(b'/' + root))
56 path.extend(_rcfiles(b'/' + root))
57 return path
57 return path
58
58
59
59
60 def userrcpath() -> List[bytes]:
60 def userrcpath() -> List[bytes]:
61 if pycompat.sysplatform == b'plan9':
61 if pycompat.sysplatform == b'plan9':
62 return [encoding.environ[b'home'] + b'/lib/hgrc']
62 return [encoding.environ[b'home'] + b'/lib/hgrc']
63 elif pycompat.isdarwin:
64 return [os.path.expanduser(b'~/.hgrc')]
65 else:
63 else:
66 confighome = encoding.environ.get(b'XDG_CONFIG_HOME')
64 confighome = encoding.environ.get(b'XDG_CONFIG_HOME')
67 if confighome is None or not os.path.isabs(confighome):
65 if confighome is None or not os.path.isabs(confighome):
68 confighome = os.path.expanduser(b'~/.config')
66 confighome = os.path.expanduser(b'~/.config')
69
67
70 return [
68 return [
71 os.path.expanduser(b'~/.hgrc'),
69 os.path.expanduser(b'~/.hgrc'),
72 os.path.join(confighome, b'hg', b'hgrc'),
70 os.path.join(confighome, b'hg', b'hgrc'),
73 ]
71 ]
74
72
75
73
76 def termsize(ui: "uimod.ui") -> Tuple[int, int]:
74 def termsize(ui: "uimod.ui") -> Tuple[int, int]:
77 try:
75 try:
78 import termios
76 import termios
79
77
80 TIOCGWINSZ = termios.TIOCGWINSZ # unavailable on IRIX (issue3449)
78 TIOCGWINSZ = termios.TIOCGWINSZ # unavailable on IRIX (issue3449)
81 except (AttributeError, ImportError):
79 except (AttributeError, ImportError):
82 return 80, 24
80 return 80, 24
83
81
84 for dev in (ui.ferr, ui.fout, ui.fin):
82 for dev in (ui.ferr, ui.fout, ui.fin):
85 try:
83 try:
86 try:
84 try:
87 fd = dev.fileno()
85 fd = dev.fileno()
88 except AttributeError:
86 except AttributeError:
89 continue
87 continue
90 if not os.isatty(fd):
88 if not os.isatty(fd):
91 continue
89 continue
92 arri = fcntl.ioctl(fd, TIOCGWINSZ, b'\0' * 8)
90 arri = fcntl.ioctl(fd, TIOCGWINSZ, b'\0' * 8)
93 height, width = array.array('h', arri)[:2]
91 height, width = array.array('h', arri)[:2]
94 if width > 0 and height > 0:
92 if width > 0 and height > 0:
95 return width, height
93 return width, height
96 except ValueError:
94 except ValueError:
97 pass
95 pass
98 except IOError as e:
96 except IOError as e:
99 if e.errno == errno.EINVAL:
97 if e.errno == errno.EINVAL:
100 pass
98 pass
101 else:
99 else:
102 raise
100 raise
103 return 80, 24
101 return 80, 24
@@ -1,11 +1,11 b''
1 #if no-windows no-osx
1 #if no-windows
2
2
3 $ mkdir -p xdgconf/hg
3 $ mkdir -p xdgconf/hg
4 $ echo '[ui]' > xdgconf/hg/hgrc
4 $ echo '[ui]' > xdgconf/hg/hgrc
5 $ echo 'username = foobar' >> xdgconf/hg/hgrc
5 $ echo 'username = foobar' >> xdgconf/hg/hgrc
6 $ XDG_CONFIG_HOME="`pwd`/xdgconf" ; export XDG_CONFIG_HOME
6 $ XDG_CONFIG_HOME="`pwd`/xdgconf" ; export XDG_CONFIG_HOME
7 $ unset HGRCPATH
7 $ unset HGRCPATH
8 $ hg config ui.username 2>/dev/null
8 $ hg config ui.username 2>/dev/null
9 foobar
9 foobar
10
10
11 #endif
11 #endif
General Comments 0
You need to be logged in to leave comments. Login now