From 8b9c6ecd6e6d81f3832a11542aa460d46e1b68ef Mon Sep 17 00:00:00 2001 From: Luka Hietala Date: Tue, 18 Nov 2025 19:30:13 +0200 Subject: [PATCH] list repo commits, with ref param --- app.py | 13 +++++++++++-- git/commit.py | 40 ++++++++++++++++++++++++++++++++++++++++ templates/commits.html | 10 ++++++++++ templates/index.html | 2 +- 4 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 templates/commits.html diff --git a/app.py b/app.py index df03977..da8d209 100644 --- a/app.py +++ b/app.py @@ -1,13 +1,22 @@ -from flask import Flask, render_template +from flask import Flask, render_template, request from git.repo import get_bare_repos +from git.commit import get_commits app = Flask(__name__) +repo_path = "/home/lhietala/git-webview/repos-example" @app.route("/") def index(): - repos = get_bare_repos("/home/lhietala/git-webview/repos-example") + repos = get_bare_repos(repo_path) return render_template("index.html", repos=repos) +@app.route("//commits") +def repo_commits(repo_name): + ref = request.args.get('ref', 'HEAD') + + commits = get_commits(f"{repo_path}/{repo_name}", ref=ref, max_count=50) + return render_template("commits.html", repo_name=repo_name, commits=commits) + if __name__ == "__main__": app.run(debug=True) \ No newline at end of file diff --git a/git/commit.py b/git/commit.py index e69de29..acb0101 100644 --- a/git/commit.py +++ b/git/commit.py @@ -0,0 +1,40 @@ +import pygit2 as git + +# retrieves commit history for given repo path and reference +def get_commits(path, ref="HEAD", max_count=None): + repo = git.Repository(path) + commits = [] + walker = repo.walk(repo.revparse_single(ref).id, git.GIT_SORT_TIME) + + n = 0 + for commit in walker: + if max_count is not None and n >= max_count: + break + if len(commit.parents) > 0: + # get diif stats against first parent + # libgit2 has very fast diff stats calculation, using that here + diff = repo.diff(commit.parents[0], commit) + stats = diff.stats + diff_stats = { + 'insertions': stats.insertions, + 'deletions': stats.deletions, + 'files_changed': stats.files_changed + } + else: + # TODO: compare to NULL_TREE + diff_stats = { + 'insertions': 0, + 'deletions': 0, + 'files_changed': 0 + } + commit_info = { + 'id': str(commit.id), + 'message': commit.message.strip(), + 'author': commit.author, + 'committer': commit.committer, + 'date': commit.commit_time, + 'diff_stats': diff_stats + } + commits.append(commit_info) + n += 1 + return commits \ No newline at end of file diff --git a/templates/commits.html b/templates/commits.html new file mode 100644 index 0000000..18e7498 --- /dev/null +++ b/templates/commits.html @@ -0,0 +1,10 @@ +{% block content %} +

Commits for {{ repo_name }}

+ +{% endblock %} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index c3cabf8..8c77402 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,7 +2,7 @@

Repositories

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