diff --git a/commitizen/bump.py b/commitizen/bump.py index a858535826..fa8da6fea9 100644 --- a/commitizen/bump.py +++ b/commitizen/bump.py @@ -7,8 +7,8 @@ from typing import TYPE_CHECKING from commitizen.defaults import BUMP_MESSAGE +from commitizen.eol import smart_open from commitizen.exceptions import CurrentVersionNotFoundError -from commitizen.git import smart_open if TYPE_CHECKING: from collections.abc import Generator, Iterable diff --git a/commitizen/commands/changelog.py b/commitizen/commands/changelog.py index abe03a9241..85ddc902bf 100644 --- a/commitizen/commands/changelog.py +++ b/commitizen/commands/changelog.py @@ -8,6 +8,7 @@ from commitizen import changelog, defaults, factory, git, out from commitizen.changelog_formats import get_changelog_format from commitizen.cz.utils import strip_local_version +from commitizen.eol import smart_open from commitizen.exceptions import ( DryRunExit, NoCommitsFoundError, @@ -16,7 +17,6 @@ NotAGitProjectError, NotAllowed, ) -from commitizen.git import GitTag, smart_open from commitizen.tags import TagRules from commitizen.version_schemes import get_version_scheme @@ -24,6 +24,7 @@ from collections.abc import Generator, Iterable from commitizen.config import BaseConfig + from commitizen.git import GitTag class ChangelogArgs(TypedDict, total=False): diff --git a/commitizen/commands/commit.py b/commitizen/commands/commit.py index 7d5a0fb975..e82f05d11c 100644 --- a/commitizen/commands/commit.py +++ b/commitizen/commands/commit.py @@ -15,6 +15,7 @@ from commitizen import factory, git, out from commitizen.cz.exceptions import CzException from commitizen.cz.utils import get_backup_file_path +from commitizen.eol import smart_open from commitizen.exceptions import ( CommitError, CommitMessageLengthExceededError, @@ -26,7 +27,6 @@ NotAllowed, NothingToCommitError, ) -from commitizen.git import smart_open if TYPE_CHECKING: from commitizen.config import BaseConfig diff --git a/commitizen/commands/init.py b/commitizen/commands/init.py index 9127f8eb4d..84ad19e534 100644 --- a/commitizen/commands/init.py +++ b/commitizen/commands/init.py @@ -11,12 +11,13 @@ from commitizen.config.factory import create_config from commitizen.cz import registry from commitizen.defaults import CONFIG_FILES, DEFAULT_SETTINGS +from commitizen.eol import smart_open from commitizen.exceptions import ( InitFailedError, MissingCzCustomizeConfigError, NoAnswersError, ) -from commitizen.git import get_latest_tag_name, get_tag_names, smart_open +from commitizen.git import get_latest_tag_name, get_tag_names from commitizen.version_schemes import ( KNOWN_SCHEMES, VersionProtocol, diff --git a/commitizen/config/json_config.py b/commitizen/config/json_config.py index 688a6b9fec..fba0b30ea3 100644 --- a/commitizen/config/json_config.py +++ b/commitizen/config/json_config.py @@ -3,8 +3,8 @@ import json from typing import TYPE_CHECKING +from commitizen.eol import smart_open from commitizen.exceptions import InvalidConfigurationError -from commitizen.git import smart_open from .base_config import BaseConfig diff --git a/commitizen/config/yaml_config.py b/commitizen/config/yaml_config.py index 1e9610e17a..7c73aa86f8 100644 --- a/commitizen/config/yaml_config.py +++ b/commitizen/config/yaml_config.py @@ -4,8 +4,8 @@ import yaml +from commitizen.eol import smart_open from commitizen.exceptions import InvalidConfigurationError -from commitizen.git import smart_open from .base_config import BaseConfig diff --git a/commitizen/eol.py b/commitizen/eol.py new file mode 100644 index 0000000000..5f95283af9 --- /dev/null +++ b/commitizen/eol.py @@ -0,0 +1,45 @@ +"""File line endings based on the `core.eol` git setting.""" + +from __future__ import annotations + +import os +from enum import Enum +from functools import lru_cache + +from commitizen import cmd + + +class EOLType(Enum): + """The EOL type from `git config core.eol`.""" + + LF = "lf" + CRLF = "crlf" + NATIVE = "native" + + @classmethod + def for_open(cls) -> str: + c = cmd.run(["git", "config", "core.eol"]) + eol = c.out.strip().upper() + return cls._char_for_open()[cls._safe_cast(eol)] + + @classmethod + def _safe_cast(cls, eol: str) -> EOLType: + try: + return cls[eol] + except KeyError: + return cls.NATIVE + + @classmethod + @lru_cache + def _char_for_open(cls) -> dict[EOLType, str]: + """Get the EOL character for `open()`.""" + return { + cls.LF: "\n", + cls.CRLF: "\r\n", + cls.NATIVE: os.linesep, + } + + +def smart_open(*args, **kwargs): # type: ignore[no-untyped-def,unused-ignore] # noqa: ANN201 + """Open a file with the EOL style determined from Git.""" + return open(*args, newline=EOLType.for_open(), **kwargs) diff --git a/commitizen/git.py b/commitizen/git.py index ce9f440c95..c0307b28d5 100644 --- a/commitizen/git.py +++ b/commitizen/git.py @@ -1,50 +1,19 @@ from __future__ import annotations import os -from enum import Enum -from functools import lru_cache from pathlib import Path from tempfile import NamedTemporaryFile from typing import TYPE_CHECKING from commitizen import cmd, out +from commitizen.eol import EOLType as EOLType +from commitizen.eol import smart_open as smart_open from commitizen.exceptions import GitCommandError if TYPE_CHECKING: from collections.abc import Sequence -class EOLType(Enum): - """The EOL type from `git config core.eol`.""" - - LF = "lf" - CRLF = "crlf" - NATIVE = "native" - - @classmethod - def for_open(cls) -> str: - c = cmd.run(["git", "config", "core.eol"]) - eol = c.out.strip().upper() - return cls._char_for_open()[cls._safe_cast(eol)] - - @classmethod - def _safe_cast(cls, eol: str) -> EOLType: - try: - return cls[eol] - except KeyError: - return cls.NATIVE - - @classmethod - @lru_cache - def _char_for_open(cls) -> dict[EOLType, str]: - """Get the EOL character for `open()`.""" - return { - cls.LF: "\n", - cls.CRLF: "\r\n", - cls.NATIVE: os.linesep, - } - - class GitObject: rev: str name: str @@ -323,11 +292,6 @@ def get_core_editor() -> str | None: return None -def smart_open(*args, **kwargs): # type: ignore[no-untyped-def,unused-ignore] # noqa: ANN201 - """Open a file with the EOL style determined from Git.""" - return open(*args, newline=EOLType.for_open(), **kwargs) - - def _get_log_as_str_list(start: str | None, end: str, args: Sequence[str]) -> list[str]: """Get string representation of each log entry""" delimiter = "----------commit-delimiter----------" diff --git a/commitizen/providers/base_provider.py b/commitizen/providers/base_provider.py index 5a5435506e..a88cee7b80 100644 --- a/commitizen/providers/base_provider.py +++ b/commitizen/providers/base_provider.py @@ -7,7 +7,7 @@ import tomlkit -from commitizen.git import smart_open +from commitizen.eol import smart_open if TYPE_CHECKING: from collections.abc import Mapping