utils: add support for versioning shallow clones
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
parent
471885c3a2
commit
d396dbd2cb
|
@ -16,6 +16,8 @@ import subprocess
|
||||||
import asyncio
|
import asyncio
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
|
import shlex
|
||||||
|
import re
|
||||||
from queue import SimpleQueue as Queue
|
from queue import SimpleQueue as Queue
|
||||||
|
|
||||||
# Annotation imports
|
# Annotation imports
|
||||||
|
@ -85,19 +87,38 @@ class MoonrakerLoggingHandler(logging.handlers.TimedRotatingFileHandler):
|
||||||
if self.stream is not None:
|
if self.stream is not None:
|
||||||
self.stream.write("\n".join(lines) + "\n")
|
self.stream.write("\n".join(lines) + "\n")
|
||||||
|
|
||||||
|
def _run_git_command(cmd: str) -> str:
|
||||||
|
prog = shlex.split(cmd)
|
||||||
|
process = subprocess.Popen(prog, stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.PIPE)
|
||||||
|
ret, err = process.communicate()
|
||||||
|
retcode = process.wait()
|
||||||
|
if retcode == 0:
|
||||||
|
return ret.strip().decode()
|
||||||
|
raise Exception(f"Failed to run git command: {cmd}")
|
||||||
|
|
||||||
|
def _retrieve_git_tag(source_path: str) -> str:
|
||||||
|
cmd = f"git -C {source_path} rev-list --tags --max-count=1"
|
||||||
|
hash = _run_git_command(cmd)
|
||||||
|
cmd = f"git -C {source_path} describe --tags {hash}"
|
||||||
|
tag = _run_git_command(cmd)
|
||||||
|
cmd = f"git -C {source_path} rev-list {tag}..HEAD --count"
|
||||||
|
count = _run_git_command(cmd)
|
||||||
|
return f"{tag}-{count}"
|
||||||
|
|
||||||
# Parse the git version from the command line. This code
|
# Parse the git version from the command line. This code
|
||||||
# is borrowed from Klipper.
|
# is borrowed from Klipper.
|
||||||
def retrieve_git_version(source_path: str) -> str:
|
def retrieve_git_version(source_path: str) -> str:
|
||||||
# Obtain version info from "git" program
|
# Obtain version info from "git" program
|
||||||
prog = ('git', '-C', source_path, 'describe', '--always',
|
cmd = f"git -C {source_path} describe --always --tags --long --dirty"
|
||||||
'--tags', '--long', '--dirty')
|
ver = _run_git_command(cmd)
|
||||||
process = subprocess.Popen(prog, stdout=subprocess.PIPE,
|
tag_match = re.match(r"v\d+\.\d+\.\d+", ver)
|
||||||
stderr=subprocess.PIPE)
|
if tag_match is not None:
|
||||||
ver, err = process.communicate()
|
return ver
|
||||||
retcode = process.wait()
|
# This is likely a shallow clone. Resolve the tag and manually create
|
||||||
if retcode == 0:
|
# the version string
|
||||||
return ver.strip().decode()
|
tag = _retrieve_git_tag(source_path)
|
||||||
raise Exception(f"Failed to retrieve git version: {err.decode()}")
|
return f"t{tag}-g{ver}-shallow"
|
||||||
|
|
||||||
def get_software_version() -> str:
|
def get_software_version() -> str:
|
||||||
version = "?"
|
version = "?"
|
||||||
|
|
Loading…
Reference in New Issue