From f15479c832d836424a070bfee7263548882ef785 Mon Sep 17 00:00:00 2001 From: epriestley Date: Fri, 16 Mar 2012 13:40:11 -0700 Subject: [PATCH] Fix an issue with unified patch generation for terminal newline changes Summary: We currently detect the "\" as a change, and may generate a hunk like this, without changes. @@ -97,4 +98,4 @@ mmm mmm mmm \ No newline at end of file While "git apply" is OK with this, "patch" is not. Instead, don't detect this as a change (it is always accompanied by - / + if it's a real change). Test Plan: Successfully applied a previously-failing SVN patch to a file without a terminal newline that had nonlocal changes. Reviewers: 20after4, nh, btrahan Reviewed By: 20after4 CC: aran, epriestley Differential Revision: https://secure.phabricator.com/D1934 --- src/parser/bundle/ArcanistBundle.php | 14 ++++++++-- .../__tests__/ArcanistBundleTestCase.php | 13 +++++++--- .../__tests__/data/trailing-newline.diff | 13 ++++++++++ .../__tests__/data/trailing-newline.new | 26 +++++++++++++++++++ .../__tests__/data/trailing-newline.old | 26 +++++++++++++++++++ 5 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 src/parser/bundle/__tests__/data/trailing-newline.diff create mode 100644 src/parser/bundle/__tests__/data/trailing-newline.new create mode 100644 src/parser/bundle/__tests__/data/trailing-newline.old diff --git a/src/parser/bundle/ArcanistBundle.php b/src/parser/bundle/ArcanistBundle.php index efd5a289..76ccd786 100644 --- a/src/parser/bundle/ArcanistBundle.php +++ b/src/parser/bundle/ArcanistBundle.php @@ -426,8 +426,18 @@ final class ArcanistBundle { $ii = 0; $jj = 0; while ($ii < $n) { - for ($jj = $ii; $jj < $n && $lines[$jj][0] == ' '; ++$jj) { - // Skip lines until we find the first line with changes. + // Skip lines until we find the next line with changes. Note: this skips + // both ' ' (no changes) and '\' (no newline at end of file) lines. If we + // don't skip the latter, we may incorrectly generate a terminal hunk + // that has no actual change information when a file doesn't have a + // terminal newline and not changed near the end of the file. 'patch' will + // fail to apply the diff if we generate a hunk that does not actually + // contain changes. + for ($jj = $ii; $jj < $n; ++$jj) { + $char = $lines[$jj][0]; + if ($char == '-' || $char == '+') { + break; + } } if ($jj >= $n) { break; diff --git a/src/parser/bundle/__tests__/ArcanistBundleTestCase.php b/src/parser/bundle/__tests__/ArcanistBundleTestCase.php index 9b486949..40bee212 100644 --- a/src/parser/bundle/__tests__/ArcanistBundleTestCase.php +++ b/src/parser/bundle/__tests__/ArcanistBundleTestCase.php @@ -1,7 +1,7 @@ toUnifiedDiff()); } - - + public function testNonlocalTrailingNewline() { + // Diffs without changes near the end of the file should not generate a + // bogus, change-free hunk if the file has no trailing newline. + $this->assertEqual( + $this->loadResource('trailing-newline.diff'), + $this->loadOneChangeBundle( + 'trailing-newline.old', + 'trailing-newline.new')->toUnifiedDiff()); + } } diff --git a/src/parser/bundle/__tests__/data/trailing-newline.diff b/src/parser/bundle/__tests__/data/trailing-newline.diff new file mode 100644 index 00000000..d5534ce0 --- /dev/null +++ b/src/parser/bundle/__tests__/data/trailing-newline.diff @@ -0,0 +1,13 @@ +Index: file +=================================================================== +--- file ++++ file +@@ -3,7 +3,7 @@ + c + d + e +-f ++QUACK + g + h + i diff --git a/src/parser/bundle/__tests__/data/trailing-newline.new b/src/parser/bundle/__tests__/data/trailing-newline.new new file mode 100644 index 00000000..8ce41406 --- /dev/null +++ b/src/parser/bundle/__tests__/data/trailing-newline.new @@ -0,0 +1,26 @@ +a +b +c +d +e +QUACK +g +h +i +j +k +l +m +n +o +p +q +r +s +t +u +v +w +x +y +z \ No newline at end of file diff --git a/src/parser/bundle/__tests__/data/trailing-newline.old b/src/parser/bundle/__tests__/data/trailing-newline.old new file mode 100644 index 00000000..fcd217f0 --- /dev/null +++ b/src/parser/bundle/__tests__/data/trailing-newline.old @@ -0,0 +1,26 @@ +a +b +c +d +e +f +g +h +i +j +k +l +m +n +o +p +q +r +s +t +u +v +w +x +y +z \ No newline at end of file -- 2.43.0