line_ending.py 25 KB

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