From: Bob Trahan Date: Wed, 30 Nov 2011 06:27:40 +0000 (-0800) Subject: make arc patch issue a warning if applying a patch against a different project X-Git-Tag: conduit-4~224 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=d81d97f9c608d2e7585e6505a5b9d196ba2522bf;p=arcanist.git make arc patch issue a warning if applying a patch against a different project 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 --- diff --git a/src/parser/bundle/ArcanistBundle.php b/src/parser/bundle/ArcanistBundle.php index f306cf10..c9f7b3f6 100644 --- a/src/parser/bundle/ArcanistBundle.php +++ b/src/parser/bundle/ArcanistBundle.php @@ -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); } diff --git a/src/workflow/base/ArcanistBaseWorkflow.php b/src/workflow/base/ArcanistBaseWorkflow.php index ad198f29..5370ab2a 100644 --- a/src/workflow/base/ArcanistBaseWorkflow.php +++ b/src/workflow/base/ArcanistBaseWorkflow.php @@ -751,6 +751,7 @@ class ArcanistBaseWorkflow { } $bundle = ArcanistBundle::newFromChanges($changes); $bundle->setConduit($conduit); + $bundle->setProjectID($diff['projectName']); return $bundle; } diff --git a/src/workflow/export/ArcanistExportWorkflow.php b/src/workflow/export/ArcanistExportWorkflow.php index 9a0fb6db..ad39e9ff 100644 --- a/src/workflow/export/ArcanistExportWorkflow.php +++ b/src/workflow/export/ArcanistExportWorkflow.php @@ -186,6 +186,7 @@ EOTEXT } $bundle = ArcanistBundle::newFromChanges($changes); + $bundle->setProjectID($this->getWorkingCopy()->getProjectID()); break; case self::SOURCE_REVISION: $bundle = $this->loadRevisionBundleFromConduit( diff --git a/src/workflow/patch/ArcanistPatchWorkflow.php b/src/workflow/patch/ArcanistPatchWorkflow.php index e1a9d6f6..f28303ec 100644 --- a/src/workflow/patch/ArcanistPatchWorkflow.php +++ b/src/workflow/patch/ArcanistPatchWorkflow.php @@ -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.)