Skip to content
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

Omit --initial-branch=master on old Git versions #427

Merged
merged 2 commits into from
Dec 26, 2022
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
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Fixed
Darker's own code base.
- Fix compatibility with ``black-22.10.1.dev19+gffaaf48`` and later – an argument was
replaced in ``black.files.gen_python_files()``.
- Fix tests to work with Git older than version 2.28.x.


1.6.0_ - 2022-12-19
Expand Down
35 changes: 34 additions & 1 deletion src/darker/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,18 @@
from functools import lru_cache
from pathlib import Path
from subprocess import DEVNULL, PIPE, CalledProcessError, check_output, run # nosec
from typing import Dict, Iterable, List, Optional, Set, Tuple, Union, overload
from typing import (
Dict,
Iterable,
List,
Match,
Optional,
Set,
Tuple,
Union,
cast,
overload,
)

from darker.diff import diff_and_get_opcodes, opcodes_to_edit_linenums
from darker.multiline_strings import get_multiline_string_ranges
Expand All @@ -33,6 +44,28 @@
PRE_COMMIT_FROM_TO_REFS = ":PRE-COMMIT:"


def git_get_version() -> Tuple[int, ...]:
"""Return the Git version as a tuple of integers

Ignores any suffixes to the dot-separated parts of the version string.

:return: The version number of Git installed on the system
:raise: ``RuntimeError`` if unable to parse the Git version

"""
output_lines = _git_check_output_lines(["--version"], Path("."))
version_string = output_lines[0].rsplit(None, 1)[-1]
# The version string might be e.g.
# - "2.39.0.windows.1"
# - "2.36.2"
part_matches = [re.match(r"\d+", part) for part in version_string.split(".")][:3]
if all(part_matches):
return tuple(
int(match.group(0)) for match in cast(List[Match[str]], part_matches)
)
raise RuntimeError(f"Unable to parse Git version: {output_lines!r}")


def git_is_repository(path: Path) -> bool:
"""Return ``True`` if ``path`` is inside a Git working tree"""
try:
Expand Down
7 changes: 5 additions & 2 deletions src/darker/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pytest
from black import find_project_root as black_find_project_root

from darker.git import _git_check_output_lines
from darker.git import _git_check_output_lines, git_get_version


class GitRepoFixture:
Expand All @@ -27,7 +27,10 @@ def create_repository(cls, root: Path) -> "GitRepoFixture":
env = {"HOME": str(root), "LC_ALL": "C", "PATH": os.environ["PATH"]}
instance = cls(root, env)
# pylint: disable=protected-access
instance._run("init", "--initial-branch=master")
force_master = (
["--initial-branch=master"] if git_get_version() >= (2, 28) else []
)
instance._run("init", *force_master)
instance._run("config", "user.email", "[email protected]")
instance._run("config", "user.name", "CI system")
return instance
Expand Down