55# the BSD License: http://www.opensource.org/licenses/bsd-license.php
66
77import os
8+ import sys
89import re
910import gzip
1011import StringIO
1516from actor import Actor
1617from refs import *
1718from objects import *
18-
19+ from config import GitConfigParser
1920
2021class Repo (object ):
2122 """
@@ -30,6 +31,10 @@ class Repo(object):
3031 re_hexsha_only = re .compile ('^[0-9A-Fa-f]{40}$' )
3132 re_author_committer_start = re .compile (r'^(author|committer)' )
3233 re_tab_full_line = re .compile (r'^\t(.*)$' )
34+
35+ # invariants
36+ # represents the configuration level of a configuration file
37+ config_level = ("system" , "global" , "repository" )
3338
3439 def __init__ (self , path = None ):
3540 """
@@ -125,6 +130,52 @@ def tags(self):
125130 """
126131 return Tag .list_items (self )
127132
133+ def _get_config_path (self , config_level ):
134+ # we do not support an absolute path of the gitconfig on windows ,
135+ # use the global config instead
136+ if sys .platform == "win32" and config_level == "system" :
137+ config_level = "global"
138+
139+ if config_level == "system" :
140+ return "/etc/gitconfig"
141+ elif config_level == "global" :
142+ return os .path .expanduser ("~/.gitconfig" )
143+ elif config_level == "repository" :
144+ return "%s/config" % self .git .git_dir
145+
146+ raise ValueError ( "Invalid configuration level: %r" % config_level )
147+
148+ @property
149+ def config_reader (self ):
150+ """
151+ Returns
152+ GitConfigParser allowing to read the full git configuration, but not to write it
153+
154+ The configuration will include values from the system, user and repository
155+ configuration files.
156+
157+ NOTE: On windows, system configuration cannot currently be read as the path is
158+ unknown, instead the global path will be used.
159+ """
160+ files = [ self ._get_config_path (f ) for f in self .config_level ]
161+ return GitConfigParser (files , read_only = True )
162+
163+ def config_writer (self , config_level = "repository" ):
164+ """
165+ Returns
166+ GitConfigParser allowing to write values of the specified configuration file level.
167+ Config writers should be retrieved, used to change the configuration ,and written
168+ right away as they will lock the configuration file in question and prevent other's
169+ to write it.
170+
171+ ``config_level``
172+ One of the following values
173+ system = sytem wide configuration file
174+ global = user level configuration file
175+ repository = configuration file for this repostory only
176+ """
177+ return GitConfigParser (self ._get_config_path (config_level ), read_only = False )
178+
128179 def commit (self , rev = None ):
129180 """
130181 The Commit object for the specified revision
0 commit comments