From d0889c4c705495aac5d22616c2d21281282d70f1 Mon Sep 17 00:00:00 2001 From: Luka Hietala Date: Wed, 19 Nov 2025 18:14:08 +0200 Subject: [PATCH] add datetime jinja filter --- app.py | 14 ++++++++++++++ git/blame.py | 1 + templates/blame.html | 2 +- templates/commit.html | 2 +- templates/commits.html | 2 +- 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index 7823067..79af3c8 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,5 @@ from flask import Flask, render_template, request +from datetime import datetime from git.repo import get_bare_repos from git.commit import get_commits, get_commit @@ -11,6 +12,19 @@ from git.blame import get_blame app = Flask(__name__) +def datetime_filter(value, format='%Y-%m-%d %H:%M:%S'): + if isinstance(value, datetime): + # format regular datetime + return value.strftime(format) + elif isinstance(value, (int, float)): + # if not datetime, but number, try to convert + # just assume its unix timestamp, git uses that + dt = datetime.fromtimestamp(value) + return dt.strftime(format) + return value + +app.jinja_env.filters['datetime'] = datetime_filter + repo_path = "/home/lhietala/git-webview/repos-example" @app.route("/") def index(): diff --git a/git/blame.py b/git/blame.py index a230b63..8ec663d 100644 --- a/git/blame.py +++ b/git/blame.py @@ -5,6 +5,7 @@ import pygit2 as git def get_blame(repo_path, ref="HEAD", file_path=""): repo = git.Repository(repo_path) obj = repo.revparse_single(ref) + # TODO: doesnt work with tree refs if obj.type == git.GIT_OBJECT_COMMIT: commit = obj else: diff --git a/templates/blame.html b/templates/blame.html index 7987724..663eafe 100644 --- a/templates/blame.html +++ b/templates/blame.html @@ -18,7 +18,7 @@ {% if line.blame %} {{ line.blame.commit_id[:8] }} {{ line.blame.author.name }} - {{ line.blame.author.time }} + {{ line.blame.author.time | datetime }} {% else %} diff --git a/templates/commit.html b/templates/commit.html index cec9e77..29d753a 100644 --- a/templates/commit.html +++ b/templates/commit.html @@ -3,7 +3,7 @@

Message: {{ commit.message }}

Author: {{ commit.author.name }} <{{ commit.author.email }}>

Committer: {{ commit.committer.name }} <{{ commit.committer.email }}>

-

Date: {{ commit.date }}

+

Date: {{ commit.date | datetime }}

{% if commit.parent_id %}

Parent: {{ commit.parent_id }}

{% endif %} diff --git a/templates/commits.html b/templates/commits.html index d766dc6..98ec25f 100644 --- a/templates/commits.html +++ b/templates/commits.html @@ -4,7 +4,7 @@ -- 2.47.3