From d8983d3b6741708d6a021b76db233f8462296cb5 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Mon, 27 Jul 2020 12:54:33 -0400 Subject: [PATCH] summary: use topological commit sort --- gitsrht/blueprints/repo.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/gitsrht/blueprints/repo.py b/gitsrht/blueprints/repo.py index 9672ffb..502c821 100644 --- a/gitsrht/blueprints/repo.py +++ b/gitsrht/blueprints/repo.py @@ -62,15 +62,13 @@ def render_empty_repo(owner, repo): return render_template("empty-repo.html", owner=owner, repo=repo, clone_urls=urls) -def get_last_3_commits(commit): - commits = [commit] - for parent in commit.parents: - commits.append(parent) - for grandparent in parent.parents: - commits.append(grandparent) - - commits = sorted(commits, key=lambda c: commit_time(c), reverse=True) - return commits[:3] +def get_last_3_commits(git_repo, commit): + commits = list() + for c in git_repo.walk(commit.id, pygit2.GIT_SORT_TOPOLOGICAL): + commits.append(c) + if len(commits) >= 3: + break + return commits @repo.route("//") def summary(owner, repo): @@ -82,7 +80,7 @@ def summary(owner, repo): default_branch = git_repo.default_branch() tip = git_repo.get(default_branch.target) - commits = get_last_3_commits(tip) + commits = get_last_3_commits(git_repo, tip) link_prefix = url_for( 'repo.tree', owner=repo.owner, repo=repo.name, ref=f"{default_branch.name}/") # Trailing slash needed -- 2.38.4