Skip to content
Merged
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
4 changes: 2 additions & 2 deletions commitizen/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from commitizen.defaults import MAJOR, MINOR, PATCH, bump_message
from commitizen.exceptions import CurrentVersionNotFoundError
from commitizen.git import GitCommit
from commitizen.git import GitCommit, smart_open


def find_increment(
Expand Down Expand Up @@ -164,7 +164,7 @@ def update_version_in_files(
)

# Write the file out again
with open(filepath, "w") as file:
with smart_open(filepath, "w") as file:
file.write("".join(version_file))


Expand Down
7 changes: 5 additions & 2 deletions commitizen/commands/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
NotAGitProjectError,
NotAllowed,
)
from commitizen.git import GitTag
from commitizen.git import GitTag, smart_open


def similar(a, b):
Expand Down Expand Up @@ -89,7 +89,7 @@ def write_changelog(
)

changelog_hook: Optional[Callable] = self.cz.changelog_hook
with open(self.file_name, "w") as changelog_file:
with smart_open(self.file_name, "w") as changelog_file:
partial_changelog: Optional[str] = None
if self.incremental:
new_lines = changelog.incremental_build(
Expand Down Expand Up @@ -121,6 +121,9 @@ def __call__(self):
if self.incremental and self.rev_range:
raise NotAllowed("--incremental cannot be combined with a rev_range")

# Don't continue if no `file_name` specified.
assert self.file_name

tags = git.get_tags()
if not tags:
tags = []
Expand Down
3 changes: 2 additions & 1 deletion commitizen/commands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
NotAGitProjectError,
NothingToCommitError,
)
from commitizen.git import smart_open


class Commit:
Expand Down Expand Up @@ -89,7 +90,7 @@ def __call__(self):
out.error(c.err)

# Create commit backup
with open(self.temp_file, "w") as f:
with smart_open(self.temp_file, "w") as f:
f.write(m)

raise CommitError()
Expand Down
4 changes: 2 additions & 2 deletions commitizen/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from commitizen.cz import registry
from commitizen.defaults import config_files
from commitizen.exceptions import NoAnswersError
from commitizen.git import get_latest_tag_name, get_tag_names
from commitizen.git import get_latest_tag_name, get_tag_names, smart_open


class Init:
Expand Down Expand Up @@ -138,7 +138,7 @@ def _install_pre_commit_hook(self):
# .pre-commit-config exists but there's no "repos" key
config_data["repos"] = [cz_hook_config]

with open(pre_commit_config_filename, "w") as config_file:
with smart_open(pre_commit_config_filename, "w") as config_file:
yaml.safe_dump(config_data, stream=config_file)

c = cmd.run("pre-commit install --hook-type commit-msg")
Expand Down
6 changes: 4 additions & 2 deletions commitizen/config/json_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from pathlib import Path
from typing import Union

from commitizen.git import smart_open

from .base_config import BaseConfig


Expand All @@ -13,7 +15,7 @@ def __init__(self, *, data: Union[bytes, str], path: Union[Path, str]):
self.add_path(path)

def init_empty_config_content(self):
with open(self.path, "a") as json_file:
with smart_open(self.path, "a") as json_file:
json.dump({"commitizen": {}}, json_file)

def set_key(self, key, value):
Expand All @@ -26,7 +28,7 @@ def set_key(self, key, value):
parser = json.load(f)

parser["commitizen"][key] = value
with open(self.path, "w") as f:
with smart_open(self.path, "w") as f:
json.dump(parser, f, indent=2)
return self

Expand Down
6 changes: 4 additions & 2 deletions commitizen/config/yaml_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import yaml

from commitizen.git import smart_open

from .base_config import BaseConfig


Expand All @@ -14,7 +16,7 @@ def __init__(self, *, data: Union[bytes, str], path: Union[Path, str]):
self.add_path(path)

def init_empty_config_content(self):
with open(self.path, "a") as json_file:
with smart_open(self.path, "a") as json_file:
yaml.dump({"commitizen": {}}, json_file)

def _parse_setting(self, data: Union[bytes, str]) -> None:
Expand All @@ -41,7 +43,7 @@ def set_key(self, key, value):
parser = yaml.load(yaml_file, Loader=yaml.FullLoader)

parser["commitizen"][key] = value
with open(self.path, "w") as yaml_file:
with smart_open(self.path, "w") as yaml_file:
yaml.dump(parser, yaml_file)

return self
51 changes: 51 additions & 0 deletions commitizen/git.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
import os
from enum import Enum
from os import linesep
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import List, Optional

from commitizen import cmd

UNIX_EOL = "\n"
WINDOWS_EOL = "\r\n"


class EOLTypes(Enum):
"""The EOL type from `git config core.eol`."""

LF = "lf"
CRLF = "crlf"
NATIVE = "native"

def get_eol_for_open(self) -> str:
"""Get the EOL character for `open()`."""
map = {
EOLTypes.CRLF: WINDOWS_EOL,
EOLTypes.LF: UNIX_EOL,
EOLTypes.NATIVE: linesep,
}

return map[self]


class GitObject:
rev: str
Expand Down Expand Up @@ -177,3 +200,31 @@ def is_git_project() -> bool:
if c.out.strip() == "true":
return True
return False


def get_eol_style() -> EOLTypes:
c = cmd.run("git config core.eol")
Comment thread
Lee-W marked this conversation as resolved.
Resolved
eol = c.out.strip().lower()

# We enumerate the EOL types of the response of
# `git config core.eol`, and map it to our enumration EOLTypes.
#
# It is just like the varient of the "match" syntax.
map = {
"lf": EOLTypes.LF,
"crlf": EOLTypes.CRLF,
"native": EOLTypes.NATIVE,
}

# If the response of `git config core.eol` is in the map:
if eol in map:
return map[eol]
else:
# The default value is "native".
# https://git-scm.com/docs/git-config#Documentation/git-config.txt-coreeol
return map["native"]


def smart_open(*args, **kargs):
"""Open a file with the EOL style determined from Git."""
return open(*args, newline=get_eol_style().get_eol_for_open(), **kargs)
21 changes: 21 additions & 0 deletions tests/test_git.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
import os
import shutil
from typing import List, Optional

Expand Down Expand Up @@ -203,3 +204,23 @@ def test_is_staging_clean_when_updating_file(tmp_commitizen_project):
cmd.run("git add test_file")

assert git.is_staging_clean() is False


def test_git_eol_style(tmp_commitizen_project):
with tmp_commitizen_project.as_cwd():
assert git.get_eol_style() == git.EOLTypes.NATIVE

cmd.run("git config core.eol lf")
assert git.get_eol_style() == git.EOLTypes.LF

cmd.run("git config core.eol crlf")
assert git.get_eol_style() == git.EOLTypes.CRLF

cmd.run("git config core.eol native")
assert git.get_eol_style() == git.EOLTypes.NATIVE


def test_eoltypes_get_eol_for_open():
assert git.EOLTypes.get_eol_for_open(git.EOLTypes.NATIVE) == os.linesep
assert git.EOLTypes.get_eol_for_open(git.EOLTypes.LF) == "\n"
assert git.EOLTypes.get_eol_for_open(git.EOLTypes.CRLF) == "\r\n"