Merge "Handle Pegleg-generated commits in deployment data"

This commit is contained in:
Zuul 2019-09-17 12:51:26 +00:00 committed by Gerrit Code Review
commit a1735882b1
2 changed files with 19 additions and 2 deletions

View File

@ -26,6 +26,7 @@ from pegleg import config
from pegleg.engine import util
from pegleg.engine.util import files
from pegleg.engine.util.files import add_representer_ordered_dict
from pegleg.engine.util.git import TEMP_PEGLEG_COMMIT_MSG
__all__ = ('collect', 'list_', 'show', 'render')
@ -219,6 +220,20 @@ def _get_repo_deployment_data_stanza(repo_path):
try:
repo = git.Repo(repo_path)
commit = repo.commit()
contains_pegleg_commit = TEMP_PEGLEG_COMMIT_MSG in commit.message
# The repo may not appear dirty if Pegleg has made a temporary commit
# on top of changed/untracked files, but we know if that temporary
# commit happened the repo is indeed dirty
dirty = (repo.is_dirty() or contains_pegleg_commit)
if contains_pegleg_commit:
# The commit grabbed above isn't really what we want this data to
# reflect, because it was a commit made by Pegleg itself.
# Grab the commit before Pegleg made its temporary commit(s)
while (TEMP_PEGLEG_COMMIT_MSG in commit.message
and len(commit.parents) > 0):
commit = commit.parents[0]
# If we're at a particular tag, reference it
tag = [tag.name for tag in repo.tags if tag.commit == commit]
@ -233,6 +248,6 @@ def _get_repo_deployment_data_stanza(repo_path):
tag = "Detached HEAD"
else:
raise e
return {"commit": commit.hexsha, "tag": tag, "dirty": repo.is_dirty()}
return {"commit": commit.hexsha, "tag": tag, "dirty": dirty}
except git.InvalidGitRepositoryError:
return {"commit": "None", "tag": "None", "dirty": "None"}

View File

@ -30,6 +30,8 @@ __all__ = (
'git_handler', 'is_repository', 'is_equal', 'repo_url', 'repo_name',
'normalize_repo_path')
TEMP_PEGLEG_COMMIT_MSG = 'Temporary Pegleg commit'
def git_handler(
repo_url, ref=None, proxy_server=None, auth_key=None, clone_path=None):
@ -107,7 +109,7 @@ def git_handler(
'tracked/untracked changes to ref=%s', repo_name(repo_url),
ref)
repo.git.add(all=True)
repo.index.commit('Temporary Pegleg commit')
repo.index.commit(TEMP_PEGLEG_COMMIT_MSG)
try:
# Check whether the ref exists locally.