Skip to content
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

Suitability inquiry: patch generation? #120

Open
rulatir opened this issue Nov 30, 2022 · 12 comments
Open

Suitability inquiry: patch generation? #120

rulatir opened this issue Nov 30, 2022 · 12 comments
Assignees

Comments

@rulatir
Copy link

rulatir commented Nov 30, 2022

Given file1, file2, file3... on disk and $modifiedContents1, $modifiedContents2, $modifiedContents3... strings (existing ONLY in memory and without ANY file representation), how difficult would it be to use this library to render a standard patch file that would apply the respective modifications to file1, file2, file3... when fed to the standard patch utility?

@rulatir
Copy link
Author

rulatir commented Dec 1, 2022

I tried to concatenate the output of multiple diffs with appropriate headers prepended, but I am facing a difficult issue. The patch format generation seems to require special handling if and only if the following conditions simultaneously hold:

  1. the last line of the file ends up emitted as part of unmodified context of the last change, and
  2. there is no newline at the end of file

This condition is non-trivial to detect, mainly because of the subcondition 1. Some support from the library would be welcome.

@JBlond
Copy link
Owner

JBlond commented Dec 1, 2022

I wonder since the text Unified output is the same as diff -u a.txt b.txt. That output can be used to "feed" patch.

@JBlond
Copy link
Owner

JBlond commented Dec 1, 2022

@rulatir It would be nice to have some example content to look at.

@jfcherng
Copy link

jfcherng commented Dec 3, 2022

I wonder since the text Unified output is the same as diff -u a.txt b.txt. That output can be used to "feed" patch.

Not exactly. You would have to add support for "No newline at end of file" situations, which I did in my lib.

If there is "No newline at end of file" in old.txt, then

--- old.txt     2022-12-04 00:29:52.529176300 +0800
+++ new.txt     2022-12-04 00:29:53.897753600 +0800
@@ -1 +1 @@
-1st
\ No newline at end of file
+1st

If there is "No newline at end of file" in new.txt, then

--- old.txt     2022-12-04 00:28:20.045745500 +0800
+++ new.txt     2022-12-04 00:28:19.206919100 +0800
@@ -1 +1 @@
-1st
+1st
\ No newline at end of file

If both are "No newline at end of file", then

--- old.txt     2022-12-04 00:29:52.529176300 +0800
+++ new.txt     2022-12-04 00:30:52.437851700 +0800
@@ -1 +1 @@
-1st
\ No newline at end of file
+1st added
\ No newline at end of file

@JBlond
Copy link
Owner

JBlond commented Dec 9, 2022

I added the function for that into https://github.com/JBlond/php-diff/tree/php-diff-120
It still must be added to the renderers. PRs are welcomed.

@DigiLive
Copy link
Collaborator

@JBlond
After a busy period, I have some spare time now to help you out, but I need a heads-up.
Please send me an email with whats supposed to happen.

@JBlond
Copy link
Owner

JBlond commented Jan 18, 2023

@DigiLive I did, and I hope that it is clear now. If not, ask rulatir for more details.

@DigiLive
Copy link
Collaborator

Nope... it's still not clear to me what's expected to happen.
Maybe @rulatir or @jfcherng can elaborate.
I need more details than already given.

@jfcherng
Copy link

jfcherng commented Jan 18, 2023

The issue is, this lib doesn't provide the correct Unified output when one of the file has no EOL at EOF.


If #120 (comment) is not clear enough, then I think you have to diff them with diff -u by yourself.

Here's a sample.zip, just run diff -u old.txt new.txt and see the output (compare with this lib's).


Since I am not really interested in this thread and I think the elaboration is pretty clear. I am leaving.

@DigiLive
Copy link
Collaborator

DigiLive commented Jan 18, 2023

I'm currently looking into this issue.
However, my knowledge is about PHP and not diffutils. ;)

It seems like GNU diffutils:

  1. Appends line \ No newline at end of file to the output of a file's last line, if this line doesn't contain EOL characters.
  2. Drops the last line for comparison if it doesn't contain any characters, including EOL.

@JBlond can you confirm my conclusions?

@JBlond
Copy link
Owner

JBlond commented Feb 14, 2023

@JBlond
Copy link
Owner

JBlond commented Apr 10, 2024

@DigiLive After reading a gazillion of implementations I can confirm that you are right with your conclusions..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants