Use magic for diff classification - #19
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the archive diffing logic to use github.com/git-pkgs/magic for classifying full extracted file contents before generating line diffs, aiming to only produce line diffs for UTF-8 text and avoid string conversion for binary signatures and malformed/unsupported text.
Changes:
- Add
github.com/git-pkgs/magicand use it to decide whether a file is diffable as UTF-8 text before generating diffs. - Replace the previous NUL-byte heuristic with
magic-based detection (isDiffableText). - Expand tests to cover Unicode, UTF-8 BOM, PDF signature without NUL, invalid UTF-8, disallowed controls, early/late NUL, and UTF-16.
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| go.mod | Adds the github.com/git-pkgs/magic dependency and converts the single require into a require block. |
| go.sum | Records checksums for the new magic dependency. |
| diff/diff.go | Switches binary/text detection to magic and gates diff generation based on diffable UTF-8 text. |
| diff/diff_test.go | Adds/updates test cases for the new detection behavior and binary-file diff suppression. |
Suppressed comments (2)
diff/diff.go:157
- Same
is_binarysemantic issue in the modified-file path:IsBinaryis set when content is not diffable text, which includes UTF-16 andmagic.KindUnknown(invalid UTF-8 without NUL). Ifis_binaryis intended to mean actual binary content (as in the PR description), set it only whenmagic.Detect(...).Kind == magic.KindBinary, and separately decide whether to generate a line diff based onKindText+ UTF-8.
if !isDiffableText(oldContent) || !isDiffableText(newContent) {
fd.IsBinary = true
} else {
diffText, added, deleted := generateUnifiedDiff(path, oldContent, newContent)
fd.Diff = diffText
diff/diff.go:137
- PR description says to keep the existing
is_binaryresult while restricting line diffs to UTF-8. The current logic setsIsBinary=truefor any non-diffable content (includingmagic.KindUnknowninvalid UTF-8 without NUL and UTF-16 text), which changes the meaning ofis_binaryfrom “binary” to “diff suppressed”. Consider only settingIsBinarywhenmagic.Detect(content).Kind == magic.KindBinary, and separately gating diff generation onKindText+ UTF-8.
This issue also appears on line 153 of the same file.
if !isDiffableText(content) {
fd.IsBinary = true
} else {
fd.Diff = generateAddedDiff(path, content)
fd.LinesAdded = countLines(content)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Use
github.com/git-pkgs/magicto classify complete extracted file content before generating line diffs. Keep the existingis_binaryresult and restrict line diffs to UTF-8 text, so binary signatures, malformed text, disallowed controls, late NUL bytes, and UTF-16 are not converted to strings.Add coverage for Unicode and BOM-marked UTF-8 text, PDF signatures without NUL, invalid UTF-8, controls, early and late NUL bytes, and UTF-16.