Skip to content

Commit

Permalink
Fix commit parent selection when there are multiple parents (#695)
Browse files Browse the repository at this point in the history
  • Loading branch information
michelletran-codecov authored Sep 16, 2024
1 parent 8c6d621 commit 4f20d85
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
33 changes: 14 additions & 19 deletions services/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from shared.yaml.user_yaml import OwnerContext
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Query

from database.enums import CommitErrorTypes
from database.models import Commit, Owner, Pull, Repository
Expand Down Expand Up @@ -163,13 +164,7 @@ async def fetch_appropriate_parent_for_commit(
~Commit.message.is_(None),
~Commit.deleted.is_(True),
)
if possible_commit_query.count() <= 1:
possible_commit = possible_commit_query.first()
else:
possible_commit = possible_commit_query.filter(
Commit.branch == commit.branch,
)
possible_commit = possible_commit_query.first()
possible_commit = possibly_filter_out_branch(commit, possible_commit_query)
if possible_commit:
return possible_commit.commitid
ancestors_tree = await repository_service.get_ancestors_tree(commitid)
Expand All @@ -183,12 +178,7 @@ async def fetch_appropriate_parent_for_commit(
~Commit.message.is_(None),
~Commit.deleted.is_(True),
)
if closest_parent_query.count() <= 1:
closest_parent = closest_parent_query.first()
else:
closest_parent = closest_parent_query.filter(
Commit.branch == commit.branch,
).first()
closest_parent = possibly_filter_out_branch(commit, closest_parent_query)
if closest_parent:
return closest_parent.commitid
if closest_parent_without_message is None:
Expand All @@ -197,12 +187,7 @@ async def fetch_appropriate_parent_for_commit(
Commit.repoid == commit.repoid,
~Commit.deleted.is_(True),
)
if res.count() <= 1:
res = res.first()
else:
res = res.filter(
Commit.branch == commit.branch,
).first()
res = possibly_filter_out_branch(commit, res)
if res:
closest_parent_without_message = res[0]
elements = parents
Expand All @@ -213,6 +198,16 @@ async def fetch_appropriate_parent_for_commit(
return closest_parent_without_message


def possibly_filter_out_branch(commit: Commit, query: Query) -> Commit | None:
if query.count() <= 1:
query = query.first()
else:
query = query.filter(
Commit.branch == commit.branch,
).first()
return query


async def possibly_update_commit_from_provider_info(commit, repository_service):
repoid = commit.repoid
commitid = commit.commitid
Expand Down
8 changes: 4 additions & 4 deletions services/tests/test_repository_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,17 +635,17 @@ async def test_fetch_appropriate_parent_for_commit_multiple_parents(
first_parent_commit_id = "8aa5be054aeb21cf5a664ecd504a1af6f5ceafba"
second_parent_commit_id = "a" * 32
repository = RepositoryFactory.create()
first_parent_commit = CommitFactory.create(
commitid=first_parent_commit_id, repository=repository, branch="1stBranch"
)
second_parent_commit = CommitFactory.create(
commitid=second_parent_commit_id, repository=repository, branch="2ndBranch"
)
first_parent_commit = CommitFactory.create(
commitid=first_parent_commit_id, repository=repository, branch="1stBranch"
)
commit = CommitFactory.create(
parent_commit_id=None, repository=repository, branch="1stBranch"
)
dbsession.add(first_parent_commit)
dbsession.add(second_parent_commit)
dbsession.add(first_parent_commit)
dbsession.add(commit)
dbsession.flush()
git_commit = {"parents": [first_parent_commit_id, second_parent_commit_id]}
Expand Down

0 comments on commit 4f20d85

Please sign in to comment.