6 from cola
import gitcmd
12 """Return a static GitConfig instance."""
19 def _find_in_path(app
):
20 """Find a program in $PATH."""
21 is_win32
= sys
.platform
== 'win32'
22 for path
in os
.environ
.get('PATH', '').split(os
.pathsep
):
23 candidate
= os
.path
.join(path
, app
)
24 if os
.path
.exists(candidate
):
27 candidate
= os
.path
.join(path
, app
+ '.exe')
28 if os
.path
.exists(candidate
):
33 class GitConfig(object):
34 """Encapsulate access to git-config values."""
37 self
.git
= gitcmd
.instance()
41 self
._cache
_key
= None
43 self
._config
_files
= {}
44 self
._find
_config
_files
()
51 self
._config
_files
= {}
52 self
._find
_config
_files
()
55 return copy
.deepcopy(self
._user
)
58 return copy
.deepcopy(self
._repo
)
60 def _find_config_files(self
):
62 Classify git config files into 'system', 'user', and 'repo'.
64 Populates self._configs with a list of the files in
65 reverse-precedence order. self._config_files is populated with
66 {category: path} where category is one of 'system', 'user', or 'repo'.
69 # Try the git config in git's installation prefix
70 git_path
= _find_in_path('git')
72 bin_dir
= os
.path
.dirname(git_path
)
73 prefix
= os
.path
.dirname(bin_dir
)
74 system_config
= os
.path
.join(prefix
, 'etc', 'gitconfig')
75 if os
.path
.exists(system_config
):
76 self
._config
_files
['system'] = system_config
77 self
._configs
.append(system_config
)
79 # Try /etc/gitconfig as a fallback for the system config
80 if 'system' not in self
._config
_files
:
81 system_config
= '/etc/gitconfig'
82 if os
.path
.exists(system_config
):
83 self
._config
_files
['system'] = system_config
84 self
._configs
.append(system_config
)
86 # Check for the user config
87 user_config
= os
.path
.expanduser('~/.gitconfig')
88 if os
.path
.exists(user_config
):
89 self
._config
_files
['user'] = user_config
90 self
._configs
.append(user_config
)
92 # Check for the repo config
93 repo_config
= self
.git
.git_path('config')
94 if os
.path
.exists(repo_config
):
95 self
._config
_files
['repo'] = repo_config
96 self
._configs
.append(repo_config
)
99 """Read config values from git."""
106 Return True when the cache matches.
108 Updates the cache and returns False when the cache does not match.
111 cache_key
= map(utils
.slurp
, self
._configs
)
112 if not self
._cache
_key
or cache_key
!= self
._cache
_key
:
113 self
._cache
_key
= cache_key
117 def _read_configs(self
):
118 """Read git config value into the system, user and repo dicts."""
123 if 'system' in self
._config
_files
:
124 self
._system
= self
.read_config(self
._config
_files
['system'])
126 if 'user' in self
._config
_files
:
127 self
._user
= self
.read_config(self
._config
_files
['user'])
129 if 'repo' in self
._config
_files
:
130 self
._repo
= self
.read_config(self
._config
_files
['repo'])
132 def read_config(self
, path
):
133 """Return git config data from a path as a dictionary."""
135 args
= ('--null', '--file', path
, '--list')
136 config_lines
= self
.git
.config(*args
).split('\0')
137 for line
in config_lines
:
139 k
, v
= line
.split('\n')
141 # the user has an invalid entry in their git config
144 if v
== 'true' or v
== 'false':
145 v
= bool(eval(v
.title()))
153 def get(self
, key
, default
=None):
154 """Return the string value for a config key."""
156 for dct
in (self
._repo
, self
._user
, self
._system
):