Skip to content

Commit 074842a

Browse files
committed
fix(config): ignore empty values in config file
Similar to git, we now ignore options which have no value. Previously it would not handle it consistently, and throw a parsing error the first time the cache was built. Afterwards, it was fully usable though. Now we specifically check for the case of no-value options instead. Closes #349
1 parent 7f8d9ca commit 074842a

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

‎git/config.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,21 @@ class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, obje
156156

157157
#} END configuration
158158

159+
optvalueonly_source = r'\s*(?P<option>[^:=\s][^:=]*)'
160+
161+
OPTVALUEONLY = re.compile(optvalueonly_source)
162+
159163
OPTCRE = re.compile(
160-
r'\s*(?P<option>[^:=\s][^:=]*)' # very permissive, incuding leading whitespace
161-
r'\s*(?P<vi>[:=])\s*' # any number of space/tab,
164+
optvalueonly_source # very permissive, incuding leading whitespace
165+
+ r'\s*(?P<vi>[:=])\s*' # any number of space/tab,
162166
# followed by separator
163167
# (either : or =), followed
164168
# by any # space/tab
165-
r'(?P<value>.*)$' # everything up to eol
169+
+ r'(?P<value>.*)$' # everything up to eol
166170
)
167171

172+
del optvalueonly_source
173+
168174
# list of RawConfigParser methods able to change the instance
169175
_mutating_methods_ = ("add_section", "remove_section", "remove_option", "set")
170176

@@ -322,9 +328,11 @@ def string_decode(v):
322328
# end handle multi-line
323329
cursect[optname] = optval
324330
else:
325-
if not e:
326-
e = cp.ParsingError(fpname)
327-
e.append(lineno, repr(line))
331+
# check if it's an option with no value - it's just ignored by git
332+
if not self.OPTVALUEONLY.match(line):
333+
if not e:
334+
e = cp.ParsingError(fpname)
335+
e.append(lineno, repr(line))
328336
continue
329337
else:
330338
line = line.rstrip()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[color]
2+
ui
3+
[core]
4+
filemode = true

‎git/test/test_config.py

+7
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,10 @@ def test_complex_aliases(self):
210210
self.assertEqual(w_config.get('alias', 'rbi'), '"!g() { git rebase -i origin/${1:-master} ; } ; g"')
211211
w_config.release()
212212
self.assertEqual(file_obj.getvalue(), self._to_memcache(fixture_path('.gitconfig')).getvalue())
213+
214+
def test_empty_config_value(self):
215+
cr = GitConfigParser(fixture_path('git_config_with_empty_value'), read_only=True)
216+
217+
assert cr.get_value('core', 'filemode'), "Should read keys with values"
218+
219+
self.failUnlessRaises(cp.NoOptionError, cr.get_value, 'color', 'ui')

0 commit comments

Comments
 (0)