From a0eb6cf4de80c2596728ea50c780daa6a013ffe2 Mon Sep 17 00:00:00 2001 From: Luka Hietala Date: Tue, 18 Nov 2025 20:18:16 +0200 Subject: [PATCH] add simple pagination for commits --- app.py | 12 +++++++++--- git/commit.py | 8 ++++++-- git/ref.py | 1 + templates/commits.html | 8 ++++++++ 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index a05ebd4..487b947 100644 --- a/app.py +++ b/app.py @@ -15,9 +15,15 @@ def index(): @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) + page = int(request.args.get('page', 0)) + # maybe pages are not the wisest way to do this? + per_page = 50 + skip = page * per_page + + commits = get_commits(f"{repo_path}/{repo_name}", ref=ref, max_count=per_page, skip=skip) + has_next = len(commits) == per_page + has_prev = page > 0 + return render_template("commits.html", repo_name=repo_name, commits=commits, page=page, has_next=has_next, has_prev=has_prev, ref=ref) @app.route("//commits/") def commit_detail(repo_name, commit_hash): diff --git a/git/commit.py b/git/commit.py index 96b8cdd..5607e8c 100644 --- a/git/commit.py +++ b/git/commit.py @@ -1,14 +1,18 @@ import pygit2 as git # retrieves commit history for given repo path and reference -def get_commits(path, ref="HEAD", max_count=None): +def get_commits(path, ref="HEAD", max_count=None, skip=0): 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: + # pagination, 50 per page, walk until skip, then collect commits until max_count + if n < skip: + n += 1 + continue + if max_count is not None and (n - skip) >= max_count: break if len(commit.parents) > 0: # get diif stats against first parent diff --git a/git/ref.py b/git/ref.py index 4e21555..1283bed 100644 --- a/git/ref.py +++ b/git/ref.py @@ -1,5 +1,6 @@ import pygit2 as git +# retrieves all refs (branches, tags) for given repo path def get_refs(path): repo = git.Repository(path) refs = [] diff --git a/templates/commits.html b/templates/commits.html index bb9ecdf..f9d63f7 100644 --- a/templates/commits.html +++ b/templates/commits.html @@ -7,4 +7,12 @@ {% endfor %} +
+ {% if has_prev %} + Previous + {% endif %} + {% if has_next %} + Next + {% endif %} +
{% endblock %} \ No newline at end of file -- 2.47.3