forked from git-cola/git-cola
/
gitcfg.py
419 lines (346 loc) · 12.2 KB
/
gitcfg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
from __future__ import division, absolute_import, unicode_literals
import copy
import fnmatch
import os
import re
import struct
from binascii import unhexlify
from os.path import join
from . import core
from . import git
from . import observable
from .compat import int_types
from .decorators import memoize
from .git import STDOUT
from .compat import ustr
BUILTIN_READER = os.environ.get('GIT_COLA_BUILTIN_CONFIG_READER', False)
_USER_CONFIG = core.expanduser(join('~', '.gitconfig'))
_USER_XDG_CONFIG = core.expanduser(
join(core.getenv('XDG_CONFIG_HOME', join('~', '.config')),
'git', 'config'))
@memoize
def current():
"""Return the GitConfig singleton."""
return GitConfig()
def _stat_info():
# Try /etc/gitconfig as a fallback for the system config
paths = [('system', '/etc/gitconfig'),
('user', _USER_XDG_CONFIG),
('user', _USER_CONFIG)]
config = git.current().git_path('config')
if config:
paths.append(('repo', config))
statinfo = []
for category, path in paths:
try:
statinfo.append((category, path, core.stat(path).st_mtime))
except OSError:
continue
return statinfo
def _cache_key():
# Try /etc/gitconfig as a fallback for the system config
paths = ['/etc/gitconfig',
_USER_XDG_CONFIG,
_USER_CONFIG]
config = git.current().git_path('config')
if config:
paths.append(config)
mtimes = []
for path in paths:
try:
mtimes.append(core.stat(path).st_mtime)
except OSError:
continue
return mtimes
def _config_to_python(v):
"""Convert a Git config string into a Python value"""
if v in ('true', 'yes'):
v = True
elif v in ('false', 'no'):
v = False
else:
try:
v = int(v)
except ValueError:
pass
return v
def _config_key_value(line, splitchar):
"""Split a config line into a (key, value) pair"""
try:
k, v = line.split(splitchar, 1)
except ValueError:
# the user has a emptyentry in their git config,
# which Git interprets as meaning "true"
k = line
v = 'true'
return k, _config_to_python(v)
class GitConfig(observable.Observable):
"""Encapsulate access to git-config values."""
message_user_config_changed = 'user_config_changed'
message_repo_config_changed = 'repo_config_changed'
def __init__(self):
observable.Observable.__init__(self)
self.git = git.current()
self._map = {}
self._system = {}
self._user = {}
self._user_or_system = {}
self._repo = {}
self._all = {}
self._cache_key = None
self._configs = []
self._config_files = {}
self._value_cache = {}
self._attr_cache = {}
self._find_config_files()
def reset(self):
self._map.clear()
self._system.clear()
self._user.clear()
self._user_or_system.clear()
self._repo.clear()
self._all.clear()
self._cache_key = None
self._configs = []
self._config_files.clear()
self._value_cache = {}
self._attr_cache = {}
self._find_config_files()
def user(self):
return copy.deepcopy(self._user)
def repo(self):
return copy.deepcopy(self._repo)
def all(self):
return copy.deepcopy(self._all)
def _find_config_files(self):
"""
Classify git config files into 'system', 'user', and 'repo'.
Populates self._configs with a list of the files in
reverse-precedence order. self._config_files is populated with
{category: path} where category is one of 'system', 'user', or 'repo'.
"""
# Try the git config in git's installation prefix
statinfo = _stat_info()
self._configs = [x[1] for x in statinfo]
self._config_files = {}
for (cat, path, mtime) in statinfo:
self._config_files[cat] = path
def update(self):
"""Read config values from git."""
if self._cached():
return
self._read_configs()
def _cached(self):
"""
Return True when the cache matches.
Updates the cache and returns False when the cache does not match.
"""
cache_key = _cache_key()
if self._cache_key is None or cache_key != self._cache_key:
self._cache_key = cache_key
return False
return True
def _read_configs(self):
"""Read git config value into the system, user and repo dicts."""
self._map.clear()
self._system.clear()
self._user.clear()
self._user_or_system.clear()
self._repo.clear()
self._all.clear()
if 'system' in self._config_files:
self._system.update(
self.read_config(self._config_files['system']))
if 'user' in self._config_files:
self._user.update(
self.read_config(self._config_files['user']))
if 'repo' in self._config_files:
self._repo.update(
self.read_config(self._config_files['repo']))
for dct in (self._system, self._user):
self._user_or_system.update(dct)
for dct in (self._system, self._user, self._repo):
self._all.update(dct)
def read_config(self, path):
"""Return git config data from a path as a dictionary."""
if BUILTIN_READER:
return self._read_config_file(path)
dest = {}
args = ('--null', '--file', path, '--list')
config_lines = self.git.config(*args)[STDOUT].split('\0')
for line in config_lines:
if not line:
# the user has an invalid entry in their git config
continue
k, v = _config_key_value(line, '\n')
self._map[k.lower()] = k
dest[k] = v
return dest
def _read_config_file(self, path):
"""Read a .gitconfig file into a dict"""
config = {}
header_simple = re.compile(r'^\[(\s+)]$')
header_subkey = re.compile(r'^\[(\s+) "(\s+)"\]$')
with core.xopen(path, 'rt') as f:
file_lines = f.readlines()
stripped_lines = [line.strip() for line in file_lines]
lines = [line for line in stripped_lines if bool(line)]
prefix = ''
for line in lines:
if line.startswith('#'):
continue
match = header_simple.match(line)
if match:
prefix = match.group(1) + '.'
continue
match = header_subkey.match(line)
if match:
prefix = match.group(1) + '.' + match.group(2) + '.'
continue
k, v = _config_key_value(line, '=')
k = prefix + k
self._map[k.lower()] = k
config[k] = v
return config
def _get(self, src, key, default):
self.update()
try:
value = self._get_with_fallback(src, key)
except KeyError:
value = default
return value
def _get_with_fallback(self, src, key):
try:
return src[key]
except KeyError:
pass
key = self._map.get(key.lower(), key)
try:
return src[key]
except KeyError:
pass
# Allow the final KeyError to bubble up
return src[key.lower()]
def get(self, key, default=None):
"""Return the string value for a config key."""
return self._get(self._all, key, default)
def get_user(self, key, default=None):
return self._get(self._user, key, default)
def get_repo(self, key, default=None):
return self._get(self._repo, key, default)
def get_user_or_system(self, key, default=None):
return self._get(self._user_or_system, key, default)
def python_to_git(self, value):
if type(value) is bool:
if value:
return 'true'
else:
return 'false'
if isinstance(value, int_types):
return ustr(value)
return value
def set_user(self, key, value):
if value is None:
self.unset_user(key)
return
self.git.config('--global', key, self.python_to_git(value))
self.update()
msg = self.message_user_config_changed
self.notify_observers(msg, key, value)
def set_repo(self, key, value):
self.git.config(key, self.python_to_git(value))
self.update()
msg = self.message_repo_config_changed
self.notify_observers(msg, key, value)
def unset_user(self, key):
self.git.config('--global', '--unset', key)
self.update()
msg = self.message_repo_config_changed
self.notify_observers(msg, key, None)
def find(self, pat):
pat = pat.lower()
match = fnmatch.fnmatch
result = {}
self.update()
for key, val in self._all.items():
if match(key.lower(), pat):
result[key] = val
return result
def get_cached(self, key, default=None):
cache = self._value_cache
try:
value = cache[key]
except KeyError:
value = cache[key] = self.get(key, default=default)
return value
def gui_encoding(self):
return self.get_cached('gui.encoding', default='utf-8')
def is_per_file_attrs_enabled(self):
return self.get_cached('cola.fileattributes', default=False)
def file_encoding(self, path):
if not self.is_per_file_attrs_enabled():
return self.gui_encoding()
cache = self._attr_cache
try:
value = cache[path]
except KeyError:
value = cache[path] = (self._file_encoding(path) or
self.gui_encoding())
return value
def _file_encoding(self, path):
"""Return the file encoding for a path"""
status, out, err = self.git.check_attr('encoding', '--', path)
if status != 0:
return None
header = '%s: encoding: ' % path
if out.startswith(header):
encoding = out[len(header):].strip()
if (encoding != 'unspecified' and
encoding != 'unset' and
encoding != 'set'):
return encoding
return None
def get_guitool_opts(self, name):
"""Return the guitool.<name> namespace as a dict
The dict keys are simplified so that "guitool.$name.cmd" is accessible
as `opts[cmd]`.
"""
prefix = len('guitool.%s.' % name)
guitools = self.find('guitool.%s.*' % name)
return dict([(key[prefix:], value)
for (key, value) in guitools.items()])
def get_guitool_names(self):
guitools = self.find('guitool.*.cmd')
prefix = len('guitool.')
suffix = len('.cmd')
return sorted([name[prefix:-suffix]
for (name, cmd) in guitools.items()])
def get_guitool_names_and_shortcuts(self):
"""Return guitool names and their configured shortcut"""
names = self.get_guitool_names()
return [(name, self.get('guitool.%s.shortcut' % name))
for name in names]
def terminal(self):
term = self.get('cola.terminal', None)
if not term:
# find a suitable default terminal
term = 'xterm -e' # for mac osx
candidates = ('xfce4-terminal', 'konsole', 'gnome-terminal')
for basename in candidates:
if core.exists('/usr/bin/%s' % basename):
term = '%s -e' % basename
break
return term
def color(self, key, default):
string = self.get('cola.color.%s' % key, default)
string = core.encode(string)
default = core.encode(default)
struct_layout = core.encode('BBB')
try:
r, g, b = struct.unpack(struct_layout, unhexlify(string))
except Exception:
r, g, b = struct.unpack(struct_layout, unhexlify(default))
return (r, g, b)
def icon_style(self):
theme = self.get('cola.iconstyle', None)
return theme