klippy: report repo branch, remote, and tracking url

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2023-04-30 07:31:45 -04:00 committed by KevinOConnor
parent 3bd0be40d5
commit b89a049fdb
2 changed files with 58 additions and 4 deletions

View File

@ -343,7 +343,7 @@ def main():
else:
logging.getLogger().setLevel(debuglevel)
logging.info("Starting Klippy...")
git_vers, git_status = util.get_git_version()
git_vers, git_status, repo_info = util.get_git_version()
extra_files = [fname for code, fname in git_status
if (code in ('??', '!!') and fname.endswith('.py')
and (fname.startswith('klippy/kinematics/')
@ -360,6 +360,10 @@ def main():
if len(modified_files) > 10:
modified_files[10:] = ["(+%d files)" % (len(modified_files) - 10,)]
extra_git_desc += "\nModified files: %s" % (', '.join(modified_files),)
if repo_info:
extra_git_desc += "\nBranch: %s" % (repo_info["branch"])
extra_git_desc += "\nRemote: %s" % (repo_info["remote"])
extra_git_desc += "\nTracked URL: %s" % (repo_info["url"])
start_args['software_version'] = git_vers
start_args['cpu_info'] = util.get_cpu_info()
if bglogger is not None:

View File

@ -133,6 +133,55 @@ def get_version_from_file(klippy_src):
pass
return "?"
def _get_repo_info(gitdir):
repo_info = {"branch": "?", "remote": "?", "url": "?"}
prog_branch = ('git', '-C', gitdir, 'branch', '--no-color')
try:
process = subprocess.Popen(prog_branch, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
branch_list, err = process.communicate()
retcode = process.wait()
if retcode != 0:
logging.debug("Error running git branch: %s", err)
return repo_info
lines = str(branch_list.strip().decode()).split("\n")
for line in lines:
if line[0] == "*":
repo_info["branch"] = line[1:].strip()
break
else:
logging.debug("Unable to find current branch:\n%s", branch_list)
return repo_info
if repo_info["branch"].startswith("(HEAD detached"):
parts = repo_info["branch"].strip("()").split()[-1].split("/", 1)
if len(parts) != 2:
return repo_info
repo_info["remote"] = parts[0]
else:
key = "branch.%s.remote" % (repo_info["branch"],)
prog_config = ('git', '-C', gitdir, 'config', '--get', key)
process = subprocess.Popen(prog_config, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
remote_info, err = process.communicate()
retcode = process.wait()
if retcode != 0:
logging.debug("Error running git config: %s", err)
return repo_info
repo_info["remote"] = str(remote_info.strip().decode())
prog_remote_url = (
'git', '-C', gitdir, 'remote', 'get-url', repo_info["remote"])
process = subprocess.Popen(prog_remote_url, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
remote_url, err = process.communicate()
retcode = process.wait()
if retcode != 0:
logging.debug("Error running git remote get-url: %s", err)
return repo_info
repo_info["url"] = str(remote_url.strip().decode())
except:
logging.debug("Error fetching repo info: %s", traceback.format_exc())
return repo_info
def get_git_version(from_file=True):
klippy_src = os.path.dirname(__file__)
@ -154,7 +203,8 @@ def get_git_version(from_file=True):
for l in str(stat.strip().decode()).split('\n')]
retcode = process.wait()
if retcode == 0:
return (str(ver.strip().decode()), status)
repo_info = _get_repo_info(gitdir)
return (str(ver.strip().decode()), status, repo_info)
else:
logging.debug("Error getting git status: %s", err)
else:
@ -163,5 +213,5 @@ def get_git_version(from_file=True):
logging.debug("Exception on run: %s", traceback.format_exc())
if from_file:
return (get_version_from_file(klippy_src), [])
return ("?", [])
return (get_version_from_file(klippy_src), [], {})
return ("?", [], {})