Skip to content

Conversation

@romamik
Copy link

@romamik romamik commented May 14, 2025

Fixes #4.

Now the parser checks for every line if it is followed by the "No newline at end of file" line, and saves it with the line. This flag is later used to determine this status for the hunk and then for the file.

Also, I renamed the flag to missing_newline as for me, it was hard to wrap my head around the inverted logic of the previous flag. I know that the original marker has negated meaning and that is the main source of confusion, but having variables that are opposite to the marker present in file was an additional source of confusion to me.

@sourcefrog
Copy link
Collaborator

I just hit this in testing cargo-mutants; it would be nice to merge and release a fix.

@vrmiguel
Copy link
Collaborator

@romamik I added a commit to fix a git conflict, hope you don't mind

@romamik romamik requested a review from sourcefrog December 8, 2025 20:06
@romamik
Copy link
Author

romamik commented Dec 8, 2025

Just pushed changes that make the resulting code much closer to the current main branch state. Mostly I removed most of the changes in the public API and pushed those flags up the stack while parsing.

@romamik
Copy link
Author

romamik commented Dec 9, 2025

"No newline at end of file" marker (the marker) can appear after a removed line, an added line, or a context line. Depending on this, it refers to old content of the file, new content of the file, or both.

To support this, while parsing the line, I expect the optional marker after every line: the function consume_content_line returns tuples of (&str, bool) instead of just &str for this.

When processing each hunk, I check if there was a line with the marker attached, both for old and new content of the file.

When processing the whole patch, I check the last hunk if it has these markers.

Skipping trailing empty lines
There was a bug that I had to solve after supporting this:
In the example file named "sample4.diff" there are lines that look like this:

+Only for testing purposes.
\ No newline at end of file

=== modified file 'modified_file'

Before the changes, when the marker was encountered, the parsing of the lines stopped, and then the empty line was skipped in the patch() function.
But after the changes, an empty line was treated as an empty Context line, which is incorrect.
I decided I have to distinguish between empty lines and empty context lines (which have one space).
After parsing the lines, I remove trailing empty lines, but not empty context lines. This allows us to still treat empty lines as empty context lines in between but prevents incorrect handling of trailing empty lines after the patch.

@sourcefrog
Copy link
Collaborator

"No newline at end of file" marker (the marker) can appear after a removed line, an added line, or a context line. Depending on this, it refers to old content of the file, new content of the file, or both.

To support this, while parsing the line, I expect the optional marker after every line: the function consume_content_line returns tuples of (&str, bool) instead of just &str for this.

Ah I see, that makes sense.

Copy link
Collaborator

@sourcefrog sourcefrog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks good to me!

Can I just ask that you also add a line to the changelog file?

@vrmiguel @whitty do you want to look too?

@romamik romamik requested a review from sourcefrog December 10, 2025 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"No newline at end of file" can be non-trailing

4 participants