Jelmer Vernooij 8 лет назад
Родитель
Сommit
4c82345e53
4 измененных файлов с 32 добавлено и 2 удалено
  1. 3 0
      NEWS
  2. 7 0
      dulwich/objects.py
  3. 2 2
      dulwich/patch.py
  4. 20 0
      dulwich/tests/test_objects.py

+ 3 - 0
NEWS

@@ -15,6 +15,9 @@
     allowing easy finding of current version based on Git tags.
     (Mark Mikofski)
 
+ * Add ``Blob.splitlines`` method.
+   (Jelmer Vernooij)
+
 
 0.15.0	2016-10-09
 

+ 7 - 0
dulwich/objects.py

@@ -568,6 +568,13 @@ class Blob(ShaFile):
         """
         super(Blob, self).check()
 
+    def splitlines(self):
+        """Return list of lines in this blob.
+
+        This preserves the original line endings.
+        """
+        return self.data.splitlines(True)
+
 
 def _parse_message(chunks):
     """Parse a message with a list of fields and a body.

+ 2 - 2
dulwich/patch.py

@@ -163,7 +163,7 @@ def write_object_diff(f, store, old_file, new_file, diff_binary=False):
         if not content:
             return []
         else:
-            return content.blob.splitlines(True)
+            return content.splitlines()
     f.writelines(gen_diff_header(
         (old_path, new_path), (old_mode, new_mode), (old_id, new_id)))
     old_content = content(old_mode, old_id)
@@ -217,7 +217,7 @@ def write_blob_diff(f, old_file, new_file):
     new_path = patch_filename(new_path, b"b")
     def lines(blob):
         if blob is not None:
-            return blob.data.splitlines(True)
+            return blob.splitlines()
         else:
             return []
     f.writelines(gen_diff_header(

+ 20 - 0
dulwich/tests/test_objects.py

@@ -136,6 +136,26 @@ class BlobReadTests(TestCase):
         b = Blob.from_string(string)
         self.assertEqual([string], b.chunked)
 
+    def test_splitlines(self):
+        b = Blob()
+        b.chunked = ['bl\na', 'blie']
+        self.assertEqual(['bl\n', 'ablie'], b.splitlines())
+
+    def test_splitlines_nonewline(self):
+        b = Blob()
+        b.chunked = ['bl\na', 'blie', 'bloe\n']
+        self.assertEqual(['bl\n', 'abliebloe\n'], b.splitlines())
+
+    def test_splitlines_firstempty(self):
+        b = Blob()
+        b.chunked = ['', 'bl\na', 'blie', 'bloe\n']
+        self.assertEqual(['bl\n', 'abliebloe\n'], b.splitlines())
+
+    def test_splitlines_nonewline(self):
+        b = Blob()
+        b.chunked = []
+        self.assertEqual([], b.splitlines())
+
     def test_set_chunks(self):
         b = Blob()
         b.chunked = [b'te', b'st', b' 5\n']