From 560b339ad3b4af4667ae1abd2f59b2dfe7fedf74 Mon Sep 17 00:00:00 2001 From: epriestley Date: Thu, 5 Jan 2012 11:26:17 -0800 Subject: [PATCH] Refactor ArcanistDiffWorkflow to be a little more manageable Summary: I want to make some changes to this workflow but it a huge mess right now. Try to refactor a bit to make it a little more manageable. Broadly: - Moved lint/unit constant mapping into separate methods. - Moved lint/unit/local properties into separate methods. - Moved diff spec construction into a separate method. - Moved some message stuff into separate methods and reorganized related methods near to one another. - Removed an unused findRevisionInformation() method. I fixed a couple of small bugs, too: - --create now conflicts with --only and --preview. - --create now probably works in Mercurial. - --create messages now have basic reviewer validation. This should have not have any significant behavioral changes. Test Plan: - Created this revision. - Ran "arc diff --create". Reviewers: btrahan, jungejason Reviewed By: btrahan CC: aran, btrahan, epriestley Differential Revision: https://secure.phabricator.com/D1320 --- src/workflow/diff/ArcanistDiffWorkflow.php | 716 ++++++++++++--------- 1 file changed, 421 insertions(+), 295 deletions(-) diff --git a/src/workflow/diff/ArcanistDiffWorkflow.php b/src/workflow/diff/ArcanistDiffWorkflow.php index 0119ce5a..f7d62384 100644 --- a/src/workflow/diff/ArcanistDiffWorkflow.php +++ b/src/workflow/diff/ArcanistDiffWorkflow.php @@ -1,7 +1,7 @@ "(EXPERIMENTAL) Create a new revision.", 'conflicts' => array( 'edit' => '--create can not be used with --edit.', + 'only' => '--create can not be used with --only.', + 'preview' => '--create can not be used with --preview.', ), ), 'nounit' => array( @@ -224,29 +231,14 @@ EOTEXT ob_start(); } - $is_create = $this->getArgument('create'); - $conduit = $this->getConduit(); $this->requireCleanWorkingCopy(); - $parent = null; - $paths = $this->generateAffectedPaths(); // Do this before we start linting or running unit tests so we can detect // things like a missing test plan or invalid reviewers immediately. - if ($is_create) { - $message_file = $this->getArgument('message-file'); - if ($message_file) { - $commit_message = $this->getCommitMessageFromFile($message_file); - } else { - $commit_message = $this->getCommitMessageFromUser(); - } - } else if ($this->shouldOnlyCreateDiff()) { - $commit_message = null; - } else { - $commit_message = $this->getGitCommitMessage(); - } + $commit_message = $this->buildCommitMessage(); $lint_result = $this->runLint($paths); $unit_result = $this->runUnit($paths); @@ -262,138 +254,25 @@ EOTEXT $change_list[] = $change->toDictionary(); } - if ($lint_result === ArcanistLintWorkflow::RESULT_OKAY) { - $lint = 'okay'; - } else if ($lint_result === ArcanistLintWorkflow::RESULT_ERRORS) { - $lint = 'fail'; - } else if ($lint_result === ArcanistLintWorkflow::RESULT_WARNINGS) { - $lint = 'warn'; - } else if ($lint_result === ArcanistLintWorkflow::RESULT_SKIP) { - $lint = 'skip'; - } else { - $lint = 'none'; - } - - if ($unit_result === ArcanistUnitWorkflow::RESULT_OKAY) { - $unit = 'okay'; - } else if ($unit_result === ArcanistUnitWorkflow::RESULT_FAIL) { - $unit = 'fail'; - } else if ($unit_result === ArcanistUnitWorkflow::RESULT_UNSOUND) { - $unit = 'warn'; - } else if ($unit_result === ArcanistUnitWorkflow::RESULT_SKIP) { - $unit = 'skip'; - } else if ($unit_result === ArcanistUnitWorkflow::RESULT_POSTPONED) { - $unit = 'postponed'; - } else { - $unit = 'none'; - } - - // NOTE: This has to happen after generateChanges(), since it may overwrite - // the SVN effective base revision. - $base_revision = $repository_api->getSourceControlBaseRevision(); - $base_path = $repository_api->getSourceControlPath(); - $repo_uuid = null; - if ($repository_api instanceof ArcanistGitAPI) { - $info = $this->getGitParentLogInfo(); - if ($info['parent']) { - $parent = $info['parent']; - } - if ($info['base_revision']) { - $base_revision = $info['base_revision']; - } - if ($info['base_path']) { - $base_path = $info['base_path']; - } - if ($info['uuid']) { - $repo_uuid = $info['uuid']; - } - } else if ($repository_api instanceof ArcanistSubversionAPI) { - $repo_uuid = $repository_api->getRepositorySVNUUID(); - } else if ($repository_api instanceof ArcanistMercurialAPI) { - // TODO: Provide this information. - } else { - throw new Exception("Unsupported repository API!"); - } - - $working_copy = $this->getWorkingCopy(); - - $diff = array( + $diff_spec = array( 'changes' => $change_list, - 'sourceMachine' => php_uname('n'), - 'sourcePath' => $repository_api->getPath(), - 'branch' => $repository_api->getBranchName(), - 'sourceControlSystem' => - $repository_api->getSourceControlSystemName(), - 'sourceControlPath' => $base_path, - 'sourceControlBaseRevision' => $base_revision, - 'parentRevisionID' => $parent, - 'lintStatus' => $lint, - 'unitStatus' => $unit, - - 'repositoryUUID' => $repo_uuid, - 'creationMethod' => 'arc', - 'arcanistProject' => $working_copy->getProjectID(), - 'authorPHID' => $this->getUserPHID(), - ); + 'lintStatus' => $this->getLintStatus($lint_result), + 'unitStatus' => $this->getUnitStatus($unit_result), + ) + $this->buildDiffSpecification(); $diff_info = $conduit->callMethodSynchronous( 'differential.creatediff', - $diff); + $diff_spec); + + $this->diffID = $diff_info['diffid']; if ($this->unitWorkflow) { $this->unitWorkflow->setDifferentialDiffID($diff_info['diffid']); } - if ($this->unresolvedLint) { - $data = array(); - foreach ($this->unresolvedLint as $message) { - $data[] = array( - 'path' => $message->getPath(), - 'line' => $message->getLine(), - 'char' => $message->getChar(), - 'code' => $message->getCode(), - 'severity' => $message->getSeverity(), - 'name' => $message->getName(), - 'description' => $message->getDescription(), - ); - } - $conduit->callMethodSynchronous( - 'differential.setdiffproperty', - array( - 'diff_id' => $diff_info['diffid'], - 'name' => 'arc:lint', - 'data' => json_encode($data), - )); - } - - $local_info = $repository_api->getLocalCommitInformation(); - if ($local_info) { - $conduit->callMethodSynchronous( - 'differential.setdiffproperty', - array( - 'diff_id' => $diff_info['diffid'], - 'name' => 'local:commits', - 'data' => json_encode($local_info), - )); - } - - if ($this->unresolvedTests) { - $data = array(); - foreach ($this->unresolvedTests as $test) { - $data[] = array( - 'name' => $test->getName(), - 'result' => $test->getResult(), - 'userdata' => $test->getUserData(), - ); - } - $conduit->callMethodSynchronous( - 'differential.setdiffproperty', - array( - 'diff_id' => $diff_info['diffid'], - 'name' => 'arc:unit', - 'data' => json_encode($data), - )); - } + $this->updateLintDiffProperty(); + $this->updateUnitDiffProperty(); + $this->updateLocalDiffProperty(); if ($this->shouldOnlyCreateDiff()) { if (!$output_json) { @@ -405,16 +284,17 @@ EOTEXT $human = ob_get_clean(); echo json_encode(array( 'diffURI' => $diff_info['uri'], - 'diffID' => $diff_info['diffid'], + 'diffID' => $this->getDiffID(), 'human' => $human, ))."\n"; ob_start(); } } else { + $message = $commit_message; $revision = array( - 'diffid' => $diff_info['diffid'], + 'diffid' => $this->getDiffID(), 'fields' => $message->getFields(), ); @@ -534,6 +414,10 @@ EOTEXT protected function shouldOnlyCreateDiff() { + if ($this->getArgument('create')) { + return false; + } + $repository_api = $this->getRepositoryAPI(); if ($repository_api instanceof ArcanistSubversionAPI) { return true; @@ -551,10 +435,6 @@ EOTEXT $this->getArgument('only'); } - protected function findRevisionInformation() { - return array(null, null); - } - private function generateAffectedPaths() { $repository_api = $this->getRepositoryAPI(); if ($repository_api instanceof ArcanistSubversionAPI) { @@ -904,122 +784,6 @@ EOTEXT return $result; } - /** - * Retrieve the git message in HEAD if it isn't a primary template message. - */ - private function getGitUpdateMessage() { - $repository_api = $this->getRepositoryAPI(); - - $parser = new ArcanistDiffParser(); - $commit_messages = $repository_api->getGitCommitLog(); - $commit_messages = $parser->parseDiff($commit_messages); - - $head = reset($commit_messages); - $message = ArcanistDifferentialCommitMessage::newFromRawCorpus( - $head->getMetadata('message')); - if ($message->getRevisionID()) { - return null; - } - - return trim($message->getRawCorpus()); - } - - private function getGitCommitMessage() { - $conduit = $this->getConduit(); - $repository_api = $this->getRepositoryAPI(); - - $parser = new ArcanistDiffParser(); - $commit_messages = $repository_api->getGitCommitLog(); - - if (!strlen($commit_messages)) { - if (!$repository_api->getHasCommits()) { - throw new ArcanistUsageException( - "This git repository doesn't have any commits yet. You need to ". - "commit something before you can diff against it."); - } else { - throw new ArcanistUsageException( - "The commit range doesn't include any commits. (Did you diff ". - "against the wrong commit?)"); - } - } - - $commit_messages = $parser->parseDiff($commit_messages); - - $problems = array(); - $parsed = array(); - foreach ($commit_messages as $key => $change) { - $problems[$key] = array(); - - try { - $message = ArcanistDifferentialCommitMessage::newFromRawCorpus( - $change->getMetadata('message')); - - $message->pullDataFromConduit($conduit); - - $parsed[$key] = $message; - } catch (ArcanistDifferentialCommitMessageParserException $ex) { - foreach ($ex->getParserErrors() as $problem) { - $problems[$key][] = $problem; - } - continue; - } - } - - $blessed = null; - $revision_id = -1; - foreach ($problems as $key => $problem_list) { - if ($problem_list) { - continue; - } - if ($revision_id === -1) { - $revision_id = $parsed[$key]->getRevisionID(); - $blessed = $parsed[$key]; - } else { - throw new ArcanistUsageException( - "Changes in the specified commit range include more than one ". - "commit with a valid template commit message. This is ambiguous, ". - "your commit range should contain only one template commit ". - "message. Alternatively, use --preview to ignore commit ". - "messages."); - } - } - - if ($revision_id === -1) { - $desc = implode("\n", array_mergev($problems)); - if (count($problems) > 1) { - throw new ArcanistUsageException( - "All changes between the specified commits have template parsing ". - "problems:\n\n".$desc."\n\nIf you only want to create a diff ". - "(not a revision), use --preview to ignore commit messages."); - } else if (count($problems) == 1) { - $user_guide = 'http://phabricator.com/docs/phabricator/'. - 'article/Arcanist_User_Guide.html'; - throw new ArcanistUsageException( - "Commit message is not properly formatted:\n\n".$desc."\n\n". - "You should use the standard git commit template to provide a ". - "commit message. If you only want to create a diff (not a ". - "revision), use --preview to ignore commit messages.\n\n". - "See this document for instructions on configuring the commit ". - "template:\n\n {$user_guide}\n"); - } - } - - if ($blessed) { - $reviewers = $blessed->getFieldValue('reviewerPHIDs'); - if (!$reviewers) { - $message = "You have not specified any reviewers. Continue anyway?"; - if (!phutil_console_confirm($message)) { - throw new ArcanistUsageException('Specify reviewers and retry.'); - } - } else if (in_array($this->getUserPHID(), $reviewers)) { - throw new ArcanistUsageException( - "You can not be a reviewer for your own revision."); - } - } - - return $blessed; - } - private function getGitParentLogInfo() { $info = array( 'parent' => null, @@ -1091,42 +855,13 @@ EOTEXT } } - private function getUpdateMessage() { - $comments = $this->getArgument('message'); - if (!strlen($comments)) { - // When updating a revision using git without specifying '--message', try - // to prefill with the message in HEAD if it isn't a template message. The - // idea is that if you do: - // - // $ git commit -a -m 'fix some junk' - // $ arc diff - // - // ...you shouldn't have to retype the update message. - $repository_api = $this->getRepositoryAPI(); - if ($repository_api instanceof ArcanistGitAPI) { - $comments = $this->getGitUpdateMessage(); - } +/* -( Lint and Unit Tests )------------------------------------------------ */ - $template = - $comments. - "\n\n". - "# Enter a brief description of the changes included in this update.". - "\n"; - $comments = id(new PhutilInteractiveEditor($template)) - ->setName('differential-update-comments') - ->editInteractively(); - $comments = preg_replace('/^\s*#.*$/m', '', $comments); - - $comments = rtrim($comments); - if (!strlen($comments)) { - throw new ArcanistUserAbortException(); - } - } - - return $comments; - } + /** + * @task lintunit + */ private function runLint($paths) { if ($this->getArgument('nolint') || $this->getArgument('only')) { @@ -1187,6 +922,10 @@ EOTEXT return null; } + + /** + * @task lintunit + */ private function runUnit($paths) { if ($this->getArgument('nounit') || $this->getArgument('only')) { @@ -1239,6 +978,35 @@ EOTEXT return null; } + +/* -( Commit and Update Messages )----------------------------------------- */ + + + /** + * @task message + */ + private function buildCommitMessage() { + $is_create = $this->getArgument('create'); + + $message = null; + if ($is_create) { + $message_file = $this->getArgument('message-file'); + if ($message_file) { + return $this->getCommitMessageFromFile($message_file); + } else { + return $this->getCommitMessageFromUser(); + } + } else if (!$this->shouldOnlyCreateDiff()) { + return $this->getGitCommitMessage(); + } + + return null; + } + + + /** + * @task message + */ private function getCommitMessageFromUser() { $conduit = $this->getConduit(); @@ -1263,6 +1031,7 @@ EOTEXT $message = ArcanistDifferentialCommitMessage::newFromRawCorpus( $template); $message->pullDataFromConduit($conduit); + $this->validateCommitMessage($message); } catch (Exception $ex) { $path = Filesystem::writeUniqueFile('arc-commit-message', $template); @@ -1277,13 +1046,370 @@ EOTEXT return $message; } + + /** + * @task message + */ private function getCommitMessageFromFile($file) { $conduit = $this->getConduit(); $data = Filesystem::readFile($file); $message = ArcanistDifferentialCommitMessage::newFromRawCorpus($data); $message->pullDataFromConduit($conduit); + + $this->validateCommitMessage($message); + return $message; } + + /** + * @task message + */ + private function validateCommitMessage( + ArcanistDifferentialCommitMessage $message) { + $reviewers = $message->getFieldValue('reviewerPHIDs'); + if (!$reviewers) { + $confirm = "You have not specified any reviewers. Continue anyway?"; + if (!phutil_console_confirm($confirm)) { + throw new ArcanistUsageException('Specify reviewers and retry.'); + } + } else if (in_array($this->getUserPHID(), $reviewers)) { + throw new ArcanistUsageException( + "You can not be a reviewer for your own revision."); + } + } + + + /** + * @task message + */ + private function getUpdateMessage() { + $comments = $this->getArgument('message'); + if (strlen($comments)) { + return $comments; + } + + // When updating a revision using git without specifying '--message', try + // to prefill with the message in HEAD if it isn't a template message. The + // idea is that if you do: + // + // $ git commit -a -m 'fix some junk' + // $ arc diff + // + // ...you shouldn't have to retype the update message. + $repository_api = $this->getRepositoryAPI(); + if ($repository_api instanceof ArcanistGitAPI) { + $comments = $this->getGitUpdateMessage(); + } + + $template = + $comments. + "\n\n". + "# Enter a brief description of the changes included in this update.". + "\n"; + + $comments = id(new PhutilInteractiveEditor($template)) + ->setName('differential-update-comments') + ->editInteractively(); + + $comments = preg_replace('/^\s*#.*$/m', '', $comments); + $comments = rtrim($comments); + + if (!strlen($comments)) { + throw new ArcanistUserAbortException(); + } + + return $comments; + } + + + /** + * @task message + */ + private function getGitCommitMessage() { + $conduit = $this->getConduit(); + $repository_api = $this->getRepositoryAPI(); + + $parser = new ArcanistDiffParser(); + $commit_messages = $repository_api->getGitCommitLog(); + + if (!strlen($commit_messages)) { + if (!$repository_api->getHasCommits()) { + throw new ArcanistUsageException( + "This git repository doesn't have any commits yet. You need to ". + "commit something before you can diff against it."); + } else { + throw new ArcanistUsageException( + "The commit range doesn't include any commits. (Did you diff ". + "against the wrong commit?)"); + } + } + + $commit_messages = $parser->parseDiff($commit_messages); + + $problems = array(); + $parsed = array(); + foreach ($commit_messages as $key => $change) { + $problems[$key] = array(); + + try { + $message = ArcanistDifferentialCommitMessage::newFromRawCorpus( + $change->getMetadata('message')); + + $message->pullDataFromConduit($conduit); + + $parsed[$key] = $message; + } catch (ArcanistDifferentialCommitMessageParserException $ex) { + foreach ($ex->getParserErrors() as $problem) { + $problems[$key][] = $problem; + } + continue; + } + } + + $blessed = null; + $revision_id = -1; + foreach ($problems as $key => $problem_list) { + if ($problem_list) { + continue; + } + if ($revision_id === -1) { + $revision_id = $parsed[$key]->getRevisionID(); + $blessed = $parsed[$key]; + } else { + throw new ArcanistUsageException( + "Changes in the specified commit range include more than one ". + "commit with a valid template commit message. This is ambiguous, ". + "your commit range should contain only one template commit ". + "message. Alternatively, use --preview to ignore commit ". + "messages."); + } + } + + if ($revision_id === -1) { + $desc = implode("\n", array_mergev($problems)); + if (count($problems) > 1) { + throw new ArcanistUsageException( + "All changes between the specified commits have template parsing ". + "problems:\n\n".$desc."\n\nIf you only want to create a diff ". + "(not a revision), use --preview to ignore commit messages."); + } else if (count($problems) == 1) { + $user_guide = 'http://phabricator.com/docs/phabricator/'. + 'article/Arcanist_User_Guide.html'; + throw new ArcanistUsageException( + "Commit message is not properly formatted:\n\n".$desc."\n\n". + "You should use the standard git commit template to provide a ". + "commit message. If you only want to create a diff (not a ". + "revision), use --preview to ignore commit messages.\n\n". + "See this document for instructions on configuring the commit ". + "template:\n\n {$user_guide}\n"); + } + } + + if ($blessed) { + $this->validateCommitMessage($blessed); + } + + return $blessed; + } + + + /** + * Retrieve the git message in HEAD if it isn't a primary template message. + * + * @task message + */ + private function getGitUpdateMessage() { + $repository_api = $this->getRepositoryAPI(); + + $parser = new ArcanistDiffParser(); + $commit_messages = $repository_api->getGitCommitLog(); + $commit_messages = $parser->parseDiff($commit_messages); + + $head = reset($commit_messages); + $message = ArcanistDifferentialCommitMessage::newFromRawCorpus( + $head->getMetadata('message')); + if ($message->getRevisionID()) { + return null; + } + + return trim($message->getRawCorpus()); + } + + +/* -( Diff Specification )------------------------------------------------- */ + + + /** + * @task diffspec + */ + private function getLintStatus($lint_result) { + $map = array( + ArcanistLintWorkflow::RESULT_OKAY => 'okay', + ArcanistLintWorkflow::RESULT_ERRORS => 'fail', + ArcanistLintWorkflow::RESULT_WARNINGS => 'warn', + ArcanistLintWorkflow::RESULT_SKIP => 'skip', + ); + return idx($map, $lint_result, 'none'); + } + + + /** + * @task diffspec + */ + private function getUnitStatus($unit_result) { + $map = array( + ArcanistUnitWorkflow::RESULT_OKAY => 'okay', + ArcanistUnitWorkflow::RESULT_FAIL => 'fail', + ArcanistUnitWorkflow::RESULT_UNSOUND => 'warn', + ArcanistUnitWorkflow::RESULT_SKIP => 'skip', + ArcanistUnitWorkflow::RESULT_POSTPONED => 'postponed', + ); + return idx($map, $unit_result, 'none'); + } + + + /** + * @task diffspec + */ + private function buildDiffSpecification() { + + $repository_api = $this->getRepositoryAPI(); + $working_copy = $this->getWorkingCopy(); + + $base_revision = $repository_api->getSourceControlBaseRevision(); + $base_path = $repository_api->getSourceControlPath(); + $vcs = $repository_api->getSourceControlSystemName(); + $repo_uuid = null; + $parent = null; + if ($repository_api instanceof ArcanistGitAPI) { + $info = $this->getGitParentLogInfo(); + if ($info['parent']) { + $parent = $info['parent']; + } + if ($info['base_revision']) { + $base_revision = $info['base_revision']; + } + if ($info['base_path']) { + $base_path = $info['base_path']; + } + if ($info['uuid']) { + $repo_uuid = $info['uuid']; + } + } else if ($repository_api instanceof ArcanistSubversionAPI) { + $repo_uuid = $repository_api->getRepositorySVNUUID(); + } else if ($repository_api instanceof ArcanistMercurialAPI) { + // TODO: Provide this information. + } else { + throw new Exception("Unsupported repository API!"); + } + + return array( + 'sourceMachine' => php_uname('n'), + 'sourcePath' => $repository_api->getPath(), + 'branch' => $repository_api->getBranchName(), + 'sourceControlSystem' => $vcs, + 'sourceControlPath' => $base_path, + 'sourceControlBaseRevision' => $base_revision, + 'parentRevisionID' => $parent, + 'repositoryUUID' => $repo_uuid, + 'creationMethod' => 'arc', + 'arcanistProject' => $working_copy->getProjectID(), + 'authorPHID' => $this->getUserPHID(), + ); + } + + +/* -( Diff Properties )---------------------------------------------------- */ + + + /** + * Update lint information for the diff. + * + * @return void + * + * @task diffprop + */ + private function updateLintDiffProperty() { + if (!$this->unresolvedLint) { + return; + } + + $data = array(); + foreach ($this->unresolvedLint as $message) { + $data[] = array( + 'path' => $message->getPath(), + 'line' => $message->getLine(), + 'char' => $message->getChar(), + 'code' => $message->getCode(), + 'severity' => $message->getSeverity(), + 'name' => $message->getName(), + 'description' => $message->getDescription(), + ); + } + + $this->updateDiffProperty('arc:lint', json_encode($data)); + } + + + /** + * Update unit test information for the diff. + * + * @return void + * + * @task diffprop + */ + private function updateUnitDiffProperty() { + if (!$this->unresolvedTests) { + return; + } + + $data = array(); + foreach ($this->unresolvedTests as $test) { + $data[] = array( + 'name' => $test->getName(), + 'result' => $test->getResult(), + 'userdata' => $test->getUserData(), + ); + } + + $this->updateDiffProperty('arc:unit', json_encode($data)); + } + + + /** + * Update local commit information for the diff. + * + * @task diffprop + */ + private function updateLocalDiffProperty() { + $local_info = $this->getRepositoryAPI()->getLocalCommitInformation(); + if (!$local_info) { + return; + } + + $this->updateDiffProperty('local:commits', json_encode($local_info)); + } + + + /** + * Update an arbitrary diff property. + * + * @param string Diff property name. + * @param string Diff property value. + * @return void + * + * @task diffprop + */ + private function updateDiffProperty($name, $data) { + $this->getConduit()->callMethodSynchronous( + 'differential.setdiffproperty', + array( + 'diff_id' => $this->getDiffID(), + 'name' => $name, + 'data' => $data, + )); + } + } -- 2.43.0