-
Notifications
You must be signed in to change notification settings - Fork 132
fix: support custom prefixes in Git diff parser #361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: support custom prefixes in Git diff parser #361
Conversation
305ee12 to
723621b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi! Interesting.
I remember writing this code and realizing that parsing it becomes ambiguous. File paths may contain forward slashes, and files may contain spaces.
I'm thinking the least we could do is to make it a little more robust. Perhaps check for [a-z]/.
Sometimes the files are not present down in the
--- i/file1.txt
+++ w/file2.txt
I've pushed a test to demonstrate this to master.
What do you think?
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #361 +/- ##
==========================================
+ Coverage 88.28% 88.29% +0.01%
==========================================
Files 66 66
Lines 6613 6647 +34
==========================================
+ Hits 5838 5869 +31
- Misses 775 778 +3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
The Git diff parser previously expected only the standard "a/" and "b/"
prefixes in diff headers, causing it to fail when parsing diffs with
custom
prefixes like "i/" and "w/" (which are used by some Git tools).
This change modifies the `parse_old_new_file_header` method to:
- Accept any prefix pattern before the file paths
- Look for "/" character instead of hardcoded "a/" and "b/" prefixes
- Maintain the same functionality for standard Git diffs
Added a test case to verify that the parser can now handle custom
prefixes.
Fixes error: "called `Result::unwrap()` on an `Err` value: ParseError {
input: "diff --git i/..."
723621b to
a188d78
Compare
Hey! Just pulled from master and saw the test was failing. Fixed it in the latest commit though, and now everything's passing. The code now handles those prefixes like "i/" and "w/" you mentioned, plus the unified diff format with the "---" and "+++" stuff. Anything else you think we should fix while I'm at it? PS. Thanks for this really nice tool btw! |
|
Without having looked at the source-code, I wonder how git itself handles parsing edge-cases like this. I think that this way should cover 99.9% of cases at least. (unless someone puts their file inside of a Perhaps using the regex crate would simplify the implementation? (it's already a dependency)
Gitu used to use libgit2 to parse diffs, but I found the output quite hard to work with. |
src/gitu_diff.rs
Outdated
|
|
||
| const CR_BYTE_SIZE: usize = 1; | ||
| let header_end = | ||
| if line_end > header_start && self.input.as_bytes()[line_end - CR_BYTE_SIZE] == b'\r' { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would actually be LF_BYTE_SIZE, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed in the new impl
Done using regex crate. |
altsem
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll merge it and just turn the regex into a LazyLock so that it's only initialized once.
gj!
The Git diff parser previously expected only the standard "a/" and "b/" prefixes in diff headers, causing it to fail when parsing diffs with custom
prefixes like "i/" and "w/" (which are used by some Git tools).
This change modifies the
parse_old_new_file_headermethod to:Added a test case to verify that the parser can now handle custom prefixes.
Fixes error: