feat: add git_creation_date method

This commit is contained in:
Corentin 2024-05-21 01:22:20 +02:00
parent 8a614fee99
commit b2748a6cf3
2 changed files with 28 additions and 1 deletions

26
jwebsite/git.py Normal file
View File

@ -0,0 +1,26 @@
from datetime import datetime
from subprocess import check_output
from jwebsite.content import Content
def git_creation_date(content: Content) -> datetime:
git_dir = check_output(["git", "rev-parse", "--show-toplevel"], encoding="utf-8")
git_dir = git_dir.strip()
log = check_output(
[
"git",
"log",
"--pretty=format:%ad",
"--date=iso-strict",
"--diff-filter=A",
"--",
str(content.path.relative_to(git_dir)),
],
encoding="utf-8",
)
log_lines = log.splitlines()
if len(log_lines) == 0:
return datetime.now()
return datetime.fromisoformat(log_lines[0])

View File

@ -6,6 +6,7 @@ from jinja2.environment import Environment
from jinja2.loaders import FileSystemLoader
from jwebsite.content import ContentDirectory
from jwebsite.git import git_creation_date
class Site:
@ -13,7 +14,7 @@ class Site:
self.__root_directory = root_directory
self.__output_directory = root_directory / "build"
self.__environment = Environment(loader=FileSystemLoader(searchpath=root_directory / "src"))
self.__environment.filters.update({"output": self.__output})
self.__environment.filters.update({"output": self.__output, "git_creation_date": git_creation_date})
self.__content = ContentDirectory(root_directory / "content")
@property