line_ending.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. import logging
  108. from collections.abc import Callable, Mapping
  109. from typing import TYPE_CHECKING, Any
  110. if TYPE_CHECKING:
  111. from .config import StackedConfig
  112. from .object_store import BaseObjectStore
  113. from . import replace_me
  114. from .attrs import GitAttributes, Pattern
  115. from .filters import FilterBlobNormalizer, FilterContext, FilterDriver, FilterRegistry
  116. from .object_store import iter_tree_contents
  117. from .objects import Blob, ObjectID
  118. from .patch import is_binary
  119. CRLF = b"\r\n"
  120. LF = b"\n"
  121. logger = logging.getLogger(__name__)
  122. class LineEndingFilter(FilterDriver):
  123. """Filter driver for line ending conversion."""
  124. def __init__(
  125. self,
  126. clean_conversion: Callable[[bytes], bytes] | None = None,
  127. smudge_conversion: Callable[[bytes], bytes] | None = None,
  128. binary_detection: bool = True,
  129. safecrlf: bytes = b"false",
  130. ):
  131. """Initialize LineEndingFilter."""
  132. self.clean_conversion = clean_conversion
  133. self.smudge_conversion = smudge_conversion
  134. self.binary_detection = binary_detection
  135. self.safecrlf = safecrlf
  136. @classmethod
  137. def from_config(
  138. cls, config: "StackedConfig | None", for_text_attr: bool = False
  139. ) -> "LineEndingFilter":
  140. """Create a LineEndingFilter from git configuration.
  141. Args:
  142. config: Git configuration stack
  143. for_text_attr: If True, always normalize on checkin (for text attribute)
  144. Returns:
  145. Configured LineEndingFilter instance
  146. """
  147. if config is None:
  148. # Default filter
  149. if for_text_attr:
  150. # For text attribute: always normalize on checkin
  151. return cls(
  152. clean_conversion=convert_crlf_to_lf,
  153. smudge_conversion=None,
  154. binary_detection=True,
  155. )
  156. else:
  157. # No config: no conversion
  158. return cls()
  159. # Get core.eol setting
  160. try:
  161. core_eol_raw = config.get("core", "eol")
  162. core_eol: str = (
  163. core_eol_raw.decode("ascii")
  164. if isinstance(core_eol_raw, bytes)
  165. else str(core_eol_raw)
  166. )
  167. except KeyError:
  168. core_eol = "native"
  169. # Get core.autocrlf setting
  170. try:
  171. autocrlf_raw = config.get("core", "autocrlf")
  172. autocrlf: bytes = (
  173. autocrlf_raw.lower()
  174. if isinstance(autocrlf_raw, bytes)
  175. else str(autocrlf_raw).lower().encode("ascii")
  176. )
  177. except KeyError:
  178. autocrlf = b"false"
  179. # Get core.safecrlf setting
  180. try:
  181. safecrlf_raw = config.get("core", "safecrlf")
  182. safecrlf = (
  183. safecrlf_raw
  184. if isinstance(safecrlf_raw, bytes)
  185. else safecrlf_raw.encode("utf-8")
  186. )
  187. except KeyError:
  188. safecrlf = b"false"
  189. if for_text_attr:
  190. # For text attribute: always normalize to LF on checkin
  191. # Smudge behavior depends on core.eol and core.autocrlf
  192. smudge_filter = get_smudge_filter(core_eol, autocrlf)
  193. clean_filter: Callable[[bytes], bytes] | None = convert_crlf_to_lf
  194. else:
  195. # Normal autocrlf behavior
  196. smudge_filter = get_smudge_filter(core_eol, autocrlf)
  197. clean_filter = get_clean_filter(core_eol, autocrlf)
  198. return cls(
  199. clean_conversion=clean_filter,
  200. smudge_conversion=smudge_filter,
  201. binary_detection=True,
  202. safecrlf=safecrlf,
  203. )
  204. def clean(self, data: bytes, path: bytes = b"") -> bytes:
  205. """Apply line ending conversion for checkin (working tree -> repository)."""
  206. if self.clean_conversion is None:
  207. return data
  208. # Skip binary files if detection is enabled
  209. if self.binary_detection and is_binary(data):
  210. return data
  211. converted = self.clean_conversion(data)
  212. # Check if conversion is safe
  213. if self.safecrlf != b"false":
  214. check_safecrlf(data, converted, self.safecrlf, path)
  215. return converted
  216. def smudge(self, data: bytes, path: bytes = b"") -> bytes:
  217. """Apply line ending conversion for checkout (repository -> working tree)."""
  218. if self.smudge_conversion is None:
  219. return data
  220. # Skip binary files if detection is enabled
  221. if self.binary_detection and is_binary(data):
  222. return data
  223. converted = self.smudge_conversion(data)
  224. # Check if conversion is safe
  225. if self.safecrlf != b"false":
  226. check_safecrlf(data, converted, self.safecrlf, path)
  227. return converted
  228. def cleanup(self) -> None:
  229. """Clean up any resources held by this filter driver."""
  230. # LineEndingFilter doesn't hold any resources that need cleanup
  231. def reuse(self, config: "StackedConfig", filter_name: str) -> bool:
  232. """Check if this filter driver should be reused with the given configuration."""
  233. # LineEndingFilter is lightweight and should always be recreated
  234. # to ensure it uses the latest configuration
  235. return False
  236. def convert_crlf_to_lf(text_hunk: bytes) -> bytes:
  237. """Convert CRLF in text hunk into LF.
  238. Args:
  239. text_hunk: A bytes string representing a text hunk
  240. Returns: The text hunk with the same type, with CRLF replaced into LF
  241. """
  242. return text_hunk.replace(CRLF, LF)
  243. def convert_lf_to_crlf(text_hunk: bytes) -> bytes:
  244. """Convert LF in text hunk into CRLF.
  245. Args:
  246. text_hunk: A bytes string representing a text hunk
  247. Returns: The text hunk with the same type, with LF replaced into CRLF
  248. """
  249. # Single-pass conversion: split on LF and join with CRLF
  250. # This avoids the double replacement issue
  251. parts = text_hunk.split(LF)
  252. # Remove any trailing CR to avoid CRCRLF
  253. cleaned_parts = []
  254. for i, part in enumerate(parts):
  255. if i < len(parts) - 1 and part.endswith(b"\r"):
  256. cleaned_parts.append(part[:-1])
  257. else:
  258. cleaned_parts.append(part)
  259. return CRLF.join(cleaned_parts)
  260. def check_safecrlf(
  261. original: bytes, converted: bytes, safecrlf: bytes, path: bytes = b""
  262. ) -> None:
  263. """Check if CRLF conversion is safe according to core.safecrlf setting.
  264. Args:
  265. original: Original content before conversion
  266. converted: Content after conversion
  267. safecrlf: Value of core.safecrlf config (b"true", b"warn", or b"false")
  268. path: Path to the file being checked (for error messages)
  269. Raises:
  270. ValueError: If safecrlf is "true" and conversion would lose data
  271. """
  272. if safecrlf == b"false":
  273. return
  274. # Check if conversion is reversible
  275. if safecrlf in (b"true", b"warn"):
  276. # For CRLF->LF conversion, check if converting back would recover original
  277. if CRLF in original and CRLF not in converted:
  278. # This was a CRLF->LF conversion
  279. recovered = convert_lf_to_crlf(converted)
  280. if recovered != original:
  281. msg = (
  282. f"CRLF would be replaced by LF in {path.decode('utf-8', 'replace')}"
  283. )
  284. if safecrlf == b"true":
  285. raise ValueError(msg)
  286. else: # warn
  287. logger.warning(msg)
  288. # For LF->CRLF conversion, check if converting back would recover original
  289. elif LF in original and CRLF in converted and CRLF not in original:
  290. # This was a LF->CRLF conversion
  291. recovered = convert_crlf_to_lf(converted)
  292. if recovered != original:
  293. msg = (
  294. f"LF would be replaced by CRLF in {path.decode('utf-8', 'replace')}"
  295. )
  296. if safecrlf == b"true":
  297. raise ValueError(msg)
  298. else: # warn
  299. logger.warning(msg)
  300. def get_smudge_filter(
  301. core_eol: str, core_autocrlf: bytes
  302. ) -> Callable[[bytes], bytes] | None:
  303. """Returns the correct smudge filter based on the passed arguments."""
  304. # Git attributes handling is done by the filter infrastructure
  305. return get_smudge_filter_autocrlf(core_autocrlf)
  306. def get_clean_filter(
  307. core_eol: str, core_autocrlf: bytes
  308. ) -> Callable[[bytes], bytes] | None:
  309. """Returns the correct clean filter based on the passed arguments."""
  310. # Git attributes handling is done by the filter infrastructure
  311. return get_clean_filter_autocrlf(core_autocrlf)
  312. def get_smudge_filter_autocrlf(
  313. core_autocrlf: bytes,
  314. ) -> Callable[[bytes], bytes] | None:
  315. """Returns the correct smudge filter base on autocrlf value.
  316. Args:
  317. core_autocrlf: The bytes configuration value of core.autocrlf.
  318. Valid values are: b'true', b'false' or b'input'.
  319. Returns: Either None if no filter has to be applied or a function
  320. accepting a single argument, a binary text hunk
  321. """
  322. if core_autocrlf == b"true":
  323. return convert_lf_to_crlf
  324. return None
  325. def get_clean_filter_autocrlf(
  326. core_autocrlf: bytes,
  327. ) -> Callable[[bytes], bytes] | None:
  328. """Returns the correct clean filter base on autocrlf value.
  329. Args:
  330. core_autocrlf: The bytes configuration value of core.autocrlf.
  331. Valid values are: b'true', b'false' or b'input'.
  332. Returns: Either None if no filter has to be applied or a function
  333. accepting a single argument, a binary text hunk
  334. """
  335. if core_autocrlf == b"true" or core_autocrlf == b"input":
  336. return convert_crlf_to_lf
  337. # Checking filter should never be `convert_lf_to_crlf`
  338. return None
  339. # Backwards compatibility wrappers
  340. @replace_me(since="0.23.1", remove_in="0.25.0")
  341. def get_checkout_filter(
  342. core_eol: str, core_autocrlf: bool | str, git_attributes: Mapping[str, Any]
  343. ) -> Callable[[bytes], bytes] | None:
  344. """Deprecated: Use get_smudge_filter instead."""
  345. # Convert core_autocrlf to bytes for compatibility
  346. if isinstance(core_autocrlf, bool):
  347. autocrlf_bytes = b"true" if core_autocrlf else b"false"
  348. else:
  349. autocrlf_bytes = (
  350. core_autocrlf.encode("utf-8")
  351. if isinstance(core_autocrlf, str)
  352. else core_autocrlf
  353. )
  354. return get_smudge_filter(core_eol, autocrlf_bytes)
  355. @replace_me(since="0.23.1", remove_in="0.25.0")
  356. def get_checkin_filter(
  357. core_eol: str, core_autocrlf: bool | str, git_attributes: Mapping[str, Any]
  358. ) -> Callable[[bytes], bytes] | None:
  359. """Deprecated: Use get_clean_filter instead."""
  360. # Convert core_autocrlf to bytes for compatibility
  361. if isinstance(core_autocrlf, bool):
  362. autocrlf_bytes = b"true" if core_autocrlf else b"false"
  363. else:
  364. autocrlf_bytes = (
  365. core_autocrlf.encode("utf-8")
  366. if isinstance(core_autocrlf, str)
  367. else core_autocrlf
  368. )
  369. return get_clean_filter(core_eol, autocrlf_bytes)
  370. @replace_me(since="0.23.1", remove_in="0.25.0")
  371. def get_checkout_filter_autocrlf(
  372. core_autocrlf: bytes,
  373. ) -> Callable[[bytes], bytes] | None:
  374. """Deprecated: Use get_smudge_filter_autocrlf instead."""
  375. return get_smudge_filter_autocrlf(core_autocrlf)
  376. @replace_me(since="0.23.1", remove_in="0.25.0")
  377. def get_checkin_filter_autocrlf(
  378. core_autocrlf: bytes,
  379. ) -> Callable[[bytes], bytes] | None:
  380. """Deprecated: Use get_clean_filter_autocrlf instead."""
  381. return get_clean_filter_autocrlf(core_autocrlf)
  382. class BlobNormalizer(FilterBlobNormalizer):
  383. """An object to store computation result of which filter to apply based on configuration, gitattributes, path and operation (checkin or checkout).
  384. This class maintains backward compatibility while using the filter infrastructure.
  385. """
  386. def __init__(
  387. self,
  388. config_stack: "StackedConfig",
  389. gitattributes: Mapping[str, Any],
  390. core_eol: str = "native",
  391. autocrlf: bytes = b"false",
  392. safecrlf: bytes = b"false",
  393. ) -> None:
  394. """Initialize FilteringBlobNormalizer."""
  395. # Set up a filter registry with line ending filters
  396. filter_registry = FilterRegistry(config_stack)
  397. # Create line ending filter if needed
  398. smudge_filter = get_smudge_filter(core_eol, autocrlf)
  399. clean_filter = get_clean_filter(core_eol, autocrlf)
  400. # Always register a text filter that can be used by gitattributes
  401. # Even if autocrlf is false, gitattributes text=true should work
  402. line_ending_filter = LineEndingFilter(
  403. clean_conversion=clean_filter or convert_crlf_to_lf,
  404. smudge_conversion=smudge_filter or convert_lf_to_crlf,
  405. binary_detection=True,
  406. safecrlf=safecrlf,
  407. )
  408. filter_registry.register_driver("text", line_ending_filter)
  409. # Convert dict gitattributes to GitAttributes object for parent class
  410. git_attrs_patterns = []
  411. for pattern_str, attrs in gitattributes.items():
  412. if isinstance(pattern_str, str):
  413. pattern_bytes = pattern_str.encode("utf-8")
  414. else:
  415. pattern_bytes = pattern_str
  416. pattern = Pattern(pattern_bytes)
  417. git_attrs_patterns.append((pattern, attrs))
  418. git_attributes = GitAttributes(git_attrs_patterns)
  419. # Create FilterContext for parent class
  420. filter_context = FilterContext(filter_registry)
  421. # Initialize parent class with gitattributes
  422. # The filter infrastructure will handle gitattributes processing
  423. super().__init__(config_stack, git_attributes, filter_context=filter_context)
  424. # Store original filters for backward compatibility
  425. self.fallback_read_filter = smudge_filter
  426. self.fallback_write_filter = clean_filter
  427. def checkin_normalize(self, blob: Blob, tree_path: bytes) -> Blob:
  428. """Normalize a blob during a checkin operation."""
  429. # First try to get filter from gitattributes (handled by parent)
  430. result = super().checkin_normalize(blob, tree_path)
  431. # Check if gitattributes explicitly disabled text conversion
  432. attrs = self.gitattributes.match_path(tree_path)
  433. if b"text" in attrs and attrs[b"text"] is False:
  434. # Explicitly marked as binary, no conversion
  435. return blob
  436. # If no filter was applied via gitattributes and we have a fallback filter
  437. # (autocrlf is enabled), apply it to all files
  438. if result is blob and self.fallback_write_filter is not None:
  439. # Apply the clean filter with binary detection
  440. # Get safecrlf from config
  441. safecrlf = b"false"
  442. if hasattr(self, "filter_registry") and hasattr(
  443. self.filter_registry, "config_stack"
  444. ):
  445. safecrlf = self.filter_registry.config_stack.get(
  446. b"core", b"safecrlf", b"false"
  447. )
  448. if hasattr(safecrlf, "encode"):
  449. safecrlf = safecrlf.encode("utf-8")
  450. line_ending_filter = LineEndingFilter(
  451. clean_conversion=self.fallback_write_filter,
  452. smudge_conversion=None,
  453. binary_detection=True,
  454. safecrlf=safecrlf,
  455. )
  456. filtered_data = line_ending_filter.clean(blob.data, tree_path)
  457. if filtered_data != blob.data:
  458. new_blob = Blob()
  459. new_blob.data = filtered_data
  460. return new_blob
  461. return result
  462. def checkout_normalize(self, blob: Blob, tree_path: bytes) -> Blob:
  463. """Normalize a blob during a checkout operation."""
  464. # First try to get filter from gitattributes (handled by parent)
  465. result = super().checkout_normalize(blob, tree_path)
  466. # Check if gitattributes explicitly disabled text conversion
  467. attrs = self.gitattributes.match_path(tree_path)
  468. if b"text" in attrs and attrs[b"text"] is False:
  469. # Explicitly marked as binary, no conversion
  470. return blob
  471. # If no filter was applied via gitattributes and we have a fallback filter
  472. # (autocrlf is enabled), apply it to all files
  473. if result is blob and self.fallback_read_filter is not None:
  474. # Apply the smudge filter with binary detection
  475. # Get safecrlf from config
  476. safecrlf = b"false"
  477. if hasattr(self, "filter_registry") and hasattr(
  478. self.filter_registry, "config_stack"
  479. ):
  480. safecrlf = self.filter_registry.config_stack.get(
  481. b"core", b"safecrlf", b"false"
  482. )
  483. if hasattr(safecrlf, "encode"):
  484. safecrlf = safecrlf.encode("utf-8")
  485. line_ending_filter = LineEndingFilter(
  486. clean_conversion=None,
  487. smudge_conversion=self.fallback_read_filter,
  488. binary_detection=True,
  489. safecrlf=safecrlf,
  490. )
  491. filtered_data = line_ending_filter.smudge(blob.data, tree_path)
  492. if filtered_data != blob.data:
  493. new_blob = Blob()
  494. new_blob.data = filtered_data
  495. return new_blob
  496. return result
  497. def normalize_blob(
  498. blob: Blob, conversion: Callable[[bytes], bytes], binary_detection: bool
  499. ) -> Blob:
  500. """Normalize blob by applying line ending conversion."""
  501. # Read the original blob
  502. data = blob.data
  503. # If we need to detect if a file is binary and the file is detected as
  504. # binary, do not apply the conversion function and return the original
  505. # chunked text
  506. if binary_detection is True:
  507. if is_binary(data):
  508. return blob
  509. # Now apply the conversion
  510. converted_data = conversion(data)
  511. new_blob = Blob()
  512. new_blob.data = converted_data
  513. return new_blob
  514. class TreeBlobNormalizer(BlobNormalizer):
  515. """Blob normalizer that tracks existing files in a tree."""
  516. def __init__(
  517. self,
  518. config_stack: "StackedConfig",
  519. git_attributes: Mapping[str, Any],
  520. object_store: "BaseObjectStore",
  521. tree: ObjectID | None = None,
  522. core_eol: str = "native",
  523. autocrlf: bytes = b"false",
  524. safecrlf: bytes = b"false",
  525. ) -> None:
  526. """Initialize TreeBlobNormalizer."""
  527. super().__init__(config_stack, git_attributes, core_eol, autocrlf, safecrlf)
  528. if tree:
  529. self.existing_paths = {
  530. name for name, _, _ in iter_tree_contents(object_store, tree)
  531. }
  532. else:
  533. self.existing_paths = set()
  534. def checkin_normalize(self, blob: Blob, tree_path: bytes) -> Blob:
  535. """Normalize blob for checkin, considering existing tree state."""
  536. # Existing files should only be normalized on checkin if:
  537. # 1. They were previously normalized on checkout (autocrlf=true), OR
  538. # 2. We have a write filter (autocrlf=true or autocrlf=input), OR
  539. # 3. They are new files
  540. if (
  541. self.fallback_read_filter is not None
  542. or self.fallback_write_filter is not None
  543. or tree_path not in self.existing_paths
  544. ):
  545. return super().checkin_normalize(blob, tree_path)
  546. return blob