]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Strip comments from all diffs before parsing them
authorepriestley <git@epriestley.com>
Thu, 3 Oct 2013 22:06:35 +0000 (15:06 -0700)
committerepriestley <git@epriestley.com>
Thu, 3 Oct 2013 22:06:35 +0000 (15:06 -0700)
Summary:
See <https://github.com/facebook/phabricator/issues/400>. Since all of `patch`, `git apply` and `hg export` either accept or emit header comments, parse them unconditionally.

This is a tiny bit messy because we already had a less-general parser for `hg export` diffs, which have a large header section.

This is less permissive than GNU `patch`, which allows comments anywhere. We could do that, but `git apply` won't read them and they seem pretty crazy.

Test Plan: Added and ran unit tests.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran
Differential Revision: https://secure.phabricator.com/D7207

src/parser/ArcanistDiffParser.php
src/parser/__tests__/ArcanistDiffParserTestCase.php
src/parser/__tests__/diff/comment.svndiff [new file with mode: 0644]

index adaba998d323edebc43c8806ab5b1481fe1d05f4..b6e8cbcd1b7c21cb02ec9f40b42d745735884973 100644 (file)
@@ -195,6 +195,14 @@ final class ArcanistDiffParser {
 
     $this->didStartParse($diff);
 
+    // Strip off header comments. While `patch` allows comments anywhere in the
+    // file, `git apply` is more strict. We get these comments in `hg export`
+    // diffs, and Eclipse can also produce them.
+    $line = $this->getLineTrimmed();
+    while (preg_match('/^#/', $line)) {
+      $line = $this->nextLine();
+    }
+
     do {
       $patterns = array(
         // This is a normal SVN text change, probably from "svn diff".
@@ -1092,12 +1100,23 @@ final class ArcanistDiffParser {
   }
 
   protected function isFirstNonEmptyLine() {
-    $count = count($this->text);
-    for ($i = 0; $i < $count; $i++) {
-      if (strlen(trim($this->text[$i])) != 0) {
-        return ($i == $this->line);
+    $len = count($this->text);
+    for ($ii = 0; $ii < $len; $ii++) {
+      $line = $this->text[$ii];
+
+      if (!strlen(trim($line))) {
+        // This line is empty, skip it.
+        continue;
       }
+
+      if (preg_match('/^#/', $line)) {
+        // This line is a comment, skip it.
+        continue;
+      }
+
+      return ($ii == $this->line);
     }
+
     // Entire file is empty.
     return false;
   }
index cccd443500845a34f500425cdc8943f7e673826d..32010f67c71cb97e17d21a37697ef3080a4edc1d 100644 (file)
@@ -563,6 +563,13 @@ EOTEXT
           ArcanistDiffChangeType::TYPE_CHANGE,
           $change->getType());
         break;
+      case 'comment.svndiff':
+        $this->assertEqual(1, count($changes));
+        $change = array_shift($changes);
+        $this->assertEqual(
+          ArcanistDiffChangeType::TYPE_CHANGE,
+          $change->getType());
+        break;
       default:
         throw new Exception("No test block for diff file {$diff_file}.");
         break;
diff --git a/src/parser/__tests__/diff/comment.svndiff b/src/parser/__tests__/diff/comment.svndiff
new file mode 100644 (file)
index 0000000..f8c6872
--- /dev/null
@@ -0,0 +1,9 @@
+### Eclipse Workspace Patch 1.0
+#P Services
+Index: example
+===================================================================
+--- example    (revision 80)
++++ example    (working copy)
+@@ -1 +1 @@
+-a
++apple