Skip to content

fix(question): strict type with pydantic for questions #1467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
036b269
refactor: improve readability and fix typos
bearomorphism May 15, 2025
c8e8959
refactor(version_scheme): cleanup
bearomorphism May 17, 2025
404afbb
refactor(commit): simplify call
bearomorphism May 17, 2025
0050be8
test(commit): when nothing is added to commit
bearomorphism May 17, 2025
0bd6ae4
refactor(git): code cleanup and better test coverage
bearomorphism May 15, 2025
04e47a7
test(git): add test for from_rev_and_commit
bearomorphism May 17, 2025
1b06258
docs(git): from_rev_and_commit docstring
bearomorphism May 17, 2025
fe00c41
refactor(EOLType): add eol enum back and reorganize methods
bearomorphism May 18, 2025
b17bbd2
refactor(git): refactor get_tag_names
bearomorphism May 18, 2025
a656a04
refactor(changelog): minor cleanup
bearomorphism May 19, 2025
06eacae
refactor(BaseConfig): use setter
bearomorphism May 20, 2025
9ca9678
build(poetry): upgrade mypy version to ^1.15.0
bearomorphism May 20, 2025
7e67154
refactor(bump): add type for out, replace function with re escape
bearomorphism May 17, 2025
b923735
refactor(bump): clean up
bearomorphism May 16, 2025
c2c8e3b
test(bump): improve test coverage
bearomorphism May 16, 2025
856ca4f
fix(defaults): add non-capitalized default constants back and depreca…
bearomorphism May 22, 2025
d0db72c
refactor: misc cleanup
bearomorphism May 17, 2025
f229e42
refactor(git): extract _create_commit_cmd_string
bearomorphism May 19, 2025
9584c49
test(test_git): mock os
bearomorphism May 23, 2025
83be38a
refactor(cli): early return and improve test coverage
bearomorphism May 23, 2025
07b68f8
build(termcolor): remove termcolor <3 restriction
bearomorphism May 23, 2025
2259b3a
build: specify importlib-metadata version to fix unit tests
bearomorphism May 25, 2025
0c7bcb8
build(poetry): regenerate lock file
Lee-W May 27, 2025
a3919c9
refactor(changelog): better typing, yield
bearomorphism May 24, 2025
c9b2ba7
fix(question): strict type with pydantic for questions
bearomorphism May 29, 2025
148babc
build: use pydantic >=1.10
bearomorphism May 30, 2025
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
Prev Previous commit
Next Next commit
refactor(EOLType): add eol enum back and reorganize methods
  • Loading branch information
bearomorphism authored and Lee-W committed May 25, 2025
commit fe00c4190b4a28e25f16e8ca890aba8d0cd516c3
48 changes: 33 additions & 15 deletions commitizen/git.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,44 @@
from __future__ import annotations

import os
from enum import Enum
from functools import lru_cache
from pathlib import Path
from tempfile import NamedTemporaryFile

from commitizen import cmd, out
from commitizen.exceptions import GitCommandError

_UNIX_EOL = "\n"
_WINDOWS_EOL = "\r\n"

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:
Expand Down Expand Up @@ -268,18 +298,6 @@ def is_git_project() -> bool:
return c.out.strip() == "true"


def get_eol_for_open() -> str:
# See: https://git-scm.com/docs/git-config#Documentation/git-config.txt-coreeol
c = cmd.run("git config core.eol")
eol = c.out.strip().lower()

if eol == "lf":
return _UNIX_EOL
if eol == "crlf":
return _WINDOWS_EOL
return os.linesep


def get_core_editor() -> str | None:
c = cmd.run("git var GIT_EDITOR")
if c.out:
Expand All @@ -289,7 +307,7 @@ def get_core_editor() -> str | None:

def smart_open(*args, **kwargs):
"""Open a file with the EOL style determined from Git."""
return open(*args, newline=get_eol_for_open(), **kwargs)
return open(*args, newline=EOLType.for_open(), **kwargs)


def _get_log_as_str_list(start: str | None, end: str, args: str) -> list[str]:
Expand Down
8 changes: 4 additions & 4 deletions tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,16 @@ def test_is_staging_clean_when_updating_file(tmp_commitizen_project):

def test_get_eol_for_open(tmp_commitizen_project):
with tmp_commitizen_project.as_cwd():
assert git.get_eol_for_open() == os.linesep
assert git.EOLType.for_open() == os.linesep

cmd.run("git config core.eol lf")
assert git.get_eol_for_open() == "\n"
assert git.EOLType.for_open() == "\n"

cmd.run("git config core.eol crlf")
assert git.get_eol_for_open() == "\r\n"
assert git.EOLType.for_open() == "\r\n"

cmd.run("git config core.eol native")
assert git.get_eol_for_open() == os.linesep
assert git.EOLType.for_open() == os.linesep


def test_get_core_editor(mocker):
Expand Down