Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F1374966
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
157 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/workflow/ArcanistDiffWorkflow.php b/src/workflow/ArcanistDiffWorkflow.php
index c7f2ff0a..14a7ef8f 100644
--- a/src/workflow/ArcanistDiffWorkflow.php
+++ b/src/workflow/ArcanistDiffWorkflow.php
@@ -1,2919 +1,2919 @@
<?php
/**
* 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
*/
final class ArcanistDiffWorkflow extends ArcanistWorkflow {
private $console;
private $hasWarnedExternals = false;
private $unresolvedLint;
private $excuses = array('lint' => null, 'unit' => null);
private $testResults;
private $diffID;
private $revisionID;
private $haveUncommittedChanges = false;
private $diffPropertyFutures = array();
private $commitMessageFromRevision;
private $hitAutotargets;
const STAGING_PUSHED = 'pushed';
const STAGING_USER_SKIP = 'user.skip';
const STAGING_DIFF_RAW = 'diff.raw';
const STAGING_REPOSITORY_UNKNOWN = 'repository.unknown';
const STAGING_REPOSITORY_UNAVAILABLE = 'repository.unavailable';
const STAGING_REPOSITORY_UNSUPPORTED = 'repository.unsupported';
const STAGING_REPOSITORY_UNCONFIGURED = 'repository.unconfigured';
const STAGING_CLIENT_UNSUPPORTED = 'client.unsupported';
public function getWorkflowName() {
return 'diff';
}
public function getCommandSynopses() {
return phutil_console_format(<<<EOTEXT
**diff** [__paths__] (svn)
**diff** [__commit__] (git, hg)
EOTEXT
);
}
public function getCommandHelp() {
return phutil_console_format(<<<EOTEXT
Supports: git, svn, hg
Generate a Differential diff or revision from local changes.
Under git and mercurial, you can specify a commit (like __HEAD^^^__
or __master__) and Differential will generate a diff against the
merge base of that commit and your current working directory parent.
Under svn, you can choose to include only some of the modified files
in the working copy in the diff by specifying their paths. If you
omit paths, all changes are included in the diff.
EOTEXT
);
}
public function requiresWorkingCopy() {
return !$this->isRawDiffSource();
}
public function requiresConduit() {
return true;
}
public function requiresAuthentication() {
return true;
}
public function requiresRepositoryAPI() {
if (!$this->isRawDiffSource()) {
return true;
}
if ($this->getArgument('use-commit-message')) {
return true;
}
return false;
}
public function getDiffID() {
return $this->diffID;
}
public function getArguments() {
$arguments = array(
'message' => array(
'short' => 'm',
'param' => 'message',
'help' => pht(
'When updating a revision, use the specified message instead of '.
'prompting.'),
),
'message-file' => array(
'short' => 'F',
'param' => 'file',
'paramtype' => 'file',
'help' => pht(
'When creating a revision, read revision information '.
'from this file.'),
),
'use-commit-message' => array(
'supports' => array(
'git',
// TODO: Support mercurial.
),
'short' => 'C',
'param' => 'commit',
'help' => pht('Read revision information from a specific commit.'),
'conflicts' => array(
'only' => null,
'preview' => null,
'update' => null,
),
),
'edit' => array(
'supports' => array(
'git',
'hg',
),
'nosupport' => array(
'svn' => pht('Edit revisions via the web interface when using SVN.'),
),
'help' => pht(
'When updating a revision under git, edit revision information '.
'before updating.'),
),
'raw' => array(
'help' => pht(
'Read diff from stdin, not from the working copy. This disables '.
'many Arcanist/Phabricator features which depend on having access '.
'to the working copy.'),
'conflicts' => array(
'less-context' => null,
'apply-patches' => pht('%s disables lint.', '--raw'),
'never-apply-patches' => pht('%s disables lint.', '--raw'),
'advice' => pht('%s disables lint.', '--raw'),
'lintall' => pht('%s disables lint.', '--raw'),
'create' => pht(
'%s and %s both need stdin. Use %s.',
'--raw',
'--create',
'--raw-command'),
'edit' => pht(
'%s and %s both need stdin. Use %s.',
'--raw',
'--edit',
'--raw-command'),
'raw-command' => null,
),
),
'raw-command' => array(
'param' => 'command',
'help' => pht(
'Generate diff by executing a specified command, not from the '.
'working copy. This disables many Arcanist/Phabricator features '.
'which depend on having access to the working copy.'),
'conflicts' => array(
'less-context' => null,
'apply-patches' => pht('%s disables lint.', '--raw-command'),
'never-apply-patches' => pht('%s disables lint.', '--raw-command'),
'advice' => pht('%s disables lint.', '--raw-command'),
'lintall' => pht('%s disables lint.', '--raw-command'),
),
),
'create' => array(
'help' => pht('Always create a new revision.'),
'conflicts' => array(
'edit' => pht(
'%s can not be used with %s.',
'--create',
'--edit'),
'only' => pht(
'%s can not be used with %s.',
'--create',
'--only'),
'preview' => pht(
'%s can not be used with %s.',
'--create',
'--preview'),
'update' => pht(
'%s can not be used with %s.',
'--create',
'--update'),
),
),
'update' => array(
'param' => 'revision_id',
'help' => pht('Always update a specific revision.'),
),
'nounit' => array(
'help' => pht('Do not run unit tests.'),
),
'nolint' => array(
'help' => pht('Do not run lint.'),
'conflicts' => array(
'lintall' => pht('%s suppresses lint.', '--nolint'),
'advice' => pht('%s suppresses lint.', '--nolint'),
'apply-patches' => pht('%s suppresses lint.', '--nolint'),
'never-apply-patches' => pht('%s suppresses lint.', '--nolint'),
),
),
'only' => array(
'help' => pht(
'Only generate a diff, without running lint, unit tests, or other '.
'auxiliary steps. See also %s.',
'--preview'),
'conflicts' => array(
'preview' => null,
'message' => pht('%s does not affect revisions.', '--only'),
'edit' => pht('%s does not affect revisions.', '--only'),
'lintall' => pht('%s suppresses lint.', '--only'),
'advice' => pht('%s suppresses lint.', '--only'),
'apply-patches' => pht('%s suppresses lint.', '--only'),
'never-apply-patches' => pht('%s suppresses lint.', '--only'),
),
),
'preview' => array(
'help' => pht(
'Instead of creating or updating a revision, only create a diff, '.
'which you may later attach to a revision. This still runs lint '.
'unit tests. See also %s.',
'--only'),
'conflicts' => array(
'only' => null,
'edit' => pht('%s does affect revisions.', '--preview'),
'message' => pht('%s does not update any revision.', '--preview'),
),
),
'plan-changes' => array(
'help' => pht(
'Create or update a revision without requesting a code review.'),
'conflicts' => array(
'only' => pht('%s does not affect revisions.', '--only'),
'preview' => pht('%s does not affect revisions.', '--preview'),
),
),
'encoding' => array(
'param' => 'encoding',
'help' => pht(
'Attempt to convert non UTF-8 hunks into specified encoding.'),
),
'allow-untracked' => array(
'help' => pht('Skip checks for untracked files in the working copy.'),
),
'excuse' => array(
'param' => 'excuse',
'help' => pht(
'Provide a prepared in advance excuse for any lints/tests '.
'shall they fail.'),
),
'less-context' => array(
'help' => pht(
"Normally, files are diffed with full context: the entire file is ".
"sent to Differential so reviewers can 'show more' and see it. If ".
"you are making changes to very large files with tens of thousands ".
"of lines, this may not work well. With this flag, a diff will ".
"be created that has only a few lines of context."),
),
'lintall' => array(
'help' => pht(
'Raise all lint warnings, not just those on lines you changed.'),
'passthru' => array(
'lint' => true,
),
),
'advice' => array(
'help' => pht(
'Require excuse for lint advice in addition to lint warnings and '.
'errors.'),
),
'only-new' => array(
'param' => 'bool',
'help' => pht(
'Display only lint messages not present in the original code.'),
'passthru' => array(
'lint' => true,
),
),
'apply-patches' => array(
'help' => pht(
'Apply patches suggested by lint to the working copy without '.
'prompting.'),
'conflicts' => array(
'never-apply-patches' => true,
),
'passthru' => array(
'lint' => true,
),
),
'never-apply-patches' => array(
'help' => pht('Never apply patches suggested by lint.'),
'conflicts' => array(
'apply-patches' => true,
),
'passthru' => array(
'lint' => true,
),
),
'amend-all' => array(
'help' => pht(
'When linting git repositories, amend HEAD with all patches '.
'suggested by lint without prompting.'),
'passthru' => array(
'lint' => true,
),
),
'amend-autofixes' => array(
'help' => pht(
'When linting git repositories, amend HEAD with autofix '.
'patches suggested by lint without prompting.'),
'passthru' => array(
'lint' => true,
),
),
'add-all' => array(
'short' => 'a',
'help' => pht(
'Automatically add all unstaged and uncommitted '.
'files to the commit.'),
),
'json' => array(
'help' => pht(
'Emit machine-readable JSON. EXPERIMENTAL! Probably does not work!'),
),
'no-amend' => array(
'help' => pht(
'Never amend commits in the working copy with lint patches.'),
),
'uncommitted' => array(
'help' => pht('Suppress warning about uncommitted changes.'),
'supports' => array(
'hg',
),
),
'verbatim' => array(
'help' => pht(
'When creating a revision, try to use the working copy commit '.
'message verbatim, without prompting to edit it. When updating a '.
'revision, update some fields from the local commit message.'),
'supports' => array(
'hg',
'git',
),
'conflicts' => array(
'use-commit-message' => true,
'update' => true,
'only' => true,
'preview' => true,
'raw' => true,
'raw-command' => true,
'message-file' => true,
),
),
'reviewers' => array(
'param' => 'usernames',
'help' => pht('When creating a revision, add reviewers.'),
'conflicts' => array(
'only' => true,
'preview' => true,
'update' => true,
),
),
'cc' => array(
'param' => 'usernames',
'help' => pht('When creating a revision, add CCs.'),
'conflicts' => array(
'only' => true,
'preview' => true,
'update' => true,
),
),
'skip-binaries' => array(
'help' => pht('Do not upload binaries (like images).'),
),
'skip-staging' => array(
'help' => pht('Do not copy changes to the staging area.'),
),
'ignore-unsound-tests' => array(
'help' => pht('Ignore unsound test failures without prompting.'),
),
'base' => array(
'param' => 'rules',
'help' => pht('Additional rules for determining base revision.'),
'nosupport' => array(
'svn' => pht('Subversion does not use base commits.'),
),
'supports' => array('git', 'hg'),
),
'no-diff' => array(
'help' => pht(
'Only run lint and unit tests. Intended for internal use.'),
),
'cache' => array(
'param' => 'bool',
'help' => pht(
'%d to disable lint cache, %d to enable (default).',
0,
1),
'passthru' => array(
'lint' => true,
),
),
'coverage' => array(
'help' => pht('Always enable coverage information.'),
'conflicts' => array(
'no-coverage' => null,
),
'passthru' => array(
'unit' => true,
),
),
'no-coverage' => array(
'help' => pht('Always disable coverage information.'),
'passthru' => array(
'unit' => true,
),
),
'browse' => array(
'help' => pht(
'After creating a diff or revision, open it in a web browser.'),
),
'*' => 'paths',
'head' => array(
'param' => 'commit',
'help' => pht(
'Specify the end of the commit range. This disables many '.
'Arcanist/Phabricator features which depend on having access to '.
'the working copy.'),
'supports' => array('git'),
'nosupport' => array(
'svn' => pht('Subversion does not support commit ranges.'),
'hg' => pht('Mercurial does not support %s yet.', '--head'),
),
'conflicts' => array(
'lintall' => pht('%s suppresses lint.', '--head'),
'advice' => pht('%s suppresses lint.', '--head'),
),
),
);
return $arguments;
}
public function isRawDiffSource() {
return $this->getArgument('raw') || $this->getArgument('raw-command');
}
public function run() {
$this->console = PhutilConsole::getConsole();
$this->runRepositoryAPISetup();
if ($this->getArgument('no-diff')) {
$this->removeScratchFile('diff-result.json');
$data = $this->runLintUnit();
$this->writeScratchJSONFile('diff-result.json', $data);
return 0;
}
$this->runDiffSetupBasics();
$commit_message = $this->buildCommitMessage();
$this->dispatchEvent(
ArcanistEventType::TYPE_DIFF_DIDBUILDMESSAGE,
array(
'message' => $commit_message,
));
if (!$this->shouldOnlyCreateDiff()) {
$revision = $this->buildRevisionFromCommitMessage($commit_message);
}
$server = $this->console->getServer();
$server->setHandler(array($this, 'handleServerMessage'));
$data = $this->runLintUnit();
$lint_result = $data['lintResult'];
$this->unresolvedLint = $data['unresolvedLint'];
$unit_result = $data['unitResult'];
$this->testResults = $data['testResults'];
if ($this->getArgument('nolint')) {
$this->excuses['lint'] = $this->getSkipExcuse(
pht('Provide explanation for skipping lint or press Enter to abort:'),
'lint-excuses');
}
if ($this->getArgument('nounit')) {
$this->excuses['unit'] = $this->getSkipExcuse(
pht(
'Provide explanation for skipping unit tests '.
'or press Enter to abort:'),
'unit-excuses');
}
$changes = $this->generateChanges();
if (!$changes) {
throw new ArcanistUsageException(
pht('There are no changes to generate a diff from!'));
}
$diff_spec = array(
'changes' => mpull($changes, 'toDictionary'),
'lintStatus' => $this->getLintStatus($lint_result),
'unitStatus' => $this->getUnitStatus($unit_result),
) + $this->buildDiffSpecification();
$conduit = $this->getConduit();
$diff_info = $conduit->callMethodSynchronous(
'differential.creatediff',
$diff_spec);
$this->diffID = $diff_info['diffid'];
$event = $this->dispatchEvent(
ArcanistEventType::TYPE_DIFF_WASCREATED,
array(
'diffID' => $diff_info['diffid'],
'lintResult' => $lint_result,
'unitResult' => $unit_result,
));
$this->submitChangesToStagingArea($this->diffID);
$phid = idx($diff_info, 'phid');
if ($phid) {
$this->hitAutotargets = $this->updateAutotargets(
$phid,
$unit_result);
}
$this->updateLintDiffProperty();
$this->updateUnitDiffProperty();
$this->updateLocalDiffProperty();
$this->updateOntoDiffProperty();
$this->resolveDiffPropertyUpdates();
$output_json = $this->getArgument('json');
if ($this->shouldOnlyCreateDiff()) {
if (!$output_json) {
echo phutil_console_format(
"%s\n **%s** __%s__\n\n",
pht('Created a new Differential diff:'),
pht('Diff URI:'),
$diff_info['uri']);
} else {
$human = ob_get_clean();
echo json_encode(array(
'diffURI' => $diff_info['uri'],
'diffID' => $this->getDiffID(),
'human' => $human,
))."\n";
ob_start();
}
if ($this->shouldOpenCreatedObjectsInBrowser()) {
$this->openURIsInBrowser(array($diff_info['uri']));
}
} else {
$revision['diffid'] = $this->getDiffID();
if ($commit_message->getRevisionID()) {
$result = $conduit->callMethodSynchronous(
'differential.updaterevision',
$revision);
foreach (array('edit-messages.json', 'update-messages.json') as $file) {
$messages = $this->readScratchJSONFile($file);
unset($messages[$revision['id']]);
$this->writeScratchJSONFile($file, $messages);
}
echo pht('Updated an existing Differential revision:')."\n";
} else {
$revision = $this->dispatchWillCreateRevisionEvent($revision);
$result = $conduit->callMethodSynchronous(
'differential.createrevision',
$revision);
$revised_message = $conduit->callMethodSynchronous(
'differential.getcommitmessage',
array(
'revision_id' => $result['revisionid'],
));
if ($this->shouldAmend()) {
$repository_api = $this->getRepositoryAPI();
if ($repository_api->supportsAmend()) {
echo pht('Updating commit message...')."\n";
$repository_api->amendCommit($revised_message);
} else {
echo pht(
'Commit message was not amended. Amending commit message is '.
'only supported in git and hg (version 2.2 or newer)');
}
}
echo pht('Created a new Differential revision:')."\n";
}
$uri = $result['uri'];
echo phutil_console_format(
" **%s** __%s__\n\n",
pht('Revision URI:'),
$uri);
if ($this->getArgument('plan-changes')) {
$conduit->callMethodSynchronous(
'differential.createcomment',
array(
'revision_id' => $result['revisionid'],
'action' => 'rethink',
));
echo pht('Planned changes to the revision.')."\n";
}
if ($this->shouldOpenCreatedObjectsInBrowser()) {
$this->openURIsInBrowser(array($uri));
}
}
echo pht('Included changes:')."\n";
foreach ($changes as $change) {
echo ' '.$change->renderTextSummary()."\n";
}
if ($output_json) {
ob_get_clean();
}
$this->removeScratchFile('create-message');
return 0;
}
private function runRepositoryAPISetup() {
if (!$this->requiresRepositoryAPI()) {
return;
}
$repository_api = $this->getRepositoryAPI();
if ($this->getArgument('less-context')) {
$repository_api->setDiffLinesOfContext(3);
}
$repository_api->setBaseCommitArgumentRules(
$this->getArgument('base', ''));
if ($repository_api->supportsCommitRanges()) {
$this->parseBaseCommitArgument($this->getArgument('paths'));
}
$head_commit = $this->getArgument('head');
if ($head_commit !== null) {
$repository_api->setHeadCommit($head_commit);
}
}
private function runDiffSetupBasics() {
$output_json = $this->getArgument('json');
if ($output_json) {
// TODO: We should move this to a higher-level and put an indirection
// layer between echoing stuff and stdout.
ob_start();
}
if ($this->requiresWorkingCopy()) {
$repository_api = $this->getRepositoryAPI();
if ($this->getArgument('add-all')) {
$this->setCommitMode(self::COMMIT_ENABLE);
} else if ($this->getArgument('uncommitted')) {
$this->setCommitMode(self::COMMIT_DISABLE);
} else {
$this->setCommitMode(self::COMMIT_ALLOW);
}
if ($repository_api instanceof ArcanistSubversionAPI) {
$repository_api->limitStatusToPaths($this->getArgument('paths'));
}
if (!$this->getArgument('head')) {
$this->requireCleanWorkingCopy();
}
}
$this->dispatchEvent(
ArcanistEventType::TYPE_DIFF_DIDCOLLECTCHANGES,
array());
}
private function buildRevisionFromCommitMessage(
ArcanistDifferentialCommitMessage $message) {
$conduit = $this->getConduit();
$revision_id = $message->getRevisionID();
$revision = array(
'fields' => $message->getFields(),
);
if ($revision_id) {
// With '--verbatim', pass the (possibly modified) local fields. This
// allows the user to edit some fields (like "title" and "summary")
// locally without '--edit' and have changes automatically synchronized.
// Without '--verbatim', we do not update the revision to reflect local
// commit message changes.
if ($this->getArgument('verbatim')) {
$use_fields = $message->getFields();
} else {
$use_fields = array();
}
$should_edit = $this->getArgument('edit');
$edit_messages = $this->readScratchJSONFile('edit-messages.json');
$remote_corpus = idx($edit_messages, $revision_id);
if (!$should_edit || !$remote_corpus || $use_fields) {
if ($this->commitMessageFromRevision) {
$remote_corpus = $this->commitMessageFromRevision;
} else {
$remote_corpus = $conduit->callMethodSynchronous(
'differential.getcommitmessage',
array(
'revision_id' => $revision_id,
'edit' => 'edit',
'fields' => $use_fields,
));
}
}
if ($should_edit) {
$edited = $this->newInteractiveEditor($remote_corpus)
->setName('differential-edit-revision-info')
->editInteractively();
if ($edited != $remote_corpus) {
$remote_corpus = $edited;
$edit_messages[$revision_id] = $remote_corpus;
$this->writeScratchJSONFile('edit-messages.json', $edit_messages);
}
}
if ($this->commitMessageFromRevision == $remote_corpus) {
$new_message = $message;
} else {
$remote_corpus = ArcanistCommentRemover::removeComments(
$remote_corpus);
$new_message = ArcanistDifferentialCommitMessage::newFromRawCorpus(
$remote_corpus);
$new_message->pullDataFromConduit($conduit);
}
$revision['fields'] = $new_message->getFields();
$revision['id'] = $revision_id;
$this->revisionID = $revision_id;
$revision['message'] = $this->getArgument('message');
if (!strlen($revision['message'])) {
$update_messages = $this->readScratchJSONFile('update-messages.json');
$update_messages[$revision_id] = $this->getUpdateMessage(
$revision['fields'],
idx($update_messages, $revision_id));
$revision['message'] = ArcanistCommentRemover::removeComments(
$update_messages[$revision_id]);
if (!strlen(trim($revision['message']))) {
throw new ArcanistUserAbortException();
}
$this->writeScratchJSONFile('update-messages.json', $update_messages);
}
}
return $revision;
}
protected function shouldOnlyCreateDiff() {
if ($this->getArgument('create')) {
return false;
}
if ($this->getArgument('update')) {
return false;
}
if ($this->getArgument('use-commit-message')) {
return false;
}
if ($this->isRawDiffSource()) {
return true;
}
return $this->getArgument('preview') ||
$this->getArgument('only');
}
private function generateAffectedPaths() {
if ($this->isRawDiffSource()) {
return array();
}
$repository_api = $this->getRepositoryAPI();
if ($repository_api instanceof ArcanistSubversionAPI) {
$file_list = new FileList($this->getArgument('paths', array()));
$paths = $repository_api->getSVNStatus($externals = true);
foreach ($paths as $path => $mask) {
if (!$file_list->contains($repository_api->getPath($path), true)) {
unset($paths[$path]);
}
}
$warn_externals = array();
foreach ($paths as $path => $mask) {
$any_mod = ($mask & ArcanistRepositoryAPI::FLAG_ADDED) ||
($mask & ArcanistRepositoryAPI::FLAG_MODIFIED) ||
($mask & ArcanistRepositoryAPI::FLAG_DELETED);
if ($mask & ArcanistRepositoryAPI::FLAG_EXTERNALS) {
unset($paths[$path]);
if ($any_mod) {
$warn_externals[] = $path;
}
}
}
if ($warn_externals && !$this->hasWarnedExternals) {
echo phutil_console_format(
"%s\n\n%s\n\n",
pht(
"The working copy includes changes to '%s' paths. These ".
"changes will not be included in the diff because SVN can not ".
"commit 'svn:externals' changes alongside normal changes.",
'svn:externals'),
pht(
"Modified '%s' files:",
'svn:externals'),
phutil_console_wrap(implode("\n", $warn_externals), 8));
$prompt = pht('Generate a diff (with just local changes) anyway?');
if (!phutil_console_confirm($prompt)) {
throw new ArcanistUserAbortException();
} else {
$this->hasWarnedExternals = true;
}
}
} else {
$paths = $repository_api->getWorkingCopyStatus();
}
foreach ($paths as $path => $mask) {
if ($mask & ArcanistRepositoryAPI::FLAG_UNTRACKED) {
unset($paths[$path]);
}
}
return $paths;
}
protected function generateChanges() {
$parser = $this->newDiffParser();
$is_raw = $this->isRawDiffSource();
if ($is_raw) {
if ($this->getArgument('raw')) {
fwrite(STDERR, pht('Reading diff from stdin...')."\n");
$raw_diff = file_get_contents('php://stdin');
} else if ($this->getArgument('raw-command')) {
list($raw_diff) = execx('%C', $this->getArgument('raw-command'));
} else {
throw new Exception(pht('Unknown raw diff source.'));
}
$changes = $parser->parseDiff($raw_diff);
foreach ($changes as $key => $change) {
// Remove "message" changes, e.g. from "git show".
if ($change->getType() == ArcanistDiffChangeType::TYPE_MESSAGE) {
unset($changes[$key]);
}
}
return $changes;
}
$repository_api = $this->getRepositoryAPI();
if ($repository_api instanceof ArcanistSubversionAPI) {
$paths = $this->generateAffectedPaths();
$this->primeSubversionWorkingCopyData($paths);
// Check to make sure the user is diffing from a consistent base revision.
// This is mostly just an abuse sanity check because it's silly to do this
// and makes the code more difficult to effectively review, but it also
// affects patches and makes them nonportable.
$bases = $repository_api->getSVNBaseRevisions();
// Remove all files with baserev "0"; these files are new.
foreach ($bases as $path => $baserev) {
if ($bases[$path] <= 0) {
unset($bases[$path]);
}
}
if ($bases) {
$rev = reset($bases);
$revlist = array();
foreach ($bases as $path => $baserev) {
$revlist[] = ' '.pht('Revision %s, %s', $baserev, $path);
}
$revlist = implode("\n", $revlist);
foreach ($bases as $path => $baserev) {
if ($baserev !== $rev) {
throw new ArcanistUsageException(
pht(
"Base revisions of changed paths are mismatched. Update all ".
"paths to the same base revision before creating a diff: ".
"\n\n%s",
$revlist));
}
}
// If you have a change which affects several files, all of which are
// at a consistent base revision, treat that revision as the effective
// base revision. The use case here is that you made a change to some
// file, which updates it to HEAD, but want to be able to change it
// again without updating the entire working copy. This is a little
// sketchy but it arises in Facebook Ops workflows with config files and
// doesn't have any real material tradeoffs (e.g., these patches are
// perfectly applyable).
$repository_api->overrideSVNBaseRevisionNumber($rev);
}
$changes = $parser->parseSubversionDiff(
$repository_api,
$paths);
} else if ($repository_api instanceof ArcanistGitAPI) {
$diff = $repository_api->getFullGitDiff(
$repository_api->getBaseCommit(),
$repository_api->getHeadCommit());
if (!strlen($diff)) {
throw new ArcanistUsageException(
pht('No changes found. (Did you specify the wrong commit range?)'));
}
$changes = $parser->parseDiff($diff);
} else if ($repository_api instanceof ArcanistMercurialAPI) {
$diff = $repository_api->getFullMercurialDiff();
if (!strlen($diff)) {
throw new ArcanistUsageException(
pht('No changes found. (Did you specify the wrong commit range?)'));
}
$changes = $parser->parseDiff($diff);
} else {
throw new Exception(pht('Repository API is not supported.'));
}
if (count($changes) > 250) {
$message = pht(
'This diff has a very large number of changes (%s). Differential '.
'works best for changes which will receive detailed human review, '.
'and not as well for large automated changes or bulk checkins. '.
'See %s for information about reviewing big checkins. Continue anyway?',
phutil_count($changes),
'https://secure.phabricator.com/book/phabricator/article/'.
'differential_large_changes/');
if (!phutil_console_confirm($message)) {
throw new ArcanistUsageException(
pht('Aborted generation of gigantic diff.'));
}
}
$limit = 1024 * 1024 * 4;
foreach ($changes as $change) {
$size = 0;
foreach ($change->getHunks() as $hunk) {
$size += strlen($hunk->getCorpus());
}
if ($size > $limit) {
$byte_warning = pht(
"Diff for '%s' with context is %s bytes in length. ".
"Generally, source changes should not be this large.",
$change->getCurrentPath(),
new PhutilNumber($size));
if (!$this->getArgument('less-context')) {
$byte_warning .= ' '.pht(
"If this file is a huge text file, try using the '%s' flag.",
'--less-context');
}
if ($repository_api instanceof ArcanistSubversionAPI) {
throw new ArcanistUsageException(
$byte_warning.' '.
pht(
"If the file is not a text file, mark it as binary with:".
"\n\n $ %s\n",
'svn propset svn:mime-type application/octet-stream <filename>'));
} else {
$confirm = $byte_warning.' '.pht(
"If the file is not a text file, you can mark it 'binary'. ".
"Mark this file as 'binary' and continue?");
if (phutil_console_confirm($confirm)) {
$change->convertToBinaryChange($repository_api);
} else {
throw new ArcanistUsageException(
pht('Aborted generation of gigantic diff.'));
}
}
}
}
$try_encoding = nonempty($this->getArgument('encoding'), null);
$utf8_problems = array();
foreach ($changes as $change) {
foreach ($change->getHunks() as $hunk) {
$corpus = $hunk->getCorpus();
if (!phutil_is_utf8($corpus)) {
// If this corpus is heuristically binary, don't try to convert it.
// mb_check_encoding() and mb_convert_encoding() are both very very
// liberal about what they're willing to process.
$is_binary = ArcanistDiffUtils::isHeuristicBinaryFile($corpus);
if (!$is_binary) {
if (!$try_encoding) {
try {
$try_encoding = $this->getRepositoryEncoding();
} catch (ConduitClientException $e) {
if ($e->getErrorCode() == 'ERR-BAD-ARCANIST-PROJECT') {
echo phutil_console_wrap(
pht('Lookup of encoding in arcanist project failed: %s',
$e->getMessage())."\n");
} else {
throw $e;
}
}
}
if ($try_encoding) {
$corpus = phutil_utf8_convert($corpus, 'UTF-8', $try_encoding);
$name = $change->getCurrentPath();
if (phutil_is_utf8($corpus)) {
$this->writeStatusMessage(
pht(
"Converted a '%s' hunk from '%s' to UTF-8.\n",
$name,
$try_encoding));
$hunk->setCorpus($corpus);
continue;
}
}
}
$utf8_problems[] = $change;
break;
}
}
}
// If there are non-binary files which aren't valid UTF-8, warn the user
// and treat them as binary changes. See D327 for discussion of why Arcanist
// has this behavior.
if ($utf8_problems) {
$utf8_warning =
sprintf(
"%s\n\n%s\n\n %s\n",
pht(
'This diff includes %s file(s) which are not valid UTF-8 (they '.
'contain invalid byte sequences). You can either stop this '.
'workflow and fix these files, or continue. If you continue, '.
'these files will be marked as binary.',
phutil_count($utf8_problems)),
pht(
"You can learn more about how Phabricator handles character ".
"encodings (and how to configure encoding settings and detect and ".
"correct encoding problems) by reading 'User Guide: UTF-8 and ".
"Character Encoding' in the Phabricator documentation."),
pht(
'%s AFFECTED FILE(S)',
phutil_count($utf8_problems)));
$confirm = pht(
'Do you want to mark these %s file(s) as binary and continue?',
phutil_count($utf8_problems));
echo phutil_console_format(
"**%s**\n",
pht('Invalid Content Encoding (Non-UTF8)'));
echo phutil_console_wrap($utf8_warning);
$file_list = mpull($utf8_problems, 'getCurrentPath');
$file_list = ' '.implode("\n ", $file_list);
echo $file_list;
if (!phutil_console_confirm($confirm, $default_no = false)) {
throw new ArcanistUsageException(pht('Aborted workflow to fix UTF-8.'));
} else {
foreach ($utf8_problems as $change) {
$change->convertToBinaryChange($repository_api);
}
}
}
$this->uploadFilesForChanges($changes);
return $changes;
}
private function getGitParentLogInfo() {
$info = array(
'parent' => null,
'base_revision' => null,
'base_path' => null,
'uuid' => null,
);
$repository_api = $this->getRepositoryAPI();
$parser = $this->newDiffParser();
$history_messages = $repository_api->getGitHistoryLog();
if (!$history_messages) {
// This can occur on the initial commit.
return $info;
}
$history_messages = $parser->parseDiff($history_messages);
foreach ($history_messages as $key => $change) {
try {
$message = ArcanistDifferentialCommitMessage::newFromRawCorpus(
$change->getMetadata('message'));
if ($message->getRevisionID() && $info['parent'] === null) {
$info['parent'] = $message->getRevisionID();
}
if ($message->getGitSVNBaseRevision() &&
$info['base_revision'] === null) {
$info['base_revision'] = $message->getGitSVNBaseRevision();
$info['base_path'] = $message->getGitSVNBasePath();
}
if ($message->getGitSVNUUID()) {
$info['uuid'] = $message->getGitSVNUUID();
}
if ($info['parent'] && $info['base_revision']) {
break;
}
} catch (ArcanistDifferentialCommitMessageParserException $ex) {
// Ignore.
} catch (ArcanistUsageException $ex) {
// Ignore an invalid Differential Revision field in the parent commit
}
}
return $info;
}
protected function primeSubversionWorkingCopyData($paths) {
$repository_api = $this->getRepositoryAPI();
$futures = array();
$targets = array();
foreach ($paths as $path => $mask) {
$futures[] = $repository_api->buildDiffFuture($path);
$targets[] = array('command' => 'diff', 'path' => $path);
$futures[] = $repository_api->buildInfoFuture($path);
$targets[] = array('command' => 'info', 'path' => $path);
}
$futures = id(new FutureIterator($futures))
->limit(8);
foreach ($futures as $key => $future) {
$target = $targets[$key];
if ($target['command'] == 'diff') {
$repository_api->primeSVNDiffResult(
$target['path'],
$future->resolve());
} else {
$repository_api->primeSVNInfoResult(
$target['path'],
$future->resolve());
}
}
}
private function shouldAmend() {
if ($this->isRawDiffSource()) {
return false;
}
if ($this->haveUncommittedChanges) {
return false;
}
if ($this->getArgument('no-amend')) {
return false;
}
if ($this->getArgument('head') !== null) {
return false;
}
// Run this last: with --raw or --raw-command, we won't have a repository
// API.
if ($this->isHistoryImmutable()) {
return false;
}
return true;
}
/* -( Lint and Unit Tests )------------------------------------------------ */
/**
* @task lintunit
*/
private function runLintUnit() {
$lint_result = $this->runLint();
$unit_result = $this->runUnit();
return array(
'lintResult' => $lint_result,
'unresolvedLint' => $this->unresolvedLint,
'unitResult' => $unit_result,
'testResults' => $this->testResults,
);
}
/**
* @task lintunit
*/
private function runLint() {
if ($this->getArgument('nolint') ||
$this->getArgument('only') ||
$this->isRawDiffSource() ||
$this->getArgument('head')) {
return ArcanistLintWorkflow::RESULT_SKIP;
}
$repository_api = $this->getRepositoryAPI();
$this->console->writeOut("%s\n", pht('Linting...'));
try {
$argv = $this->getPassthruArgumentsAsArgv('lint');
if ($repository_api->supportsCommitRanges()) {
$argv[] = '--rev';
$argv[] = $repository_api->getBaseCommit();
}
$lint_workflow = $this->buildChildWorkflow('lint', $argv);
if ($this->shouldAmend()) {
// TODO: We should offer to create a checkpoint commit.
$lint_workflow->setShouldAmendChanges(true);
}
$lint_result = $lint_workflow->run();
switch ($lint_result) {
case ArcanistLintWorkflow::RESULT_OKAY:
if ($this->getArgument('advice') &&
$lint_workflow->getUnresolvedMessages()) {
$this->getErrorExcuse(
'lint',
pht('Lint issued unresolved advice.'),
'lint-excuses');
} else {
$this->console->writeOut(
"<bg:green>** %s **</bg> %s\n",
pht('LINT OKAY'),
pht('No lint problems.'));
}
break;
case ArcanistLintWorkflow::RESULT_WARNINGS:
$this->getErrorExcuse(
'lint',
pht('Lint issued unresolved warnings.'),
'lint-excuses');
break;
case ArcanistLintWorkflow::RESULT_ERRORS:
$this->console->writeOut(
"<bg:red>** %s **</bg> %s\n",
pht('LINT ERRORS'),
pht('Lint raised errors!'));
$this->getErrorExcuse(
'lint',
pht('Lint issued unresolved errors!'),
'lint-excuses');
break;
}
$this->unresolvedLint = array();
foreach ($lint_workflow->getUnresolvedMessages() as $message) {
$this->unresolvedLint[] = $message->toDictionary();
}
return $lint_result;
} catch (ArcanistNoEngineException $ex) {
$this->console->writeOut(
"%s\n",
pht('No lint engine configured for this project.'));
} catch (ArcanistNoEffectException $ex) {
$this->console->writeOut("%s\n", $ex->getMessage());
}
return null;
}
/**
* @task lintunit
*/
private function runUnit() {
if ($this->getArgument('nounit') ||
$this->getArgument('only') ||
$this->isRawDiffSource() ||
$this->getArgument('head')) {
return ArcanistUnitWorkflow::RESULT_SKIP;
}
$repository_api = $this->getRepositoryAPI();
$this->console->writeOut("%s\n", pht('Running unit tests...'));
try {
$argv = $this->getPassthruArgumentsAsArgv('unit');
if ($repository_api->supportsCommitRanges()) {
$argv[] = '--rev';
$argv[] = $repository_api->getBaseCommit();
}
$unit_workflow = $this->buildChildWorkflow('unit', $argv);
$unit_result = $unit_workflow->run();
switch ($unit_result) {
case ArcanistUnitWorkflow::RESULT_OKAY:
$this->console->writeOut(
"<bg:green>** %s **</bg> %s\n",
pht('UNIT OKAY'),
pht('No unit test failures.'));
break;
case ArcanistUnitWorkflow::RESULT_UNSOUND:
if ($this->getArgument('ignore-unsound-tests')) {
echo phutil_console_format(
"<bg:yellow>** %s **</bg> %s\n",
pht('UNIT UNSOUND'),
pht(
'Unit testing raised errors, but all '.
'failing tests are unsound.'));
} else {
- $continue = $this->console->confirm(
+ $continue = phutil_console_confirm(
pht(
'Unit test results included failures, but all failing tests '.
'are known to be unsound. Ignore unsound test failures?'));
if (!$continue) {
throw new ArcanistUserAbortException();
}
}
break;
case ArcanistUnitWorkflow::RESULT_FAIL:
$this->console->writeOut(
"<bg:red>** %s **</bg> %s\n",
pht('UNIT ERRORS'),
pht('Unit testing raised errors!'));
$this->getErrorExcuse(
'unit',
pht('Unit test results include failures!'),
'unit-excuses');
break;
}
$this->testResults = array();
foreach ($unit_workflow->getTestResults() as $test) {
$this->testResults[] = $test->toDictionary();
}
return $unit_result;
} catch (ArcanistNoEngineException $ex) {
$this->console->writeOut(
"%s\n",
pht('No unit test engine is configured for this project.'));
} catch (ArcanistNoEffectException $ex) {
$this->console->writeOut("%s\n", $ex->getMessage());
}
return null;
}
public function getTestResults() {
return $this->testResults;
}
private function getSkipExcuse($prompt, $history) {
$excuse = $this->getArgument('excuse');
if ($excuse === null) {
$history = $this->getRepositoryAPI()->getScratchFilePath($history);
$excuse = phutil_console_prompt($prompt, $history);
if ($excuse == '') {
throw new ArcanistUserAbortException();
}
}
return $excuse;
}
private function getErrorExcuse($type, $prompt, $history) {
if ($this->getArgument('excuse')) {
$this->console->sendMessage(array(
'type' => $type,
'confirm' => $prompt.' '.pht('Ignore them?'),
));
return;
}
$history = $this->getRepositoryAPI()->getScratchFilePath($history);
$prompt .= ' '.
pht('Provide explanation to continue or press Enter to abort.');
$this->console->writeOut("\n\n%s", phutil_console_wrap($prompt));
$this->console->sendMessage(array(
'type' => $type,
'prompt' => pht('Explanation:'),
'history' => $history,
));
}
public function handleServerMessage(PhutilConsoleMessage $message) {
$data = $message->getData();
if ($this->getArgument('excuse')) {
try {
phutil_console_require_tty();
} catch (PhutilConsoleStdinNotInteractiveException $ex) {
$this->excuses[$data['type']] = $this->getArgument('excuse');
return null;
}
}
$response = '';
if (isset($data['prompt'])) {
$response = phutil_console_prompt($data['prompt'], idx($data, 'history'));
} else if (phutil_console_confirm($data['confirm'])) {
$response = $this->getArgument('excuse');
}
if ($response == '') {
throw new ArcanistUserAbortException();
}
$this->excuses[$data['type']] = $response;
return null;
}
/* -( Commit and Update Messages )----------------------------------------- */
/**
* @task message
*/
private function buildCommitMessage() {
if ($this->getArgument('preview') || $this->getArgument('only')) {
return null;
}
$is_create = $this->getArgument('create');
$is_update = $this->getArgument('update');
$is_raw = $this->isRawDiffSource();
$is_message = $this->getArgument('use-commit-message');
$is_verbatim = $this->getArgument('verbatim');
if ($is_message) {
return $this->getCommitMessageFromCommit($is_message);
}
if ($is_verbatim) {
return $this->getCommitMessageFromUser();
}
if (!$is_raw && !$is_create && !$is_update) {
$repository_api = $this->getRepositoryAPI();
$revisions = $repository_api->loadWorkingCopyDifferentialRevisions(
$this->getConduit(),
array(
'authors' => array($this->getUserPHID()),
'status' => 'status-open',
));
if (!$revisions) {
$is_create = true;
} else if (count($revisions) == 1) {
$revision = head($revisions);
$is_update = $revision['id'];
} else {
throw new ArcanistUsageException(
pht(
"There are several revisions which match the working copy:\n\n%s\n".
"Use '%s' to choose one, or '%s' to create a new revision.",
$this->renderRevisionList($revisions),
'--update',
'--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 ($is_update) {
$revision_id = $this->normalizeRevisionID($is_update);
if (!is_numeric($revision_id)) {
throw new ArcanistUsageException(
pht(
'Parameter to %s must be a Differential Revision number.',
'--update'));
}
return $this->getCommitMessageFromRevision($revision_id);
} else {
// This is --raw without enough info to create a revision, so force just
// a diff.
return null;
}
}
/**
* @task message
*/
private function getCommitMessageFromCommit($commit) {
$text = $this->getRepositoryAPI()->getCommitMessage($commit);
$message = ArcanistDifferentialCommitMessage::newFromRawCorpus($text);
$message->pullDataFromConduit($this->getConduit());
$this->validateCommitMessage($message);
return $message;
}
/**
* @task message
*/
private function getCommitMessageFromUser() {
$conduit = $this->getConduit();
$template = null;
if (!$this->getArgument('verbatim')) {
$saved = $this->readScratchFile('create-message');
if ($saved) {
$where = $this->getReadableScratchFilePath('create-message');
$preview = explode("\n", $saved);
$preview = array_shift($preview);
$preview = trim($preview);
$preview = id(new PhutilUTF8StringTruncator())
->setMaximumGlyphs(64)
->truncateString($preview);
if ($preview) {
$preview = pht('Message begins:')."\n\n {$preview}\n\n";
} else {
$preview = null;
}
echo pht(
"You have a saved revision message in '%s'.\n%s".
"You can use this message, or discard it.",
$where,
$preview);
$use = phutil_console_confirm(
pht('Do you want to use this message?'),
$default_no = false);
if ($use) {
$template = $saved;
} else {
$this->removeScratchFile('create-message');
}
}
}
$template_is_default = false;
$notes = array();
$included = array();
list($fields, $notes, $included_commits) = $this->getDefaultCreateFields();
if ($template) {
$fields = array();
$notes = array();
} else {
if (!$fields) {
$template_is_default = true;
}
if ($notes) {
$commit = head($this->getRepositoryAPI()->getLocalCommitInformation());
$template = $commit['message'];
} else {
$template = $conduit->callMethodSynchronous(
'differential.getcommitmessage',
array(
'revision_id' => null,
'edit' => 'create',
'fields' => $fields,
));
}
}
$old_message = $template;
$included = array();
if ($included_commits) {
foreach ($included_commits as $commit) {
$included[] = ' '.$commit;
}
if (!$this->isRawDiffSource()) {
$message = pht(
'Included commits in branch %s:',
$this->getRepositoryAPI()->getBranchName());
} else {
$message = pht('Included commits:');
}
$included = array_merge(
array(
'',
$message,
'',
),
$included);
}
$issues = array_merge(
array(
pht('NEW DIFFERENTIAL REVISION'),
pht('Describe the changes in this new revision.'),
),
$included,
array(
'',
pht(
'arc could not identify any existing revision in your working copy.'),
pht('If you intended to update an existing revision, use:'),
'',
' $ arc diff --update <revision>',
));
if ($notes) {
$issues = array_merge($issues, array(''), $notes);
}
$done = false;
$first = true;
while (!$done) {
$template = rtrim($template, "\r\n")."\n\n";
foreach ($issues as $issue) {
$template .= rtrim('# '.$issue)."\n";
}
$template .= "\n";
if ($first && $this->getArgument('verbatim') && !$template_is_default) {
$new_template = $template;
} else {
$new_template = $this->newInteractiveEditor($template)
->setName('new-commit')
->editInteractively();
}
$first = false;
if ($template_is_default && ($new_template == $template)) {
throw new ArcanistUsageException(pht('Template not edited.'));
}
$template = ArcanistCommentRemover::removeComments($new_template);
// With --raw-command, we may not have a repository API.
if ($this->hasRepositoryAPI()) {
$repository_api = $this->getRepositoryAPI();
// special check for whether to amend here. optimizes a common git
// workflow. we can't do this for mercurial because the mq extension
// is popular and incompatible with hg commit --amend ; see T2011.
$should_amend = (count($included_commits) == 1 &&
$repository_api instanceof ArcanistGitAPI &&
$this->shouldAmend());
} else {
$should_amend = false;
}
if ($should_amend) {
$wrote = (rtrim($old_message) != rtrim($template));
if ($wrote) {
$repository_api->amendCommit($template);
$where = pht('commit message');
}
} else {
$wrote = $this->writeScratchFile('create-message', $template);
$where = "'".$this->getReadableScratchFilePath('create-message')."'";
}
try {
$message = ArcanistDifferentialCommitMessage::newFromRawCorpus(
$template);
$message->pullDataFromConduit($conduit);
$this->validateCommitMessage($message);
$done = true;
} catch (ArcanistDifferentialCommitMessageParserException $ex) {
echo pht('Commit message has errors:')."\n\n";
$issues = array(pht('Resolve these errors:'));
foreach ($ex->getParserErrors() as $error) {
echo phutil_console_wrap("- ".$error."\n", 6);
$issues[] = ' - '.$error;
}
echo "\n";
echo pht('You must resolve these errors to continue.');
$again = phutil_console_confirm(
pht('Do you want to edit the message?'),
$default_no = false);
if ($again) {
// Keep going.
} else {
$saved = null;
if ($wrote) {
$saved = pht('A copy was saved to %s.', $where);
}
throw new ArcanistUsageException(
pht('Message has unresolved errors.')." {$saved}");
}
} catch (Exception $ex) {
if ($wrote) {
echo phutil_console_wrap(pht('(Message saved to %s.)', $where)."\n");
}
throw $ex;
}
}
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 getCommitMessageFromRevision($revision_id) {
$id = $revision_id;
$revision = $this->getConduit()->callMethodSynchronous(
'differential.query',
array(
'ids' => array($id),
));
$revision = head($revision);
if (!$revision) {
throw new ArcanistUsageException(
pht(
"Revision '%s' does not exist!",
$revision_id));
}
$this->checkRevisionOwnership($revision);
$message = $this->getConduit()->callMethodSynchronous(
'differential.getcommitmessage',
array(
'revision_id' => $id,
'edit' => false,
));
$this->commitMessageFromRevision = $message;
$obj = ArcanistDifferentialCommitMessage::newFromRawCorpus($message);
$obj->pullDataFromConduit($this->getConduit());
return $obj;
}
/**
* @task message
*/
private function validateCommitMessage(
ArcanistDifferentialCommitMessage $message) {
$futures = array();
$revision_id = $message->getRevisionID();
if ($revision_id) {
$futures['revision'] = $this->getConduit()->callMethod(
'differential.query',
array(
'ids' => array($revision_id),
));
}
$reviewers = $message->getFieldValue('reviewerPHIDs');
if ($reviewers) {
$futures['reviewers'] = $this->getConduit()->callMethod(
'user.query',
array(
'phids' => $reviewers,
));
}
foreach (new FutureIterator($futures) as $key => $future) {
$result = $future->resolve();
switch ($key) {
case 'revision':
if (empty($result)) {
throw new ArcanistUsageException(
pht(
'There is no revision %s.',
"D{$revision_id}"));
}
$this->checkRevisionOwnership(head($result));
break;
case 'reviewers':
$untils = array();
foreach ($result as $user) {
if (idx($user, 'currentStatus') == 'away') {
$untils[] = $user['currentStatusUntil'];
}
}
if (count($untils) == count($reviewers)) {
$until = date('l, M j Y', min($untils));
$confirm = pht(
'All reviewers are away until %s. Continue anyway?',
$until);
if (!phutil_console_confirm($confirm)) {
throw new ArcanistUsageException(
pht('Specify available reviewers and retry.'));
}
}
break;
}
}
}
/**
* @task message
*/
private function getUpdateMessage(array $fields, $template = '') {
if ($this->getArgument('raw')) {
throw new ArcanistUsageException(
pht(
"When using '%s' to update a revision, specify an update message ".
"with '%s'. (Normally, we'd launch an editor to ask you for a ".
"message, but can not do that because stdin is the diff source.)",
'--raw',
'--message'));
}
// 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. Similar things apply
// to Mercurial.
if ($template == '') {
$comments = $this->getDefaultUpdateMessage();
$template = sprintf(
"%s\n\n# %s\n#\n# %s\n# %s\n#\n# %s\n# $ %s\n\n",
rtrim($comments),
pht(
'Updating %s: %s',
"D{$fields['revisionID']}",
$fields['title']),
pht(
'Enter a brief description of the changes included in this update.'),
pht('The first line is used as subject, next lines as comment.'),
pht('If you intended to create a new revision, use:'),
'arc diff --create');
}
$comments = $this->newInteractiveEditor($template)
->setName('differential-update-comments')
->editInteractively();
return $comments;
}
private function getDefaultCreateFields() {
$result = array(array(), array(), array());
if ($this->isRawDiffSource()) {
return $result;
}
$repository_api = $this->getRepositoryAPI();
$local = $repository_api->getLocalCommitInformation();
if ($local) {
$result = $this->parseCommitMessagesIntoFields($local);
if ($this->getArgument('create')) {
unset($result[0]['revisionID']);
}
}
$result[0] = $this->dispatchWillBuildEvent($result[0]);
return $result;
}
/**
* Convert a list of commits from `getLocalCommitInformation()` into
* a format usable by arc to create a new diff. Specifically, we emit:
*
* - A dictionary of commit message fields.
* - A list of errors encountered while parsing the messages.
* - A human-readable list of the commits themselves.
*
* For example, if the user runs "arc diff HEAD^^^" and selects a diff range
* which includes several diffs, we attempt to merge them somewhat
* intelligently into a single message, because we can only send one
* "Summary:", "Reviewers:", etc., field to Differential. We also return
* errors (e.g., if the user typed a reviewer name incorrectly) and a
* summary of the commits themselves.
*
* @param dict Local commit information.
* @return list Complex output, see summary.
* @task message
*/
private function parseCommitMessagesIntoFields(array $local) {
$conduit = $this->getConduit();
$local = ipull($local, null, 'commit');
// If the user provided "--reviewers" or "--ccs", add a faux message to
// the list with the implied fields.
$faux_message = array();
if ($this->getArgument('reviewers')) {
$faux_message[] = pht('Reviewers: %s', $this->getArgument('reviewers'));
}
if ($this->getArgument('cc')) {
$faux_message[] = pht('CC: %s', $this->getArgument('cc'));
}
// See T12069. After T10312, the first line of a message is always parsed
// as a title. Add a placeholder so "Reviewers" and "CC" are never the
// first line.
$placeholder_title = pht('<placeholder>');
if ($faux_message) {
array_unshift($faux_message, $placeholder_title);
$faux_message = implode("\n\n", $faux_message);
$local = array(
'(Flags) ' => array(
'message' => $faux_message,
'summary' => pht('Command-Line Flags'),
),
) + $local;
}
// Build a human-readable list of the commits, so we can show the user which
// commits are included in the diff.
$included = array();
foreach ($local as $hash => $info) {
$included[] = substr($hash, 0, 12).' '.$info['summary'];
}
// Parse all of the messages into fields.
$messages = array();
foreach ($local as $hash => $info) {
$text = $info['message'];
$obj = ArcanistDifferentialCommitMessage::newFromRawCorpus($text);
$messages[$hash] = $obj;
}
$notes = array();
$fields = array();
foreach ($messages as $hash => $message) {
try {
$message->pullDataFromConduit($conduit, $partial = true);
$fields[$hash] = $message->getFields();
} catch (ArcanistDifferentialCommitMessageParserException $ex) {
if ($this->getArgument('verbatim')) {
// In verbatim mode, just bail when we hit an error. The user can
// rerun without --verbatim if they want to fix it manually. Most
// users will probably `git commit --amend` instead.
throw $ex;
}
$fields[$hash] = $message->getFields();
$frev = substr($hash, 0, 12);
$notes[] = pht(
'NOTE: commit %s could not be completely parsed:',
$frev);
foreach ($ex->getParserErrors() as $error) {
$notes[] = " - {$error}";
}
}
}
// Merge commit message fields. We do this somewhat-intelligently so that
// multiple "Reviewers" or "CC" fields will merge into the concatenation
// of all values.
// We have special parsing rules for 'title' because we can't merge
// multiple titles, and one-line commit messages like "fix stuff" will
// parse as titles. Instead, pick the first title we encounter. When we
// encounter subsequent titles, treat them as part of the summary. Then
// we merge all the summaries together below.
$result = array();
// Process fields in oldest-first order, so earlier commits get to set the
// title of record and reviewers/ccs are listed in chronological order.
$fields = array_reverse($fields);
foreach ($fields as $hash => $dict) {
$title = idx($dict, 'title');
if (!strlen($title)) {
continue;
}
if ($title === $placeholder_title) {
continue;
}
if (!isset($result['title'])) {
// We don't have a title yet, so use this one.
$result['title'] = $title;
} else {
// We already have a title, so merge this new title into the summary.
$summary = idx($dict, 'summary');
if ($summary) {
$summary = $title."\n\n".$summary;
} else {
$summary = $title;
}
$fields[$hash]['summary'] = $summary;
}
}
// Now, merge all the other fields in a general sort of way.
foreach ($fields as $hash => $dict) {
foreach ($dict as $key => $value) {
if ($key == 'title') {
// This has been handled above, and either assigned directly or
// merged into the summary.
continue;
}
if (is_array($value)) {
// For array values, merge the arrays, appending the new values.
// Examples are "Reviewers" and "Cc", where this produces a list of
// all users specified as reviewers.
$cur = idx($result, $key, array());
$new = array_merge($cur, $value);
$result[$key] = $new;
continue;
} else {
if (!strlen(trim($value))) {
// Ignore empty fields.
continue;
}
// For string values, append the new field to the old field with
// a blank line separating them. Examples are "Test Plan" and
// "Summary".
$cur = idx($result, $key, '');
if (strlen($cur)) {
$new = $cur."\n\n".$value;
} else {
$new = $value;
}
$result[$key] = $new;
}
}
}
return array($result, $notes, $included);
}
private function getDefaultUpdateMessage() {
if ($this->isRawDiffSource()) {
return null;
}
$repository_api = $this->getRepositoryAPI();
if ($repository_api instanceof ArcanistGitAPI) {
return $this->getGitUpdateMessage();
}
if ($repository_api instanceof ArcanistMercurialAPI) {
return $this->getMercurialUpdateMessage();
}
return null;
}
/**
* Retrieve the git messages between HEAD and the last update.
*
* @task message
*/
private function getGitUpdateMessage() {
$repository_api = $this->getRepositoryAPI();
$parser = $this->newDiffParser();
$commit_messages = $repository_api->getGitCommitLog();
$commit_messages = $parser->parseDiff($commit_messages);
if (count($commit_messages) == 1) {
// If there's only one message, assume this is an amend-based workflow and
// that using it to prefill doesn't make sense.
return null;
}
// We have more than one message, so figure out which ones are new. We
// do this by pulling the current diff and comparing commit hashes in the
// working copy with attached commit hashes. It's not super important that
// we always get this 100% right, we're just trying to do something
// reasonable.
$local = $this->loadActiveLocalCommitInfo();
$hashes = ipull($local, null, 'commit');
$usable = array();
foreach ($commit_messages as $message) {
$text = $message->getMetadata('message');
$parsed = ArcanistDifferentialCommitMessage::newFromRawCorpus($text);
if ($parsed->getRevisionID()) {
// If this is an amended commit message with a revision ID, it's
// certainly not new. Stop marking commits as usable and break out.
break;
}
if (isset($hashes[$message->getCommitHash()])) {
// If this commit is currently part of the diff, stop using commit
// messages, since anything older than this isn't new.
break;
}
// Otherwise, this looks new, so it's a usable commit message.
$usable[] = $text;
}
if (!$usable) {
// No new commit messages, so we don't have anywhere to start from.
return null;
}
return $this->formatUsableLogs($usable);
}
/**
* Retrieve the hg messages between tip and the last update.
*
* @task message
*/
private function getMercurialUpdateMessage() {
$repository_api = $this->getRepositoryAPI();
$messages = $repository_api->getCommitMessageLog();
if (count($messages) == 1) {
// If there's only one message, assume this is an amend-based workflow and
// that using it to prefill doesn't make sense.
return null;
}
$local = $this->loadActiveLocalCommitInfo();
$hashes = ipull($local, null, 'commit');
$usable = array();
foreach ($messages as $rev => $message) {
if (isset($hashes[$rev])) {
// If this commit is currently part of the active diff on the revision,
// stop using commit messages, since anything older than this isn't new.
break;
}
// Otherwise, this looks new, so it's a usable commit message.
$usable[] = $message;
}
if (!$usable) {
// No new commit messages, so we don't have anywhere to start from.
return null;
}
return $this->formatUsableLogs($usable);
}
/**
* Format log messages to prefill a diff update.
*
* @task message
*/
private function formatUsableLogs(array $usable) {
// Flip messages so they'll read chronologically (oldest-first) in the
// template, e.g.:
//
// - Added foobar.
// - Fixed foobar bug.
// - Documented foobar.
$usable = array_reverse($usable);
$default = array();
foreach ($usable as $message) {
// Pick the first line out of each message.
$text = trim($message);
$text = head(explode("\n", $text));
$default[] = ' - '.$text."\n";
}
return implode('', $default);
}
private function loadActiveLocalCommitInfo() {
$current_diff = $this->getConduit()->callMethodSynchronous(
'differential.querydiffs',
array(
'revisionIDs' => array($this->revisionID),
));
$current_diff = head($current_diff);
$properties = idx($current_diff, 'properties', array());
return idx($properties, 'local:commits', array());
}
/* -( 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',
);
return idx($map, $unit_result, 'none');
}
/**
* @task diffspec
*/
private function buildDiffSpecification() {
$base_revision = null;
$base_path = null;
$vcs = null;
$repo_uuid = null;
$parent = null;
$source_path = null;
$branch = null;
$bookmark = null;
if (!$this->isRawDiffSource()) {
$repository_api = $this->getRepositoryAPI();
$base_revision = $repository_api->getSourceControlBaseRevision();
$base_path = $repository_api->getSourceControlPath();
$vcs = $repository_api->getSourceControlSystemName();
$source_path = $repository_api->getPath();
$branch = $repository_api->getBranchName();
$repo_uuid = $repository_api->getRepositoryUUID();
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 ArcanistMercurialAPI) {
$bookmark = $repository_api->getActiveBookmark();
$svn_info = $repository_api->getSubversionInfo();
$repo_uuid = idx($svn_info, 'uuid');
$base_path = idx($svn_info, 'base_path', $base_path);
$base_revision = idx($svn_info, 'base_revision', $base_revision);
// TODO: provide parent info
}
}
$data = array(
'sourceMachine' => php_uname('n'),
'sourcePath' => $source_path,
'branch' => $branch,
'bookmark' => $bookmark,
'sourceControlSystem' => $vcs,
'sourceControlPath' => $base_path,
'sourceControlBaseRevision' => $base_revision,
'creationMethod' => 'arc',
);
if (!$this->isRawDiffSource()) {
$repository_phid = $this->getRepositoryPHID();
if ($repository_phid) {
$data['repositoryPHID'] = $repository_phid;
}
}
return $data;
}
/* -( Diff Properties )---------------------------------------------------- */
/**
* Update lint information for the diff.
*
* @return void
*
* @task diffprop
*/
private function updateLintDiffProperty() {
if (strlen($this->excuses['lint'])) {
$this->updateDiffProperty(
'arc:lint-excuse',
json_encode($this->excuses['lint']));
}
if (!$this->hitAutotargets) {
if ($this->unresolvedLint) {
$this->updateDiffProperty(
'arc:lint',
json_encode($this->unresolvedLint));
}
}
}
/**
* Update unit test information for the diff.
*
* @return void
*
* @task diffprop
*/
private function updateUnitDiffProperty() {
if (strlen($this->excuses['unit'])) {
$this->updateDiffProperty('arc:unit-excuse',
json_encode($this->excuses['unit']));
}
if (!$this->hitAutotargets) {
if ($this->testResults) {
$this->updateDiffProperty('arc:unit', json_encode($this->testResults));
}
}
}
/**
* Update local commit information for the diff.
*
* @task diffprop
*/
private function updateLocalDiffProperty() {
if ($this->isRawDiffSource()) {
return;
}
$local_info = $this->getRepositoryAPI()->getLocalCommitInformation();
if (!$local_info) {
return;
}
$this->updateDiffProperty('local:commits', json_encode($local_info));
}
private function updateOntoDiffProperty() {
$onto = $this->getDiffOntoTargets();
if (!$onto) {
return;
}
$this->updateDiffProperty('arc:onto', json_encode($onto));
}
private function getDiffOntoTargets() {
if ($this->isRawDiffSource()) {
return null;
}
$api = $this->getRepositoryAPI();
if (!($api instanceof ArcanistGitAPI)) {
return null;
}
// If we track an upstream branch either directly or indirectly, use that.
$branch = $api->getBranchName();
if (strlen($branch)) {
$upstream_path = $api->getPathToUpstream($branch);
$remote_branch = $upstream_path->getRemoteBranchName();
if (strlen($remote_branch)) {
return array(
array(
'type' => 'branch',
'name' => $remote_branch,
'kind' => 'upstream',
),
);
}
}
// If "arc.land.onto.default" is configured, use that.
$config_key = 'arc.land.onto.default';
$onto = $this->getConfigFromAnySource($config_key);
if (strlen($onto)) {
return array(
array(
'type' => 'branch',
'name' => $onto,
'kind' => 'arc.land.onto.default',
),
);
}
return null;
}
/**
* 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->diffPropertyFutures[] = $this->getConduit()->callMethod(
'differential.setdiffproperty',
array(
'diff_id' => $this->getDiffID(),
'name' => $name,
'data' => $data,
));
}
/**
* Wait for finishing all diff property updates.
*
* @return void
*
* @task diffprop
*/
private function resolveDiffPropertyUpdates() {
id(new FutureIterator($this->diffPropertyFutures))
->resolveAll();
$this->diffPropertyFutures = array();
}
private function dispatchWillCreateRevisionEvent(array $fields) {
$event = $this->dispatchEvent(
ArcanistEventType::TYPE_REVISION_WILLCREATEREVISION,
array(
'specification' => $fields,
));
return $event->getValue('specification');
}
private function dispatchWillBuildEvent(array $fields) {
$event = $this->dispatchEvent(
ArcanistEventType::TYPE_DIFF_WILLBUILDMESSAGE,
array(
'fields' => $fields,
));
return $event->getValue('fields');
}
private function checkRevisionOwnership(array $revision) {
if ($revision['authorPHID'] == $this->getUserPHID()) {
return;
}
$id = $revision['id'];
$title = $revision['title'];
$prompt = pht(
"You don't own revision %s: \"%s\". Normally, you should ".
"only update revisions you own. You can \"Commandeer\" this revision ".
"from the web interface if you want to become the owner.\n\n".
"Update this revision anyway?",
"D{$id}",
$title);
$ok = phutil_console_confirm($prompt, $default_no = true);
if (!$ok) {
throw new ArcanistUsageException(
pht('Aborted update of revision: You are not the owner.'));
}
}
/* -( File Uploads )------------------------------------------------------- */
private function uploadFilesForChanges(array $changes) {
assert_instances_of($changes, 'ArcanistDiffChange');
// Collect all the files we need to upload.
$need_upload = array();
foreach ($changes as $key => $change) {
if ($change->getFileType() != ArcanistDiffChangeType::FILE_BINARY) {
continue;
}
if ($this->getArgument('skip-binaries')) {
continue;
}
$name = basename($change->getCurrentPath());
$need_upload[] = array(
'type' => 'old',
'name' => $name,
'data' => $change->getOriginalFileData(),
'change' => $change,
);
$need_upload[] = array(
'type' => 'new',
'name' => $name,
'data' => $change->getCurrentFileData(),
'change' => $change,
);
}
if (!$need_upload) {
return;
}
// Determine mime types and file sizes. Update changes from "binary" to
// "image" if the file is an image. Set image metadata.
$type_image = ArcanistDiffChangeType::FILE_IMAGE;
foreach ($need_upload as $key => $spec) {
$change = $need_upload[$key]['change'];
if ($spec['data'] === null) {
// This covers the case where a file was added or removed; we don't
// need to upload the other half of it (e.g., the old file data for
// a file which was just added). This is distinct from an empty
// file, which we do upload.
unset($need_upload[$key]);
continue;
}
$type = $spec['type'];
$size = strlen($spec['data']);
$change->setMetadata("{$type}:file:size", $size);
$mime = $this->getFileMimeType($spec['data']);
if (preg_match('@^image/@', $mime)) {
$change->setFileType($type_image);
}
$change->setMetadata("{$type}:file:mime-type", $mime);
}
$uploader = id(new ArcanistFileUploader())
->setConduitClient($this->getConduit());
foreach ($need_upload as $key => $spec) {
$ref = id(new ArcanistFileDataRef())
->setName($spec['name'])
->setData($spec['data']);
$uploader->addFile($ref, $key);
}
$files = $uploader->uploadFiles();
$errors = false;
foreach ($files as $key => $file) {
if ($file->getErrors()) {
unset($files[$key]);
$errors = true;
echo pht(
'Failed to upload binary "%s".',
$file->getName());
}
}
if ($errors) {
$prompt = pht('Continue?');
$ok = phutil_console_confirm($prompt, $default_no = false);
if (!$ok) {
throw new ArcanistUsageException(
pht(
'Aborted due to file upload failure. You can use %s '.
'to skip binary uploads.',
'--skip-binaries'));
}
}
foreach ($files as $key => $file) {
$spec = $need_upload[$key];
$phid = $file->getPHID();
$change = $spec['change'];
$type = $spec['type'];
$change->setMetadata("{$type}:binary-phid", $phid);
echo pht('Uploaded binary data for "%s".', $file->getName())."\n";
}
echo pht('Upload complete.')."\n";
}
private function getFileMimeType($data) {
$tmp = new TempFile();
Filesystem::writeFile($tmp, $data);
return Filesystem::getMimeType($tmp);
}
private function shouldOpenCreatedObjectsInBrowser() {
return $this->getArgument('browse');
}
private function submitChangesToStagingArea($id) {
$result = $this->pushChangesToStagingArea($id);
// We'll either get a failure constant on error, or a list of pushed
// refs on success.
$ok = is_array($result);
if ($ok) {
$staging = array(
'status' => self::STAGING_PUSHED,
'refs' => $result,
);
} else {
$staging = array(
'status' => $result,
'refs' => array(),
);
}
$this->updateDiffProperty(
'arc.staging',
phutil_json_encode($staging));
}
private function pushChangesToStagingArea($id) {
if ($this->getArgument('skip-staging')) {
$this->writeInfo(
pht('SKIP STAGING'),
pht('Flag --skip-staging was specified.'));
return self::STAGING_USER_SKIP;
}
if ($this->isRawDiffSource()) {
$this->writeInfo(
pht('SKIP STAGING'),
pht('Raw changes can not be pushed to a staging area.'));
return self::STAGING_DIFF_RAW;
}
if (!$this->getRepositoryPHID()) {
$this->writeInfo(
pht('SKIP STAGING'),
pht('Unable to determine repository for this change.'));
return self::STAGING_REPOSITORY_UNKNOWN;
}
$staging = $this->getRepositoryStagingConfiguration();
if ($staging === null) {
$this->writeInfo(
pht('SKIP STAGING'),
pht('The server does not support staging areas.'));
return self::STAGING_REPOSITORY_UNAVAILABLE;
}
$supported = idx($staging, 'supported');
if (!$supported) {
$this->writeInfo(
pht('SKIP STAGING'),
pht('Phabricator does not support staging areas for this repository.'));
return self::STAGING_REPOSITORY_UNSUPPORTED;
}
$staging_uri = idx($staging, 'uri');
if (!$staging_uri) {
$this->writeInfo(
pht('SKIP STAGING'),
pht('No staging area is configured for this repository.'));
return self::STAGING_REPOSITORY_UNCONFIGURED;
}
$api = $this->getRepositoryAPI();
if (!($api instanceof ArcanistGitAPI)) {
$this->writeInfo(
pht('SKIP STAGING'),
pht('This client version does not support staging this repository.'));
return self::STAGING_CLIENT_UNSUPPORTED;
}
$commit = $api->getHeadCommit();
$prefix = idx($staging, 'prefix', 'phabricator');
$base_tag = "refs/tags/{$prefix}/base/{$id}";
$diff_tag = "refs/tags/{$prefix}/diff/{$id}";
$this->writeOkay(
pht('PUSH STAGING'),
pht('Pushing changes to staging area...'));
$push_flags = array();
if (version_compare($api->getGitVersion(), '1.8.2', '>=')) {
$push_flags[] = '--no-verify';
}
$refs = array();
$remote = array(
'uri' => $staging_uri,
);
// If the base commit is a real commit, we're going to push it. We don't
// use this, but pushing it to a ref reduces the amount of redundant work
// that Git does on later pushes by helping it figure out that the remote
// already has most of the history. See T10509.
// In the future, we could avoid this push if the staging area is the same
// as the main repository, or if the staging area is a virtual repository.
// In these cases, the staging area should automatically have up-to-date
// refs.
$base_commit = $api->getSourceControlBaseRevision();
if ($base_commit !== ArcanistGitAPI::GIT_MAGIC_ROOT_COMMIT) {
$refs[] = array(
'ref' => $base_tag,
'type' => 'base',
'commit' => $base_commit,
'remote' => $remote,
);
}
// We're always going to push the change itself.
$refs[] = array(
'ref' => $diff_tag,
'type' => 'diff',
'commit' => $commit,
'remote' => $remote,
);
$ref_list = array();
foreach ($refs as $ref) {
$ref_list[] = $ref['commit'].':'.$ref['ref'];
}
$err = phutil_passthru(
'git push %Ls -- %s %Ls',
$push_flags,
$staging_uri,
$ref_list);
if ($err) {
$this->writeWarn(
pht('STAGING FAILED'),
pht('Unable to push changes to the staging area.'));
throw new ArcanistUsageException(
pht(
'Failed to push changes to staging area. Correct the issue, or '.
'use --skip-staging to skip this step.'));
}
return $refs;
}
/**
* Try to upload lint and unit test results into modern Harbormaster build
* targets.
*
* @return bool True if everything was uploaded to build targets.
*/
private function updateAutotargets($diff_phid, $unit_result) {
$lint_key = 'arcanist.lint';
$unit_key = 'arcanist.unit';
try {
$result = $this->getConduit()->callMethodSynchronous(
'harbormaster.queryautotargets',
array(
'objectPHID' => $diff_phid,
'targetKeys' => array(
$lint_key,
$unit_key,
),
));
$targets = idx($result, 'targetMap', array());
} catch (Exception $ex) {
return false;
}
$futures = array();
$lint_target = idx($targets, $lint_key);
if ($lint_target) {
$lint = nonempty($this->unresolvedLint, array());
foreach ($lint as $key => $message) {
$lint[$key] = $this->getModernLintDictionary($message);
}
// Consider this target to have failed if there are any unresolved
// errors or warnings.
$type = 'pass';
foreach ($lint as $message) {
switch (idx($message, 'severity')) {
case ArcanistLintSeverity::SEVERITY_WARNING:
case ArcanistLintSeverity::SEVERITY_ERROR:
$type = 'fail';
break;
}
}
$futures[] = $this->getConduit()->callMethod(
'harbormaster.sendmessage',
array(
'buildTargetPHID' => $lint_target,
'lint' => array_values($lint),
'type' => $type,
));
}
$unit_target = idx($targets, $unit_key);
if ($unit_target) {
$unit = nonempty($this->testResults, array());
foreach ($unit as $key => $message) {
$unit[$key] = $this->getModernUnitDictionary($message);
}
$type = ArcanistUnitWorkflow::getHarbormasterTypeFromResult($unit_result);
$futures[] = $this->getConduit()->callMethod(
'harbormaster.sendmessage',
array(
'buildTargetPHID' => $unit_target,
'unit' => array_values($unit),
'type' => $type,
));
}
try {
foreach (new FutureIterator($futures) as $future) {
$future->resolve();
}
return true;
} catch (Exception $ex) {
// TODO: Eventually, we should expect these to succeed if we get this
// far, but just log errors for now.
phlog($ex);
return false;
}
}
}
diff --git a/src/workflow/ArcanistLandWorkflow.php b/src/workflow/ArcanistLandWorkflow.php
index 66ce42d5..5f13683a 100644
--- a/src/workflow/ArcanistLandWorkflow.php
+++ b/src/workflow/ArcanistLandWorkflow.php
@@ -1,1450 +1,1450 @@
<?php
/**
* Lands a branch by rebasing, merging and amending it.
*/
final class ArcanistLandWorkflow extends ArcanistWorkflow {
private $isGit;
private $isGitSvn;
private $isHg;
private $isHgSvn;
private $oldBranch;
private $branch;
private $onto;
private $ontoRemoteBranch;
private $remote;
private $useSquash;
private $keepBranch;
private $branchType;
private $ontoType;
private $preview;
private $revision;
private $messageFile;
const REFTYPE_BRANCH = 'branch';
const REFTYPE_BOOKMARK = 'bookmark';
public function getRevisionDict() {
return $this->revision;
}
public function getWorkflowName() {
return 'land';
}
public function getCommandSynopses() {
return phutil_console_format(<<<EOTEXT
**land** [__options__] [__ref__]
EOTEXT
);
}
public function getCommandHelp() {
return phutil_console_format(<<<EOTEXT
Supports: git, hg
Publish an accepted revision after review. This command is the last
step in the standard Differential pre-publish code review workflow.
This command merges and pushes changes associated with an accepted
revision that are currently sitting in __ref__, which is usually the
name of a local branch. Without __ref__, the current working copy
state will be used.
Under Git: branches, tags, and arbitrary commits (detached HEADs)
may be landed.
Under Mercurial: branches and bookmarks may be landed, but only
onto a target of the same type. See T3855.
The workflow selects a target branch to land onto and a remote where
the change will be pushed to.
A target branch is selected by examining these sources in order:
- the **--onto** flag;
- the upstream of the current branch, recursively (Git only);
- the __arc.land.onto.default__ configuration setting;
- or by falling back to a standard default:
- "master" in Git;
- "default" in Mercurial.
A remote is selected by examining these sources in order:
- the **--remote** flag;
- the upstream of the current branch, recursively (Git only);
- or by falling back to a standard default:
- "origin" in Git;
- the default remote in Mercurial.
After selecting a target branch and a remote, the commits which will
be landed are printed.
With **--preview**, execution stops here, before the change is
merged.
The change is merged with the changes in the target branch,
following these rules:
In repositories with mutable history or with **--squash**, this will
perform a squash merge (the entire branch will be represented as one
commit after the merge).
In repositories with immutable history or with **--merge**, this will
perform a strict merge (a merge commit will always be created, and
local commits will be preserved).
The resulting commit will be given an up-to-date commit message
describing the final state of the revision in Differential.
In Git, the merge occurs in a detached HEAD. The local branch
reference (if one exists) is not updated yet.
With **--hold**, execution stops here, before the change is pushed.
The change is pushed into the remote.
Consulting mystical sources of power, the workflow makes a guess
about what state you wanted to end up in after the process finishes
and the working copy is put into that state.
The branch which was landed is deleted, unless the **--keep-branch**
flag was passed or the landing branch is the same as the target
branch.
EOTEXT
);
}
public function requiresWorkingCopy() {
return true;
}
public function requiresConduit() {
return true;
}
public function requiresAuthentication() {
return true;
}
public function requiresRepositoryAPI() {
return true;
}
public function getArguments() {
return array(
'onto' => array(
'param' => 'master',
'help' => pht(
"Land feature branch onto a branch other than the default ".
"('master' in git, 'default' in hg). You can change the default ".
"by setting '%s' with `%s` or for the entire project in %s.",
'arc.land.onto.default',
'arc set-config',
'.arcconfig'),
),
'hold' => array(
'help' => pht(
'Prepare the change to be pushed, but do not actually push it.'),
),
'keep-branch' => array(
'help' => pht(
'Keep the feature branch after pushing changes to the '.
'remote (by default, it is deleted).'),
),
'remote' => array(
'param' => 'origin',
'help' => pht(
"Push to a remote other than the default ('origin' in git)."),
),
'merge' => array(
'help' => pht(
'Perform a %s merge, not a %s merge. If the project '.
'is marked as having an immutable history, this is the default '.
'behavior.',
'--no-ff',
'--squash'),
'supports' => array(
'git',
),
'nosupport' => array(
'hg' => pht(
'Use the %s strategy when landing in mercurial.',
'--squash'),
),
),
'squash' => array(
'help' => pht(
'Perform a %s merge, not a %s merge. If the project is '.
'marked as having a mutable history, this is the default behavior.',
'--squash',
'--no-ff'),
'conflicts' => array(
'merge' => pht(
'%s and %s are conflicting merge strategies.',
'--merge',
'--squash'),
),
),
'delete-remote' => array(
'help' => pht(
'Delete the feature branch in the remote after landing it.'),
'conflicts' => array(
'keep-branch' => true,
),
'supports' => array(
'hg',
),
),
'revision' => array(
'param' => 'id',
'help' => pht(
'Use the message from a specific revision, rather than '.
'inferring the revision based on branch content.'),
),
'preview' => array(
'help' => pht(
'Prints the commits that would be landed. Does not '.
'actually modify or land the commits.'),
),
'*' => 'branch',
);
}
public function run() {
$this->readArguments();
$engine = null;
if ($this->isGit && !$this->isGitSvn) {
$engine = new ArcanistGitLandEngine();
}
if ($engine) {
$this->readEngineArguments();
$this->requireCleanWorkingCopy();
$should_hold = $this->getArgument('hold');
$engine
->setWorkflow($this)
->setRepositoryAPI($this->getRepositoryAPI())
->setSourceRef($this->branch)
->setTargetRemote($this->remote)
->setTargetOnto($this->onto)
->setShouldHold($should_hold)
->setShouldKeep($this->keepBranch)
->setShouldSquash($this->useSquash)
->setShouldPreview($this->preview)
->setBuildMessageCallback(array($this, 'buildEngineMessage'));
$engine->execute();
if (!$should_hold && !$this->preview) {
$this->didPush();
}
return 0;
}
$this->validate();
try {
$this->pullFromRemote();
} catch (Exception $ex) {
$this->restoreBranch();
throw $ex;
}
$this->printPendingCommits();
if ($this->preview) {
$this->restoreBranch();
return 0;
}
$this->checkoutBranch();
$this->findRevision();
if ($this->useSquash) {
$this->rebase();
$this->squash();
} else {
$this->merge();
}
$this->push();
if (!$this->keepBranch) {
$this->cleanupBranch();
}
if ($this->oldBranch != $this->onto) {
// If we were on some branch A and the user ran "arc land B",
// switch back to A.
if ($this->keepBranch || $this->oldBranch != $this->branch) {
$this->restoreBranch();
}
}
echo pht('Done.'), "\n";
return 0;
}
private function getUpstreamMatching($branch, $pattern) {
if ($this->isGit) {
$repository_api = $this->getRepositoryAPI();
list($err, $fullname) = $repository_api->execManualLocal(
'rev-parse --symbolic-full-name %s@{upstream}',
$branch);
if (!$err) {
$matches = null;
if (preg_match($pattern, $fullname, $matches)) {
return last($matches);
}
}
}
return null;
}
private function readEngineArguments() {
// NOTE: This is hard-coded for Git right now.
// TODO: Clean this up and move it into LandEngines.
$onto = $this->getEngineOnto();
$remote = $this->getEngineRemote();
// This just overwrites work we did earlier, but it has to be up in this
// class for now because other parts of the workflow still depend on it.
$this->onto = $onto;
$this->remote = $remote;
$this->ontoRemoteBranch = $this->remote.'/'.$onto;
}
private function getEngineOnto() {
$onto = $this->getArgument('onto');
if ($onto !== null) {
$this->writeInfo(
pht('TARGET'),
pht(
'Landing onto "%s", selected by the --onto flag.',
$onto));
return $onto;
}
$api = $this->getRepositoryAPI();
$path = $api->getPathToUpstream($this->branch);
if ($path->getLength()) {
$cycle = $path->getCycle();
if ($cycle) {
$this->writeWarn(
pht('LOCAL CYCLE'),
pht(
'Local branch tracks an upstream, but following it leads to a '.
'local cycle; ignoring branch upstream.'));
echo tsprintf(
"\n %s\n\n",
implode(' -> ', $cycle));
} else {
if ($path->isConnectedToRemote()) {
$onto = $path->getRemoteBranchName();
$this->writeInfo(
pht('TARGET'),
pht(
'Landing onto "%s", selected by following tracking branches '.
'upstream to the closest remote.',
$onto));
return $onto;
} else {
$this->writeInfo(
pht('NO PATH TO UPSTREAM'),
pht(
'Local branch tracks an upstream, but there is no path '.
'to a remote; ignoring branch upstream.'));
}
}
}
$config_key = 'arc.land.onto.default';
$onto = $this->getConfigFromAnySource($config_key);
if ($onto !== null) {
$this->writeInfo(
pht('TARGET'),
pht(
'Landing onto "%s", selected by "%s" configuration.',
$onto,
$config_key));
return $onto;
}
$onto = 'master';
$this->writeInfo(
pht('TARGET'),
pht(
'Landing onto "%s", the default target under git.',
$onto));
return $onto;
}
private function getEngineRemote() {
$remote = $this->getArgument('remote');
if ($remote !== null) {
$this->writeInfo(
pht('REMOTE'),
pht(
'Using remote "%s", selected by the --remote flag.',
$remote));
return $remote;
}
$api = $this->getRepositoryAPI();
$path = $api->getPathToUpstream($this->branch);
$remote = $path->getRemoteRemoteName();
if ($remote !== null) {
$this->writeInfo(
pht('REMOTE'),
pht(
'Using remote "%s", selected by following tracking branches '.
'upstream to the closest remote.',
$remote));
return $remote;
}
$remote = 'origin';
$this->writeInfo(
pht('REMOTE'),
pht(
'Using remote "%s", the default remote under git.',
$remote));
return $remote;
}
private function readArguments() {
$repository_api = $this->getRepositoryAPI();
$this->isGit = $repository_api instanceof ArcanistGitAPI;
$this->isHg = $repository_api instanceof ArcanistMercurialAPI;
if ($this->isGit) {
$repository = $this->loadProjectRepository();
$this->isGitSvn = (idx($repository, 'vcs') == 'svn');
}
if ($this->isHg) {
$this->isHgSvn = $repository_api->isHgSubversionRepo();
}
$branch = $this->getArgument('branch');
if (empty($branch)) {
$branch = $this->getBranchOrBookmark();
if ($branch) {
$this->branchType = $this->getBranchType($branch);
// TODO: This message is misleading when landing a detached head or
// a tag in Git.
echo pht("Landing current %s '%s'.", $this->branchType, $branch), "\n";
$branch = array($branch);
}
}
if (count($branch) !== 1) {
throw new ArcanistUsageException(
pht('Specify exactly one branch or bookmark to land changes from.'));
}
$this->branch = head($branch);
$this->keepBranch = $this->getArgument('keep-branch');
$this->preview = $this->getArgument('preview');
if (!$this->branchType) {
$this->branchType = $this->getBranchType($this->branch);
}
$onto_default = $this->isGit ? 'master' : 'default';
$onto_default = nonempty(
$this->getConfigFromAnySource('arc.land.onto.default'),
$onto_default);
$onto_default = coalesce(
$this->getUpstreamMatching($this->branch, '/^refs\/heads\/(.+)$/'),
$onto_default);
$this->onto = $this->getArgument('onto', $onto_default);
$this->ontoType = $this->getBranchType($this->onto);
$remote_default = $this->isGit ? 'origin' : '';
$remote_default = coalesce(
$this->getUpstreamMatching($this->onto, '/^refs\/remotes\/(.+?)\//'),
$remote_default);
$this->remote = $this->getArgument('remote', $remote_default);
if ($this->getArgument('merge')) {
$this->useSquash = false;
} else if ($this->getArgument('squash')) {
$this->useSquash = true;
} else {
$this->useSquash = !$this->isHistoryImmutable();
}
$this->ontoRemoteBranch = $this->onto;
if ($this->isGitSvn) {
$this->ontoRemoteBranch = 'trunk';
} else if ($this->isGit) {
$this->ontoRemoteBranch = $this->remote.'/'.$this->onto;
}
$this->oldBranch = $this->getBranchOrBookmark();
}
private function validate() {
$repository_api = $this->getRepositoryAPI();
if ($this->onto == $this->branch) {
$message = pht(
"You can not land a %s onto itself -- you are trying ".
"to land '%s' onto '%s'. For more information on how to push ".
"changes, see 'Pushing and Closing Revisions' in 'Arcanist User ".
"Guide: arc diff' in the documentation.",
$this->branchType,
$this->branch,
$this->onto);
if (!$this->isHistoryImmutable()) {
$message .= ' '.pht("You may be able to '%s' instead.", 'arc amend');
}
throw new ArcanistUsageException($message);
}
if ($this->isHg) {
if ($this->useSquash) {
if (!$repository_api->supportsRebase()) {
throw new ArcanistUsageException(
pht(
'You must enable the rebase extension to use the %s strategy.',
'--squash'));
}
}
if ($this->branchType != $this->ontoType) {
throw new ArcanistUsageException(pht(
'Source %s is a %s but destination %s is a %s. When landing a '.
'%s, the destination must also be a %s. Use %s to specify a %s, '.
'or set %s in %s.',
$this->branch,
$this->branchType,
$this->onto,
$this->ontoType,
$this->branchType,
$this->branchType,
'--onto',
$this->branchType,
'arc.land.onto.default',
'.arcconfig'));
}
}
if ($this->isGit) {
list($err) = $repository_api->execManualLocal(
'rev-parse --verify %s',
$this->branch);
if ($err) {
throw new ArcanistUsageException(
pht("Branch '%s' does not exist.", $this->branch));
}
}
$this->requireCleanWorkingCopy();
}
private function checkoutBranch() {
$repository_api = $this->getRepositoryAPI();
if ($this->getBranchOrBookmark() != $this->branch) {
$repository_api->execxLocal('checkout %s', $this->branch);
}
switch ($this->branchType) {
case self::REFTYPE_BOOKMARK:
$message = pht(
'Switched to bookmark **%s**. Identifying and merging...',
$this->branch);
break;
case self::REFTYPE_BRANCH:
default:
$message = pht(
'Switched to branch **%s**. Identifying and merging...',
$this->branch);
break;
}
echo phutil_console_format($message."\n");
}
private function printPendingCommits() {
$repository_api = $this->getRepositoryAPI();
if ($repository_api instanceof ArcanistGitAPI) {
list($out) = $repository_api->execxLocal(
'log --oneline %s %s --',
$this->branch,
'^'.$this->onto);
} else if ($repository_api instanceof ArcanistMercurialAPI) {
$common_ancestor = $repository_api->getCanonicalRevisionName(
hgsprintf('ancestor(%s,%s)',
$this->onto,
$this->branch));
$branch_range = hgsprintf(
'reverse((%s::%s) - %s)',
$common_ancestor,
$this->branch,
$common_ancestor);
list($out) = $repository_api->execxLocal(
'log -r %s --template %s',
$branch_range,
'{node|short} {desc|firstline}\n');
}
if (!trim($out)) {
$this->restoreBranch();
throw new ArcanistUsageException(
pht('No commits to land from %s.', $this->branch));
}
echo pht("The following commit(s) will be landed:\n\n%s", $out), "\n";
}
private function findRevision() {
$repository_api = $this->getRepositoryAPI();
$this->parseBaseCommitArgument(array($this->ontoRemoteBranch));
$revision_id = $this->getArgument('revision');
if ($revision_id) {
$revision_id = $this->normalizeRevisionID($revision_id);
$revisions = $this->getConduit()->callMethodSynchronous(
'differential.query',
array(
'ids' => array($revision_id),
));
if (!$revisions) {
throw new ArcanistUsageException(pht(
"No such revision '%s'!",
"D{$revision_id}"));
}
} else {
$revisions = $repository_api->loadWorkingCopyDifferentialRevisions(
$this->getConduit(),
array());
}
if (!count($revisions)) {
throw new ArcanistUsageException(pht(
"arc can not identify which revision exists on %s '%s'. Update the ".
"revision with recent changes to synchronize the %s name and hashes, ".
"or use '%s' to amend the commit message at HEAD, or use ".
"'%s' to select a revision explicitly.",
$this->branchType,
$this->branch,
$this->branchType,
'arc amend',
'--revision <id>'));
} else if (count($revisions) > 1) {
switch ($this->branchType) {
case self::REFTYPE_BOOKMARK:
$message = pht(
"There are multiple revisions on feature bookmark '%s' which are ".
"not present on '%s':\n\n".
"%s\n".
'Separate these revisions onto different bookmarks, or use '.
'--revision <id> to use the commit message from <id> '.
'and land them all.',
$this->branch,
$this->onto,
$this->renderRevisionList($revisions));
break;
case self::REFTYPE_BRANCH:
default:
$message = pht(
"There are multiple revisions on feature branch '%s' which are ".
"not present on '%s':\n\n".
"%s\n".
'Separate these revisions onto different branches, or use '.
'--revision <id> to use the commit message from <id> '.
'and land them all.',
$this->branch,
$this->onto,
$this->renderRevisionList($revisions));
break;
}
throw new ArcanistUsageException($message);
}
$this->revision = head($revisions);
$rev_status = $this->revision['status'];
$rev_id = $this->revision['id'];
$rev_title = $this->revision['title'];
$rev_auxiliary = idx($this->revision, 'auxiliary', array());
if ($this->revision['authorPHID'] != $this->getUserPHID()) {
$other_author = $this->getConduit()->callMethodSynchronous(
'user.query',
array(
'phids' => array($this->revision['authorPHID']),
));
$other_author = ipull($other_author, 'userName', 'phid');
$other_author = $other_author[$this->revision['authorPHID']];
$ok = phutil_console_confirm(pht(
"This %s has revision '%s' but you are not the author. Land this ".
"revision by %s?",
$this->branchType,
"D{$rev_id}: {$rev_title}",
$other_author));
if (!$ok) {
throw new ArcanistUserAbortException();
}
}
if ($rev_status != ArcanistDifferentialRevisionStatus::ACCEPTED) {
$ok = phutil_console_confirm(pht(
"Revision '%s' has not been accepted. Continue anyway?",
"D{$rev_id}: {$rev_title}"));
if (!$ok) {
throw new ArcanistUserAbortException();
}
}
if ($rev_auxiliary) {
$phids = idx($rev_auxiliary, 'phabricator:depends-on', array());
if ($phids) {
$dep_on_revs = $this->getConduit()->callMethodSynchronous(
'differential.query',
array(
'phids' => $phids,
'status' => 'status-open',
));
$open_dep_revs = array();
foreach ($dep_on_revs as $dep_on_rev) {
$dep_on_rev_id = $dep_on_rev['id'];
$dep_on_rev_title = $dep_on_rev['title'];
$dep_on_rev_status = $dep_on_rev['status'];
$open_dep_revs[$dep_on_rev_id] = $dep_on_rev_title;
}
if (!empty($open_dep_revs)) {
$open_revs = array();
foreach ($open_dep_revs as $id => $title) {
$open_revs[] = ' - D'.$id.': '.$title;
}
$open_revs = implode("\n", $open_revs);
echo pht(
"Revision '%s' depends on open revisions:\n\n%s",
"D{$rev_id}: {$rev_title}",
$open_revs);
$ok = phutil_console_confirm(pht('Continue anyway?'));
if (!$ok) {
throw new ArcanistUserAbortException();
}
}
}
}
$message = $this->getConduit()->callMethodSynchronous(
'differential.getcommitmessage',
array(
'revision_id' => $rev_id,
));
$this->messageFile = new TempFile();
Filesystem::writeFile($this->messageFile, $message);
echo pht(
"Landing revision '%s'...",
"D{$rev_id}: {$rev_title}")."\n";
$diff_phid = idx($this->revision, 'activeDiffPHID');
if ($diff_phid) {
$this->checkForBuildables($diff_phid);
}
}
private function pullFromRemote() {
$repository_api = $this->getRepositoryAPI();
$local_ahead_of_remote = false;
if ($this->isGit) {
$repository_api->execxLocal('checkout %s', $this->onto);
echo phutil_console_format(pht(
"Switched to branch **%s**. Updating branch...\n",
$this->onto));
try {
$repository_api->execxLocal('pull --ff-only --no-stat');
} catch (CommandException $ex) {
if (!$this->isGitSvn) {
throw $ex;
}
}
list($out) = $repository_api->execxLocal(
'log %s..%s',
$this->ontoRemoteBranch,
$this->onto);
if (strlen(trim($out))) {
$local_ahead_of_remote = true;
} else if ($this->isGitSvn) {
$repository_api->execxLocal('svn rebase');
}
} else if ($this->isHg) {
echo phutil_console_format(pht('Updating **%s**...', $this->onto)."\n");
try {
list($out, $err) = $repository_api->execxLocal('pull');
$divergedbookmark = $this->onto.'@'.$repository_api->getBranchName();
if (strpos($err, $divergedbookmark) !== false) {
throw new ArcanistUsageException(phutil_console_format(pht(
"Local bookmark **%s** has diverged from the server's **%s** ".
"(now labeled **%s**). Please resolve this divergence and run ".
"'%s' again.",
$this->onto,
$this->onto,
$divergedbookmark,
'arc land')));
}
} catch (CommandException $ex) {
$err = $ex->getError();
$stdout = $ex->getStdout();
// Copied from: PhabricatorRepositoryPullLocalDaemon.php
// NOTE: Between versions 2.1 and 2.1.1, Mercurial changed the
// behavior of "hg pull" to return 1 in case of a successful pull
// with no changes. This behavior has been reverted, but users who
// updated between Feb 1, 2012 and Mar 1, 2012 will have the
// erroring version. Do a dumb test against stdout to check for this
// possibility.
// See: https://github.com/phacility/phabricator/issues/101/
// NOTE: Mercurial has translated versions, which translate this error
// string. In a translated version, the string will be something else,
// like "aucun changement trouve". There didn't seem to be an easy way
// to handle this (there are hard ways but this is not a common
// problem and only creates log spam, not application failures).
// Assume English.
// TODO: Remove this once we're far enough in the future that
// deployment of 2.1 is exceedingly rare?
if ($err != 1 || !preg_match('/no changes found/', $stdout)) {
throw $ex;
}
}
// Pull succeeded. Now make sure master is not on an outgoing change
if ($repository_api->supportsPhases()) {
list($out) = $repository_api->execxLocal(
'log -r %s --template %s', $this->onto, '{phase}');
if ($out != 'public') {
$local_ahead_of_remote = true;
}
} else {
// execManual instead of execx because outgoing returns
// code 1 when there is nothing outgoing
list($err, $out) = $repository_api->execManualLocal(
'outgoing -r %s',
$this->onto);
// $err === 0 means something is outgoing
if ($err === 0) {
$local_ahead_of_remote = true;
}
}
}
if ($local_ahead_of_remote) {
throw new ArcanistUsageException(pht(
"Local %s '%s' is ahead of remote %s '%s', so landing a feature ".
"%s would push additional changes. Push or reset the changes in '%s' ".
"before running '%s'.",
$this->ontoType,
$this->onto,
$this->ontoType,
$this->ontoRemoteBranch,
$this->ontoType,
$this->onto,
'arc land'));
}
}
private function rebase() {
$repository_api = $this->getRepositoryAPI();
chdir($repository_api->getPath());
if ($this->isHg) {
$onto_tip = $repository_api->getCanonicalRevisionName($this->onto);
$common_ancestor = $repository_api->getCanonicalRevisionName(
hgsprintf('ancestor(%s, %s)', $this->onto, $this->branch));
// Only rebase if the local branch is not at the tip of the onto branch.
if ($onto_tip != $common_ancestor) {
// keep branch here so later we can decide whether to remove it
$err = $repository_api->execPassthru(
'rebase -d %s --keepbranches',
$this->onto);
if ($err) {
echo phutil_console_format("%s\n", pht('Aborting rebase'));
$repository_api->execManualLocal('rebase --abort');
$this->restoreBranch();
throw new ArcanistUsageException(pht(
"'%s' failed and the rebase was aborted. This is most ".
"likely due to conflicts. Manually rebase %s onto %s, resolve ".
"the conflicts, then run '%s' again.",
sprintf('hg rebase %s', $this->onto),
$this->branch,
$this->onto,
'arc land'));
}
}
}
$repository_api->reloadWorkingCopy();
}
private function squash() {
$repository_api = $this->getRepositoryAPI();
if ($this->isGit) {
$repository_api->execxLocal('checkout %s', $this->onto);
$repository_api->execxLocal(
'merge --no-stat --squash --ff-only %s',
$this->branch);
} else if ($this->isHg) {
// The hg code is a little more complex than git's because we
// need to handle the case where the landing branch has child branches:
// -a--------b master
// \
// w--x mybranch
// \--y subbranch1
// \--z subbranch2
//
// arc land --branch mybranch --onto master :
// -a--b--wx master
// \--y subbranch1
// \--z subbranch2
$branch_rev_id = $repository_api->getCanonicalRevisionName($this->branch);
// At this point $this->onto has been pulled from remote and
// $this->branch has been rebased on top of onto(by the rebase()
// function). So we're guaranteed to have onto as an ancestor of branch
// when we use first((onto::branch)-onto) below.
$branch_root = $repository_api->getCanonicalRevisionName(
hgsprintf('first((%s::%s)-%s)',
$this->onto,
$this->branch,
$this->onto));
$branch_range = hgsprintf(
'(%s::%s)',
$branch_root,
$this->branch);
if (!$this->keepBranch) {
$this->handleAlternateBranches($branch_root, $branch_range);
}
// Collapse just the landing branch onto master.
// Leave its children on the original branch.
$err = $repository_api->execPassthru(
'rebase --collapse --keep --logfile %s -r %s -d %s',
$this->messageFile,
$branch_range,
$this->onto);
if ($err) {
$repository_api->execManualLocal('rebase --abort');
$this->restoreBranch();
throw new ArcanistUsageException(
pht(
"Squashing the commits under %s failed. ".
"Manually squash your commits and run '%s' again.",
$this->branch,
'arc land'));
}
if ($repository_api->isBookmark($this->branch)) {
// a bug in mercurial means bookmarks end up on the revision prior
// to the collapse when using --collapse with --keep,
// so we manually move them to the correct spots
// see: http://bz.selenic.com/show_bug.cgi?id=3716
$repository_api->execxLocal(
'bookmark -f %s',
$this->onto);
$repository_api->execxLocal(
'bookmark -f %s -r %s',
$this->branch,
$branch_rev_id);
}
// check if the branch had children
list($output) = $repository_api->execxLocal(
'log -r %s --template %s',
hgsprintf('children(%s)', $this->branch),
'{node}\n');
$child_branch_roots = phutil_split_lines($output, false);
$child_branch_roots = array_filter($child_branch_roots);
if ($child_branch_roots) {
// move the branch's children onto the collapsed commit
foreach ($child_branch_roots as $child_root) {
$repository_api->execxLocal(
'rebase -d %s -s %s --keep --keepbranches',
$this->onto,
$child_root);
}
}
// All the rebases may have moved us to another branch
// so we move back.
$repository_api->execxLocal('checkout %s', $this->onto);
}
}
/**
* Detect alternate branches and prompt the user for how to handle
* them. An alternate branch is a branch that forks from the landing
* branch prior to the landing branch tip.
*
* In a situation like this:
* -a--------b master
* \
* w--x landingbranch
* \ \-- g subbranch
* \--y altbranch1
* \--z altbranch2
*
* y and z are alternate branches and will get deleted by the squash,
* so we need to detect them and ask the user what they want to do.
*
* @param string The revision id of the landing branch's root commit.
* @param string The revset specifying all the commits in the landing branch.
* @return void
*/
private function handleAlternateBranches($branch_root, $branch_range) {
$repository_api = $this->getRepositoryAPI();
// Using the tree in the doccomment, the revset below resolves as follows:
// 1. roots(descendants(w) - descendants(x) - (w::x))
// 2. roots({x,g,y,z} - {g} - {w,x})
// 3. roots({y,z})
// 4. {y,z}
$alt_branch_revset = hgsprintf(
'roots(descendants(%s)-descendants(%s)-%R)',
$branch_root,
$this->branch,
$branch_range);
list($alt_branches) = $repository_api->execxLocal(
'log --template %s -r %s',
'{node}\n',
$alt_branch_revset);
$alt_branches = phutil_split_lines($alt_branches, false);
$alt_branches = array_filter($alt_branches);
$alt_count = count($alt_branches);
if ($alt_count > 0) {
$input = phutil_console_prompt(pht(
"%s '%s' has %s %s(s) forking off of it that would be deleted ".
"during a squash. Would you like to keep a non-squashed copy, rebase ".
"them on top of '%s', or abort and deal with them yourself? ".
"(k)eep, (r)ebase, (a)bort:",
ucfirst($this->branchType),
$this->branch,
$alt_count,
$this->branchType,
$this->branch));
if ($input == 'k' || $input == 'keep') {
$this->keepBranch = true;
} else if ($input == 'r' || $input == 'rebase') {
foreach ($alt_branches as $alt_branch) {
$repository_api->execxLocal(
'rebase --keep --keepbranches -d %s -s %s',
$this->branch,
$alt_branch);
}
} else if ($input == 'a' || $input == 'abort') {
$branch_string = implode("\n", $alt_branches);
echo
"\n",
pht(
"Remove the %s starting at these revisions and run %s again:\n%s",
$this->branchType.'s',
$branch_string,
'arc land'),
"\n\n";
throw new ArcanistUserAbortException();
} else {
throw new ArcanistUsageException(
pht('Invalid choice. Aborting arc land.'));
}
}
}
private function merge() {
$repository_api = $this->getRepositoryAPI();
// In immutable histories, do a --no-ff merge to force a merge commit with
// the right message.
$repository_api->execxLocal('checkout %s', $this->onto);
chdir($repository_api->getPath());
if ($this->isGit) {
$err = phutil_passthru(
'git merge --no-stat --no-ff --no-commit %s',
$this->branch);
if ($err) {
throw new ArcanistUsageException(pht(
"'%s' failed. Your working copy has been left in a partially ".
"merged state. You can: abort with '%s'; or follow the ".
"instructions to complete the merge.",
'git merge',
'git merge --abort'));
}
} else if ($this->isHg) {
// HG arc land currently doesn't support --merge.
// When merging a bookmark branch to a master branch that
// hasn't changed since the fork, mercurial fails to merge.
// Instead of only working in some cases, we just disable --merge
// until there is a demand for it.
// The user should never reach this line, since --merge is
// forbidden at the command line argument level.
throw new ArcanistUsageException(
pht('%s is not currently supported for hg repos.', '--merge'));
}
}
private function push() {
$repository_api = $this->getRepositoryAPI();
// These commands can fail legitimately (e.g. commit hooks)
try {
if ($this->isGit) {
$repository_api->execxLocal('commit -F %s', $this->messageFile);
if (phutil_is_windows()) {
// Occasionally on large repositories on Windows, Git can exit with
// an unclean working copy here. This prevents reverts from being
// pushed to the remote when this occurs.
$this->requireCleanWorkingCopy();
}
} else if ($this->isHg) {
// hg rebase produces a commit earlier as part of rebase
if (!$this->useSquash) {
$repository_api->execxLocal(
'commit --logfile %s',
$this->messageFile);
}
}
// We dispatch this event so we can run checks on the merged revision,
// right before it gets pushed out. It's easier to do this in arc land
// than to try to hook into git/hg.
$this->didCommitMerge();
} catch (Exception $ex) {
$this->executeCleanupAfterFailedPush();
throw $ex;
}
if ($this->getArgument('hold')) {
echo phutil_console_format(pht(
'Holding change in **%s**: it has NOT been pushed yet.',
$this->onto)."\n");
} else {
echo pht('Pushing change...'), "\n\n";
chdir($repository_api->getPath());
if ($this->isGitSvn) {
$err = phutil_passthru('git svn dcommit');
$cmd = 'git svn dcommit';
} else if ($this->isGit) {
$err = phutil_passthru('git push %s %s', $this->remote, $this->onto);
$cmd = 'git push';
} else if ($this->isHgSvn) {
// hg-svn doesn't support 'push -r', so we do a normal push
// which hg-svn modifies to only push the current branch and
// ancestors.
$err = $repository_api->execPassthru('push %s', $this->remote);
$cmd = 'hg push';
} else if ($this->isHg) {
if (strlen($this->remote)) {
$err = $repository_api->execPassthru(
'push -r %s %s',
$this->onto,
$this->remote);
} else {
$err = $repository_api->execPassthru(
'push -r %s',
$this->onto);
}
$cmd = 'hg push';
}
if ($err) {
echo phutil_console_format(
"<bg:red>** %s **</bg>\n",
pht('PUSH FAILED!'));
$this->executeCleanupAfterFailedPush();
if ($this->isGit) {
throw new ArcanistUsageException(pht(
"'%s' failed! Fix the error and run '%s' again.",
$cmd,
'arc land'));
}
throw new ArcanistUsageException(pht(
"'%s' failed! Fix the error and push this change manually.",
$cmd));
}
$this->didPush();
echo "\n";
}
}
private function executeCleanupAfterFailedPush() {
$repository_api = $this->getRepositoryAPI();
if ($this->isGit) {
$repository_api->execxLocal('reset --hard HEAD^');
$this->restoreBranch();
} else if ($this->isHg) {
$repository_api->execxLocal(
'--config extensions.mq= strip %s',
$this->onto);
$this->restoreBranch();
}
}
private function cleanupBranch() {
$repository_api = $this->getRepositoryAPI();
echo pht('Cleaning up feature %s...', $this->branchType), "\n";
if ($this->isGit) {
list($ref) = $repository_api->execxLocal(
'rev-parse --verify %s',
$this->branch);
$ref = trim($ref);
$recovery_command = csprintf(
'git checkout -b %s %s',
$this->branch,
$ref);
echo pht('(Use `%s` if you want it back.)', $recovery_command), "\n";
$repository_api->execxLocal('branch -D %s', $this->branch);
} else if ($this->isHg) {
$common_ancestor = $repository_api->getCanonicalRevisionName(
hgsprintf('ancestor(%s,%s)', $this->onto, $this->branch));
$branch_root = $repository_api->getCanonicalRevisionName(
hgsprintf('first((%s::%s)-%s)',
$common_ancestor,
$this->branch,
$common_ancestor));
$repository_api->execxLocal(
'--config extensions.mq= strip -r %s',
$branch_root);
if ($repository_api->isBookmark($this->branch)) {
$repository_api->execxLocal('bookmark -d %s', $this->branch);
}
}
if ($this->getArgument('delete-remote')) {
if ($this->isHg) {
// named branches were closed as part of the earlier commit
// so only worry about bookmarks
if ($repository_api->isBookmark($this->branch)) {
$repository_api->execxLocal(
'push -B %s %s',
$this->branch,
$this->remote);
}
}
}
}
public function getSupportedRevisionControlSystems() {
return array('git', 'hg');
}
private function getBranchOrBookmark() {
$repository_api = $this->getRepositoryAPI();
if ($this->isGit) {
$branch = $repository_api->getBranchName();
// If we don't have a branch name, just use whatever's at HEAD.
if (!strlen($branch) && !$this->isGitSvn) {
$branch = $repository_api->getWorkingCopyRevision();
}
} else if ($this->isHg) {
$branch = $repository_api->getActiveBookmark();
if (!$branch) {
$branch = $repository_api->getBranchName();
}
}
return $branch;
}
private function getBranchType($branch) {
$repository_api = $this->getRepositoryAPI();
if ($this->isHg && $repository_api->isBookmark($branch)) {
return 'bookmark';
}
return 'branch';
}
/**
* Restore the original branch, e.g. after a successful land or a failed
* pull.
*/
private function restoreBranch() {
$repository_api = $this->getRepositoryAPI();
$repository_api->execxLocal('checkout %s', $this->oldBranch);
if ($this->isGit) {
$repository_api->execxLocal('submodule update --init --recursive');
}
echo pht(
"Switched back to %s %s.\n",
$this->branchType,
phutil_console_format('**%s**', $this->oldBranch));
}
/**
* Check if a diff has a running or failed buildable, and prompt the user
* before landing if it does.
*/
private function checkForBuildables($diff_phid) {
// NOTE: Since Harbormaster is still beta and this stuff all got added
// recently, just bail if we can't find a buildable. This is just an
// advisory check intended to prevent human error.
try {
$buildables = $this->getConduit()->callMethodSynchronous(
'harbormaster.querybuildables',
array(
'buildablePHIDs' => array($diff_phid),
'manualBuildables' => false,
));
} catch (ConduitClientException $ex) {
return;
}
if (!$buildables['data']) {
// If there's no corresponding buildable, we're done.
return;
}
$console = PhutilConsole::getConsole();
$buildable = head($buildables['data']);
if ($buildable['buildableStatus'] == 'passed') {
$console->writeOut(
"**<bg:green> %s </bg>** %s\n",
pht('BUILDS PASSED'),
pht('Harbormaster builds for the active diff completed successfully.'));
return;
}
switch ($buildable['buildableStatus']) {
case 'building':
$message = pht(
'Harbormaster is still building the active diff for this revision:');
$prompt = pht('Land revision anyway, despite ongoing build?');
break;
case 'failed':
$message = pht(
'Harbormaster failed to build the active diff for this revision. '.
'Build failures:');
$prompt = pht('Land revision anyway, despite build failures?');
break;
default:
// If we don't recognize the status, just bail.
return;
}
$builds = $this->getConduit()->callMethodSynchronous(
'harbormaster.querybuilds',
array(
'buildablePHIDs' => array($buildable['phid']),
));
$console->writeOut($message."\n\n");
foreach ($builds['data'] as $build) {
switch ($build['buildStatus']) {
case 'failed':
$color = 'red';
break;
default:
$color = 'yellow';
break;
}
$console->writeOut(
" **<bg:".$color."> %s </bg>** %s: %s\n",
phutil_utf8_strtoupper($build['buildStatusName']),
pht('Build %d', $build['id']),
$build['name']);
}
$console->writeOut(
"\n%s\n\n **%s**: __%s__",
pht('You can review build details here:'),
pht('Harbormaster URI'),
$buildable['uri']);
- if (!$console->confirm($prompt)) {
+ if (!phutil_console_confirm($prompt)) {
throw new ArcanistUserAbortException();
}
}
public function buildEngineMessage(ArcanistLandEngine $engine) {
// TODO: This is oh-so-gross.
$this->findRevision();
$engine->setCommitMessageFile($this->messageFile);
}
public function didCommitMerge() {
$this->dispatchEvent(
ArcanistEventType::TYPE_LAND_WILLPUSHREVISION,
array());
}
public function didPush() {
$this->askForRepositoryUpdate();
$mark_workflow = $this->buildChildWorkflow(
'close-revision',
array(
'--finalize',
'--quiet',
$this->revision['id'],
));
$mark_workflow->run();
}
}
diff --git a/src/workflow/ArcanistLintWorkflow.php b/src/workflow/ArcanistLintWorkflow.php
index 36126c5d..de215aad 100644
--- a/src/workflow/ArcanistLintWorkflow.php
+++ b/src/workflow/ArcanistLintWorkflow.php
@@ -1,662 +1,662 @@
<?php
/**
* Runs lint rules on changes.
*/
final class ArcanistLintWorkflow extends ArcanistWorkflow {
const RESULT_OKAY = 0;
const RESULT_WARNINGS = 1;
const RESULT_ERRORS = 2;
const RESULT_SKIP = 3;
const DEFAULT_SEVERITY = ArcanistLintSeverity::SEVERITY_ADVICE;
private $unresolvedMessages;
private $shouldLintAll;
private $shouldAmendChanges = false;
private $shouldAmendWithoutPrompt = false;
private $shouldAmendAutofixesWithoutPrompt = false;
private $engine;
public function getWorkflowName() {
return 'lint';
}
public function setShouldAmendChanges($should_amend) {
$this->shouldAmendChanges = $should_amend;
return $this;
}
public function setShouldAmendWithoutPrompt($should_amend) {
$this->shouldAmendWithoutPrompt = $should_amend;
return $this;
}
public function setShouldAmendAutofixesWithoutPrompt($should_amend) {
$this->shouldAmendAutofixesWithoutPrompt = $should_amend;
return $this;
}
public function getCommandSynopses() {
return phutil_console_format(<<<EOTEXT
**lint** [__options__] [__paths__]
**lint** [__options__] --rev [__rev__]
EOTEXT
);
}
public function getCommandHelp() {
return phutil_console_format(<<<EOTEXT
Supports: git, svn, hg
Run static analysis on changes to check for mistakes. If no files
are specified, lint will be run on all files which have been modified.
EOTEXT
);
}
public function getArguments() {
return array(
'lintall' => array(
'help' => pht(
'Show all lint warnings, not just those on changed lines. When '.
'paths are specified, this is the default behavior.'),
'conflicts' => array(
'only-changed' => true,
),
),
'only-changed' => array(
'help' => pht(
'Show lint warnings just on changed lines. When no paths are '.
'specified, this is the default. This differs from only-new '.
'in cases where line modifications introduce lint on other '.
'unmodified lines.'),
'conflicts' => array(
'lintall' => true,
),
),
'rev' => array(
'param' => 'revision',
'help' => pht('Lint changes since a specific revision.'),
'supports' => array(
'git',
'hg',
),
'nosupport' => array(
'svn' => pht('Lint does not currently support %s in SVN.', '--rev'),
),
),
'output' => array(
'param' => 'format',
'help' => pht(
"With 'summary', show lint warnings in a more compact format. ".
"With 'json', show lint warnings in machine-readable JSON format. ".
"With 'none', show no lint warnings. ".
"With 'compiler', show lint warnings in suitable for your editor. ".
"With 'xml', show lint warnings in the Checkstyle XML format."),
),
'outfile' => array(
'param' => 'path',
'help' => pht(
'Output the linter results to a file. Defaults to stdout.'),
),
'only-new' => array(
'param' => 'bool',
'supports' => array('git', 'hg'), // TODO: svn
'help' => pht(
'Display only messages not present in the original code.'),
),
'engine' => array(
'param' => 'classname',
'help' => pht('Override configured lint engine for this project.'),
),
'apply-patches' => array(
'help' => pht(
'Apply patches suggested by lint to the working copy without '.
'prompting.'),
'conflicts' => array(
'never-apply-patches' => true,
),
),
'never-apply-patches' => array(
'help' => pht('Never apply patches suggested by lint.'),
'conflicts' => array(
'apply-patches' => true,
),
),
'amend-all' => array(
'help' => pht(
'When linting git repositories, amend HEAD with all patches '.
'suggested by lint without prompting.'),
),
'amend-autofixes' => array(
'help' => pht(
'When linting git repositories, amend HEAD with autofix '.
'patches suggested by lint without prompting.'),
),
'everything' => array(
'help' => pht('Lint all files in the project.'),
'conflicts' => array(
'cache' => pht('%s lints all files', '--everything'),
'rev' => pht('%s lints all files', '--everything'),
),
),
'severity' => array(
'param' => 'string',
'help' => pht(
"Set minimum message severity. One of: %s. Defaults to '%s'.",
sprintf(
"'%s'",
implode(
"', '",
array_keys(ArcanistLintSeverity::getLintSeverities()))),
self::DEFAULT_SEVERITY),
),
'cache' => array(
'param' => 'bool',
'help' => pht(
"%d to disable cache, %d to enable. The default value is determined ".
"by '%s' in configuration, which defaults to off. See notes in '%s'.",
0,
1,
'arc.lint.cache',
'arc.lint.cache'),
),
'*' => 'paths',
);
}
public function requiresAuthentication() {
return (bool)$this->getArgument('only-new');
}
public function requiresWorkingCopy() {
return true;
}
public function requiresRepositoryAPI() {
return true;
}
private function getCacheKey() {
return implode("\n", array(
get_class($this->engine),
$this->getArgument('severity', self::DEFAULT_SEVERITY),
$this->shouldLintAll,
));
}
public function run() {
$console = PhutilConsole::getConsole();
$working_copy = $this->getWorkingCopy();
$configuration_manager = $this->getConfigurationManager();
$engine = $this->newLintEngine($this->getArgument('engine'));
$rev = $this->getArgument('rev');
$paths = $this->getArgument('paths');
$use_cache = $this->getArgument('cache', null);
$everything = $this->getArgument('everything');
if ($everything && $paths) {
throw new ArcanistUsageException(
pht(
'You can not specify paths with %s. The %s flag lints every file.',
'--everything',
'--everything'));
}
if ($use_cache === null) {
$use_cache = (bool)$configuration_manager->getConfigFromAnySource(
'arc.lint.cache',
false);
}
if ($rev && $paths) {
throw new ArcanistUsageException(
pht('Specify either %s or paths.', '--rev'));
}
// NOTE: When the user specifies paths, we imply --lintall and show all
// warnings for the paths in question. This is easier to deal with for
// us and less confusing for users.
$this->shouldLintAll = $paths ? true : false;
if ($this->getArgument('lintall')) {
$this->shouldLintAll = true;
} else if ($this->getArgument('only-changed')) {
$this->shouldLintAll = false;
}
if ($everything) {
$paths = iterator_to_array($this->getRepositoryAPI()->getAllFiles());
$this->shouldLintAll = true;
} else {
$paths = $this->selectPathsForWorkflow($paths, $rev);
}
$this->engine = $engine;
$engine->setMinimumSeverity(
$this->getArgument('severity', self::DEFAULT_SEVERITY));
$file_hashes = array();
if ($use_cache) {
$engine->setRepositoryVersion($this->getRepositoryVersion());
$cache = $this->readScratchJSONFile('lint-cache.json');
$cache = idx($cache, $this->getCacheKey(), array());
$cached = array();
foreach ($paths as $path) {
$abs_path = $engine->getFilePathOnDisk($path);
if (!Filesystem::pathExists($abs_path)) {
continue;
}
$file_hashes[$abs_path] = md5_file($abs_path);
if (!isset($cache[$path])) {
continue;
}
$messages = idx($cache[$path], $file_hashes[$abs_path]);
if ($messages !== null) {
$cached[$path] = $messages;
}
}
if ($cached) {
$console->writeErr(
"%s\n",
pht(
"Using lint cache, use '%s' to disable it.",
'--cache 0'));
}
$engine->setCachedResults($cached);
}
// Propagate information about which lines changed to the lint engine.
// This is used so that the lint engine can drop warning messages
// concerning lines that weren't in the change.
$engine->setPaths($paths);
if (!$this->shouldLintAll) {
foreach ($paths as $path) {
// Note that getChangedLines() returns null to indicate that a file
// is binary or a directory (i.e., changed lines are not relevant).
$engine->setPathChangedLines(
$path,
$this->getChangedLines($path, 'new'));
}
}
// Enable possible async linting only for 'arc diff' not 'arc lint'
if ($this->getParentWorkflow()) {
$engine->setEnableAsyncLint(true);
} else {
$engine->setEnableAsyncLint(false);
}
if ($this->getArgument('only-new')) {
$conduit = $this->getConduit();
$api = $this->getRepositoryAPI();
if ($rev) {
$api->setBaseCommit($rev);
}
$svn_root = id(new PhutilURI($api->getSourceControlPath()))->getPath();
$all_paths = array();
foreach ($paths as $path) {
$path = str_replace(DIRECTORY_SEPARATOR, '/', $path);
$full_paths = array($path);
$change = $this->getChange($path);
$type = $change->getType();
if (ArcanistDiffChangeType::isOldLocationChangeType($type)) {
$full_paths = $change->getAwayPaths();
} else if (ArcanistDiffChangeType::isNewLocationChangeType($type)) {
continue;
} else if (ArcanistDiffChangeType::isDeleteChangeType($type)) {
continue;
}
foreach ($full_paths as $full_path) {
$all_paths[$svn_root.'/'.$full_path] = $path;
}
}
$lint_future = $conduit->callMethod('diffusion.getlintmessages', array(
'repositoryPHID' => idx($this->loadProjectRepository(), 'phid'),
'branch' => '', // TODO: Tracking branch.
'commit' => $api->getBaseCommit(),
'files' => array_keys($all_paths),
));
}
$failed = null;
try {
$engine->run();
} catch (Exception $ex) {
$failed = $ex;
}
$results = $engine->getResults();
if ($this->getArgument('only-new')) {
$total = 0;
foreach ($results as $result) {
$total += count($result->getMessages());
}
// Don't wait for response with default value of --only-new.
$timeout = null;
if ($this->getArgument('only-new') === null || !$total) {
$timeout = 0;
}
$raw_messages = $this->resolveCall($lint_future, $timeout);
if ($raw_messages && $total) {
$old_messages = array();
$line_maps = array();
foreach ($raw_messages as $message) {
$path = $all_paths[$message['path']];
$line = $message['line'];
$code = $message['code'];
if (!isset($line_maps[$path])) {
$line_maps[$path] = $this->getChange($path)->buildLineMap();
}
$new_lines = idx($line_maps[$path], $line);
if (!$new_lines) { // Unmodified lines after last hunk.
$last_old = ($line_maps[$path] ? last_key($line_maps[$path]) : 0);
$news = array_filter($line_maps[$path]);
$last_new = ($news ? last(end($news)) : 0);
$new_lines = array($line + $last_new - $last_old);
}
$error = array($code => array(true));
foreach ($new_lines as $new) {
if (isset($old_messages[$path][$new])) {
$old_messages[$path][$new][$code][] = true;
break;
}
$old_messages[$path][$new] = &$error;
}
unset($error);
}
foreach ($results as $result) {
foreach ($result->getMessages() as $message) {
$path = str_replace(DIRECTORY_SEPARATOR, '/', $message->getPath());
$line = $message->getLine();
$code = $message->getCode();
if (!empty($old_messages[$path][$line][$code])) {
$message->setObsolete(true);
array_pop($old_messages[$path][$line][$code]);
}
}
$result->sortAndFilterMessages();
}
}
}
if ($this->getArgument('never-apply-patches')) {
$apply_patches = false;
} else {
$apply_patches = true;
}
if ($this->getArgument('apply-patches')) {
$prompt_patches = false;
} else {
$prompt_patches = true;
}
if ($this->getArgument('amend-all')) {
$this->shouldAmendChanges = true;
$this->shouldAmendWithoutPrompt = true;
}
if ($this->getArgument('amend-autofixes')) {
$prompt_autofix_patches = false;
$this->shouldAmendChanges = true;
$this->shouldAmendAutofixesWithoutPrompt = true;
} else {
$prompt_autofix_patches = true;
}
$repository_api = $this->getRepositoryAPI();
if ($this->shouldAmendChanges) {
$this->shouldAmendChanges = $repository_api->supportsAmend() &&
!$this->isHistoryImmutable();
}
$wrote_to_disk = false;
switch ($this->getArgument('output')) {
case 'json':
$renderer = new ArcanistJSONLintRenderer();
$prompt_patches = false;
$apply_patches = $this->getArgument('apply-patches');
break;
case 'summary':
$renderer = new ArcanistSummaryLintRenderer();
break;
case 'none':
$prompt_patches = false;
$apply_patches = $this->getArgument('apply-patches');
$renderer = new ArcanistNoneLintRenderer();
break;
case 'compiler':
$renderer = new ArcanistCompilerLintRenderer();
$prompt_patches = false;
$apply_patches = $this->getArgument('apply-patches');
break;
case 'xml':
$renderer = new ArcanistCheckstyleXMLLintRenderer();
$prompt_patches = false;
$apply_patches = $this->getArgument('apply-patches');
break;
default:
$renderer = new ArcanistConsoleLintRenderer();
$renderer->setShowAutofixPatches($prompt_autofix_patches);
break;
}
$all_autofix = true;
$tmp = null;
if ($this->getArgument('outfile') !== null) {
$tmp = id(new TempFile())
->setPreserveFile(true);
}
$preamble = $renderer->renderPreamble();
if ($tmp) {
Filesystem::appendFile($tmp, $preamble);
} else {
$console->writeOut('%s', $preamble);
}
foreach ($results as $result) {
$result_all_autofix = $result->isAllAutofix();
if (!$result->getMessages() && !$result_all_autofix) {
continue;
}
if (!$result_all_autofix) {
$all_autofix = false;
}
$lint_result = $renderer->renderLintResult($result);
if ($lint_result) {
if ($tmp) {
Filesystem::appendFile($tmp, $lint_result);
} else {
$console->writeOut('%s', $lint_result);
}
}
if ($apply_patches && $result->isPatchable()) {
$patcher = ArcanistLintPatcher::newFromArcanistLintResult($result);
$old_file = $result->getFilePathOnDisk();
if ($prompt_patches &&
!($result_all_autofix && !$prompt_autofix_patches)) {
if (!Filesystem::pathExists($old_file)) {
$old_file = '/dev/null';
}
$new_file = new TempFile();
$new = $patcher->getModifiedFileContent();
Filesystem::writeFile($new_file, $new);
// TODO: Improve the behavior here, make it more like
// difference_render().
list(, $stdout, $stderr) =
exec_manual('diff -u %s %s', $old_file, $new_file);
$console->writeOut('%s', $stdout);
$console->writeErr('%s', $stderr);
$prompt = pht(
'Apply this patch to %s?',
phutil_console_format('__%s__', $result->getPath()));
- if (!$console->confirm($prompt, $default = true)) {
+ if (!phutil_console_confirm($prompt, $default_no = false)) {
continue;
}
}
$patcher->writePatchToDisk();
$wrote_to_disk = true;
$file_hashes[$old_file] = md5_file($old_file);
}
}
$postamble = $renderer->renderPostamble();
if ($tmp) {
Filesystem::appendFile($tmp, $postamble);
Filesystem::rename($tmp, $this->getArgument('outfile'));
} else {
$console->writeOut('%s', $postamble);
}
if ($wrote_to_disk && $this->shouldAmendChanges) {
if ($this->shouldAmendWithoutPrompt ||
($this->shouldAmendAutofixesWithoutPrompt && $all_autofix)) {
$console->writeOut(
"<bg:yellow>** %s **</bg> %s\n",
pht('LINT NOTICE'),
pht('Automatically amending HEAD with lint patches.'));
$amend = true;
} else {
- $amend = $console->confirm(pht('Amend HEAD with lint patches?'));
+ $amend = phutil_console_confirm(pht('Amend HEAD with lint patches?'));
}
if ($amend) {
if ($repository_api instanceof ArcanistGitAPI) {
// Add the changes to the index before amending
$repository_api->execxLocal('add -u');
}
$repository_api->amendCommit();
} else {
throw new ArcanistUsageException(
pht(
'Sort out the lint changes that were applied to the working '.
'copy and relint.'));
}
}
if ($this->getArgument('output') == 'json') {
// NOTE: Required by save_lint.php in Phabricator.
return 0;
}
if ($failed) {
if ($failed instanceof ArcanistNoEffectException) {
if ($renderer instanceof ArcanistNoneLintRenderer) {
return 0;
}
}
throw $failed;
}
$unresolved = array();
$has_warnings = false;
$has_errors = false;
foreach ($results as $result) {
foreach ($result->getMessages() as $message) {
if (!$message->isPatchApplied()) {
if ($message->isError()) {
$has_errors = true;
} else if ($message->isWarning()) {
$has_warnings = true;
}
$unresolved[] = $message;
}
}
}
$this->unresolvedMessages = $unresolved;
$cache = $this->readScratchJSONFile('lint-cache.json');
$cached = idx($cache, $this->getCacheKey(), array());
if ($cached || $use_cache) {
$stopped = $engine->getStoppedPaths();
foreach ($results as $result) {
$path = $result->getPath();
if (!$use_cache) {
unset($cached[$path]);
continue;
}
$abs_path = $engine->getFilePathOnDisk($path);
if (!Filesystem::pathExists($abs_path)) {
continue;
}
$version = $result->getCacheVersion();
$cached_path = array();
if (isset($stopped[$path])) {
$cached_path['stopped'] = $stopped[$path];
}
$cached_path['repository_version'] = $this->getRepositoryVersion();
foreach ($result->getMessages() as $message) {
$granularity = $message->getGranularity();
if ($granularity == ArcanistLinter::GRANULARITY_GLOBAL) {
continue;
}
if (!$message->isPatchApplied()) {
$cached_path[] = $message->toDictionary();
}
}
$hash = idx($file_hashes, $abs_path);
if (!$hash) {
$hash = md5_file($abs_path);
}
$cached[$path] = array($hash => array($version => $cached_path));
}
$cache[$this->getCacheKey()] = $cached;
// TODO: Garbage collection.
$this->writeScratchJSONFile('lint-cache.json', $cache);
}
// Take the most severe lint message severity and use that
// as the result code.
if ($has_errors) {
$result_code = self::RESULT_ERRORS;
} else if ($has_warnings) {
$result_code = self::RESULT_WARNINGS;
} else {
$result_code = self::RESULT_OKAY;
}
if (!$this->getParentWorkflow()) {
if ($result_code == self::RESULT_OKAY) {
$console->writeOut('%s', $renderer->renderOkayResult());
}
}
return $result_code;
}
public function getUnresolvedMessages() {
return $this->unresolvedMessages;
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Wed, Feb 26, 03:29 (1 d, 20 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
610630
Default Alt Text
(157 KB)
Attached To
Mode
R118 Arcanist - fork
Attached
Detach File
Event Timeline
Log In to Comment