Skip to content

Commit 8899dc2

Browse files
committed
refactor(git): Rename FileChanges to ChangedFile
1 parent 30cc13b commit 8899dc2

File tree

5 files changed

+32
-32
lines changed

5 files changed

+32
-32
lines changed

src/dda/utils/git/changeset.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ChangeType(StrEnum):
2323
DELETED = "D"
2424

2525

26-
class FileChanges(Struct, frozen=True):
26+
class ChangedFile(Struct, frozen=True):
2727
"""Represents changes to a single file in a git repository."""
2828

2929
file: Path
@@ -161,7 +161,7 @@ def dec_hook(cls, obj_type: type, obj: Any) -> Any: # type: ignore[valid-type]
161161

162162
# Need dict=True so that cached_property can be used
163163
class ChangeSet(Struct, dict=True, frozen=True):
164-
_changes: dict[Path, FileChanges] = field(default_factory=dict)
164+
_changes: dict[Path, ChangedFile] = field(default_factory=dict)
165165

166166
"""
167167
Represents a set of changes to files in a git repository.
@@ -171,16 +171,16 @@ class ChangeSet(Struct, dict=True, frozen=True):
171171
"""
172172

173173
# == dict proxy methods == #
174-
def keys(self) -> dict_keys[Path, FileChanges]:
174+
def keys(self) -> dict_keys[Path, ChangedFile]:
175175
return self._changes.keys()
176176

177-
def values(self) -> dict_values[Path, FileChanges]:
177+
def values(self) -> dict_values[Path, ChangedFile]:
178178
return self._changes.values()
179179

180-
def items(self) -> dict_items[Path, FileChanges]:
180+
def items(self) -> dict_items[Path, ChangedFile]:
181181
return self._changes.items()
182182

183-
def __getitem__(self, key: Path) -> FileChanges:
183+
def __getitem__(self, key: Path) -> ChangedFile:
184184
return self._changes[key]
185185

186186
def __contains__(self, key: Path) -> bool:
@@ -230,7 +230,7 @@ def digest(self) -> str:
230230
return str(digester.hexdigest())
231231

232232
@classmethod
233-
def from_iter(cls, data: Iterable[FileChanges]) -> Self:
233+
def from_iter(cls, data: Iterable[ChangedFile]) -> Self:
234234
"""Create a ChangeSet from an iterable of FileChanges."""
235235
items = {change.file: change for change in data}
236236
return cls(_changes=items)
@@ -241,7 +241,7 @@ def generate_from_diff_output(cls, diff_output: str | list[str]) -> Self:
241241
Generate a changeset from the output of a git diff command.
242242
The output should be passed as a string or a list of lines.
243243
"""
244-
return cls.from_iter(FileChanges.generate_from_diff_output(diff_output))
244+
return cls.from_iter(ChangedFile.generate_from_diff_output(diff_output))
245245

246246
@classmethod
247247
def enc_hook(cls, obj: Any) -> Any:

src/dda/utils/git/remote.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ def get_details_and_changes_for_commit(self, commit: Commit) -> tuple[CommitDeta
7070
from datetime import datetime
7171

7272
from dda.utils.fs import Path
73-
from dda.utils.git.changeset import ChangeSet, FileChanges
73+
from dda.utils.git.changeset import ChangeSet, ChangedFile
7474
from dda.utils.network.http.client import get_http_client
7575

7676
client = get_http_client()
7777
data = client.get(self.get_commit_github_api_url(commit)).json()
7878

7979
# Compute ChangeSet
8080
changes = ChangeSet.from_iter(
81-
FileChanges(
81+
ChangedFile(
8282
file=Path(file_obj["filename"]),
8383
type=get_change_type_from_github_status(file_obj["status"]),
8484
patch=file_obj["patch"],

tests/tools/git/test_git.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import pytest
1212

1313
from dda.utils.fs import Path
14-
from dda.utils.git.changeset import ChangeSet, ChangeType, FileChanges
14+
from dda.utils.git.changeset import ChangedFile, ChangeSet, ChangeType
1515
from dda.utils.git.commit import Commit
1616
from dda.utils.git.constants import GitEnvVars
1717
from dda.utils.process import EnvVars
@@ -196,12 +196,12 @@ def test_get_changes_with_base(app: Application, mocker: Any, repo_testcase: str
196196

197197
# Test with working tree changes
198198
working_tree_changes = ChangeSet({
199-
Path("test.txt"): FileChanges(file=Path("test.txt"), type=ChangeType.ADDED, patch="@@ -0,0 +1 @@\n+test")
199+
Path("test.txt"): ChangedFile(file=Path("test.txt"), type=ChangeType.ADDED, patch="@@ -0,0 +1 @@\n+test")
200200
})
201201
mocker.patch("dda.tools.git.Git.get_working_tree_changes", return_value=working_tree_changes)
202202

203203
changeset_with_working_tree = git.get_changes_with_base(base_commit.sha1, include_working_tree=True)
204204
expected_changeset_with_working_tree = expected_changeset | ChangeSet.from_iter([
205-
FileChanges(file=Path("test.txt"), type=ChangeType.ADDED, patch="@@ -0,0 +1 @@\n+test")
205+
ChangedFile(file=Path("test.txt"), type=ChangeType.ADDED, patch="@@ -0,0 +1 @@\n+test")
206206
])
207207
assert_changesets_equal(changeset_with_working_tree, expected_changeset_with_working_tree)

tests/utils/git/test_changeset.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
import pytest
88

99
from dda.utils.fs import Path
10-
from dda.utils.git.changeset import ChangeSet, ChangeType, FileChanges
10+
from dda.utils.git.changeset import ChangedFile, ChangeSet, ChangeType
1111
from tests.tools.git.conftest import REPO_TESTCASES
1212

1313

1414
class TestFileChangesClass:
1515
def test_basic(self):
16-
file_changes = FileChanges(file=Path("/path/to/file"), type=ChangeType.ADDED, patch="patch")
16+
file_changes = ChangedFile(file=Path("/path/to/file"), type=ChangeType.ADDED, patch="patch")
1717
assert file_changes.file == Path("/path/to/file")
1818
assert file_changes.type == ChangeType.ADDED
1919
assert file_changes.patch == "patch"
@@ -37,7 +37,7 @@ def test_generate_from_diff_output(self, repo_testcase):
3737
key=lambda x: x.file.as_posix(),
3838
)
3939

40-
seen_filechanges = sorted(FileChanges.generate_from_diff_output(diff_output), key=lambda x: x.file.as_posix())
40+
seen_filechanges = sorted(ChangedFile.generate_from_diff_output(diff_output), key=lambda x: x.file.as_posix())
4141

4242
assert len(seen_filechanges) == len(expected_filechanges)
4343
for seen, expected in zip(seen_filechanges, expected_filechanges, strict=True):
@@ -46,39 +46,39 @@ def test_generate_from_diff_output(self, repo_testcase):
4646
assert seen.patch == expected.patch
4747

4848
def test_encode_decode(self):
49-
file_changes = FileChanges(file=Path("/path/to/file"), type=ChangeType.ADDED, patch="patch")
50-
encoded_file_changes = msgspec.json.encode(file_changes, enc_hook=FileChanges.enc_hook)
49+
file_changes = ChangedFile(file=Path("/path/to/file"), type=ChangeType.ADDED, patch="patch")
50+
encoded_file_changes = msgspec.json.encode(file_changes, enc_hook=ChangedFile.enc_hook)
5151
decoded_file_changes = msgspec.json.decode(
52-
encoded_file_changes, type=FileChanges, dec_hook=FileChanges.dec_hook
52+
encoded_file_changes, type=ChangedFile, dec_hook=ChangedFile.dec_hook
5353
)
5454
assert decoded_file_changes == file_changes
5555

5656

5757
class TestChangeSetClass:
5858
def test_basic(self):
59-
change = FileChanges(file=Path("/path/to/file"), type=ChangeType.ADDED, patch="patch")
59+
change = ChangedFile(file=Path("/path/to/file"), type=ChangeType.ADDED, patch="patch")
6060
changeset = ChangeSet({change.file: change})
6161
assert changeset[Path("/path/to/file")] == change
6262

6363
def test_add(self):
64-
change = FileChanges(file=Path("/path/to/file"), type=ChangeType.ADDED, patch="patch")
64+
change = ChangedFile(file=Path("/path/to/file"), type=ChangeType.ADDED, patch="patch")
6565
changeset = ChangeSet.from_iter([change])
6666
assert changeset[Path("/path/to/file")] == change
6767

6868
def test_digest(self):
6969
changes = [
70-
FileChanges(file=Path("/path/to/file"), type=ChangeType.ADDED, patch="patch"),
71-
FileChanges(file=Path("file2"), type=ChangeType.MODIFIED, patch="patch2"),
72-
FileChanges(file=Path("/path/../file3"), type=ChangeType.DELETED, patch="patch3"),
70+
ChangedFile(file=Path("/path/to/file"), type=ChangeType.ADDED, patch="patch"),
71+
ChangedFile(file=Path("file2"), type=ChangeType.MODIFIED, patch="patch2"),
72+
ChangedFile(file=Path("/path/../file3"), type=ChangeType.DELETED, patch="patch3"),
7373
]
7474
changeset = ChangeSet.from_iter(changes)
7575
assert changeset.digest() == "95a9fe4d808bdda19da9285b6d1a31a6e29ddbfa"
7676

7777
def test_properties(self):
7878
changes = [
79-
FileChanges(file=Path("/path/to/file"), type=ChangeType.ADDED, patch="patch"),
80-
FileChanges(file=Path("file2"), type=ChangeType.MODIFIED, patch="patch2"),
81-
FileChanges(file=Path("/path/../file3"), type=ChangeType.DELETED, patch="patch3"),
79+
ChangedFile(file=Path("/path/to/file"), type=ChangeType.ADDED, patch="patch"),
80+
ChangedFile(file=Path("file2"), type=ChangeType.MODIFIED, patch="patch2"),
81+
ChangedFile(file=Path("/path/../file3"), type=ChangeType.DELETED, patch="patch3"),
8282
]
8383
changeset = ChangeSet.from_iter(changes)
8484
assert changeset.added == {Path("/path/to/file")}
@@ -111,9 +111,9 @@ def test_generate_from_diff_output(self, repo_testcase):
111111

112112
def test_encode_decode(self):
113113
changes = [
114-
FileChanges(file=Path("/path/to/file"), type=ChangeType.ADDED, patch="patch"),
115-
FileChanges(file=Path("file2"), type=ChangeType.MODIFIED, patch="patch2"),
116-
FileChanges(file=Path("/path/../file3"), type=ChangeType.DELETED, patch="patch3"),
114+
ChangedFile(file=Path("/path/to/file"), type=ChangeType.ADDED, patch="patch"),
115+
ChangedFile(file=Path("file2"), type=ChangeType.MODIFIED, patch="patch2"),
116+
ChangedFile(file=Path("/path/../file3"), type=ChangeType.DELETED, patch="patch3"),
117117
]
118118
changeset = ChangeSet.from_iter(changes)
119119
encoded_changeset = msgspec.json.encode(changeset, enc_hook=ChangeSet.enc_hook)

tests/utils/git/test_remote.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from httpx import Response
1111

1212
from dda.utils.fs import Path
13-
from dda.utils.git.changeset import ChangeSet, FileChanges
13+
from dda.utils.git.changeset import ChangeSet, ChangedFile
1414
from dda.utils.git.commit import Commit, CommitDetails
1515
from dda.utils.git.remote import HTTPSRemote, Remote, SSHRemote, get_change_type_from_github_status
1616

@@ -87,7 +87,7 @@ def test_get_commit_details_and_changes_from_remote(self, mocker, github_payload
8787

8888
# Create a ChangeSet object
8989
changes = [
90-
FileChanges(
90+
ChangedFile(
9191
file=Path(file["filename"]),
9292
type=get_change_type_from_github_status(file["status"]),
9393
patch=file["patch"],

0 commit comments

Comments
 (0)