sebastianbergmann/diff

Missing `\ No newline at end of file` on `UnifiedDiffOutputBuilder`

cedric-anne opened this issue · 3 comments

With the UnifiedDiffOutputBuilder, when the $to content has no new line at end of file, the \ No newline at end of file is missing.

Here is a simple code snippet that shows the difference between StrictUnifiedDiffOutputBuilder result and UnifiedDiffOutputBuilder result.

<?php
include __DIR__ . '/vendor/autoload.php';

use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\StrictUnifiedDiffOutputBuilder;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;

$differ = new Differ(new UnifiedDiffOutputBuilder("--- /dev/null\n+++ /path/to/file.txt\n", true));
echo $differ->diff(
    '',
    "This is a text file\nwith no newline at end of file."
);
echo PHP_EOL;
$differ = new Differ(new StrictUnifiedDiffOutputBuilder(['fromFile' => '/dev/null', 'toFile' => '/path/to/file.txt']));
echo $differ->diff(
    '',
    "This is a text file\nwith no newline at end of file."
);

Result will be

--- /dev/null
+++ /path/to/file.txt
@@ -1,0 +1,2 @@
+This is a text file
+with no newline at end of file.

--- /dev/null
+++ /path/to/file.txt
@@ -1,0 +1,2 @@
+This is a text file
+with no newline at end of file.
\ No newline at end of file

Hi!
The missing No newline... is a known deviation of the udiff format. The StrictUnifiedDiffOutputBuilder was developed and added as alternative such that people who like the output to be the same as udiff can use it. So if you want strict udiff, with the No newline... output I would suggest using the strict -builder, unless maybe I'm not sure what you question/suggestion is here.

Hi,

Honestly, I think this deviation is not known by most of developers. As far as I remember, this missing No newline was causing issues with usage of the patch utility. Indeed, I was not able to apply a reverse patch generated this way.

Anyway, I found a solution to this, so I will close this issue as it seems that this behaviour looks correct to you.

Thanks

I agree it is not widely known to developers and I think in some respects it might be better for the diff package on its own that the default would be the strict-format output builder. However, given the package history and BC promise, it might difficult or more of problem than it would solve.
Just a friendly reminder that I'm just someone saying things, I'm not not a member or anything so you could always make your case to the maintainer :) Thanks!