|
@@ -20,10 +20,18 @@
|
|
|
|
|
|
"""
|
|
|
|
|
|
+import errno
|
|
|
+import os
|
|
|
+
|
|
|
from dulwich.file import GitFile
|
|
|
|
|
|
|
|
|
-class ConfigFile(object):
|
|
|
+class Config(object):
|
|
|
+ """A Git configuration."""
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+class ConfigFile(Config):
|
|
|
"""A Git configuration file, like .git/config or ~/.gitconfig."""
|
|
|
|
|
|
def __init__(self):
|
|
@@ -42,15 +50,19 @@ class ConfigFile(object):
|
|
|
@classmethod
|
|
|
def from_path(cls, path):
|
|
|
"""Read configuration from a file on disk."""
|
|
|
- f = GitFile(path, 'r')
|
|
|
+ f = GitFile(path, 'rb')
|
|
|
try:
|
|
|
- return cls.from_file(f)
|
|
|
+ ret = cls.from_file(f)
|
|
|
+ ret.path = path
|
|
|
+ return ret
|
|
|
finally:
|
|
|
f.close()
|
|
|
|
|
|
- def write_to_path(self, path):
|
|
|
+ def write_to_path(self, path=None):
|
|
|
"""Write configuration to a file on disk."""
|
|
|
- f = GitFile(path, 'w')
|
|
|
+ if path is None:
|
|
|
+ path = self.path
|
|
|
+ f = GitFile(path, 'wb')
|
|
|
try:
|
|
|
self.write_to_file(f)
|
|
|
finally:
|
|
@@ -59,3 +71,38 @@ class ConfigFile(object):
|
|
|
def write_to_file(self, f):
|
|
|
"""Write configuration to a file-like object."""
|
|
|
# FIXME
|
|
|
+
|
|
|
+
|
|
|
+class StackedConfig(Config):
|
|
|
+ """Configuration which reads from multiple config files.."""
|
|
|
+
|
|
|
+ def __init__(self, backends):
|
|
|
+ self._backends = backends
|
|
|
+
|
|
|
+ def __repr__(self):
|
|
|
+ return "<%s for %r>" % (self.__class__.__name__, self._backends)
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def default(cls, for_path=None):
|
|
|
+ """Retrieve the default configuration.
|
|
|
+
|
|
|
+ This will look in the repository configuration (if for_path is
|
|
|
+ specified), the users' home directory and the system
|
|
|
+ configuration.
|
|
|
+ """
|
|
|
+ paths = []
|
|
|
+ if for_path is not None:
|
|
|
+ paths.append(for_path)
|
|
|
+ paths.append(os.path.expanduser("~/.gitconfig"))
|
|
|
+ paths.append("/etc/gitconfig")
|
|
|
+ backends = []
|
|
|
+ for path in paths:
|
|
|
+ try:
|
|
|
+ cf = ConfigFile.from_path(path)
|
|
|
+ except IOError, e:
|
|
|
+ if e.errno != errno.ENOENT:
|
|
|
+ raise
|
|
|
+ else:
|
|
|
+ continue
|
|
|
+ backends.append(cf)
|
|
|
+ return cls(backends)
|