]> khamidou — GitHub mirrors - arcanist.git/commitdiff
make arc patch issue a warning if applying a patch against a different project
authorBob Trahan <bob.trahan@gmail.com>
Wed, 30 Nov 2011 06:27:40 +0000 (22:27 -0800)
committerBob Trahan <bob.trahan@gmail.com>
Thu, 1 Dec 2011 17:44:55 +0000 (09:44 -0800)
Summary: adds a little bit of sanity checking to the arc patch workflow.  in
short, if the working copy project is not the same as the patch project, don't
apply the patch

Test Plan:
ran

arc patch DX
arc patch DX --force

in the top line directory for project A and proejct B.   DX is for project A.

verified for project A that the patch was applied and for project B i was issued
warnings as expected.  also verified in project B case that saying Y or N to the
warning had the desired effect.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, btrahan, epriestley
Differential Revision: 1140

src/parser/bundle/ArcanistBundle.php
src/workflow/base/ArcanistBaseWorkflow.php
src/workflow/export/ArcanistExportWorkflow.php
src/workflow/patch/ArcanistPatchWorkflow.php

index f306cf105095bc0a050a5e287c62ab5dea9b9498..c9f7b3f61f14c859669d181fd7510901f21bdae0 100644 (file)
@@ -27,11 +27,20 @@ class ArcanistBundle {
   private $conduit;
   private $blobs = array();
   private $diskPath;
+  private $projectID;
 
   public function setConduit(ConduitClient $conduit) {
     $this->conduit = $conduit;
   }
 
+  public function setProjectID($project_id) {
+    $this->projectID = $project_id;
+  }
+
+  public function getProjectID() {
+    return $this->projectID;
+  }
+
   public static function newFromChanges(array $changes) {
     $obj = new ArcanistBundle();
     $obj->changes = $changes;
@@ -41,6 +50,27 @@ class ArcanistBundle {
   public static function newFromArcBundle($path) {
     $path = Filesystem::resolvePath($path);
 
+    $future = new ExecFuture(
+      csprintf(
+        'tar tfO %s',
+        $path));
+    list($stdout, $file_list) = $future->resolvex();
+    $file_list = explode("\n", trim($file_list));
+
+    if (in_array('meta.json', $file_list)) {
+      $future = new ExecFuture(
+        csprintf(
+          'tar xfO %s meta.json',
+          $path));
+      $meta_info = $future->resolveJSON();
+      $version = idx($meta_info, 'version', 0);
+      $project_name = idx($meta_info, 'projectName');
+    // this arc bundle was probably made before we started storing meta info
+    } else {
+      $version = 0;
+      $project_name = null;
+    }
+
     $future = new ExecFuture(
       csprintf(
         'tar xfO %s changes.json',
@@ -54,7 +84,6 @@ class ArcanistBundle {
       }
     }
 
-
     foreach ($changes as $change_key => $change) {
       $changes[$change_key] = ArcanistDiffChange::newFromDictionary($change);
     }
@@ -62,6 +91,7 @@ class ArcanistBundle {
     $obj = new ArcanistBundle();
     $obj->changes = $changes;
     $obj->diskPath = $path;
+    $obj->setProjectID($project_name);
 
     return $obj;
   }
@@ -108,10 +138,14 @@ class ArcanistBundle {
       $blobs[$phid] = $this->getBlob($phid);
     }
 
+    $meta_info = array('version' => 1,
+                       'projectName' => $this->getProjectID());
+
     $dir = Filesystem::createTemporaryDirectory();
     Filesystem::createDirectory($dir.'/hunks');
     Filesystem::createDirectory($dir.'/blobs');
     Filesystem::writeFile($dir.'/changes.json', json_encode($change_list));
+    Filesystem::writeFile($dir.'/meta.json', json_encode($meta_info));
     foreach ($hunks as $key => $hunk) {
       Filesystem::writeFile($dir.'/hunks/'.$key, $hunk);
     }
index ad198f29c3d21c459b3095e6363267b405622a46..5370ab2a6e62900c5405c713bbcd0f1ff155f45b 100644 (file)
@@ -751,6 +751,7 @@ class ArcanistBaseWorkflow {
     }
     $bundle = ArcanistBundle::newFromChanges($changes);
     $bundle->setConduit($conduit);
+    $bundle->setProjectID($diff['projectName']);
     return $bundle;
   }
 
index 9a0fb6dbcd3d592cdd5a8b2beccb707d162f934f..ad39e9fff2193b6b93f68c56cdd956ee1cd72f7a 100644 (file)
@@ -186,6 +186,7 @@ EOTEXT
         }
 
         $bundle = ArcanistBundle::newFromChanges($changes);
+        $bundle->setProjectID($this->getWorkingCopy()->getProjectID());
         break;
       case self::SOURCE_REVISION:
         $bundle = $this->loadRevisionBundleFromConduit(
index e1a9d6f6f91b6ab3ef7e92c3aaac34436bd59c13..f28303eceef5e6c27165d7542b9cfa1fa6e11427 100644 (file)
@@ -75,6 +75,10 @@ EOTEXT
         'help' =>
           "Apply changes from a git patchfile or unified patchfile.",
       ),
+      'force' => array(
+        'help' =>
+          "Do not run any sanity checks.",
+      ),
       '*' => 'name',
     );
   }
@@ -186,7 +190,7 @@ EOTEXT
       }
     } catch (Exception $ex) {
       if ($ex->getErrorCode() == 'ERR-INVALID-SESSION') {
-        // Phabricator is not configured to allow anonymos access to
+        // Phabricator is not configured to allow anonymous access to
         // Differential.
         $this->authenticateConduit();
         return $this->run();
@@ -195,6 +199,13 @@ EOTEXT
       }
     }
 
+    $force = $this->getArgument('force', false);
+    if ($force) {
+      // force means don't do any sanity checks about the patch
+    } else {
+      $this->sanityCheckPatch($bundle);
+    }
+
     $repository_api = $this->getRepositoryAPI();
     if ($repository_api instanceof ArcanistSubversionAPI) {
       $patch_err = 0;
@@ -412,6 +423,33 @@ EOTEXT
     return array('ARGUMENT');
   }
 
+  /**
+   * Do the best we can to prevent PEBKAC and id10t issues.
+   */
+  private function sanityCheckPatch(ArcanistBundle $bundle) {
+
+    // Check to see if the bundle project id matches the working copy
+    // project id
+    $bundle_project_id = $bundle->getProjectID();
+    $working_copy_project_id = $this->getWorkingCopy()->getProjectID();
+    if (empty($bundle_project_id)) {
+      // this means $source is SOURCE_PATCH || SOURCE_BUNDLE
+      // they don't come with a project id so just do nothing
+    } else if ($bundle_project_id != $working_copy_project_id) {
+      $ok = phutil_console_confirm(
+        "This diff is for the '{$bundle_project_id}' project but the working ".
+        "copy belongs to the '{$working_copy_project_id}' project. ".
+        "Still try to apply it?",
+        $default_no = false
+      );
+      if (!$ok) {
+        throw new ArcanistUserAbortException();
+      }
+    }
+
+    // TODO -- more sanity checks here
+  }
+
   /**
    * Create parent directories one at a time, since we need to "svn add" each
    * one. (Technically we could "svn add" just the topmost new directory.)