|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import os |
4 | | -from enum import Enum |
5 | | -from functools import lru_cache |
6 | 4 | from pathlib import Path |
7 | 5 | from tempfile import NamedTemporaryFile |
8 | 6 | from typing import TYPE_CHECKING |
9 | 7 |
|
10 | 8 | from commitizen import cmd, out |
| 9 | +from commitizen.eol import EOLType as EOLType |
| 10 | +from commitizen.eol import smart_open as smart_open |
11 | 11 | from commitizen.exceptions import GitCommandError |
12 | 12 |
|
13 | 13 | if TYPE_CHECKING: |
14 | 14 | from collections.abc import Sequence |
15 | 15 |
|
16 | 16 |
|
17 | | -class EOLType(Enum): |
18 | | - """The EOL type from `git config core.eol`.""" |
19 | | - |
20 | | - LF = "lf" |
21 | | - CRLF = "crlf" |
22 | | - NATIVE = "native" |
23 | | - |
24 | | - @classmethod |
25 | | - def for_open(cls) -> str: |
26 | | - c = cmd.run(["git", "config", "core.eol"]) |
27 | | - eol = c.out.strip().upper() |
28 | | - return cls._char_for_open()[cls._safe_cast(eol)] |
29 | | - |
30 | | - @classmethod |
31 | | - def _safe_cast(cls, eol: str) -> EOLType: |
32 | | - try: |
33 | | - return cls[eol] |
34 | | - except KeyError: |
35 | | - return cls.NATIVE |
36 | | - |
37 | | - @classmethod |
38 | | - @lru_cache |
39 | | - def _char_for_open(cls) -> dict[EOLType, str]: |
40 | | - """Get the EOL character for `open()`.""" |
41 | | - return { |
42 | | - cls.LF: "\n", |
43 | | - cls.CRLF: "\r\n", |
44 | | - cls.NATIVE: os.linesep, |
45 | | - } |
46 | | - |
47 | | - |
48 | 17 | class GitObject: |
49 | 18 | rev: str |
50 | 19 | name: str |
@@ -323,11 +292,6 @@ def get_core_editor() -> str | None: |
323 | 292 | return None |
324 | 293 |
|
325 | 294 |
|
326 | | -def smart_open(*args, **kwargs): # type: ignore[no-untyped-def,unused-ignore] # noqa: ANN201 |
327 | | - """Open a file with the EOL style determined from Git.""" |
328 | | - return open(*args, newline=EOLType.for_open(), **kwargs) |
329 | | - |
330 | | - |
331 | 295 | def _get_log_as_str_list(start: str | None, end: str, args: Sequence[str]) -> list[str]: |
332 | 296 | """Get string representation of each log entry""" |
333 | 297 | delimiter = "----------commit-delimiter----------" |
|
0 commit comments