forked from bmwill/diffy
-
Notifications
You must be signed in to change notification settings - Fork 1
feat: parse indicated range to delimit hunks #2
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ab0daa7
feat: include From boundary to parse_header for unified git commit files
zelosleone 08c082b
count lines in hunks instead of trying to find the next start marker
wolfv 736dd66
trigger CI
wolfv 92e9472
fix clippy
wolfv cc3832c
use a newer rust
wolfv 1e05ce9
disable lints for a second
wolfv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
//! Parse a Patch | ||
|
||
use super::{ESCAPED_CHARS_BYTES, Hunk, HunkRange, Line, NO_NEWLINE_AT_EOF}; | ||
use super::{Hunk, HunkRange, Line, ESCAPED_CHARS_BYTES, NO_NEWLINE_AT_EOF}; | ||
use crate::{ | ||
LineEnd, | ||
patch::Diff, | ||
utils::{LineIter, Text}, | ||
LineEnd, | ||
}; | ||
use std::{borrow::Cow, fmt}; | ||
|
||
|
@@ -244,7 +244,7 @@ fn patch_header<'a, T: Text + ToOwned + ?Sized>( | |
} | ||
|
||
// Skip to the first filename header ("--- " or "+++ ") or hunk line, | ||
// skipping any preamble lines like "diff --git", etc. | ||
// skipping any preamble lines like "diff --git", git metadata, etc. | ||
fn skip_header_preamble<T: Text + ?Sized>(parser: &mut Parser<'_, T>) -> Result<()> { | ||
while let Some((line, _end)) = parser.peek() { | ||
if line.starts_with("--- ") | line.starts_with("+++ ") | line.starts_with("@@ ") { | ||
|
@@ -386,7 +386,7 @@ fn hunk<'a, T: Text + ?Sized + ToOwned>(parser: &mut Parser<'a, T>) -> Result<Hu | |
let n = *parser.peek().ok_or(ParsePatchError::UnexpectedEof)?; | ||
let (mut range1, mut range2, function_context) = hunk_header(n)?; | ||
let _ = parser.next(); | ||
let mut lines = hunk_lines(parser)?; | ||
let mut lines = hunk_lines(parser, &range1, &range2)?; | ||
|
||
// check counts of lines to see if they match the ranges in the hunk header | ||
let (len1, len2) = super::hunk_lines_count(&lines); | ||
|
@@ -429,7 +429,7 @@ fn hunk<'a, T: Text + ?Sized + ToOwned>(parser: &mut Parser<'a, T>) -> Result<Hu | |
|
||
type HunkHeader<'a, T> = (HunkRange, HunkRange, Option<(&'a T, Option<LineEnd>)>); | ||
|
||
fn hunk_header<T: Text + ?Sized>(oinput: (&T, Option<LineEnd>)) -> Result<HunkHeader<T>> { | ||
fn hunk_header<T: Text + ?Sized>(oinput: (&T, Option<LineEnd>)) -> Result<HunkHeader<'_, T>> { | ||
let input = oinput | ||
.0 | ||
.strip_prefix("@@ ") | ||
|
@@ -471,35 +471,54 @@ fn range<T: Text + ?Sized>(s: &T) -> Result<HunkRange> { | |
|
||
fn hunk_lines<'a, T: Text + ?Sized + ToOwned>( | ||
parser: &mut Parser<'a, T>, | ||
old_range: &HunkRange, | ||
new_range: &HunkRange, | ||
) -> Result<Vec<Line<'a, T>>> { | ||
let mut lines: Vec<Line<'a, T>> = Vec::new(); | ||
let mut no_newline_context = false; | ||
let mut no_newline_delete = false; | ||
let mut no_newline_insert = false; | ||
|
||
// Track how many lines we've seen for each side | ||
let mut old_lines_seen = 0; | ||
let mut new_lines_seen = 0; | ||
|
||
// Calculate maximum lines we should read based on ranges | ||
let expected_old_lines = old_range.len; | ||
let expected_new_lines = new_range.len; | ||
|
||
while let Some(line) = parser.peek() { | ||
let line = if line.0.starts_with("@") | ||
|| line.0.starts_with("diff ") | ||
|| line.0.starts_with("-- ") | ||
|| (line.0.starts_with("--") && line.0.len() == 2) | ||
|| line.0.starts_with("--- ") | ||
{ | ||
break; | ||
} else if no_newline_context { | ||
// Check if we've read enough lines based on the ranges, | ||
// but continue to check for the "No newline at end of file" marker | ||
if old_lines_seen >= expected_old_lines && new_lines_seen >= expected_new_lines { | ||
// Check if the next line is the "No newline at end of file" marker | ||
if !line.0.starts_with(NO_NEWLINE_AT_EOF) { | ||
// We've read all the lines we expect for this hunk | ||
break; | ||
} | ||
} | ||
|
||
let line = if no_newline_context { | ||
return Err(ParsePatchError::ExpectedEndOfHunk); | ||
} else if let Some(l) = line.0.strip_prefix(" ") { | ||
old_lines_seen += 1; | ||
new_lines_seen += 1; | ||
Line::Context((l, line.1)) | ||
} else if line.0.len() == 0 && line.1.is_some() { | ||
old_lines_seen += 1; | ||
new_lines_seen += 1; | ||
Line::Context(*line) | ||
} else if let Some(l) = line.0.strip_prefix("-") { | ||
if no_newline_delete { | ||
return Err(ParsePatchError::UnexpectedDeletedLine); | ||
} | ||
old_lines_seen += 1; | ||
Line::Delete((l, line.1)) | ||
} else if let Some(l) = line.0.strip_prefix("+") { | ||
if no_newline_insert { | ||
return Err(ParsePatchError::UnexpectedInsertLine); | ||
} | ||
new_lines_seen += 1; | ||
Line::Insert((l, line.1)) | ||
} else if line.0.starts_with(NO_NEWLINE_AT_EOF) { | ||
let last_line = lines | ||
|
@@ -532,7 +551,8 @@ fn hunk_lines<'a, T: Text + ?Sized + ToOwned>( | |
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::patch::parse::{HunkRangeStrategy, ParserConfig, parse_multiple_with_config}; | ||
use crate::patch::parse::{parse_multiple_with_config, HunkRangeStrategy, ParserConfig}; | ||
use crate::patch::Line; | ||
|
||
use super::{parse, parse_bytes}; | ||
|
||
|
@@ -682,4 +702,62 @@ mod tests { | |
insta::assert_debug_snapshot!(patches); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_multi_patch_file() { | ||
let input = std::fs::read_to_string("src/patch/test-data/40.patch").unwrap(); | ||
|
||
let result = parse_multiple_with_config( | ||
&input, | ||
ParserConfig { | ||
hunk_strategy: HunkRangeStrategy::Recount, | ||
}, | ||
); | ||
|
||
match &result { | ||
Ok(patches) => { | ||
// Should parse all 16 individual file changes from the 4 commits | ||
assert_eq!( | ||
patches.len(), | ||
16, | ||
"Should parse all 16 file changes from the multi-commit patch" | ||
); | ||
} | ||
Err(e) => { | ||
panic!("Failed to parse multi-patch file: {:?}", e); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_from_in_patch_content() { | ||
// Test that "From " with diff prefixes is correctly parsed as content, | ||
// while "From " without prefix acts as a boundary | ||
let patch_with_from_content = r#"--- a/email.txt | ||
+++ b/email.txt | ||
@@ -1,4 +1,4 @@ | ||
To: [email protected] | ||
-From: [email protected] | ||
+From: [email protected] | ||
Subject: Test | ||
Hello world | ||
"#; | ||
|
||
let result = parse(patch_with_from_content).unwrap(); | ||
assert_eq!(result.hunks().len(), 1); | ||
|
||
let hunk = &result.hunks()[0]; | ||
let lines: Vec<_> = hunk.lines().iter().collect(); | ||
assert_eq!(lines.len(), 5); | ||
|
||
// Verify the "From" lines are correctly parsed as delete/insert, not as boundary | ||
match lines[1] { | ||
Line::Delete((content, _)) => assert_eq!(*content, "From: [email protected]"), | ||
_ => panic!("Expected delete line with 'From: [email protected]'"), | ||
} | ||
match lines[2] { | ||
Line::Insert((content, _)) => assert_eq!(*content, "From: [email protected]"), | ||
_ => panic!("Expected insert line with 'From: [email protected]'"), | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
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.
zed auto organized these with rust-analyzer