Skip to content

Commit 5b3b652

Browse files
committed
Greatly improved robustness of config parser - it can now take pretty much everything. Includes an updated config file which includes all the new additions
1 parent b306169 commit 5b3b652

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

Diff for: git/config.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class GitConfigParser(cp.RawConfigParser, object):
120120
# They must be compatible to the LockFile interface.
121121
# A suitable alternative would be the BlockingLockFile
122122
t_lock = LockFile
123+
re_comment = re.compile('^\s*[#;]')
123124

124125
#} END configuration
125126

@@ -211,16 +212,16 @@ def _read(self, fp, fpname):
211212
break
212213
lineno = lineno + 1
213214
# comment or blank line?
214-
if line.strip() == '' or line[0] in '#;':
215+
if line.strip() == '' or self.re_comment.match(line):
215216
continue
216217
if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR":
217218
# no leading whitespace
218219
continue
219220
else:
220221
# is it a section header?
221-
mo = self.SECTCRE.match(line)
222+
mo = self.SECTCRE.match(line.strip())
222223
if mo:
223-
sectname = mo.group('header')
224+
sectname = mo.group('header').strip()
224225
if sectname in self._sections:
225226
cursect = self._sections[sectname]
226227
elif sectname == cp.DEFAULTSECT:
@@ -332,6 +333,10 @@ def write(self):
332333
close_fp = True
333334
else:
334335
fp.seek(0)
336+
# make sure we do not overwrite into an existing file
337+
if hasattr(fp, 'truncate'):
338+
fp.truncate()
339+
#END
335340
# END handle stream or file
336341

337342
# WRITE DATA

Diff for: git/test/fixtures/git_config

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
[core]
22
repositoryformatversion = 0
33
filemode = true
4-
bare = false
5-
logallrefupdates = true
4+
bare = false
5+
logallrefupdates = true
66
[remote "origin"]
77
fetch = +refs/heads/*:refs/remotes/origin/*
88
url = git://gitorious.org/~byron/git-python/byrons-clone.git
99
pushurl = [email protected]:~byron/git-python/byrons-clone.git
10-
[branch "master"]
10+
# a tab indented section header
11+
[branch "master"]
1112
remote = origin
1213
merge = refs/heads/master
13-
[remote "mainline"]
14+
# an space indented section header
15+
[remote "mainline"]
16+
# space indented comment
1417
url = git://gitorious.org/git-python/mainline.git
1518
fetch = +refs/heads/*:refs/remotes/mainline/*
19+
1620
[remote "MartinMarcher"]
21+
# tab indented comment
1722
url = git://gitorious.org/~martin.marcher/git-python/serverhorror.git
1823
fetch = +refs/heads/*:refs/remotes/MartinMarcher/*
19-
[gui]
24+
# can handle comments - the section name is supposed to be stripped
25+
[ gui ]
2026
geometry = 1316x820+219+243 207 192
2127
[branch "mainline_performance"]
2228
remote = mainline

Diff for: git/test/test_config.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ def test_read_write(self):
3030
w_config.read() # enforce reading
3131
assert w_config._sections
3232
w_config.write() # enforce writing
33-
assert file_obj.getvalue() == file_obj_orig.getvalue()
33+
34+
# we stripped lines when reading, so the results differ
35+
assert file_obj.getvalue() != file_obj_orig.getvalue()
3436

3537
# creating an additional config writer must fail due to exclusive access
3638
self.failUnlessRaises(IOError, GitConfigParser, file_obj, read_only = False)
@@ -56,10 +58,10 @@ def test_read_write(self):
5658

5759
file_obj.seek(0)
5860
r_config = GitConfigParser(file_obj, read_only=True)
61+
#print file_obj.getvalue()
5962
assert r_config.has_section(sname)
6063
assert r_config.has_option(sname, oname)
6164
assert r_config.get(sname, oname) == val
62-
6365
# END for each filename
6466

6567
def test_base(self):

0 commit comments

Comments
 (0)