php-diff icon indicating copy to clipboard operation
php-diff copied to clipboard

Suitability inquiry: patch generation?

Open rulatir opened this issue 3 years ago • 13 comments

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 avatar Nov 30 '22 20:11 rulatir

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.

rulatir avatar Dec 01 '22 00:12 rulatir

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 avatar Dec 01 '22 10:12 JBlond

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

JBlond avatar Dec 01 '22 10:12 JBlond

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

jfcherng avatar Dec 03 '22 16:12 jfcherng

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.

JBlond avatar Dec 09 '22 11:12 JBlond

@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.

DigiLive avatar Jan 13 '23 12:01 DigiLive

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

JBlond avatar Jan 18 '23 14:01 JBlond

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.

DigiLive avatar Jan 18 '23 14:01 DigiLive

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


If https://github.com/JBlond/php-diff/issues/120#issuecomment-1336193044 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.

jfcherng avatar Jan 18 '23 14:01 jfcherng

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?

DigiLive avatar Jan 18 '23 22:01 DigiLive

I'm a bit puzzled by the definition. https://github.com/Distrotech/diffutils/blob/9e70e1ce7aaeff0f9c428d1abc9821589ea054f1/doc/diffutils.texi#L1718-1748

JBlond avatar Feb 14 '23 09:02 JBlond

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

JBlond avatar Apr 10 '24 15:04 JBlond