Преглед изворни кода

Merge branch 'quoted-comments-in-config' of https://github.com/dandersson/dulwich

Jelmer Vernooij пре 7 година
родитељ
комит
cd1df5b04f
3 измењених фајлова са 24 додато и 2 уклоњено
  1. 4 0
      NEWS
  2. 10 2
      dulwich/config.py
  3. 10 0
      dulwich/tests/test_config.py

+ 4 - 0
NEWS

@@ -5,6 +5,10 @@
   * Make `dulwich.archive` set the gzip header file modification time so that
     archives created from the same Git tree are always identical.
     (#577, Jonas Haag)
+
+  * Allow comment characters (#, ;) within configuration file strings
+    (Daniel Andersson, #579)
+
 0.18.6	2017-11-11
 
  BUG FIXES

+ 10 - 2
dulwich/config.py

@@ -262,8 +262,16 @@ def _check_section_name(name):
 
 
 def _strip_comments(line):
-    line = line.split(b"#")[0]
-    line = line.split(b";")[0]
+    comment_bytes = {ord(b"#"), ord(b";")}
+    quote = ord(b'"')
+    string_open = False
+    # Normalize line to bytearray for simple 2/3 compatibility
+    for i, character in enumerate(bytearray(line)):
+        # Comment characters outside balanced quotes denote comment start
+        if character == quote:
+            string_open = not string_open
+        elif not string_open and character in comment_bytes:
+            return line[:i]
     return line
 
 

+ 10 - 0
dulwich/tests/test_config.py

@@ -81,6 +81,16 @@ class ConfigFileTests(TestCase):
         cf = self.from_file(b"[section]\nbar= foo # a comment\n")
         self.assertEqual(ConfigFile({(b"section", ): {b"bar": b"foo"}}), cf)
 
+    def test_comment_character_within_value_string(self):
+        cf = self.from_file(b"[section]\nbar= \"foo#bar\"\n")
+        self.assertEqual(
+            ConfigFile({(b"section", ): {b"bar": b"foo#bar"}}), cf)
+
+    def test_comment_character_within_section_string(self):
+        cf = self.from_file(b"[branch \"foo#bar\"] # a comment\nbar= foo\n")
+        self.assertEqual(
+            ConfigFile({(b"branch", b"foo#bar"): {b"bar": b"foo"}}), cf)
+
     def test_from_file_section(self):
         cf = self.from_file(b"[core]\nfoo = bar\n")
         self.assertEqual(b"bar", cf.get((b"core", ), b"foo"))