]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Remove only trailing "#" and empty lines as comments from CLI editors
authorepriestley <git@epriestley.com>
Wed, 18 Apr 2012 13:08:41 +0000 (06:08 -0700)
committerepriestley <git@epriestley.com>
Wed, 18 Apr 2012 13:08:41 +0000 (06:08 -0700)
Summary: Don't strip numbered lists in comment bodies, etc.

Test Plan: Unit tests, meta-testing this diff.

Reviewers: vrana, btrahan

Reviewed By: vrana

CC: aran
Maniphest Tasks: T1124

Differential Revision: https://secure.phabricator.com/D2262

src/__phutil_library_map__.php
src/parser/commentremover/ArcanistCommentRemover.php [new file with mode: 0644]
src/parser/commentremover/__init__.php [new file with mode: 0644]
src/parser/commentremover/__tests__/ArcanistCommentRemoverTestCase.php [new file with mode: 0644]
src/parser/commentremover/__tests__/__init__.php [new file with mode: 0644]
src/workflow/diff/ArcanistDiffWorkflow.php
src/workflow/diff/__init__.php
src/workflow/patch/ArcanistPatchWorkflow.php
src/workflow/patch/__init__.php

index 54701ee5905e2852dc60aa65150278611360469a..930431b7d3abe217c8ac1e8c1647a659851eb4fe 100644 (file)
@@ -23,6 +23,8 @@ phutil_register_library_map(array(
     'ArcanistChooseNoRevisionsException' => 'exception',
     'ArcanistCloseRevisionWorkflow' => 'workflow/close-revision',
     'ArcanistCloseWorkflow' => 'workflow/close',
+    'ArcanistCommentRemover' => 'parser/commentremover',
+    'ArcanistCommentRemoverTestCase' => 'parser/commentremover/__tests__',
     'ArcanistCommitWorkflow' => 'workflow/commit',
     'ArcanistConduitLinter' => 'lint/linter/conduit',
     'ArcanistConfiguration' => 'configuration',
@@ -129,6 +131,7 @@ phutil_register_library_map(array(
     'ArcanistCallConduitWorkflow' => 'ArcanistBaseWorkflow',
     'ArcanistCloseRevisionWorkflow' => 'ArcanistBaseWorkflow',
     'ArcanistCloseWorkflow' => 'ArcanistBaseWorkflow',
+    'ArcanistCommentRemoverTestCase' => 'ArcanistPhutilTestCase',
     'ArcanistCommitWorkflow' => 'ArcanistBaseWorkflow',
     'ArcanistConduitLinter' => 'ArcanistLinter',
     'ArcanistCoverWorkflow' => 'ArcanistBaseWorkflow',
diff --git a/src/parser/commentremover/ArcanistCommentRemover.php b/src/parser/commentremover/ArcanistCommentRemover.php
new file mode 100644 (file)
index 0000000..ee06db5
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+final class ArcanistCommentRemover {
+
+  /**
+   * Remove comment lines from a commit message. Strips trailing lines only,
+   * and requires "#" to appear at the beginning of a line for it to be
+   * considered a comment.
+   */
+  public static function removeComments($body) {
+    $lines = explode("\n", $body);
+    $lines = array_reverse($lines);
+    foreach ($lines as $key => $line) {
+      if (!strlen($line)) {
+        unset($lines[$key]);
+        continue;
+      }
+      if ($line[0] == '#') {
+        unset($lines[$key]);
+        continue;
+      }
+      break;
+    }
+    $lines = array_reverse($lines);
+    return implode("\n", $lines)."\n";
+  }
+
+}
diff --git a/src/parser/commentremover/__init__.php b/src/parser/commentremover/__init__.php
new file mode 100644 (file)
index 0000000..36cdd7b
--- /dev/null
@@ -0,0 +1,10 @@
+<?php
+/**
+ * This file is automatically generated. Lint this module to rebuild it.
+ * @generated
+ */
+
+
+
+
+phutil_require_source('ArcanistCommentRemover.php');
diff --git a/src/parser/commentremover/__tests__/ArcanistCommentRemoverTestCase.php b/src/parser/commentremover/__tests__/ArcanistCommentRemoverTestCase.php
new file mode 100644 (file)
index 0000000..75883c8
--- /dev/null
@@ -0,0 +1,48 @@
+<?php
+
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+final class ArcanistCommentRemoverTestCase extends ArcanistPhutilTestCase {
+
+  public function testRemover() {
+    $test = <<<EOTEXT
+Here is a list:
+
+  # Stuff
+  # More Stuff
+
+The end.
+
+# Instructional comments.
+# Appear here.
+# At the bottom.
+EOTEXT;
+
+    $expect = <<<EOTEXT
+Here is a list:
+
+  # Stuff
+  # More Stuff
+
+The end.
+
+EOTEXT;
+
+    $this->assertEqual($expect, ArcanistCommentRemover::removeComments($test));
+  }
+
+}
diff --git a/src/parser/commentremover/__tests__/__init__.php b/src/parser/commentremover/__tests__/__init__.php
new file mode 100644 (file)
index 0000000..05d0a50
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+/**
+ * This file is automatically generated. Lint this module to rebuild it.
+ * @generated
+ */
+
+
+
+phutil_require_module('arcanist', 'parser/commentremover');
+phutil_require_module('arcanist', 'unit/engine/phutil/testcase');
+
+
+phutil_require_source('ArcanistCommentRemoverTestCase.php');
index e8e400785b96e0223cceae9220a0bcb00e55f74f..3e602793cc4cb1835399d673b0826af374e0dfb0 100644 (file)
@@ -1186,10 +1186,7 @@ EOTEXT
         "No explanation provided.");
     }
 
-    $template = preg_replace('/^\s*#.*$/m', '', $new_template);
-    $template = rtrim($template)."\n";
-
-    return $template;
+    return ArcanistCommentRemover::removeComments($new_template);
   }
 
 
@@ -1357,8 +1354,7 @@ EOTEXT
         throw new ArcanistUsageException("Template not edited.");
       }
 
