From 97f4325e0f1f40a6918ceadd420c839b222edd20 Mon Sep 17 00:00:00 2001 From: Luka Hietala Date: Tue, 18 Nov 2025 19:53:03 +0200 Subject: [PATCH] use url for and add commit details page --- README.md | 2 +- app.py | 7 ++++++- git/commit.py | 34 +++++++++++++++++++++++++++++++++- templates/commit.html | 15 +++++++++++++++ templates/commits.html | 2 +- templates/index.html | 2 +- 6 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 templates/commit.html diff --git a/README.md b/README.md index 8b4e76e..1395b59 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -Fast webview using pygit2 (lib2git) bindings. +Fast webview using pygit2 (lib2git) bindings. For my personal use, for now image diff --git a/app.py b/app.py index da8d209..c7210b0 100644 --- a/app.py +++ b/app.py @@ -1,7 +1,7 @@ from flask import Flask, render_template, request from git.repo import get_bare_repos -from git.commit import get_commits +from git.commit import get_commits, get_commit app = Flask(__name__) @@ -18,5 +18,10 @@ def repo_commits(repo_name): commits = get_commits(f"{repo_path}/{repo_name}", ref=ref, max_count=50) return render_template("commits.html", repo_name=repo_name, commits=commits) +@app.route("//commits/") +def commit_detail(repo_name, commit_hash): + commit = get_commit(f"{repo_path}/{repo_name}", commit_hash) + return render_template("commit.html", repo_name=repo_name, commit=commit) + if __name__ == "__main__": app.run(debug=True) \ No newline at end of file diff --git a/git/commit.py b/git/commit.py index acb0101..96b8cdd 100644 --- a/git/commit.py +++ b/git/commit.py @@ -37,4 +37,36 @@ def get_commits(path, ref="HEAD", max_count=None): } commits.append(commit_info) n += 1 - return commits \ No newline at end of file + return commits + +# retrieves a single commit by its hash +def get_commit(path, commit_hash): + repo = git.Repository(path) + commit = repo.revparse_single(commit_hash) + + if len(commit.parents) > 0: + diff = repo.diff(commit.parents[0], commit) + stats = diff.stats + diff_stats = { + 'insertions': stats.insertions, + 'deletions': stats.deletions, + 'files_changed': stats.files_changed + } + else: + diff_stats = { + 'insertions': 0, + 'deletions': 0, + 'files_changed': 0 + } + diff = None + + commit_info = { + 'id': str(commit.id), + 'message': commit.message.strip(), + 'author': commit.author, + 'committer': commit.committer, + 'date': commit.commit_time, + 'diff_stats': diff_stats, + 'diff': diff.patch if diff else None + } + return commit_info \ No newline at end of file diff --git a/templates/commit.html b/templates/commit.html new file mode 100644 index 0000000..ed8eeeb --- /dev/null +++ b/templates/commit.html @@ -0,0 +1,15 @@ +{% block content %} +

Commit {{ commit.id }}

+

Message: {{ commit.message }}

+

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

+

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

+

Date: {{ commit.date }}

+

Diff Stats

+
    +
  • Files Changed: {{ commit.diff_stats.files_changed }}
  • +
  • Insertions: {{ commit.diff_stats.insertions }}
  • +
  • Deletions: {{ commit.diff_stats.deletions }}
  • +
+

Diff

+
{{ commit.diff }}
+{% endblock %} \ No newline at end of file diff --git a/templates/commits.html b/templates/commits.html index 18e7498..bb9ecdf 100644 --- a/templates/commits.html +++ b/templates/commits.html @@ -3,7 +3,7 @@
    {% for commit in commits %}
  • - {{ commit.id }} - {{ commit.message }} - {{ commit.author.name }} - {{ commit.date }} {{ commit.diff_stats.files_changed }} files changed, {{ commit.diff_stats.insertions }} insertions, {{ commit.diff_stats.deletions }} deletions + {{ commit.id }} - {{ commit.message }} - {{ commit.author.name }} - {{ commit.date }} {{ commit.diff_stats.files_changed }} files changed, {{ commit.diff_stats.insertions }} insertions, {{ commit.diff_stats.deletions }} deletions
  • {% endfor %}
diff --git a/templates/index.html b/templates/index.html index 8c77402..c70ffad 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,7 +2,7 @@

Repositories

{% endblock %} \ No newline at end of file -- 2.47.3