line_ending.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. # line_ending.py -- Line ending conversion functions
  2. # Copyright (C) 2018-2018 Boris Feld <boris.feld@comet.ml>
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as published by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. r"""All line-ending related functions, from conversions to config processing.
  22. Line-ending normalization is a complex beast. Here is some notes and details
  23. about how it seems to work.
  24. The normalization is a two-fold process that happens at two moments:
  25. - When reading a file from the index and to the working directory. For example
  26. when doing a ``git clone`` or ``git checkout`` call. This is called the
  27. smudge filter (repository -> working tree).
  28. - When writing a file to the index from the working directory. For example
  29. when doing a ``git add`` call. This is called the clean filter (working tree
  30. -> repository).
  31. Note that when checking status (getting unstaged changes), whether or not
  32. normalization is done on write depends on whether or not the file in the
  33. working dir has also been normalized on read:
  34. - For autocrlf=true all files are always normalized on both read and write.
  35. - For autocrlf=input files are only normalized on write if they are newly
  36. "added". Since files which are already committed are not normalized on
  37. checkout into the working tree, they are also left alone when staging
  38. modifications into the index.
  39. One thing to know is that Git does line-ending normalization only on text
  40. files. How does Git know that a file is text? We can either mark a file as a
  41. text file, a binary file or ask Git to automatically decides. Git has an
  42. heuristic to detect if a file is a text file or a binary file. It seems based
  43. on the percentage of non-printable characters in files.
  44. The code for this heuristic is here:
  45. https://git.kernel.org/pub/scm/git/git.git/tree/convert.c#n46
  46. Dulwich have an implementation with a slightly different heuristic, the
  47. `dulwich.patch.is_binary` function.
  48. The binary detection heuristic implementation is close to the one in JGit:
  49. https://github.com/eclipse/jgit/blob/f6873ffe522bbc3536969a3a3546bf9a819b92bf/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawText.java#L300
  50. There is multiple variables that impact the normalization.
  51. First, a repository can contains a ``.gitattributes`` file (or more than one...)
  52. that can further customize the operation on some file patterns, for example:
  53. \*.txt text
  54. Force all ``.txt`` files to be treated as text files and to have their lines
  55. endings normalized.
  56. \*.jpg -text
  57. Force all ``.jpg`` files to be treated as binary files and to not have their
  58. lines endings converted.
  59. \*.vcproj text eol=crlf
  60. Force all ``.vcproj`` files to be treated as text files and to have their lines
  61. endings converted into ``CRLF`` in working directory no matter the native EOL of
  62. the platform.
  63. \*.sh text eol=lf
  64. Force all ``.sh`` files to be treated as text files and to have their lines
  65. endings converted into ``LF`` in working directory no matter the native EOL of
  66. the platform.
  67. If the ``eol`` attribute is not defined, Git uses the ``core.eol`` configuration
  68. value described later.
  69. \* text=auto
  70. Force all files to be scanned by the text file heuristic detection and to have
  71. their line endings normalized in case they are detected as text files.
  72. Git also have a obsolete attribute named ``crlf`` that can be translated to the
  73. corresponding text attribute value.
  74. Then there are some configuration option (that can be defined at the
  75. repository or user level):
  76. - core.autocrlf
  77. - core.eol
  78. ``core.autocrlf`` is taken into account for all files that doesn't have a ``text``
  79. attribute defined in ``.gitattributes``; it takes three possible values:
  80. - ``true``: This forces all files on the working directory to have CRLF
  81. line-endings in the working directory and convert line-endings to LF
  82. when writing to the index. When autocrlf is set to true, eol value is
  83. ignored.
  84. - ``input``: Quite similar to the ``true`` value but only applies the clean
  85. filter, ie line-ending of new files added to the index will get their
  86. line-endings converted to LF.
  87. - ``false`` (default): No normalization is done.
  88. ``core.eol`` is the top-level configuration to define the line-ending to use
  89. when applying the smudge filter. It takes three possible values:
  90. - ``lf``: When normalization is done, force line-endings to be ``LF`` in the
  91. working directory.
  92. - ``crlf``: When normalization is done, force line-endings to be ``CRLF`` in
  93. the working directory.
  94. - ``native`` (default): When normalization is done, force line-endings to be
  95. the platform's native line ending.
  96. One thing to remember is when line-ending normalization is done on a file, Git
  97. always normalize line-ending to ``LF`` when writing to the index.
  98. There are sources that seems to indicate that Git won't do line-ending
  99. normalization when a file contains mixed line-endings. I think this logic
  100. might be in text / binary detection heuristic but couldn't find it yet.
  101. Sources:
  102. - https://git-scm.com/docs/git-config#git-config-coreeol
  103. - https://git-scm.com/docs/git-config#git-config-coreautocrlf
  104. - https://git-scm.com/docs/gitattributes#_checking_out_and_checking_in
  105. - https://adaptivepatchwork.com/2012/03/01/mind-the-end-of-your-line/
  106. """
  107. from typing import TYPE_CHECKING, Any, Callable, Optional, Union
  108. if TYPE_CHECKING:
  109. from .config import StackedConfig
  110. from .object_store import BaseObjectStore
  111. from . import replace_me
  112. from .attrs import GitAttributes, Pattern
  113. from .filters import FilterBlobNormalizer, FilterDriver, FilterRegistry
  114. from .object_store import iter_tree_contents
  115. from .objects import Blob, ObjectID
  116. from .patch import is_binary
  117. CRLF = b"\r\n"
  118. LF = b"\n"
  119. class LineEndingFilter(FilterDriver):
  120. """Filter driver for line ending conversion."""
  121. def __init__(
  122. self,
  123. clean_conversion: Optional[Callable[[bytes], bytes]] = None,
  124. smudge_conversion: Optional[Callable[[bytes], bytes]] = None,
  125. binary_detection: bool = True,
  126. ):
  127. """Initialize LineEndingFilter."""
  128. self.clean_conversion = clean_conversion
  129. self.smudge_conversion = smudge_conversion
  130. self.binary_detection = binary_detection
  131. def clean(self, data: bytes) -> bytes:
  132. """Apply line ending conversion for checkin (working tree -> repository)."""
  133. if self.clean_conversion is None:
  134. return data
  135. # Skip binary files if detection is enabled
  136. if self.binary_detection and is_binary(data):
  137. return data
  138. return self.clean_conversion(data)
  139. def smudge(self, data: bytes, path: bytes = b"") -> bytes:
  140. """Apply line ending conversion for checkout (repository -> working tree)."""
  141. if self.smudge_conversion is None:
  142. return data
  143. # Skip binary files if detection is enabled
  144. if self.binary_detection and is_binary(data):
  145. return data
  146. return self.smudge_conversion(data)
  147. def convert_crlf_to_lf(text_hunk: bytes) -> bytes:
  148. """Convert CRLF in text hunk into LF.
  149. Args:
  150. text_hunk: A bytes string representing a text hunk
  151. Returns: The text hunk with the same type, with CRLF replaced into LF
  152. """
  153. return text_hunk.replace(CRLF, LF)
  154. def convert_lf_to_crlf(text_hunk: bytes) -> bytes:
  155. """Convert LF in text hunk into CRLF.
  156. Args:
  157. text_hunk: A bytes string representing a text hunk
  158. Returns: The text hunk with the same type, with LF replaced into CRLF
  159. """
  160. # Single-pass conversion: split on LF and join with CRLF
  161. # This avoids the double replacement issue
  162. parts = text_hunk.split(LF)
  163. # Remove any trailing CR to avoid CRCRLF
  164. cleaned_parts = []
  165. for i, part in enumerate(parts):
  166. if i < len(parts) - 1 and part.endswith(b"\r"):
  167. cleaned_parts.append(part[:-1])
  168. else:
  169. cleaned_parts.append(part)
  170. return CRLF.join(cleaned_parts)
  171. def get_smudge_filter(
  172. core_eol: str, core_autocrlf: bytes
  173. ) -> Optional[Callable[[bytes], bytes]]:
  174. """Returns the correct smudge filter based on the passed arguments."""
  175. # Git attributes handling is done by the filter infrastructure
  176. return get_smudge_filter_autocrlf(core_autocrlf)
  177. def get_clean_filter(
  178. core_eol: str, core_autocrlf: bytes
  179. ) -> Optional[Callable[[bytes], bytes]]:
  180. """Returns the correct clean filter based on the passed arguments."""
  181. # Git attributes handling is done by the filter infrastructure
  182. return get_clean_filter_autocrlf(core_autocrlf)
  183. def get_smudge_filter_autocrlf(
  184. core_autocrlf: bytes,
  185. ) -> Optional[Callable[[bytes], bytes]]:
  186. """Returns the correct smudge filter base on autocrlf value.
  187. Args:
  188. core_autocrlf: The bytes configuration value of core.autocrlf.
  189. Valid values are: b'true', b'false' or b'input'.
  190. Returns: Either None if no filter has to be applied or a function
  191. accepting a single argument, a binary text hunk
  192. """
  193. if core_autocrlf == b"true":
  194. return convert_lf_to_crlf
  195. return None
  196. def get_clean_filter_autocrlf(
  197. core_autocrlf: bytes,
  198. ) -> Optional[Callable[[bytes], bytes]]:
  199. """Returns the correct clean filter base on autocrlf value.
  200. Args:
  201. core_autocrlf: The bytes configuration value of core.autocrlf.
  202. Valid values are: b'true', b'false' or b'input'.
  203. Returns: Either None if no filter has to be applied or a function
  204. accepting a single argument, a binary text hunk
  205. """
  206. if core_autocrlf == b"true" or core_autocrlf == b"input":
  207. return convert_crlf_to_lf
  208. # Checking filter should never be `convert_lf_to_crlf`
  209. return None
  210. # Backwards compatibility wrappers
  211. @replace_me(since="0.23.1", remove_in="0.25.0")
  212. def get_checkout_filter(
  213. core_eol: str, core_autocrlf: Union[bool, str], git_attributes: dict[str, Any]
  214. ) -> Optional[Callable[[bytes], bytes]]:
  215. """Deprecated: Use get_smudge_filter instead."""
  216. # Convert core_autocrlf to bytes for compatibility
  217. if isinstance(core_autocrlf, bool):
  218. autocrlf_bytes = b"true" if core_autocrlf else b"false"
  219. else:
  220. autocrlf_bytes = (
  221. core_autocrlf.encode("utf-8")
  222. if isinstance(core_autocrlf, str)
  223. else core_autocrlf
  224. )
  225. return get_smudge_filter(core_eol, autocrlf_bytes)
  226. @replace_me(since="0.23.1", remove_in="0.25.0")
  227. def get_checkin_filter(
  228. core_eol: str, core_autocrlf: Union[bool, str], git_attributes: dict[str, Any]
  229. ) -> Optional[Callable[[bytes], bytes]]:
  230. """Deprecated: Use get_clean_filter instead."""
  231. # Convert core_autocrlf to bytes for compatibility
  232. if isinstance(core_autocrlf, bool):
  233. autocrlf_bytes = b"true" if core_autocrlf else b"false"
  234. else:
  235. autocrlf_bytes = (
  236. core_autocrlf.encode("utf-8")
  237. if isinstance(core_autocrlf, str)
  238. else core_autocrlf
  239. )
  240. return get_clean_filter(core_eol, autocrlf_bytes)
  241. @replace_me(since="0.23.1", remove_in="0.25.0")
  242. def get_checkout_filter_autocrlf(
  243. core_autocrlf: bytes,
  244. ) -> Optional[Callable[[bytes], bytes]]:
  245. """Deprecated: Use get_smudge_filter_autocrlf instead."""
  246. return get_smudge_filter_autocrlf(core_autocrlf)
  247. @replace_me(since="0.23.1", remove_in="0.25.0")
  248. def get_checkin_filter_autocrlf(
  249. core_autocrlf: bytes,
  250. ) -> Optional[Callable[[bytes], bytes]]:
  251. """Deprecated: Use get_clean_filter_autocrlf instead."""
  252. return get_clean_filter_autocrlf(core_autocrlf)
  253. class BlobNormalizer(FilterBlobNormalizer):
  254. """An object to store computation result of which filter to apply based on configuration, gitattributes, path and operation (checkin or checkout).
  255. This class maintains backward compatibility while using the filter infrastructure.
  256. """
  257. def __init__(
  258. self,
  259. config_stack: "StackedConfig",
  260. gitattributes: dict[str, Any],
  261. core_eol: str = "native",
  262. autocrlf: bytes = b"false",
  263. ) -> None:
  264. """Initialize FilteringBlobNormalizer."""
  265. # Set up a filter registry with line ending filters
  266. filter_registry = FilterRegistry(config_stack)
  267. # Create line ending filter if needed
  268. smudge_filter = get_smudge_filter(core_eol, autocrlf)
  269. clean_filter = get_clean_filter(core_eol, autocrlf)
  270. # Always register a text filter that can be used by gitattributes
  271. # Even if autocrlf is false, gitattributes text=true should work
  272. line_ending_filter = LineEndingFilter(
  273. clean_conversion=clean_filter or convert_crlf_to_lf,
  274. smudge_conversion=smudge_filter or convert_lf_to_crlf,
  275. binary_detection=True,
  276. )
  277. filter_registry.register_driver("text", line_ending_filter)
  278. # Convert dict gitattributes to GitAttributes object for parent class
  279. git_attrs_patterns = []
  280. for pattern_str, attrs in gitattributes.items():
  281. if isinstance(pattern_str, str):
  282. pattern_bytes = pattern_str.encode("utf-8")
  283. else:
  284. pattern_bytes = pattern_str
  285. pattern = Pattern(pattern_bytes)
  286. git_attrs_patterns.append((pattern, attrs))
  287. git_attributes = GitAttributes(git_attrs_patterns)
  288. # Initialize parent class with gitattributes
  289. # The filter infrastructure will handle gitattributes processing
  290. super().__init__(config_stack, git_attributes, filter_registry)
  291. # Store original filters for backward compatibility
  292. self.fallback_read_filter = smudge_filter
  293. self.fallback_write_filter = clean_filter
  294. def checkin_normalize(self, blob: Blob, tree_path: bytes) -> Blob:
  295. """Normalize a blob during a checkin operation."""
  296. # First try to get filter from gitattributes (handled by parent)
  297. result = super().checkin_normalize(blob, tree_path)
  298. # Check if gitattributes explicitly disabled text conversion
  299. attrs = self.gitattributes.match_path(tree_path)
  300. if b"text" in attrs and attrs[b"text"] is False:
  301. # Explicitly marked as binary, no conversion
  302. return blob
  303. # If no filter was applied via gitattributes and we have a fallback filter
  304. # (autocrlf is enabled), apply it to all files
  305. if result is blob and self.fallback_write_filter is not None:
  306. # Apply the clean filter with binary detection
  307. line_ending_filter = LineEndingFilter(
  308. clean_conversion=self.fallback_write_filter,
  309. smudge_conversion=None,
  310. binary_detection=True,
  311. )
  312. filtered_data = line_ending_filter.clean(blob.data)
  313. if filtered_data != blob.data:
  314. new_blob = Blob()
  315. new_blob.data = filtered_data
  316. return new_blob
  317. return result
  318. def checkout_normalize(self, blob: Blob, tree_path: bytes) -> Blob:
  319. """Normalize a blob during a checkout operation."""
  320. # First try to get filter from gitattributes (handled by parent)
  321. result = super().checkout_normalize(blob, tree_path)
  322. # Check if gitattributes explicitly disabled text conversion
  323. attrs = self.gitattributes.match_path(tree_path)
  324. if b"text" in attrs and attrs[b"text"] is False:
  325. # Explicitly marked as binary, no conversion
  326. return blob
  327. # If no filter was applied via gitattributes and we have a fallback filter
  328. # (autocrlf is enabled), apply it to all files
  329. if result is blob and self.fallback_read_filter is not None:
  330. # Apply the smudge filter with binary detection
  331. line_ending_filter = LineEndingFilter(
  332. clean_conversion=None,
  333. smudge_conversion=self.fallback_read_filter,
  334. binary_detection=True,
  335. )
  336. filtered_data = line_ending_filter.smudge(blob.data)
  337. if filtered_data != blob.data:
  338. new_blob = Blob()
  339. new_blob.data = filtered_data
  340. return new_blob
  341. return result
  342. def normalize_blob(
  343. blob: Blob, conversion: Callable[[bytes], bytes], binary_detection: bool
  344. ) -> Blob:
  345. """Normalize blob by applying line ending conversion."""
  346. # Read the original blob
  347. data = blob.data
  348. # If we need to detect if a file is binary and the file is detected as
  349. # binary, do not apply the conversion function and return the original
  350. # chunked text
  351. if binary_detection is True:
  352. if is_binary(data):
  353. return blob
  354. # Now apply the conversion
  355. converted_data = conversion(data)
  356. new_blob = Blob()
  357. new_blob.data = converted_data
  358. return new_blob
  359. class TreeBlobNormalizer(BlobNormalizer):
  360. """Blob normalizer that tracks existing files in a tree."""
  361. def __init__(
  362. self,
  363. config_stack: "StackedConfig",
  364. git_attributes: dict[str, Any],
  365. object_store: "BaseObjectStore",
  366. tree: Optional[ObjectID] = None,
  367. core_eol: str = "native",
  368. autocrlf: bytes = b"false",
  369. ) -> None:
  370. """Initialize TreeBlobNormalizer."""
  371. super().__init__(config_stack, git_attributes, core_eol, autocrlf)
  372. if tree:
  373. self.existing_paths = {
  374. name for name, _, _ in iter_tree_contents(object_store, tree)
  375. }
  376. else:
  377. self.existing_paths = set()
  378. def checkin_normalize(self, blob: Blob, tree_path: bytes) -> Blob:
  379. """Normalize blob for checkin, considering existing tree state."""
  380. # Existing files should only be normalized on checkin if it was
  381. # previously normalized on checkout
  382. if (
  383. self.fallback_read_filter is not None
  384. or tree_path not in self.existing_paths
  385. ):
  386. return super().checkin_normalize(blob, tree_path)
  387. return blob