-      $template = preg_replace('/^\s*#.*$/m', '', $new_template);
-      $template = rtrim($template)."\n";
+      $template = ArcanistCommentRemover::removeComments($new_template);
       $wrote = $this->writeScratchFile('create-message', $template);
       $where = $this->getReadableScratchFilePath('create-message');
 
@@ -1516,10 +1512,8 @@ EOTEXT
       ->setName('differential-update-comments')
       ->editInteractively();
 
-    $comments = preg_replace('/^\s*#.*$/m', '', $comments);
-    $comments = rtrim($comments);
-
-    if (!strlen($comments)) {
+    $comments = ArcanistCommentRemover::removeComments($comments);
+    if (!strlen(trim($comments))) {
       throw new ArcanistUserAbortException();
     }
 
index 9956cab597c29018cd4c2ab6381358810073fc04..db89e37ad82f5644949c8310ad4f533dbc68aefe 100644 (file)
@@ -10,6 +10,7 @@ phutil_require_module('arcanist', 'difference');
 phutil_require_module('arcanist', 'differential/commitmessage');
 phutil_require_module('arcanist', 'exception/usage');
 phutil_require_module('arcanist', 'exception/usage/userabort');
+phutil_require_module('arcanist', 'parser/commentremover');
 phutil_require_module('arcanist', 'parser/diff');
 phutil_require_module('arcanist', 'parser/diff/changetype');
 phutil_require_module('arcanist', 'repository/api/base');
index 49d87512df414145dddb62e5f074fbab6ab2fd71..d585b1aaaf12dfb3fe6aa3a210a300fb9c402b79 100644 (file)
@@ -662,11 +662,8 @@ EOTEXT
         ->setName('arcanist-patch-commit-message')
         ->editInteractively();
 
-      $commit_message = preg_replace('/^\s*#.*$/m',
-                                     '',
-                                     $commit_message);
-      $commit_message = rtrim($commit_message);
-      if (!strlen($commit_message)) {
+      $commit_message = ArcanistCommentRemover::removeComments($commit_message);
+      if (!strlen(trim($commit_message))) {
         throw new ArcanistUserAbortException();
       }
     }
index 7e2852ad73ca4adcc5be79465ace957c93709790..dd479a0ff35b27f2b27ac484ed504f7890a967e4 100644 (file)
@@ -11,6 +11,7 @@ phutil_require_module('arcanist', 'differential/constants/revisionstatus');
 phutil_require_module('arcanist', 'exception/usage');
 phutil_require_module('arcanist', 'exception/usage/userabort');
 phutil_require_module('arcanist', 'parser/bundle');
+phutil_require_module('arcanist', 'parser/commentremover');
 phutil_require_module('arcanist', 'parser/diff/changetype');
 phutil_require_module('arcanist', 'workflow/base');