Browse Source

Properly quote config values with a # character. Fixes #511

Jelmer Vernooij 8 years ago
parent
commit
60eaa905ff
3 changed files with 12 additions and 1 deletions
  1. 3 0
      NEWS
  2. 2 1
      dulwich/config.py
  3. 7 0
      dulwich/tests/test_config.py

+ 3 - 0
NEWS

@@ -6,6 +6,9 @@
    https://bitbucket.org/pypy/pypy/issues/2499/cpyext-pystring_asstring-doesnt-work,
    fixing Dulwich when used with C extensions on pypy < 5.6. (Victor Stinner)
 
+ * Properly quote config values with a '#' character in them.
+   (Jelmer Vernooij, #511)
+
 0.17.1	2017-03-01
 
  IMPROVEMENTS

+ 2 - 1
dulwich/config.py

@@ -174,6 +174,7 @@ def _format_string(value):
     if (value.startswith(b" ") or
         value.startswith(b"\t") or
         value.endswith(b" ") or
+        b'#' in value or
         value.endswith(b"\t")):
         return b'"' + _escape_value(value) + b'"'
     return _escape_value(value)
@@ -365,7 +366,7 @@ class ConfigFile(ConfigDict):
                 elif value is False:
                     value = b"false"
                 else:
-                    value = _escape_value(value)
+                    value = _format_string(value)
                 f.write(b"\t" + key + b" = " + value + b"\n")
 
 

+ 7 - 0
dulwich/tests/test_config.py

@@ -173,6 +173,13 @@ who\"
         self.assertEqual(ConfigFile({(b'alias', ): {
             b'who': b"!who() {git log --no-merges --pretty=format:'%an - %ae' $@ | sort | uniq -c | sort -rn;};who"}}), cf)
 
+    def test_set_hash_gets_quoted(self):
+        c = ConfigFile()
+        c.set(b"xandikos", b"color", b"#665544")
+        f = BytesIO()
+        c.write_to_file(f)
+        self.assertEqual(b"[xandikos]\n\tcolor = \"#665544\"\n", f.getvalue())
+
 
 class ConfigDictTests(TestCase):