bad_clone_prediction/CommitRelationExtractor.py

58 lines
2.1 KiB
Python

from dulwich.objects import Commit
from dulwich.repo import Repo
from models.RepoInfo import RepoInfo
from MySQLOperator import MySQLOperator
def extract_commit_relations(repoInfo: RepoInfo, mysqlOp: MySQLOperator):
step_name = "commit relation extraction"
mysqlOp.cursor.execute(
"select handled from `{steps_tablename}` where step_name=%s".format(
steps_tablename=mysqlOp.tablename_dict["steps"]
),
(step_name),
)
handled = mysqlOp.cursor.fetchone()["handled"]
if not handled:
mysqlOp.truncate_table(mysqlOp.tablename_dict["commit_relations"])
commit_sha_id_dict = mysqlOp.get_commit_sha_id_dict()
repo = Repo(repoInfo.bare_repo_path)
for sha, id in commit_sha_id_dict.items():
commit: Commit = repo.object_store[sha]
parent_shas = commit.parents
if len(parent_shas) == 0:
mysqlOp.cursor.execute(
"insert into `{commit_relation_tablename}` (id, `parent_id`) values (%s, %s)".format(
commit_relation_tablename=mysqlOp.tablename_dict[
"commit_relations"
]
),
(id, None),
)
else:
for parent_sha in parent_shas:
parent_id = commit_sha_id_dict[parent_sha]
mysqlOp.cursor.execute(
"insert into `{commit_relation_tablename}` (id, parent_id) values (%s, %s)".format(
commit_relation_tablename=mysqlOp.tablename_dict[
"commit_relations"
]
),
(id, parent_id),
)
# update steps table
mysqlOp.cursor.execute(
"update `{steps_tablename}` set handled=%s where step_name=%s".format(
steps_tablename=mysqlOp.tablename_dict["steps"]
),
(1, step_name),
)
mysqlOp.connection.commit()