Skip to content

Commit 23456b9

Browse files
committed
refactor(git): move smart_open and EOLType to commitizen.eol
These handle file line endings, not git commands. commitizen.git still exports both names, so existing imports keep working.
1 parent 0839f9a commit 23456b9

9 files changed

Lines changed: 56 additions & 45 deletions

File tree

‎commitizen/bump.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from typing import TYPE_CHECKING
88

99
from commitizen.defaults import BUMP_MESSAGE
10+
from commitizen.eol import smart_open
1011
from commitizen.exceptions import CurrentVersionNotFoundError
11-
from commitizen.git import smart_open
1212

1313
if TYPE_CHECKING:
1414
from collections.abc import Generator, Iterable

‎commitizen/commands/changelog.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from commitizen import changelog, defaults, factory, git, out
99
from commitizen.changelog_formats import get_changelog_format
1010
from commitizen.cz.utils import strip_local_version
11+
from commitizen.eol import smart_open
1112
from commitizen.exceptions import (
1213
DryRunExit,
1314
NoCommitsFoundError,
@@ -16,14 +17,14 @@
1617
NotAGitProjectError,
1718
NotAllowed,
1819
)
19-
from commitizen.git import GitTag, smart_open
2020
from commitizen.tags import TagRules
2121
from commitizen.version_schemes import get_version_scheme
2222

2323
if TYPE_CHECKING:
2424
from collections.abc import Generator, Iterable
2525

2626
from commitizen.config import BaseConfig
27+
from commitizen.git import GitTag
2728

2829

2930
class ChangelogArgs(TypedDict, total=False):

‎commitizen/commands/commit.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from commitizen import factory, git, out
1616
from commitizen.cz.exceptions import CzException
1717
from commitizen.cz.utils import get_backup_file_path
18+
from commitizen.eol import smart_open
1819
from commitizen.exceptions import (
1920
CommitError,
2021
CommitMessageLengthExceededError,
@@ -26,7 +27,6 @@
2627
NotAllowed,
2728
NothingToCommitError,
2829
)
29-
from commitizen.git import smart_open
3030

3131
if TYPE_CHECKING:
3232
from commitizen.config import BaseConfig

‎commitizen/commands/init.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
from commitizen.config.factory import create_config
1212
from commitizen.cz import registry
1313
from commitizen.defaults import CONFIG_FILES, DEFAULT_SETTINGS
14+
from commitizen.eol import smart_open
1415
from commitizen.exceptions import (
1516
InitFailedError,
1617
MissingCzCustomizeConfigError,
1718
NoAnswersError,
1819
)
19-
from commitizen.git import get_latest_tag_name, get_tag_names, smart_open
20+
from commitizen.git import get_latest_tag_name, get_tag_names
2021
from commitizen.version_schemes import (
2122
KNOWN_SCHEMES,
2223
VersionProtocol,

‎commitizen/config/json_config.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import json
44
from typing import TYPE_CHECKING
55

6+
from commitizen.eol import smart_open
67
from commitizen.exceptions import InvalidConfigurationError
7-
from commitizen.git import smart_open
88

99
from .base_config import BaseConfig
1010

‎commitizen/config/yaml_config.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import yaml
66

7+
from commitizen.eol import smart_open
78
from commitizen.exceptions import InvalidConfigurationError
8-
from commitizen.git import smart_open
99

1010
from .base_config import BaseConfig
1111

‎commitizen/eol.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""File line endings based on the `core.eol` git setting."""
2+
3+
from __future__ import annotations
4+
5+
import os
6+
from enum import Enum
7+
from functools import lru_cache
8+
9+
from commitizen import cmd
10+
11+
12+
class EOLType(Enum):
13+
"""The EOL type from `git config core.eol`."""
14+
15+
LF = "lf"
16+
CRLF = "crlf"
17+
NATIVE = "native"
18+
19+
@classmethod
20+
def for_open(cls) -> str:
21+
c = cmd.run(["git", "config", "core.eol"])
22+
eol = c.out.strip().upper()
23+
return cls._char_for_open()[cls._safe_cast(eol)]
24+
25+
@classmethod
26+
def _safe_cast(cls, eol: str) -> EOLType:
27+
try:
28+
return cls[eol]
29+
except KeyError:
30+
return cls.NATIVE
31+
32+
@classmethod
33+
@lru_cache
34+
def _char_for_open(cls) -> dict[EOLType, str]:
35+
"""Get the EOL character for `open()`."""
36+
return {
37+
cls.LF: "\n",
38+
cls.CRLF: "\r\n",
39+
cls.NATIVE: os.linesep,
40+
}
41+
42+
43+
def smart_open(*args, **kwargs): # type: ignore[no-untyped-def,unused-ignore] # noqa: ANN201
44+
"""Open a file with the EOL style determined from Git."""
45+
return open(*args, newline=EOLType.for_open(), **kwargs)

‎commitizen/git.py‎

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,19 @@
11
from __future__ import annotations
22

33
import os
4-
from enum import Enum
5-
from functools import lru_cache
64
from pathlib import Path
75
from tempfile import NamedTemporaryFile
86
from typing import TYPE_CHECKING
97

108
from commitizen import cmd, out
9+
from commitizen.eol import EOLType as EOLType
10+
from commitizen.eol import smart_open as smart_open
1111
from commitizen.exceptions import GitCommandError
1212

1313
if TYPE_CHECKING:
1414
from collections.abc import Sequence
1515

1616

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-
4817
class GitObject:
4918
rev: str
5019
name: str
@@ -323,11 +292,6 @@ def get_core_editor() -> str | None:
323292
return None
324293

325294

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-
331295
def _get_log_as_str_list(start: str | None, end: str, args: Sequence[str]) -> list[str]:
332296
"""Get string representation of each log entry"""
333297
delimiter = "----------commit-delimiter----------"

‎commitizen/providers/base_provider.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import tomlkit
99

10-
from commitizen.git import smart_open
10+
from commitizen.eol import smart_open
1111

1212
if TYPE_CHECKING:
1313
from collections.abc import Mapping

0 commit comments

Comments
 (0)