2022-10-22 12:03:21 +02:00
|
|
|
from lib.db_revision import DBRevision
|
|
|
|
from lib.request import Request
|
|
|
|
|
|
|
|
|
2022-10-27 13:16:50 +02:00
|
|
|
class TreeNode:
|
|
|
|
"""
|
|
|
|
Nodes in this "tree" have either no parent (root), one parent (in a chain)
|
|
|
|
or two parents (in this case the merged revision wins in conflicts).
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, rev):
|
|
|
|
self.parent = None
|
|
|
|
self.merged = None
|
|
|
|
self.revision = rev
|
|
|
|
self.merged_into = None
|
|
|
|
|
|
|
|
def print(self):
|
|
|
|
node = self
|
|
|
|
while node:
|
|
|
|
print(node.revision, node.revision.files_hash)
|
|
|
|
if node.merged:
|
|
|
|
source_node = node.merged
|
|
|
|
while source_node:
|
|
|
|
print(" ", source_node.revision, source_node.revision.files_hash)
|
|
|
|
source_node = source_node.parent
|
|
|
|
if source_node and source_node.merged_into:
|
|
|
|
break
|
|
|
|
node = node.parent
|
|
|
|
|
|
|
|
def as_list(self):
|
|
|
|
"""Return a list for test cases"""
|
|
|
|
node = self
|
|
|
|
ret = []
|
|
|
|
while node:
|
|
|
|
repr = {"commit": node.revision.short_string()}
|
|
|
|
if node.merged:
|
|
|
|
source_node = node.merged
|
|
|
|
repr["merged"] = []
|
|
|
|
while source_node:
|
|
|
|
repr["merged"].append(source_node.revision.short_string())
|
|
|
|
source_node = source_node.parent
|
|
|
|
if source_node and source_node.merged_into:
|
|
|
|
break
|
|
|
|
node = node.parent
|
|
|
|
ret.append(repr)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2022-10-22 12:03:21 +02:00
|
|
|
class TreeBuilder:
|
|
|
|
def __init__(self, db):
|
|
|
|
self.db = db
|
|
|
|
|
2022-10-27 13:16:50 +02:00
|
|
|
def revisions_chain(self, project, package):
|
2022-10-22 12:03:21 +02:00
|
|
|
revisions = DBRevision.all_revisions(self.db, project, package)
|
|
|
|
revisions.sort()
|
|
|
|
prev = None
|
2022-10-27 13:16:50 +02:00
|
|
|
tree = None
|
2022-10-22 12:03:21 +02:00
|
|
|
for rev in revisions:
|
2022-10-24 12:01:28 +02:00
|
|
|
if rev.broken:
|
|
|
|
continue
|
2022-10-24 20:21:53 +02:00
|
|
|
if prev and prev.files_hash == rev.files_hash:
|
2022-10-22 12:03:21 +02:00
|
|
|
continue
|
|
|
|
prev = rev
|
2022-10-27 13:16:50 +02:00
|
|
|
new_tree = TreeNode(rev)
|
|
|
|
if tree:
|
|
|
|
new_tree.parent = tree
|
|
|
|
tree = new_tree
|
2022-10-22 12:03:21 +02:00
|
|
|
|
2022-10-27 13:16:50 +02:00
|
|
|
return tree
|
|
|
|
|
|
|
|
def find_merge(self, revision, source_chain):
|
|
|
|
node = source_chain
|
|
|
|
while node:
|
|
|
|
# exclude reverts happening after the merge
|
|
|
|
if (
|
|
|
|
node.revision.commit_time <= revision.commit_time
|
|
|
|
and node.revision.files_hash == revision.files_hash
|
|
|
|
):
|
|
|
|
return node
|
|
|
|
node = node.parent
|
|
|
|
|
|
|
|
def add_merge_points(self, factory_revisions):
|
2022-10-22 12:03:21 +02:00
|
|
|
source_revisions = dict()
|
2022-10-27 13:16:50 +02:00
|
|
|
factory_node = factory_revisions
|
|
|
|
while factory_node:
|
|
|
|
if factory_node.revision.request_id:
|
|
|
|
req = Request.find(self.db, factory_node.revision.request_id)
|
2022-10-22 12:03:21 +02:00
|
|
|
key = f"{req.source_project}/{req.source_package}"
|
|
|
|
if key not in source_revisions:
|
2022-10-27 13:16:50 +02:00
|
|
|
source_revisions[key] = self.revisions_chain(
|
2022-10-22 12:03:21 +02:00
|
|
|
req.source_project, req.source_package
|
|
|
|
)
|
2022-10-27 13:16:50 +02:00
|
|
|
factory_node.merged = self.find_merge(
|
|
|
|
factory_node.revision, source_revisions[key]
|
|
|
|
)
|
|
|
|
# add a reverse lookup
|
|
|
|
if factory_node.merged:
|
|
|
|
factory_node.merged.merged_into = factory_node
|
|
|
|
|
|
|
|
factory_node = factory_node.parent
|
|
|
|
|
|
|
|
def prune_loose_end(self, factory_node):
|
|
|
|
"""Look for source revisions that end in a new root and prune them"""
|
|
|
|
last_merge = None
|
|
|
|
while factory_node:
|
|
|
|
if factory_node.merged:
|
|
|
|
source_node = factory_node.merged
|
|
|
|
ended_without_merge = False
|
|
|
|
while source_node:
|
|
|
|
source_node = source_node.parent
|
|
|
|
if source_node and source_node.merged_into:
|
|
|
|
ended_without_merge = True
|
|
|
|
break
|
|
|
|
if not ended_without_merge:
|
|
|
|
factory_node.merged = None
|
2022-10-27 13:34:04 +02:00
|
|
|
if last_merge:
|
|
|
|
last_merge.parent = None
|
2022-10-27 13:16:50 +02:00
|
|
|
else:
|
|
|
|
last_merge = factory_node.merged
|
|
|
|
factory_node = factory_node.parent
|
|
|
|
|
|
|
|
def build(self, package):
|
|
|
|
factory_revisions = self.revisions_chain("openSUSE:Factory", package)
|
|
|
|
self.add_merge_points(factory_revisions)
|
|
|
|
|
|
|
|
self.prune_loose_end(factory_revisions)
|
|
|
|
return factory_revisions
|