瀏覽代碼

Handle continuation lines that use windows-style line endings.

Jelmer Vernooij 2 年之前
父節點
當前提交
5143716abe
共有 3 個文件被更改,包括 16 次插入8 次删除
  1. 3 0
      NEWS
  2. 5 0
      dulwich/config.py
  3. 8 8
      dulwich/tests/test_config.py

+ 3 - 0
NEWS

@@ -1,5 +1,8 @@
 0.20.47	UNRELEASED
 
+ * Support \r\n line endings with continuations when parsing
+   configuration files.  (Jelmer Vernooij)
+
  * Fix handling of SymrefLoop in RefsContainer.__setitem__.
    (Dominic Davis-Foster, Jelmer Vernooij)
 

+ 5 - 0
dulwich/config.py

@@ -447,6 +447,7 @@ def _parse_string(value: bytes) -> bytes:
 def _escape_value(value: bytes) -> bytes:
     """Escape a value."""
     value = value.replace(b"\\", b"\\\\")
+    value = value.replace(b"\r", b"\\r")
     value = value.replace(b"\n", b"\\n")
     value = value.replace(b"\t", b"\\t")
     value = value.replace(b'"', b'\\"')
@@ -565,6 +566,8 @@ class ConfigFile(ConfigDict):
                     raise ValueError("invalid variable name %r" % setting)
                 if value.endswith(b"\\\n"):
                     continuation = value[:-2]
+                elif value.endswith(b"\\\r\n"):
+                    continuation = value[:-3]
                 else:
                     continuation = None
                     value = _parse_string(value)
@@ -573,6 +576,8 @@ class ConfigFile(ConfigDict):
             else:  # continuation line
                 if line.endswith(b"\\\n"):
                     continuation += line[:-2]
+                elif line.endswith(b"\\\r\n"):
+                    continuation += line[:-3]
                 else:
                     continuation += line
                     value = _parse_string(continuation)

+ 8 - 8
dulwich/tests/test_config.py

@@ -196,14 +196,14 @@ class ConfigFileTests(TestCase):
         cf = self.from_file(b"[branch.foo] foo = bar\n")
         self.assertEqual(b"bar", cf.get((b"branch", b"foo"), b"foo"))
 
-    def test_quoted_newlines(self):
-        cf = self.from_file(br"""
-[alias]
-  c = '!f() { \
-    printf '[git commit -m \"%s\"]\n' \"$*\" && \
-    git commit -m \"$*\"; \
-  }; f'
-""")
+    def test_quoted_newlines_windows(self):
+        cf = self.from_file(
+            b"[alias]\r\n"
+            b"c = '!f() { \\\r\n"
+            b" printf '[git commit -m \\\"%s\\\"]\\n' \\\"$*\\\" && \\\r\n"
+            b" git commit -m \\\"$*\\\"; \\\r\n"
+            b" }; f'\r\n")
+        self.assertEqual(list(cf.sections()), [(b'alias', )])
         self.assertEqual(
             b'\'!f() { printf \'[git commit -m "%s"]\n\' '
             b'"$*" && git commit -m "$*"',