Plug the end-of-line filter computing in the repo
authorBoris Feld <boris.feld@comet.ml>
Tue, 11 Dec 2018 09:06:16 +0000 (10:06 +0100)
committerBoris Feld <boris.feld@comet.ml>
Tue, 11 Dec 2018 09:06:16 +0000 (10:06 +0100)
It's pretty basic right now but does the job.

dulwich/config.py
dulwich/line_ending.py
dulwich/repo.py
dulwich/tests/test_porcelain.py

index 416d5524657066bca326ee415a4cc7d938d987cb..e7bf6ab160a4b6e1eefd86555742afef852d7262 100644 (file)
@@ -129,7 +129,7 @@ class Config(object):
     def get_boolean(self, section, name, default=None):
         """Retrieve a configuration setting as boolean.
 
-        :param section: Tuple with section name and optional subsection namee
+        :param section: Tuple with section name and optional subsection name
         :param name: Name of the setting, including section and possible
             subsection.
         :return: Contents of the setting
index d16564b77dc8fc491ac214bfd3a0be20230b8e3a..e6819dc71698d8527eb7591a289ef46625b8fdf9 100644 (file)
@@ -152,6 +152,24 @@ def convert_lf_to_crlf(text_hunk):
     return intermediary.replace(LF, CRLF)
 
 
+def get_checkout_filter(core_eol, core_autocrlf, git_attributes):
+    """ Returns the correct checkout filter based on the passed arguments
+    """
+    # TODO this function should process the git_attributes for the path and if
+    # the text attribute is not defined, fallback on the
+    # get_checkout_filter_autocrlf function with the autocrlf value
+    return get_checkout_filter_autocrlf(core_autocrlf)
+
+
+def get_checkin_filter(core_eol, core_autocrlf, git_attributes):
+    """ Returns the correct checkin filter based on the passed arguments
+    """
+    # TODO this function should process the git_attributes for the path and if
+    # the text attribute is not defined, fallback on the
+    # get_checkin_filter_autocrlf function with the autocrlf value
+    return get_checkin_filter_autocrlf(core_autocrlf)
+
+
 def get_checkout_filter_autocrlf(core_autocrlf):
     """ Returns the correct checkout filter base on autocrlf value
 
@@ -188,13 +206,27 @@ class BlobNormalizer(object):
     on configuration, gitattributes, path and operation (checkin or checkout)
     """
 
-    def __init__(self, config_stack, gitattributes, read_filter, write_filter):
+    def __init__(self, config_stack, gitattributes):
         self.config_stack = config_stack
         self.gitattributes = gitattributes
 
-        # TODO compute them based on passed values
-        self.fallback_read_filter = read_filter
-        self.fallback_write_filter = write_filter
+        # Compute which filters we needs based on parameters
+        try:
+            core_eol = config_stack.get("core", "eol")
+        except KeyError:
+            core_eol = "native"
+
+        try:
+            core_autocrlf = config_stack.get("core", "autocrlf").lower()
+        except KeyError:
+            core_autocrlf = False
+
+        self.fallback_read_filter = get_checkout_filter(
+            core_eol, core_autocrlf, self.gitattributes
+        )
+        self.fallback_write_filter = get_checkin_filter(
+            core_eol, core_autocrlf, self.gitattributes
+        )
 
     def checkin_normalize(self, blob, tree_path):
         """ Normalize a blob during a checkin operation
index f5151cfacbad797544681452c1a4a7bab521b236..e5efb7175bc56f091b0f037f0f6d74ca7ee46f05 100644 (file)
@@ -866,11 +866,6 @@ class Repo(BaseRepo):
         self.hooks['commit-msg'] = CommitMsgShellHook(self.controldir())
         self.hooks['post-commit'] = PostCommitShellHook(self.controldir())
 
-        # Line ending convert filters
-        # TODO: Set them based on configuration
-        self.read_filter = None
-        self.write_filter = None
-
     def _write_reflog(self, ref, old_sha, new_sha, committer, timestamp,
                       timezone, message):
         from .reflog import format_reflog_line
@@ -1273,7 +1268,7 @@ class Repo(BaseRepo):
         """
         # TODO Parse the git attributes files
         git_attributes = {}
-        return BlobNormalizer(self.get_config_stack(), git_attributes, self.read_filter, self.write_filter)
+        return BlobNormalizer(self.get_config_stack(), git_attributes)
 
 
 class MemoryRepo(BaseRepo):
index 3b7ce5ed8233525f08501510523d7ba6032eee06..4b24c6737d39cc970cb1f39903287f4b9c06d3f1 100644 (file)
@@ -33,7 +33,6 @@ import time
 
 from dulwich import porcelain
 from dulwich.diff_tree import tree_changes
-from dulwich.line_ending import convert_crlf_to_lf
 from dulwich.objects import (
     Blob,
     Tag,
@@ -949,7 +948,9 @@ class StatusTests(PorcelainTestCase):
             f.write(b'line1\r\nline2')
 
         # TODO: It should be set automatically by looking at the configuration
-        self.repo.write_filter = convert_crlf_to_lf
+        c = self.repo.get_config()
+        c.set("core", "autocrlf", True)
+        c.write_to_path()
 
         results = porcelain.status(self.repo)
         self.assertDictEqual(