diff --git a/app.py b/app.py
index a05ebd4..487b947 100644
--- a/app.py
+++ b/app.py
@app.route("/<repo_name>/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("/<repo_name>/commits/<commit_hash>")
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
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
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
</li>
{% endfor %}
</ul>
+ <div>
+ {% if has_prev %}
+ <a href="{{ url_for('repo_commits', repo_name=repo_name, page=page-1, ref=ref) }}">Previous</a>
+ {% endif %}
+ {% if has_next %}
+ <a href="{{ url_for('repo_commits', repo_name=repo_name, page=page+1, ref=ref) }}">Next</a>
+ {% endif %}
+ </div>
{% endblock %}
\ No newline at end of file