Commit a370bb2 Optimize diffing two branches

1 file Authored and Committed by pingou 4 months ago
Optimize diffing two branches

Instead of building the list of all the commits of one and then iterating
through the other to see if it has commits shared with the first, we now
go through the commits on both branches one step at a time and check if
they have overlapping commits at each steps.

    
 1 @@ -270,12 +270,6 @@
 2           if not repo_obj.head_is_unborn:
 3               compare_branch = repo_obj.lookup_branch(
 4                   repo_obj.head.shorthand)
 5 -             compare_commits = [
 6 -                 commit.oid.hex
 7 -                 for commit in repo_obj.walk(
 8 -                     compare_branch.get_object().hex,
 9 -                     pygit2.GIT_SORT_TIME)
10 -             ]
11           else:
12               compare_branch = None
13   
14 @@ -290,18 +284,35 @@
15                       and compare_branch.branch_name == branch.branch_name:
16                   continue
17   
18 -             diff_commits = []
19 - 
20               repo_commit = repo_obj[branch.get_object().hex]
21   
22 -             for commit in repo_obj.walk(
23 -                     repo_commit.oid.hex, pygit2.GIT_SORT_TIME):
24 -                 if commit.oid.hex in compare_commits:
25 +             if compare_branch:
26 +                 main_walker = repo_obj.walk(
27 +                     compare_branch.get_object().hex,
28 +                     pygit2.GIT_SORT_TIME)
29 +             branch_walker = repo_obj.walk(
30 +                     repo_commit.oid.hex,
31 +                     pygit2.GIT_SORT_TIME)
32 +             main_commits = set()
33 +             branch_commits = list()
34 +             while 1:
35 +                 if compare_branch:
36 +                     try:
37 +                         com = main_walker.next()
38 +                         main_commits.add(com.hex)
39 +                     except StopIteration:
40 +                         pass
41 +                 try:
42 +                     com = branch_walker.next()
43 +                     branch_commits.append(com.hex)
44 +                 except StopIteration:
45 +                     break
46 + 
47 +                 if main_commits.intersection(set(branch_commits)):
48                       break
49 -                 diff_commits.append(commit.oid.hex)
50   
51 -             if diff_commits:
52 -                 branches[branchname] = diff_commits
53 +             if branch_commits:
54 +                 branches[branchname] = branch_commits
55   
56       prs = pagure.lib.search_pull_requests(
57           pagure.SESSION,