diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2011-06-07 19:58:06 +0200 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2011-06-07 20:09:40 +0200 |
commit | 5b3b65287e6849628066d5b9d08ec40c260a94dc (patch) | |
tree | 86fa935d8237af1ebb67b14baac3585086655333 | |
parent | b3061698403f0055d1d63be6ab3fbb43bd954e8e (diff) | |
download | gitpython-5b3b65287e6849628066d5b9d08ec40c260a94dc.tar.gz |
Greatly improved robustness of config parser - it can now take pretty much everything. Includes an updated config file which includes all the new additions
-rw-r--r-- | git/config.py | 11 | ||||
-rw-r--r-- | git/test/fixtures/git_config | 16 | ||||
-rw-r--r-- | git/test/test_config.py | 6 |
3 files changed, 23 insertions, 10 deletions
diff --git a/git/config.py b/git/config.py index 209f2ffe..c71bb8ca 100644 --- a/git/config.py +++ b/git/config.py @@ -120,6 +120,7 @@ class GitConfigParser(cp.RawConfigParser, object): # They must be compatible to the LockFile interface. # A suitable alternative would be the BlockingLockFile t_lock = LockFile + re_comment = re.compile('^\s*[#;]') #} END configuration @@ -211,16 +212,16 @@ class GitConfigParser(cp.RawConfigParser, object): break lineno = lineno + 1 # comment or blank line? - if line.strip() == '' or line[0] in '#;': + if line.strip() == '' or self.re_comment.match(line): continue if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR": # no leading whitespace continue else: # is it a section header? - mo = self.SECTCRE.match(line) + mo = self.SECTCRE.match(line.strip()) if mo: - sectname = mo.group('header') + sectname = mo.group('header').strip() if sectname in self._sections: cursect = self._sections[sectname] elif sectname == cp.DEFAULTSECT: @@ -332,6 +333,10 @@ class GitConfigParser(cp.RawConfigParser, object): close_fp = True else: fp.seek(0) + # make sure we do not overwrite into an existing file + if hasattr(fp, 'truncate'): + fp.truncate() + #END # END handle stream or file # WRITE DATA diff --git a/git/test/fixtures/git_config b/git/test/fixtures/git_config index 3c91985f..ff8e7114 100644 --- a/git/test/fixtures/git_config +++ b/git/test/fixtures/git_config @@ -1,22 +1,28 @@ [core] repositoryformatversion = 0 filemode = true - bare = false - logallrefupdates = true + bare = false + logallrefupdates = true [remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* url = git://gitorious.org/~byron/git-python/byrons-clone.git pushurl = git@gitorious.org:~byron/git-python/byrons-clone.git -[branch "master"] +# a tab indented section header + [branch "master"] remote = origin merge = refs/heads/master -[remote "mainline"] +# an space indented section header + [remote "mainline"] + # space indented comment url = git://gitorious.org/git-python/mainline.git fetch = +refs/heads/*:refs/remotes/mainline/* + [remote "MartinMarcher"] + # tab indented comment url = git://gitorious.org/~martin.marcher/git-python/serverhorror.git fetch = +refs/heads/*:refs/remotes/MartinMarcher/* -[gui] + # can handle comments - the section name is supposed to be stripped +[ gui ] geometry = 1316x820+219+243 207 192 [branch "mainline_performance"] remote = mainline diff --git a/git/test/test_config.py b/git/test/test_config.py index 173e380c..b397b193 100644 --- a/git/test/test_config.py +++ b/git/test/test_config.py @@ -30,7 +30,9 @@ class TestBase(TestCase): w_config.read() # enforce reading assert w_config._sections w_config.write() # enforce writing - assert file_obj.getvalue() == file_obj_orig.getvalue() + + # we stripped lines when reading, so the results differ + assert file_obj.getvalue() != file_obj_orig.getvalue() # creating an additional config writer must fail due to exclusive access self.failUnlessRaises(IOError, GitConfigParser, file_obj, read_only = False) @@ -56,10 +58,10 @@ class TestBase(TestCase): file_obj.seek(0) r_config = GitConfigParser(file_obj, read_only=True) + #print file_obj.getvalue() assert r_config.has_section(sname) assert r_config.has_option(sname, oname) assert r_config.get(sname, oname) == val - # END for each filename def test_base(self): |