Skip to content

Commit 872d051

Browse files
authored
Updates pygit2 (#911)
* Updates pygit2 to pygit 1.15 and enables type checking. * Fixes isort config.
1 parent 548ee07 commit 872d051

File tree

17 files changed

+137
-86
lines changed

17 files changed

+137
-86
lines changed

.isort.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[settings]
2-
known_third_party = PyQt5,_pytest,argparse_utils,benchbuild,click,cryptography,distro,git,github,graphviz,jinja2,matplotlib,networkx,numpy,packaging,pandas,pkg_resources,plotly,plumbum,pydriller,pygit2,pygtrie,pylatex,pytest,requests,rich,scipy,seaborn,setuptools,sklearn,tabulate,yaml
2+
known_firstparty = varats
33
multi_line_output=3
44
use_parentheses = True
55
include_trailing_comma: True

docs/source/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
import click # isort:skip
9797
import git # isort:skip
9898
import github # isort:skip
99+
import pygit2 # isort:skip
99100
import urllib3.exceptions # isort:skip
100101

101102
# Some packages use new syntax for type checking that isn't available to us

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ dependencies = [
2222
"pandas>=1.5.3",
2323
"plotly>=5.13.1",
2424
"plumbum>=1.6",
25-
"pygit2>=1.10,<1.14.0",
25+
"pygit2>=1.15.0,<1.16.0", # 1.16 drops support for python 3.9
2626
"pygraphviz>=1.7",
2727
"pygtrie>=2.3",
2828
"pylatex>=1.4.1",
@@ -98,7 +98,7 @@ module = [
9898
"pandas",
9999
"plotly.*",
100100
"pydriller",
101-
"pygit2",
101+
"pygtrie",
102102
"scipy.*",
103103
"sklearn.*",
104104
"PyQt5.*",

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ plotly>=5.13.1
1616
plumbum>=1.6
1717
pre-commit>=3.2.0
1818
PyDriller>=2.4.1
19-
pygit2>=1.10
19+
pygit2>=1.15.0,<1.16.0
2020
PyGithub>=1.58
2121
pygraphviz>=1.7
2222
pygtrie>=2.3
@@ -31,6 +31,7 @@ rich>=12.6
3131
scikit-learn>=1.2.2
3232
seaborn>=0.13.0
3333
tabulate>=0.9
34+
types-pygit2 # remove when updating to pygit2 >=1.16
3435
types-PyYAML
3536
types-requests
3637
types-tabulate

tests/experiment/test_workload_util.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
import unittest
33
from pathlib import Path
44

5-
import varats.experiment.workload_util as wu
65
from benchbuild.command import Command, PathToken, RootRenderer
76
from benchbuild.source.base import Revision, Variant
7+
8+
import varats.experiment.workload_util as wu
9+
from tests.helper_utils import run_in_test_environment, UnitTestFixtures
810
from varats.paper.paper_config import load_paper_config
911
from varats.projects.c_projects.xz import Xz
1012
from varats.projects.perf_tests.feature_perf_cs_collection import (
@@ -14,8 +16,6 @@
1416
from varats.utils.git_util import ShortCommitHash
1517
from varats.utils.settings import vara_cfg
1618

17-
from tests.helper_utils import run_in_test_environment, UnitTestFixtures
18-
1919
TT = PathToken.make_token(RootRenderer())
2020

2121

tests/provider/test_bug_provider.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def setUp(self) -> None:
160160

161161
def get(commit_id: str) -> pygit2.Commit:
162162
"""Method that creates simple pygit2 Commit mocks for given ID."""
163-
mock_commit = mock.create_autospec(pygit2.Commit)
163+
mock_commit: pygit2.Commit = mock.create_autospec(pygit2.Commit)
164164
mock_commit.id = pygit2.Oid(hex=commit_id)
165165
return mock_commit
166166

@@ -169,6 +169,7 @@ def get(commit_id: str) -> pygit2.Commit:
169169
self.mock_pygit.get = get
170170
self.mock_repo_handle = mock.create_autospec(RepositoryHandle)
171171
self.mock_repo_handle.pygit_repo = self.mock_pygit
172+
self.mock_repo_handle.pygit_commit = get
172173

173174
def test_issue_events_closing_bug(self) -> None:
174175
"""Test identifying issue events that close a bug related issue, with
@@ -260,8 +261,8 @@ def test_pygit_bug_creation(self, mock_pydriller_git) -> None:
260261
mock_pydriller_git.return_value = DummyPydrillerRepo("")
261262

262263
pybug = _create_corresponding_bug(
263-
self.mock_pygit.get(issue_event.commit_id), self.mock_pygit,
264-
issue_event.issue.number
264+
self.mock_repo_handle.pygit_commit(issue_event.commit_id),
265+
self.mock_repo_handle, issue_event.issue.number
265266
)
266267

267268
self.assertEqual(issue_event.commit_id, str(pybug.fixing_commit.id))
@@ -357,20 +358,20 @@ def test_filter_commit_message_bugs(
357358
of bugs is created correctly."""
358359

359360
first_fixing_commit = mock.create_autospec(pygit2.Commit)
360-
first_fixing_commit.hex = "1241"
361+
first_fixing_commit.id = "1241"
361362
first_fixing_commit.message = "Fixed first issue"
362363

363364
first_non_fixing_commit = mock.create_autospec(pygit2.Commit)
364-
first_non_fixing_commit.hex = "1242"
365+
first_non_fixing_commit.id = "1242"
365366
first_non_fixing_commit.message = "Added documentation\n" + \
366367
"Grammar Errors need to be fixed"
367368

368369
second_non_fixing_commit = mock.create_autospec(pygit2.Commit)
369-
second_non_fixing_commit.hex = "1243"
370+
second_non_fixing_commit.id = "1243"
370371
second_non_fixing_commit.message = "Added feature X"
371372

372373
second_fixing_commit = mock.create_autospec(pygit2.Commit)
373-
second_fixing_commit.hex = "1244"
374+
second_fixing_commit.id = "1244"
374375
second_fixing_commit.message = "fixes second problem"
375376

376377
def mock_walk(_start_id: str, _sort_mode: int):
@@ -388,14 +389,14 @@ def mock_walk(_start_id: str, _sort_mode: int):
388389
mock_pydriller_git.return_value = DummyPydrillerRepo("")
389390

390391
# commit filter method for pygit bugs
391-
def accept_pybugs(repo: pygit2.Repository,
392+
def accept_pybugs(repo: RepositoryHandle,
392393
commit: pygit2.Commit) -> tp.Optional[PygitBug]:
393394
if _is_closing_message(commit.message):
394-
return _create_corresponding_bug(commit, self.mock_pygit)
395+
return _create_corresponding_bug(commit, self.mock_repo_handle)
395396
return None
396397

397398
pybug_ids = set(
398-
pybug.fixing_commit.hex
399+
pybug.fixing_commit.id
399400
for pybug in _filter_commit_message_bugs("", accept_pybugs)
400401
)
401402
expected_ids = {"1241", "1244"}
@@ -451,31 +452,31 @@ def test_basic_repo_pygit_bugs(self) -> None:
451452
fixing_commit="ddf0ba95408dc5508504c84e6616c49128410389"
452453
)
453454
pybug_first_intro_ids = set(
454-
intro_commit.hex
455+
intro_commit.id
455456
for intro_commit in next(iter(pybug_first)).introducing_commits
456457
)
457458

458459
pybug_second = provider.find_pygit_bugs(
459460
fixing_commit="d846bdbe45e4d64a34115f5285079e1b5f84007f"
460461
)
461462
pybug_second_intro_ids = set(
462-
intro_commit.hex
463+
intro_commit.id
463464
for intro_commit in next(iter(pybug_second)).introducing_commits
464465
)
465466

466467
pybug_third = provider.find_pygit_bugs(
467468
fixing_commit="2da78b2820370f6759e9086fad74155d6655e93b"
468469
)
469470
pybug_third_intro_ids = set(
470-
intro_commit.hex
471+
intro_commit.id
471472
for intro_commit in next(iter(pybug_third)).introducing_commits
472473
)
473474

474475
pybug_fourth = provider.find_pygit_bugs(
475476
fixing_commit="3b76c8d295385358375fefdb0cf045d97ad2d193"
476477
)
477478
pybug_fourth_intro_ids = set(
478-
intro_commit.hex
479+
intro_commit.id
479480
for intro_commit in next(iter(pybug_fourth)).introducing_commits
480481
)
481482

tests/test_containers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
RunLayer,
1414
EntryPoint,
1515
)
16+
17+
from tests.helper_utils import run_in_test_environment
1618
from varats.containers.containers import (
1719
ImageBase,
1820
StageBuilder,
@@ -23,8 +25,6 @@
2325
from varats.tools.research_tools.research_tool import Distro
2426
from varats.utils.settings import vara_cfg, bb_cfg
2527

26-
from tests.helper_utils import run_in_test_environment
27-
2828

2929
class TestImageBase(unittest.TestCase):
3030
"""Test ImageBase class."""

tests/utils/test_github_util.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
from github import Github, PaginatedList
66
from github.GithubObject import GithubObject, NonCompletableGithubObject
77
from github.PaginatedList import PaginatedListBase
8+
from github.Requester import Requester
9+
10+
from tests.helper_utils import run_in_test_environment
811
from varats.utils.github_util import (
912
get_cached_github_object,
1013
_get_cached_pygithub_object,
1114
_get_cached_pygithub_object_list,
1215
get_cached_github_object_list,
1316
)
1417

15-
from tests.helper_utils import run_in_test_environment
16-
1718

1819
class DummyGithubObject(NonCompletableGithubObject):
1920
"""Dummy GithubObject class."""
@@ -27,8 +28,24 @@ def _useAttributes(self, attributes: tp.Any) -> None:
2728
pass
2829

2930

31+
class DummyRequester(Requester):
32+
"""Dummy Requester class."""
33+
34+
def __init__(self) -> None:
35+
super().__init__(
36+
auth=None,
37+
base_url="https://base.url",
38+
timeout=1,
39+
user_agent="user agent",
40+
per_page=10,
41+
verify=False,
42+
retry=None,
43+
pool_size=None
44+
)
45+
46+
3047
def create_dummy_github_object() -> GithubObject:
31-
return DummyGithubObject(None, {}, None)
48+
return DummyGithubObject(DummyRequester(), {}, {})
3249

3350

3451
class DummyPaginatedList(PaginatedListBase):

varats-core/varats/project/project_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def get_commit(crp: CommitRepoPair) -> pygit2.Commit:
161161
Returns:
162162
the commit corresponding to the given CommitRepoPair
163163
"""
164-
commit = repos[crp.repository_name].pygit_repo.get(crp.commit_hash.hash)
164+
commit = repos[crp.repository_name].maybe_pygit_commit(crp.commit_hash)
165165
if not commit:
166166
raise LookupError(
167167
f"Could not find commit {crp} for project {project_name}."

varats-core/varats/provider/bug/bug.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
get_project_cls_by_name,
1313
get_local_project_repo,
1414
)
15-
from varats.utils.git_util import FullCommitHash
15+
from varats.utils.git_util import FullCommitHash, RepositoryHandle
1616
from varats.utils.github_util import (
1717
get_cached_github_object_list,
1818
get_github_repo_name_for_project,
@@ -101,14 +101,14 @@ def as_raw_bug(pygit_bug: PygitBug) -> RawBug:
101101
)
102102

103103

104-
def as_pygit_bug(raw_bug: RawBug, repo: pygit2.Repository) -> PygitBug:
104+
def as_pygit_bug(raw_bug: RawBug, repo: RepositoryHandle) -> PygitBug:
105105
"""Converts a ``RawBug`` to a ``PygitBug``."""
106106
introducing_commits: tp.Set[pygit2.Commit] = set()
107107
for intro_commit in raw_bug.introducing_commits:
108-
introducing_commits.add(repo.get(intro_commit.hash))
108+
introducing_commits.add(repo.pygit_commit(intro_commit))
109109
return PygitBug(
110-
repo.get(raw_bug.fixing_commit.hash), introducing_commits,
111-
raw_bug.issue_id, raw_bug.creation_date, raw_bug.resolution_date
110+
repo[raw_bug.fixing_commit.hash], introducing_commits, raw_bug.issue_id,
111+
raw_bug.creation_date, raw_bug.resolution_date
112112
)
113113

114114

@@ -239,7 +239,7 @@ def load_issue_events(github: Github) -> 'PaginatedList[IssueEvent]':
239239

240240
def _create_corresponding_bug(
241241
closing_commit: pygit2.Commit,
242-
project_repo: pygit2.Repository,
242+
project_repo: RepositoryHandle,
243243
issue_id: tp.Optional[int] = None,
244244
creation_date: tp.Optional[datetime] = None,
245245
resolution_date: tp.Optional[datetime] = None
@@ -258,7 +258,7 @@ def _create_corresponding_bug(
258258
Returns:
259259
the specified bug
260260
"""
261-
pydrill_repo = pydriller.Git(project_repo.path)
261+
pydrill_repo = pydriller.Git(str(project_repo.repo_path))
262262

263263
introducing_commits: tp.Set[pygit2.Commit] = set()
264264
blame_dict = pydrill_repo.get_commits_last_modified_lines(
@@ -267,7 +267,7 @@ def _create_corresponding_bug(
267267

268268
for _, introducing_set in blame_dict.items():
269269
for introducing_id in introducing_set:
270-
introducing_commits.add(project_repo.get(introducing_id))
270+
introducing_commits.add(project_repo.pygit_commit(introducing_id))
271271

272272
return PygitBug(
273273
closing_commit, introducing_commits, issue_id, creation_date,
@@ -294,12 +294,12 @@ def _find_corresponding_pygit_suspect_tuple(
294294
A PygitSuspectTuple if the issue event represents the closing of a bug,
295295
None otherwise
296296
"""
297-
pygit_repo = get_local_project_repo(project_name).pygit_repo
298-
pydrill_repo = pydriller.Git(pygit_repo.path)
297+
repo = get_local_project_repo(project_name)
298+
pydrill_repo = pydriller.Git(str(repo.repo_path))
299299

300300
if _has_closed_a_bug(issue_event) and issue_event.commit_id:
301301
issue_date = issue_event.issue.created_at
302-
fixing_commit = pygit_repo.get(issue_event.commit_id)
302+
fixing_commit = repo.pygit_commit(issue_event.commit_id)
303303
pydrill_fixing_commit = pydrill_repo.get_commit(issue_event.commit_id)
304304
blame_dict = pydrill_repo.get_commits_last_modified_lines(
305305
pydrill_fixing_commit
@@ -317,9 +317,9 @@ def _find_corresponding_pygit_suspect_tuple(
317317
).committer_date.astimezone(timezone.utc)
318318

319319
if introduction_date > issue_date: # commit is a suspect
320-
suspect_commits.add(pygit_repo.get(introducing_id))
320+
suspect_commits.add(repo.pygit_commit(introducing_id))
321321
else:
322-
non_suspect_commits.add(pygit_repo.get(introducing_id))
322+
non_suspect_commits.add(repo.pygit_commit(introducing_id))
323323

324324
return PygitSuspectTuple(
325325
fixing_commit, non_suspect_commits, suspect_commits,
@@ -386,7 +386,7 @@ def _filter_issue_bugs(
386386

387387
def _filter_commit_message_bugs(
388388
project_name: str,
389-
commit_filter_function: tp.Callable[[pygit2.Repository, pygit2.Commit],
389+
commit_filter_function: tp.Callable[[RepositoryHandle, pygit2.Commit],
390390
tp.Optional[PygitBug]]
391391
) -> tp.FrozenSet[PygitBug]:
392392
"""
@@ -400,10 +400,11 @@ def _filter_commit_message_bugs(
400400
the set of bugs created by the given filter
401401
"""
402402
filtered_bugs = set()
403-
project_repo = get_local_project_repo(project_name).pygit_repo
403+
project_repo = get_local_project_repo(project_name)
404+
pygit_repo = project_repo.pygit_repo
404405

405-
for commit in project_repo.walk(
406-
project_repo.head.target, pygit2.GIT_SORT_TIME
406+
for commit in pygit_repo.walk(
407+
pygit_repo.head.target, pygit2.enums.SortMode.TIME
407408
):
408409
pybug = commit_filter_function(project_repo, commit)
409410
if pybug:
@@ -435,7 +436,7 @@ def accept_suspect_with_certain_introduction(
435436
) -> tp.Optional[PygitBug]:
436437
bug = suspect.create_corresponding_bug()
437438

438-
if fixing_commit and bug.fixing_commit.hex != fixing_commit:
439+
if fixing_commit and bug.fixing_commit.id != fixing_commit:
439440
return None
440441

441442
if introducing_commit and introducing_commit not in [
@@ -470,12 +471,12 @@ def find_commit_message_bugs(
470471
"""
471472

472473
def accept_commit_message_pybug(
473-
repo: pygit2.Repository, commit: pygit2.Commit
474+
repo: RepositoryHandle, commit: pygit2.Commit
474475
) -> tp.Optional[PygitBug]:
475476
if _is_closing_message(commit.message):
476477
bug = _create_corresponding_bug(commit, repo)
477478

478-
if fixing_commit and bug.fixing_commit.hex != fixing_commit:
479+
if fixing_commit and bug.fixing_commit.id != fixing_commit:
479480
return None
480481

481482
if introducing_commit and introducing_commit not in [

0 commit comments

Comments
 (0)