Include tag distance and commit hash in dev versions when possible (#5664)

* Include tag distance and commit hash in dev versions when possible

* Fix test
This commit is contained in:
jack1142
2022-04-03 03:51:34 +02:00
committed by GitHub
parent febca8ccbb
commit 35f1681dc1
6 changed files with 80 additions and 11 deletions

View File

@@ -12,6 +12,8 @@ from typing import (
Union as _Union,
)
from redbot._version import _get_version
MIN_PYTHON_VERSION = (3, 8, 1)
@@ -42,6 +44,7 @@ class VersionInfo:
r"(?:(?P<releaselevel>a|b|rc)(?P<serial>0|[1-9]\d*))?"
r"(?:\.post(?P<post_release>0|[1-9]\d*))?"
r"(?:\.dev(?P<dev_release>0|[1-9]\d*))?"
r"(?:\+(?P<local_version>g[a-z0-9]+(?:\.dirty)?))?"
r"$",
flags=_re.IGNORECASE,
)
@@ -61,6 +64,7 @@ class VersionInfo:
serial: _Optional[int] = None,
post_release: _Optional[int] = None,
dev_release: _Optional[int] = None,
local_version: _Optional[str] = None,
) -> None:
self.major: int = major
self.minor: int = minor
@@ -73,6 +77,17 @@ class VersionInfo:
self.serial: _Optional[int] = serial
self.post_release: _Optional[int] = post_release
self.dev_release: _Optional[int] = dev_release
self.local_version: _Optional[str] = local_version
@property
def short_commit_hash(self) -> _Optional[str]:
if self.local_version is None:
return None
return self.local_version[1:].split(".", 1)[0]
@property
def dirty(self) -> bool:
return self.local_version is not None and self.local_version.endswith(".dirty")
@classmethod
def from_str(cls, version_str: str) -> "VersionInfo":
@@ -99,6 +114,7 @@ class VersionInfo:
for key in ("serial", "post_release", "dev_release"):
if match[key] is not None:
kwargs[key] = int(match[key])
kwargs["local_version"] = match["local_version"]
return cls(**kwargs)
@classmethod
@@ -121,15 +137,18 @@ class VersionInfo:
"serial": self.serial,
"post_release": self.post_release,
"dev_release": self.dev_release,
"local_version": self.local_version,
}
def _generate_comparison_tuples(
self, other: "VersionInfo"
) -> _List[
_Tuple[int, int, int, int, _Union[int, float], _Union[int, float], _Union[int, float]]
_Tuple[int, int, int, int, _Union[int, float], _Union[int, float], _Union[int, float], int]
]:
tups: _List[
_Tuple[int, int, int, int, _Union[int, float], _Union[int, float], _Union[int, float]]
_Tuple[
int, int, int, int, _Union[int, float], _Union[int, float], _Union[int, float], int
]
] = []
for obj in (self, other):
tups.append(
@@ -141,6 +160,7 @@ class VersionInfo:
obj.serial if obj.serial is not None else _inf,
obj.post_release if obj.post_release is not None else -_inf,
obj.dev_release if obj.dev_release is not None else _inf,
int(obj.dirty),
)
)
return tups
@@ -168,14 +188,16 @@ class VersionInfo:
ret += f".post{self.post_release}"
if self.dev_release is not None:
ret += f".dev{self.dev_release}"
if self.local_version is not None:
ret += f"+{self.local_version}"
return ret
def __repr__(self) -> str:
return (
"VersionInfo(major={major}, minor={minor}, micro={micro}, "
"releaselevel={releaselevel}, serial={serial}, post={post_release}, "
"dev={dev_release})".format(**self.to_json())
)
"dev={dev_release}, local={local_version})"
).format(**self.to_json())
def _update_event_loop_policy():
@@ -223,7 +245,7 @@ def _early_init():
_ensure_no_colorama()
__version__ = "3.5.0.dev1"
__version__ = _get_version()
version_info = VersionInfo.from_str(__version__)
# Filter fuzzywuzzy slow sequence matcher warning