From b2748a6cf3bee48cd2b426c3b407b6e28b9a46b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corentin=20S=C3=A9chet?= Date: Tue, 21 May 2024 01:22:20 +0200 Subject: [PATCH] feat: add git_creation_date method --- jwebsite/git.py | 26 ++++++++++++++++++++++++++ jwebsite/site.py | 3 ++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 jwebsite/git.py diff --git a/jwebsite/git.py b/jwebsite/git.py new file mode 100644 index 0000000..8b4bfc1 --- /dev/null +++ b/jwebsite/git.py @@ -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]) diff --git a/jwebsite/site.py b/jwebsite/site.py index 93de39d..a2b22d4 100644 --- a/jwebsite/site.py +++ b/jwebsite/site.py @@ -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