better commit msg on build

This commit is contained in:
Maximilian Keßler 2021-10-08 09:17:56 +02:00
parent f97e7eec3f
commit 6101618f6b
3 changed files with 38 additions and 22 deletions

View file

@ -15,6 +15,7 @@ install:
- pip install GitPython - pip install GitPython
script: script:
- export COMMIT_MSG=$(python3 -c 'from git_version import get_deploy_message; print(get_deploy_message())'
- python3 -c 'from build import build; build("build/LatexPackages/")' - python3 -c 'from build import build; build("build/LatexPackages/")'
- cd build - cd build
- zip -r LatexPackages.zip LatexPackages - zip -r LatexPackages.zip LatexPackages
@ -46,7 +47,7 @@ deploy:
commiter_from_gh: true commiter_from_gh: true
allow_empty_commit: true allow_empty_commit: true
target_branch: $TRAVIS_BRANCH target_branch: $TRAVIS_BRANCH
commit_message: "%{git_commit_msg}\n\nBuilt commit %{git_sha} from %{repo}:%{git_branch}" commit_message: $COMMIT_MSG
on: on:
all_branches: true all_branches: true
condition: $TRAVIS_PULL_REQUEST = false condition: $TRAVIS_PULL_REQUEST = false

View file

@ -2,11 +2,12 @@ from pathlib import *
import sys import sys
import git import git
from datetime import * from datetime import *
sys.path.insert(0, 'PyTeX/') sys.path.insert(0, 'PyTeX/')
from package_formatter import PackageFormatter from package_formatter import PackageFormatter
from replacements import make_default_commands from replacements import make_default_commands
from git_version import git_describe from git_version import git_describe, get_latest_commit
BUILD_DETAILS = [ BUILD_DETAILS = [
"Build details:", "Build details:",
@ -16,45 +17,39 @@ BUILD_DETAILS = [
] ]
def get_latest_commit(repo):
if repo.head.is_detached:
return repo.head.commit
else:
return repo.head.ref.commit
def build_details(): def build_details():
repo = git.Repo() repo = git.Repo()
repo_description = git_describe(get_latest_commit(repo)) repo_description = git_describe(get_latest_commit(repo))
pytex_repo = repo.submodule('PyTeX').module() pytex_repo = repo.submodule('PyTeX').module()
pytex_repo_description = git_describe(get_latest_commit(pytex_repo)) pytex_repo_description = git_describe(get_latest_commit(pytex_repo))
return list(map(lambda line: line.format( return list(map(lambda line: line.format(
build_time=datetime.now().strftime('%Y/%m/%d %H:%M'), build_time=datetime.now().strftime('%Y/%m/%d %H:%M'),
pytex_version=pytex_repo_description, pytex_version=pytex_repo_description,
pytex_commit_hash=get_latest_commit(pytex_repo).hexsha[0:7], pytex_commit_hash=get_latest_commit(pytex_repo).hexsha[0:7],
packages_version=repo_description, packages_version=repo_description,
packages_commit_hash=get_latest_commit(repo).hexsha[0:7] packages_commit_hash=get_latest_commit(repo).hexsha[0:7]
), BUILD_DETAILS)) ), BUILD_DETAILS)), repo_description
def build(build_dir: str): def build(build_dir: str):
input_root = Path('./packages').resolve() input_root = Path('./packages').resolve()
output = input_root / build_dir output = input_root / build_dir
print('[PyTeX] Getting git repository information...') print('[PyTeX] Getting git repository information...')
extra_header = build_details() extra_header, repo_description = build_details()
print('[PyTeX] ..done.') print('[PyTeX] Building version {version} of LatexPackages'.format(version=repo_description))
print('[PyTeX] Latest commit message: ' + get_latest_commit(git.Repo()).message.strip())
if git.Repo().is_dirty(): if git.Repo().is_dirty():
extra_header += ['WARNING: Local changes to git repository detected.', extra_header += ['WARNING: Local changes to git repository detected.',
' The build will not be reproducible (!)'] ' The build will not be reproducible (!)']
num_files = 0 num_files = 0
for file in input_root.rglob('*.pysty'): for file in input_root.rglob('*.pysty'):
num_files+=1 num_files += 1
formatter = PackageFormatter(package_name=file.with_suffix('').name, extra_header=extra_header) formatter = PackageFormatter(package_name=file.with_suffix('').name, extra_header=extra_header)
print('[PyTeX] Writing file {}'.format(formatter.file_name)) print('[PyTeX] Writing file {}'.format(formatter.file_name))
make_default_commands(formatter) make_default_commands(formatter)
formatter.format_package(file, Path('./').resolve() / build_dir / str(file.parent.relative_to(input_root))) formatter.format_package(file, Path('./').resolve() / build_dir / str(file.parent.relative_to(input_root)))
print(f'[PyTeX] Successfully built {num_files} packages in {build_dir}/') print(f'[PyTeX] Successfully built {num_files} packages in {build_dir}/')
if __name__ == "__main__": if __name__ == "__main__":
build('build') build('build')

View file

@ -1,5 +1,25 @@
import git import git
from typing import Dict from typing import Dict
import os
def get_latest_commit(repo):
if repo.head.is_detached:
return repo.head.commit
else:
return repo.head.ref.commit
def get_deploy_message():
repo = git.Repo()
old_msg = get_latest_commit(repo).message
return "{old_msg}\n" \
"\n" \
"Build branch {branch} ({hexsha}) from {repo_name}" \
.format(old_msg=old_msg,
branch=os.environ['TRAVIS_BRANCH'],
hexsha=get_latest_commit(repo).hexsha[0:7],
repo_name='kesslermaximilian/LatexPackages')
def get_history(commit: git.objects.commit.Commit, priority=0, depth=0) -> Dict: def get_history(commit: git.objects.commit.Commit, priority=0, depth=0) -> Dict:
@ -9,9 +29,9 @@ def get_history(commit: git.objects.commit.Commit, priority=0, depth=0) -> Dict:
}} }}
try: try:
if len(commit.parents) > 0: if len(commit.parents) > 0:
commit_history.update(get_history(commit.parents[0], priority, depth+1)) commit_history.update(get_history(commit.parents[0], priority, depth + 1))
for parent in commit.parents[1:]: for parent in commit.parents[1:]:
commit_history.update(get_history(parent, priority+1, depth+1)) commit_history.update(get_history(parent, priority + 1, depth + 1))
except ValueError: except ValueError:
pass pass
return commit_history return commit_history