Skip to content

Commit

Permalink
Merge pull request #427 from akaihola/old-git-initial-branch
Browse files Browse the repository at this point in the history
  • Loading branch information
akaihola authored Dec 26, 2022
2 parents 4785ed1 + 2328e7a commit 4e1282e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
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", "ci@example.com")
instance._run("config", "user.name", "CI system")
return instance
Expand Down

0 comments on commit 4e1282e

Please sign in to comment.