Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion commitizen/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion commitizen/commands/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -16,14 +17,14 @@
NotAGitProjectError,
NotAllowed,
)
from commitizen.git import GitTag, smart_open
from commitizen.tags import TagRules
from commitizen.version_schemes import get_version_scheme

if TYPE_CHECKING:
from collections.abc import Generator, Iterable

from commitizen.config import BaseConfig
from commitizen.git import GitTag


class ChangelogArgs(TypedDict, total=False):
Expand Down
2 changes: 1 addition & 1 deletion commitizen/commands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -26,7 +27,6 @@
NotAllowed,
NothingToCommitError,
)
from commitizen.git import smart_open

if TYPE_CHECKING:
from commitizen.config import BaseConfig
Expand Down
3 changes: 2 additions & 1 deletion commitizen/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion commitizen/config/json_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion commitizen/config/yaml_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
45 changes: 45 additions & 0 deletions commitizen/eol.py
Original file line number Diff line number Diff line change
@@ -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)
40 changes: 2 additions & 38 deletions commitizen/git.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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----------"
Expand Down
2 changes: 1 addition & 1 deletion commitizen/providers/base_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading