Skip to content

Commit c2bd3df

Browse files
committed
feat(git): Add binary field to FileChanges objects
1 parent 533cc75 commit c2bd3df

File tree

11 files changed

+264
-23
lines changed

11 files changed

+264
-23
lines changed

src/dda/utils/git/changeset.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class ChangedFile(Struct, frozen=True):
3131
type: ChangeType
3232
"""The type of change that was made to the file: added, modified, or deleted."""
3333

34+
binary: bool
35+
"""Whether the changed file was a binary file."""
36+
3437
patch: str
3538
"""
3639
The patch representing the changes to the file, in unified diff format.
@@ -135,6 +138,7 @@ def generate_from_diff_output(cls, diff_output: str | list[str]) -> Generator[Se
135138
yield cls(
136139
file=current_file,
137140
type=current_type,
141+
binary=False, # TODO: Support binaries
138142
patch="\n".join(current_patch_lines),
139143
)
140144
current_file = None

src/dda/utils/git/remote.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ def get_details_and_changes_for_commit(self, commit: Commit) -> tuple[CommitDeta
6666
ChangedFile(
6767
file=Path(file_obj["filename"]),
6868
type=get_change_type_from_github_status(file_obj["status"]),
69-
patch=file_obj["patch"],
69+
# GitHub does not have anything else to indicate binary files
70+
binary="patch" not in file_obj,
71+
patch=file_obj.get("patch", ""),
7072
)
7173
for file_obj in data["files"]
7274
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"_changes":{"Path('file2.txt')":{"file":"Path('file2.txt')","type":"D","patch":"@@ -1,4 +0,0 @@\n-I am file2 !\n-I feel like I take up space for nothing...\n-I have a feeling like I won't exist pretty soon :/\n-"},"Path('file3.txt')":{"file":"Path('file3.txt')","type":"A","patch":"@@ -0,0 +1,2 @@\n+I am file3 !\n+I'm new around here, hopefully everyone treats me nice :)"},"Path('file4.txt')":{"file":"Path('file4.txt')","type":"M","patch":"@@ -2 +2 @@ I am file4.\n-People often tell me I am unreliable.\n+People often tell me I am THE BEST.\n@@ -4,3 +4,2 @@ Things like:\n-- You always change !\n-- I can never count on you...\n-- I didn't recognize you !\n+- You rock !\n+- I wish I were you !\n@@ -8 +7,3 @@ Do you think they have a point ?\n-I'd need to look at my own history to know...\n+Pah ! Who am I kidding, they're OBVIOUSLY RIGHT.\n+Arrogance ? What is that, an italian ice cream flavor ?\n+Get outta here !"},"Path('file5.txt')":{"file":"Path('file5.txt')","type":"D","patch":"@@ -1,5 +0,0 @@\n-I am a humble file.\n-Soon I will change name.\n-I think I'll also take this as an opportunity to change myself.\n-New name, new me !\n-Or is that not how the saying goes ?"},"Path('file5_new.txt')":{"file":"Path('file5_new.txt')","type":"A","patch":"@@ -0,0 +1,5 @@\n+I am a humble file.\n+Hey I have a new name !\n+Wow, I feel much better now.\n+New name, new me !\n+Or is that not how the saying goes ?"}}}
1+
{"_changes":{"Path('file2.txt')":{"file":"Path('file2.txt')","type":"D","binary":false,"patch":"@@ -1,4 +0,0 @@\n-I am file2 !\n-I feel like I take up space for nothing...\n-I have a feeling like I won't exist pretty soon :/\n-"},"Path('file3.txt')":{"file":"Path('file3.txt')","type":"A","binary":false,"patch":"@@ -0,0 +1,2 @@\n+I am file3 !\n+I'm new around here, hopefully everyone treats me nice :)"},"Path('file4.txt')":{"file":"Path('file4.txt')","type":"M","binary":false,"patch":"@@ -2 +2 @@ I am file4.\n-People often tell me I am unreliable.\n+People often tell me I am THE BEST.\n@@ -4,3 +4,2 @@ Things like:\n-- You always change !\n-- I can never count on you...\n-- I didn't recognize you !\n+- You rock !\n+- I wish I were you !\n@@ -8 +7,3 @@ Do you think they have a point ?\n-I'd need to look at my own history to know...\n+Pah ! Who am I kidding, they're OBVIOUSLY RIGHT.\n+Arrogance ? What is that, an italian ice cream flavor ?\n+Get outta here !"},"Path('file5.txt')":{"file":"Path('file5.txt')","type":"D","binary":false,"patch":"@@ -1,5 +0,0 @@\n-I am a humble file.\n-Soon I will change name.\n-I think I'll also take this as an opportunity to change myself.\n-New name, new me !\n-Or is that not how the saying goes ?"},"Path('file5_new.txt')":{"file":"Path('file5_new.txt')","type":"A","binary":false,"patch":"@@ -0,0 +1,5 @@\n+I am a humble file.\n+Hey I have a new name !\n+Wow, I feel much better now.\n+New name, new me !\n+Or is that not how the saying goes ?"}}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"_changes":{"Path('file2.txt')":{"file":"Path('file2.txt')","type":"A","patch":"@@ -0,0 +1,3 @@\n+file2\n+I am a new file in the repo !\n+That's incredible."}}}
1+
{"_changes":{"Path('file2.txt')":{"file":"Path('file2.txt')","type":"A","binary":false,"patch":"@@ -0,0 +1,3 @@\n+file2\n+I am a new file in the repo !\n+That's incredible."}}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"_changes":{"Path('file2.txt')":{"file":"Path('file2.txt')","type":"D","patch":"@@ -1,3 +0,0 @@\n-file2\n-I will be deleted, unfortunately.\n-That's quite sad."}}}
1+
{"_changes":{"Path('file2.txt')":{"file":"Path('file2.txt')","type":"D","binary":false,"patch":"@@ -1,3 +0,0 @@\n-file2\n-I will be deleted, unfortunately.\n-That's quite sad."}}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"_changes":{"Path('added_lines.txt')":{"file":"Path('added_lines.txt')","type":"M","patch":"@@ -2,0 +3,4 @@ I wonder what that could mean ?\n+\n+But of course !\n+I get some added lines.\n+That makes sense."},"Path('added_lines_middle.txt')":{"file":"Path('added_lines_middle.txt')","type":"M","patch":"@@ -2,0 +3,2 @@ I have a bit more text than added_lines.\n+Nobody expects the Spanish Inquisition !\n+My developer really wonders if cracking jokes in test data is against company policy."},"Path('deleted_lines.txt')":{"file":"Path('deleted_lines.txt')","type":"M","patch":"@@ -2,2 +2 @@ Hmm, my name is deleted_lines.\n-I wonder what that could mean ?\n-I don't want to be shortened, that does not seem fun.\n+"},"Path('deleted_lines_middle.txt')":{"file":"Path('deleted_lines_middle.txt')","type":"M","patch":"@@ -2,3 +1,0 @@ Hmm, my name is deleted_lines_middle.\n-I wonder what that could mean ?\n-I don't want to be shortened, that does not seem fun.\n-Thinking about it, being short is also kind of nice though."},"Path('edited_lines.txt')":{"file":"Path('edited_lines.txt')","type":"M","patch":"@@ -4,8 +4,5 @@ I have a big block of text here:\n-Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n-Vestibulum ornare cursus diam, quis hendrerit nibh.\n-Donec sollicitudin neque in tempus ornare.\n-Integer sit amet pretium quam.\n-Maecenas lacinia augue id est malesuada, vitae fermentum justo faucibus.\n-Aenean posuere nisi tincidunt nisi pharetra blandit.\n-Integer sed nulla sed eros aliquet eleifend quis ac quam.\n-Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.\n+What kind of dev uses lorem ipsum ?\n+Keyboard mashing is where it's at !\n+Check this out:\n+phq w4q3t'p£tfnjklifewqkpjnoq23bjikkijq4ovikobjqv4kioblj;\n+vqpewkvnkqknbpjlo[iqervkb[jplofqvwer[kpjlqfp[okqolp[f;vn]]]]]"}}}
1+
{"_changes":{"Path('added_lines.txt')":{"file":"Path('added_lines.txt')","type":"M","binary":false,"patch":"@@ -2,0 +3,4 @@ I wonder what that could mean ?\n+\n+But of course !\n+I get some added lines.\n+That makes sense."},"Path('added_lines_middle.txt')":{"file":"Path('added_lines_middle.txt')","type":"M","binary":false,"patch":"@@ -2,0 +3,2 @@ I have a bit more text than added_lines.\n+Nobody expects the Spanish Inquisition !\n+My developer really wonders if cracking jokes in test data is against company policy."},"Path('deleted_lines.txt')":{"file":"Path('deleted_lines.txt')","type":"M","binary":false,"patch":"@@ -2,2 +2 @@ Hmm, my name is deleted_lines.\n-I wonder what that could mean ?\n-I don't want to be shortened, that does not seem fun.\n+"},"Path('deleted_lines_middle.txt')":{"file":"Path('deleted_lines_middle.txt')","type":"M","binary":false,"patch":"@@ -2,3 +1,0 @@ Hmm, my name is deleted_lines_middle.\n-I wonder what that could mean ?\n-I don't want to be shortened, that does not seem fun.\n-Thinking about it, being short is also kind of nice though."},"Path('edited_lines.txt')":{"file":"Path('edited_lines.txt')","type":"M","binary":false,"patch":"@@ -4,8 +4,5 @@ I have a big block of text here:\n-Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n-Vestibulum ornare cursus diam, quis hendrerit nibh.\n-Donec sollicitudin neque in tempus ornare.\n-Integer sit amet pretium quam.\n-Maecenas lacinia augue id est malesuada, vitae fermentum justo faucibus.\n-Aenean posuere nisi tincidunt nisi pharetra blandit.\n-Integer sed nulla sed eros aliquet eleifend quis ac quam.\n-Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.\n+What kind of dev uses lorem ipsum ?\n+Keyboard mashing is where it's at !\n+Check this out:\n+phq w4q3t'p£tfnjklifewqkpjnoq23bjikkijq4ovikobjqv4kioblj;\n+vqpewkvnkqknbpjlo[iqervkb[jplofqvwer[kpjlqfp[okqolp[f;vn]]]]]"}}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"_changes":{"Path('file1.txt')":{"file":"Path('file1.txt')","type":"D","patch":"@@ -1,3 +0,0 @@\n-I am a humble file.\n-Soon I will change name.\n-But that won't change who I am, my contents !"},"Path('file1_new.txt')":{"file":"Path('file1_new.txt')","type":"A","patch":"@@ -0,0 +1,3 @@\n+I am a humble file.\n+Soon I will change name.\n+But that won't change who I am, my contents !"},"Path('file2.txt')":{"file":"Path('file2.txt')","type":"D","patch":"@@ -1,5 +0,0 @@\n-I am a humble file.\n-Soon I will change name.\n-I think I'll also take this as an opportunity to change myself.\n-New name, new me !\n-Or is that not how the saying goes ?"},"Path('file2_new.txt')":{"file":"Path('file2_new.txt')","type":"A","patch":"@@ -0,0 +1,5 @@\n+I am a humble file.\n+Hey I have a new name !\n+Wow, I feel much better now.\n+New name, new me !\n+Or is that not how the saying goes ?"}}}
1+
{"_changes":{"Path('file1.txt')":{"file":"Path('file1.txt')","type":"D","binary":false,"patch":"@@ -1,3 +0,0 @@\n-I am a humble file.\n-Soon I will change name.\n-But that won't change who I am, my contents !"},"Path('file1_new.txt')":{"file":"Path('file1_new.txt')","type":"A","binary":false,"patch":"@@ -0,0 +1,3 @@\n+I am a humble file.\n+Soon I will change name.\n+But that won't change who I am, my contents !"},"Path('file2.txt')":{"file":"Path('file2.txt')","type":"D","binary":false,"patch":"@@ -1,5 +0,0 @@\n-I am a humble file.\n-Soon I will change name.\n-I think I'll also take this as an opportunity to change myself.\n-New name, new me !\n-Or is that not how the saying goes ?"},"Path('file2_new.txt')":{"file":"Path('file2_new.txt')","type":"A","binary":false,"patch":"@@ -0,0 +1,5 @@\n+I am a humble file.\n+Hey I have a new name !\n+Wow, I feel much better now.\n+New name, new me !\n+Or is that not how the saying goes ?"}}}

tests/tools/git/test_git.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,14 @@ def test_get_changes_with_base(app: Application, mocker: Any, repo_testcase: str
197197

198198
# Test with working tree changes
199199
working_tree_changes = ChangeSet({
200-
Path("test.txt"): ChangedFile(file=Path("test.txt"), type=ChangeType.ADDED, patch="@@ -0,0 +1 @@\n+test")
200+
Path("test.txt"): ChangedFile(
201+
file=Path("test.txt"), type=ChangeType.ADDED, binary=False, patch="@@ -0,0 +1 @@\n+test"
202+
)
201203
})
202204
mocker.patch("dda.tools.git.Git.get_working_tree_changes", return_value=working_tree_changes)
203205

204206
changeset_with_working_tree = git.get_changes_with_base(base_commit.sha1, include_working_tree=True)
205207
expected_changeset_with_working_tree = expected_changeset | ChangeSet.from_iter([
206-
ChangedFile(file=Path("test.txt"), type=ChangeType.ADDED, patch="@@ -0,0 +1 @@\n+test")
208+
ChangedFile(file=Path("test.txt"), type=ChangeType.ADDED, binary=False, patch="@@ -0,0 +1 @@\n+test")
207209
])
208210
assert_changesets_equal(changeset_with_working_tree, expected_changeset_with_working_tree)

0 commit comments

Comments
 (0)