]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Fix an issue with unified patch generation for terminal newline changes
authorepriestley <git@epriestley.com>
Fri, 16 Mar 2012 20:40:11 +0000 (13:40 -0700)
committerepriestley <git@epriestley.com>
Fri, 16 Mar 2012 20:40:11 +0000 (13:40 -0700)
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
src/parser/bundle/__tests__/ArcanistBundleTestCase.php
src/parser/bundle/__tests__/data/trailing-newline.diff [new file with mode: 0644]
src/parser/bundle/__tests__/data/trailing-newline.new [new file with mode: 0644]
src/parser/bundle/__tests__/data/trailing-newline.old [new file with mode: 0644]

index efd5a28959e2ffd9d14746f2f134cd97287364df..76ccd78675068f8962fd9300669a858837e43be8 100644 (file)
@@ -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;
index 9b4869490a23cd1e460985dae3ddf93a9aacaf43..40bee212205c8d5fa090d57bd41436bc47f913f5 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 
 /*
- * Copyright 2011 Facebook, Inc.
+ * Copyright 2012 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -64,7 +64,14 @@ final class ArcanistBundleTestCase extends ArcanistPhutilTestCase {
         'disjoint-hunks.new')->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 (file)
index 0000000..d5534ce
--- /dev/null
@@ -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 (file)
index 0000000..8ce4140
--- /dev/null
@@ -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 (file)
index 0000000..fcd217f
--- /dev/null
@@ -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