From 97500b8a2c868d6b8e7ca573df9631ecac218f3f Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Mar 24 2017 09:36:25 +0000 Subject: Optimize the algorithm used to create the diff used to open a new PR Here as well, we used to iterate through all the commits to find which ones are present on the other branch. Now we just iterate through both branch and stop when we encounter the first commit present in both branches. We will need to consolidate our code base a little as this is the first time we are using a similar piece of code, so we definitively have duplication here. --- diff --git a/pagure/ui/fork.py b/pagure/ui/fork.py index c1113b8..d22e1a3 100644 --- a/pagure/ui/fork.py +++ b/pagure/ui/fork.py @@ -84,20 +84,31 @@ def _get_pr_info(repo_obj, orig_repo, branch_from, branch_to): if not repo_obj.is_empty and not orig_repo.is_empty: orig_commit = orig_repo[ orig_repo.lookup_branch(branch_to).get_object().hex] + repo_commit = repo_obj[commitid] - master_commits = [ - commit.oid.hex - for commit in orig_repo.walk( - orig_commit.oid.hex, pygit2.GIT_SORT_TIME) - ] + main_walker = repo_obj.walk( + orig_commit.oid.hex, pygit2.GIT_SORT_TIME) + branch_walker = repo_obj.walk( + repo_commit.oid.hex, pygit2.GIT_SORT_TIME) + main_commits = set() + branch_commits = set() - repo_commit = repo_obj[commitid] + while 1: + try: + com = main_walker.next() + main_commits.add(com.hex) + except StopIteration: + pass + try: + branch_commit = branch_walker.next() + except StopIteration: + branch_commit = None - for commit in repo_obj.walk( - repo_commit.oid.hex, pygit2.GIT_SORT_TIME): - if commit.oid.hex in master_commits: + branch_commits.add(branch_commit.oid.hex) + if main_commits.intersection(branch_commits): break - diff_commits.append(commit) + + diff_commits.append(branch_commit) if diff_commits: first_commit = repo_obj[diff_commits[-1].oid.hex]