<?php
/*
- * Copyright 2011 Facebook, Inc.
+ * 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.
/**
* Sends changes from your working copy to Differential for code review.
*
+ * @task lintunit Lint and Unit Tests
+ * @task message Commit and Update Messages
+ * @task diffspec Diff Specification
+ * @task diffprop Diff Properties
+ *
* @group workflow
*/
class ArcanistDiffWorkflow extends ArcanistBaseWorkflow {
'help' => "(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(
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);
$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) {
$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(),
);
protected function shouldOnlyCreateDiff() {
+ if ($this->getArgument('create')) {
+ return false;
+ }
+
$repository_api = $this->getRepositoryAPI();
if ($repository_api instanceof ArcanistSubversionAPI) {
return true;
$this->getArgument('only');
}
- protected function findRevisionInformation() {
- return array(null, null);
- }
-
private function generateAffectedPaths() {
$repository_api = $this->getRepositoryAPI();
if ($repository_api instanceof ArcanistSubversionAPI) {
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,
}
}
- 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')) {
return null;
}
+
+ /**
+ * @task lintunit
+ */
private function runUnit($paths) {
if ($this->getArgument('nounit') ||
$this->getArgument('only')) {
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();
$message = ArcanistDifferentialCommitMessage::newFromRawCorpus(
$template);
$message->pullDataFromConduit($conduit);
+ $this->validateCommitMessage($message);
} catch (Exception $ex) {
$path = Filesystem::writeUniqueFile('arc-commit-message', $template);
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,
+ ));
+ }
+
}