From 5f8f0629638aa5a1c94de35edbbc04b0e49f7ba1 Mon Sep 17 00:00:00 2001 From: Joseph Doiron <57968347+jdoiro3@users.noreply.github.com> Date: Sat, 22 Jun 2024 10:26:58 -0400 Subject: [PATCH] Improve Git Version Parsing/Checking and Added `mypy` (#148) * improving git version parsing and sparse clone checking; added mypy to pre-commit * bumped version to trigger new patched release --- .pre-commit-config.yaml | 13 ++++++++ CHANGELOG.md | 6 ++++ mkdocs_multirepo_plugin/util.py | 54 ++++++++++++++++++++++++--------- pyproject.toml | 2 +- 4 files changed, 59 insertions(+), 16 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 20dacfe..a2e1105 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,3 +1,12 @@ +exclude: > + (?x)( + \.mypy_cache/ + | \.pytest_cache/ + | \.venv/ + | build/ + | dist/ + | \S+\.egg-info/ + ) repos: - repo: https://github.com/timothycrosley/isort rev: 5.10.1 @@ -25,6 +34,10 @@ repos: hooks: - id: flake8 args: ["--select=E9,F63,F7,F82", "--show-source", "--max-complexity=10", "--max-line-length=127", "--statistics"] + - repo: https://github.com/pre-commit/mirrors-mypy + rev: 'v1.10.0' # Use the sha / tag you want to point at + hooks: + - id: mypy - repo: local hooks: - id: unit-tests diff --git a/CHANGELOG.md b/CHANGELOG.md index 18a752b..122c321 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 0.8.1 + +### Prs in Release + +- [Better Git Version Parsing and Checking](https://github.com/jdoiro3/mkdocs-multirepo-plugin/pull/148) + ## 0.8.0 ### Prs in Release diff --git a/mkdocs_multirepo_plugin/util.py b/mkdocs_multirepo_plugin/util.py index 6d5e1db..6e84194 100644 --- a/mkdocs_multirepo_plugin/util.py +++ b/mkdocs_multirepo_plugin/util.py @@ -1,24 +1,31 @@ import asyncio import logging +import re import subprocess -from collections import namedtuple from pathlib import Path -from re import search from sys import platform, version_info -from typing import Any, Dict +from typing import Any, Dict, NamedTuple -# used for getting Git version -GitVersion = namedtuple("GitVersion", "major minor") LINUX_LIKE_PLATFORMS = ["linux", "linux2", "darwin"] # This is a global variable imported by other modules log = logging.getLogger("mkdocs.plugins." + __name__) +class Version(NamedTuple): + major: int + minor: int + patch: int + + class ImportDocsException(Exception): pass +class VersionException(Exception): + pass + + class GitException(Exception): pass @@ -60,7 +67,20 @@ def remove_parents(path: str, num_to_remove: int) -> str: return "/" + str(Path(*parts_to_keep)).replace("\\", "/") -def git_version() -> GitVersion: +def parse_version(val: str) -> Version: + match = re.match(r"[^0-9]*(([0-9]+\.){2}[0-9]+).*", val) + if not match: + raise VersionException(f"Could not match version in {val}") + version: str = match.group(1) if match else "" + major, minor, patch = version.split(".", maxsplit=2) + return Version( + major=int(major.ljust(2, "0")), + minor=int(minor.ljust(2, "0")), + patch=int(patch.ljust(2, "0")), + ) + + +def git_version() -> Version: extra_run_args = get_subprocess_run_extra_args() try: output = subprocess.run(["git", "--version"], **extra_run_args) @@ -71,15 +91,19 @@ def git_version() -> GitVersion: stdout = output.stdout if isinstance(stdout, bytes): stdout = output.stdout.decode() - version = search(r"([\d.]+)", stdout).group(1).split(".")[:2] - return GitVersion(int(version[0]), int(version[1])) + # thanks @matt + match = re.match(r"[^0-9]*(([0-9]+\.){2}[0-9]+).*", stdout) + if not match: + raise GitException(f"Could not match Git version number in {stdout}") + version: str = match.group(1) if match else "" + return parse_version(version) def git_supports_sparse_clone() -> bool: - git_v = git_version() - if (git_v.major == 2 and git_v.minor < 25) or (git_v.major < 2): - return False - return True + """The sparse-checkout was added in 2.25.0 + See RelNotes here: https://github.com/git/git/blob/9005149a4a77e2d3409c6127bf4fd1a0893c3495/Documentation/RelNotes/2.25.0.txt#L67 + """ + return git_version() < Version(2, 25, 0) async def execute_bash_script( @@ -101,10 +125,10 @@ async def execute_bash_script( ) stdout, stderr = await process.communicate() - stdout, stderr = stdout.decode(), stderr.decode() + stdout_str, stderr_str = stdout.decode(), stderr.decode() if process.returncode == 1: - raise BashException(f"\n{stderr}\n") - return stdout + raise BashException(f"\n{stderr_str}\n") + return stdout_str def asyncio_run(futures) -> None: diff --git a/pyproject.toml b/pyproject.toml index 930cd02..c46d7df 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "mkdocs-multirepo-plugin" -version = "0.8.0" +version = "0.8.1" description = "Build documentation in multiple repos into one site." authors = ["jdoiro3 "] license = "MIT"