Ajout d'info git au contexte global

This commit is contained in:
theo@manjaro 2024-03-22 16:59:00 +01:00
parent 12fb20d91f
commit dda111f4cf
2 changed files with 29 additions and 0 deletions

7
app.py
View File

@ -16,6 +16,7 @@ import pytz
import json
import os
import traceback
import git
from flask import Flask
from flask import render_template
@ -68,6 +69,12 @@ GLOBAL_CONTEXT["DOMAIN"] = "https://unisquat.alwaysdata.net"
# Timezone du serveur :
TIMEZONE = pytz.timezone("Europe/Paris")
# Branche actuelle git
GLOBAL_CONTEXT["GIT_BRANCH"] = git.get_active_branch_name()
# Hash du commit actuel git
GLOBAL_CONTEXT["GIT_COMMIT"] = git.get_git_revision()
# Globales :
app = Flask(__name__)

22
git.py Normal file
View File

@ -0,0 +1,22 @@
from pathlib import Path
def get_active_branch_name(base_path="."):
head_dir = Path(base_path) / ".git" / "HEAD"
with head_dir.open("r") as f:
content = f.read().splitlines()
for line in content:
if line[0:4] == "ref:":
return line.partition("refs/heads/")[2]
def get_git_revision(base_path=".", short = True):
git_dir = Path(base_path) / '.git'
with (git_dir / 'HEAD').open('r') as head:
ref = head.readline().split(' ')[-1].strip()
with (git_dir / ref).open('r') as git_hash:
hash = git_hash.readline().strip()
if short:
return hash[:7]
else:
return hash