Skip to content

Commit

Permalink
Add git count for devN without hash
Browse files Browse the repository at this point in the history
  • Loading branch information
EvieePy committed Nov 18, 2024
1 parent a1309bb commit dee0033
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
6 changes: 6 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from setuptools import setup # type: ignore

from twitchio._version import _get_version


setup(version=_get_version(with_hash=False))
32 changes: 2 additions & 30 deletions twitchio/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@

import argparse
import platform
import re
import sys

import aiohttp

from ._version import _get_version


try:
import starlette
Expand All @@ -51,35 +52,6 @@
args = parser.parse_args()


def _get_version() -> str:
version = ""
with open("twitchio/__init__.py") as f:
match = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', f.read(), re.MULTILINE)

if not match or not match.group(1):
raise RuntimeError("Version is not set")

version = match.group(1)

if version.endswith(("dev", "a", "b", "rc")):
# append version identifier based on commit count
try:
import subprocess

p = subprocess.Popen(["git", "rev-list", "--count", "HEAD"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, _ = p.communicate()
if out:
version += out.decode("utf-8").strip()
p = subprocess.Popen(["git", "rev-parse", "--short", "HEAD"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, _ = p.communicate()
if out:
version += "+g" + out.decode("utf-8").strip()
except Exception:
pass

return version


def version_info() -> None:
python_info = "\n".join(sys.version.split("\n"))

Expand Down
32 changes: 32 additions & 0 deletions twitchio/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import re


def _get_version(with_hash: bool = True) -> str: # type: ignore
version = ""
with open("twitchio/__init__.py") as f:
match = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', f.read(), re.MULTILINE)

if not match or not match.group(1):
raise RuntimeError("Version is not set")

version = match.group(1)

if version.endswith(("dev", "a", "b", "rc")):
# append version identifier based on commit count
try:
import subprocess

p = subprocess.Popen(["git", "rev-list", "--count", "HEAD"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, _ = p.communicate()
if out:
version += out.decode("utf-8").strip()

if with_hash:
p = subprocess.Popen(["git", "rev-parse", "--short", "HEAD"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, _ = p.communicate()
if out:
version += "+g" + out.decode("utf-8").strip()
except Exception:
pass

return version

0 comments on commit dee0033

Please sign in to comment.