瀏覽代碼

Plug the end-of-line filter computing in the repo

It's pretty basic right now but does the job.
Boris Feld 6 年之前
父節點
當前提交
debcedf952
共有 4 個文件被更改,包括 41 次插入13 次删除
  1. 1 1
      dulwich/config.py
  2. 36 4
      dulwich/line_ending.py
  3. 1 6
      dulwich/repo.py
  4. 3 2
      dulwich/tests/test_porcelain.py

+ 1 - 1
dulwich/config.py

@@ -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

+ 36 - 4
dulwich/line_ending.py

@@ -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

+ 1 - 6
dulwich/repo.py

@@ -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):

+ 3 - 2
dulwich/tests/test_porcelain.py

@@ -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(