diff --git a/src/land/engine/ArcanistLandEngine.php b/src/land/engine/ArcanistLandEngine.php index 9dc7eb50..29365a9d 100644 --- a/src/land/engine/ArcanistLandEngine.php +++ b/src/land/engine/ArcanistLandEngine.php @@ -1,1576 +1,1581 @@ ontoRemote = $onto_remote; return $this; } final public function getOntoRemote() { return $this->ontoRemote; } final public function setOntoRefs($onto_refs) { $this->ontoRefs = $onto_refs; return $this; } final public function getOntoRefs() { return $this->ontoRefs; } final public function setIntoRemote($into_remote) { $this->intoRemote = $into_remote; return $this; } final public function getIntoRemote() { return $this->intoRemote; } final public function setIntoRef($into_ref) { $this->intoRef = $into_ref; return $this; } final public function getIntoRef() { return $this->intoRef; } final public function setIntoEmpty($into_empty) { $this->intoEmpty = $into_empty; return $this; } final public function getIntoEmpty() { return $this->intoEmpty; } final public function setPickArgument($pick_argument) { $this->pickArgument = $pick_argument; return $this; } final public function getPickArgument() { return $this->pickArgument; } final public function setIntoLocal($into_local) { $this->intoLocal = $into_local; return $this; } final public function getIntoLocal() { return $this->intoLocal; } final public function setShouldHold($should_hold) { $this->shouldHold = $should_hold; return $this; } final public function getShouldHold() { return $this->shouldHold; } final public function setShouldKeep($should_keep) { $this->shouldKeep = $should_keep; return $this; } final public function getShouldKeep() { return $this->shouldKeep; } final public function setStrategyArgument($strategy_argument) { $this->strategyArgument = $strategy_argument; return $this; } final public function getStrategyArgument() { return $this->strategyArgument; } final public function setStrategy($strategy) { $this->strategy = $strategy; return $this; } final public function getStrategy() { return $this->strategy; } final public function setRevisionSymbol($revision_symbol) { $this->revisionSymbol = $revision_symbol; return $this; } final public function getRevisionSymbol() { return $this->revisionSymbol; } final public function setRevisionSymbolRef( ArcanistRevisionSymbolRef $revision_ref) { $this->revisionSymbolRef = $revision_ref; return $this; } final public function getRevisionSymbolRef() { return $this->revisionSymbolRef; } final public function setShouldPreview($should_preview) { $this->shouldPreview = $should_preview; return $this; } final public function getShouldPreview() { return $this->shouldPreview; } final public function setSourceRefs(array $source_refs) { $this->sourceRefs = $source_refs; return $this; } final public function getSourceRefs() { return $this->sourceRefs; } final public function setOntoRemoteArgument($remote_argument) { $this->ontoRemoteArgument = $remote_argument; return $this; } final public function getOntoRemoteArgument() { return $this->ontoRemoteArgument; } final public function setOntoArguments(array $onto_arguments) { $this->ontoArguments = $onto_arguments; return $this; } final public function getOntoArguments() { return $this->ontoArguments; } final public function setIsIncremental($is_incremental) { $this->isIncremental = $is_incremental; return $this; } final public function getIsIncremental() { return $this->isIncremental; } final public function setIntoEmptyArgument($into_empty_argument) { $this->intoEmptyArgument = $into_empty_argument; return $this; } final public function getIntoEmptyArgument() { return $this->intoEmptyArgument; } final public function setIntoLocalArgument($into_local_argument) { $this->intoLocalArgument = $into_local_argument; return $this; } final public function getIntoLocalArgument() { return $this->intoLocalArgument; } final public function setIntoRemoteArgument($into_remote_argument) { $this->intoRemoteArgument = $into_remote_argument; return $this; } final public function getIntoRemoteArgument() { return $this->intoRemoteArgument; } final public function setIntoArgument($into_argument) { $this->intoArgument = $into_argument; return $this; } final public function getIntoArgument() { return $this->intoArgument; } private function setLocalState(ArcanistRepositoryLocalState $local_state) { $this->localState = $local_state; return $this; } final protected function getLocalState() { return $this->localState; } private function setHasUnpushedChanges($unpushed) { $this->hasUnpushedChanges = $unpushed; return $this; } final protected function getHasUnpushedChanges() { return $this->hasUnpushedChanges; } final protected function getOntoConfigurationKey() { return 'arc.land.onto'; } final protected function getOntoFromConfiguration() { $config_key = $this->getOntoConfigurationKey(); return $this->getWorkflow()->getConfig($config_key); } final protected function getOntoRemoteConfigurationKey() { return 'arc.land.onto-remote'; } final protected function getOntoRemoteFromConfiguration() { $config_key = $this->getOntoRemoteConfigurationKey(); return $this->getWorkflow()->getConfig($config_key); } final protected function getStrategyConfigurationKey() { return 'arc.land.strategy'; } final protected function getStrategyFromConfiguration() { $config_key = $this->getStrategyConfigurationKey(); return $this->getWorkflow()->getConfig($config_key); } final protected function confirmRevisions(array $sets) { assert_instances_of($sets, 'ArcanistLandCommitSet'); $revision_refs = mpull($sets, 'getRevisionRef'); $viewer = $this->getViewer(); $viewer_phid = $viewer->getPHID(); $unauthored = array(); foreach ($revision_refs as $revision_ref) { $author_phid = $revision_ref->getAuthorPHID(); if ($author_phid !== $viewer_phid) { $unauthored[] = $revision_ref; } } if ($unauthored) { $this->getWorkflow()->loadHardpoints( $unauthored, array( ArcanistRevisionRef::HARDPOINT_AUTHORREF, )); echo tsprintf( "\n%!\n%W\n\n", pht('NOT REVISION AUTHOR'), pht( 'You are landing revisions which you ("%s") are not the author of:', $viewer->getMonogram())); foreach ($unauthored as $revision_ref) { $display_ref = $revision_ref->newDisplayRef(); $author_ref = $revision_ref->getAuthorRef(); if ($author_ref) { $display_ref->appendLine( pht( 'Author: %s', $author_ref->getMonogram())); } echo tsprintf('%s', $display_ref); } echo tsprintf( "\n%?\n", pht( 'Use "Commandeer" in the web interface to become the author of '. 'a revision.')); $query = pht('Land revisions you are not the author of?'); $this->getWorkflow() ->getPrompt('arc.land.unauthored') ->setQuery($query) ->execute(); } $planned = array(); $published = array(); $not_accepted = array(); foreach ($revision_refs as $revision_ref) { if ($revision_ref->isStatusChangesPlanned()) { $planned[] = $revision_ref; } else if ($revision_ref->isStatusPublished()) { $published[] = $revision_ref; } else if (!$revision_ref->isStatusAccepted()) { $not_accepted[] = $revision_ref; } } // See T10233. Previously, this prompt was bundled with the generic "not // accepted" prompt, but users found it confusing and interpreted the // prompt as a bug. if ($planned) { $example_ref = head($planned); echo tsprintf( "\n%!\n%W\n\n%W\n\n%W\n\n", pht('%s REVISION(S) HAVE CHANGES PLANNED', phutil_count($planned)), pht( 'You are landing %s revision(s) which are currently in the state '. '"%s", indicating that you expect to revise them before moving '. 'forward.', phutil_count($planned), $example_ref->getStatusDisplayName()), pht( 'Normally, you should update these %s revision(s), submit them '. 'for review, and wait for reviewers to accept them before '. 'you continue. To resubmit a revision for review, either: '. 'update the revision with revised changes; or use '. '"Request Review" from the web interface.', phutil_count($planned)), pht( 'These %s revision(s) have changes planned:', phutil_count($planned))); foreach ($planned as $revision_ref) { echo tsprintf('%s', $revision_ref->newDisplayRef()); } $query = pht( 'Land %s revision(s) with changes planned?', phutil_count($planned)); $this->getWorkflow() ->getPrompt('arc.land.changes-planned') ->setQuery($query) ->execute(); } // See PHI1727. Previously, this prompt was bundled with the generic // "not accepted" prompt, but at least one user found it confusing. if ($published) { $example_ref = head($published); echo tsprintf( "\n%!\n%W\n\n", pht('%s REVISION(S) ARE ALREADY PUBLISHED', phutil_count($published)), pht( 'You are landing %s revision(s) which are already in the state '. '"%s", indicating that they have previously landed:', phutil_count($published), $example_ref->getStatusDisplayName())); foreach ($published as $revision_ref) { echo tsprintf('%s', $revision_ref->newDisplayRef()); } $query = pht( 'Land %s revision(s) that are already published?', phutil_count($published)); $this->getWorkflow() ->getPrompt('arc.land.published') ->setQuery($query) ->execute(); } if ($not_accepted) { $example_ref = head($not_accepted); echo tsprintf( "\n%!\n%W\n\n", pht('%s REVISION(S) ARE NOT ACCEPTED', phutil_count($not_accepted)), pht( 'You are landing %s revision(s) which are not in state "Accepted", '. 'indicating that they have not been accepted by reviewers. '. 'Normally, you should land changes only once they have been '. 'accepted. These revisions are in the wrong state:', phutil_count($not_accepted))); foreach ($not_accepted as $revision_ref) { $display_ref = $revision_ref->newDisplayRef(); $display_ref->appendLine( pht( 'Status: %s', $revision_ref->getStatusDisplayName())); echo tsprintf('%s', $display_ref); } $query = pht( 'Land %s revision(s) in the wrong state?', phutil_count($not_accepted)); $this->getWorkflow() ->getPrompt('arc.land.not-accepted') ->setQuery($query) ->execute(); } $this->getWorkflow()->loadHardpoints( $revision_refs, array( ArcanistRevisionRef::HARDPOINT_PARENTREVISIONREFS, )); $open_parents = array(); foreach ($revision_refs as $revision_phid => $revision_ref) { $parent_refs = $revision_ref->getParentRevisionRefs(); foreach ($parent_refs as $parent_ref) { $parent_phid = $parent_ref->getPHID(); // If we're landing a parent revision in this operation, we don't need // to complain that it hasn't been closed yet. if (isset($revision_refs[$parent_phid])) { continue; } if ($parent_ref->isClosed()) { continue; } if (!isset($open_parents[$parent_phid])) { $open_parents[$parent_phid] = array( 'ref' => $parent_ref, 'children' => array(), ); } $open_parents[$parent_phid]['children'][] = $revision_ref; } } if ($open_parents) { echo tsprintf( "\n%!\n%W\n\n", pht('%s OPEN PARENT REVISION(S) ', phutil_count($open_parents)), pht( 'The changes you are landing depend on %s open parent revision(s). '. 'Usually, you should land parent revisions before landing the '. 'changes which depend on them. These parent revisions are open:', phutil_count($open_parents))); foreach ($open_parents as $parent_phid => $spec) { $parent_ref = $spec['ref']; $display_ref = $parent_ref->newDisplayRef(); $display_ref->appendLine( pht( 'Status: %s', $parent_ref->getStatusDisplayName())); foreach ($spec['children'] as $child_ref) { $display_ref->appendLine( pht( 'Parent of: %s %s', $child_ref->getMonogram(), $child_ref->getName())); } echo tsprintf('%s', $display_ref); } $query = pht( 'Land changes that depend on %s open revision(s)?', phutil_count($open_parents)); $this->getWorkflow() ->getPrompt('arc.land.open-parents') ->setQuery($query) ->execute(); } $this->confirmBuilds($revision_refs); // This is a reasonable place to bulk-load the commit messages, which // we'll need soon. $this->getWorkflow()->loadHardpoints( $revision_refs, array( ArcanistRevisionRef::HARDPOINT_COMMITMESSAGE, )); } private function confirmBuilds(array $revision_refs) { assert_instances_of($revision_refs, 'ArcanistRevisionRef'); $this->getWorkflow()->loadHardpoints( $revision_refs, array( ArcanistRevisionRef::HARDPOINT_BUILDABLEREF, )); $buildable_refs = array(); foreach ($revision_refs as $revision_ref) { $ref = $revision_ref->getBuildableRef(); if ($ref) { $buildable_refs[] = $ref; } } $this->getWorkflow()->loadHardpoints( $buildable_refs, array( ArcanistBuildableRef::HARDPOINT_BUILDREFS, )); $build_refs = array(); foreach ($buildable_refs as $buildable_ref) { foreach ($buildable_ref->getBuildRefs() as $build_ref) { $build_refs[] = $build_ref; } } $this->getWorkflow()->loadHardpoints( $build_refs, array( ArcanistBuildRef::HARDPOINT_BUILDPLANREF, )); $problem_builds = array(); $has_failures = false; $has_ongoing = false; $build_refs = msortv($build_refs, 'getStatusSortVector'); foreach ($build_refs as $build_ref) { $plan_ref = $build_ref->getBuildPlanRef(); if (!$plan_ref) { continue; } $plan_behavior = $plan_ref->getBehavior('arc-land', 'always'); $if_building = ($plan_behavior == 'building'); $if_complete = ($plan_behavior == 'complete'); $if_never = ($plan_behavior == 'never'); // If the build plan "Never" warns when landing, skip it. if ($if_never) { continue; } // If the build plan warns when landing "If Complete" but the build is // not complete, skip it. if ($if_complete && !$build_ref->isComplete()) { continue; } // If the build plan warns when landing "If Building" but the build is // complete, skip it. if ($if_building && $build_ref->isComplete()) { continue; } // Ignore passing builds. if ($build_ref->isPassed()) { continue; } if ($build_ref->isComplete()) { $has_failures = true; } else { $has_ongoing = true; } $problem_builds[] = $build_ref; } if (!$problem_builds) { return; } $build_map = array(); $failure_map = array(); $buildable_map = mpull($buildable_refs, null, 'getPHID'); $revision_map = mpull($revision_refs, null, 'getDiffPHID'); foreach ($problem_builds as $build_ref) { $buildable_phid = $build_ref->getBuildablePHID(); $buildable_ref = $buildable_map[$buildable_phid]; $object_phid = $buildable_ref->getObjectPHID(); $revision_ref = $revision_map[$object_phid]; $revision_phid = $revision_ref->getPHID(); if (!isset($build_map[$revision_phid])) { $build_map[$revision_phid] = array( - 'revisionRef' => $revision_phid, + 'revisionRef' => $revision_ref, 'buildRefs' => array(), ); } $build_map[$revision_phid]['buildRefs'][] = $build_ref; } $log = $this->getLogEngine(); if ($has_failures) { if ($has_ongoing) { $message = pht( '%s revision(s) have build failures or ongoing builds:', phutil_count($build_map)); $query = pht( 'Land %s revision(s) anyway, despite ongoing and failed builds?', phutil_count($build_map)); } else { $message = pht( '%s revision(s) have build failures:', phutil_count($build_map)); $query = pht( 'Land %s revision(s) anyway, despite failed builds?', phutil_count($build_map)); } echo tsprintf( - "%!\n%s\n\n", + "%!\n%s\n", pht('BUILD FAILURES'), $message); $prompt_key = 'arc.land.failed-builds'; } else if ($has_ongoing) { echo tsprintf( - "%!\n%s\n\n", + "%!\n%s\n", pht('ONGOING BUILDS'), pht( '%s revision(s) have ongoing builds:', phutil_count($build_map))); $query = pht( 'Land %s revision(s) anyway, despite ongoing builds?', phutil_count($build_map)); $prompt_key = 'arc.land.ongoing-builds'; } echo tsprintf("\n"); foreach ($build_map as $build_item) { $revision_ref = $build_item['revisionRef']; echo tsprintf('%s', $revision_ref->newDisplayRef()); foreach ($build_item['buildRefs'] as $build_ref) { echo tsprintf('%s', $build_ref->newDisplayRef()); } echo tsprintf("\n"); } echo tsprintf( - "\n%s\n\n", + "\n%s\n", pht('You can review build details here:')); // TODO: Only show buildables with problem builds. + $workflow = $this->getWorkflow(); + foreach ($buildable_refs as $buildable) { $display_ref = $buildable->newDisplayRef(); - // TODO: Include URI here. + $raw_uri = $buildable->getURI(); + $raw_uri = $workflow->getAbsoluteURI($raw_uri); + + $display_ref->setURI($raw_uri); echo tsprintf('%s', $display_ref); } $this->getWorkflow() ->getPrompt($prompt_key) ->setQuery($query) ->execute(); } final protected function confirmImplicitCommits(array $sets, array $symbols) { assert_instances_of($sets, 'ArcanistLandCommitSet'); assert_instances_of($symbols, 'ArcanistLandSymbol'); $implicit = array(); foreach ($sets as $set) { if ($set->hasImplicitCommits()) { $implicit[] = $set; } } if (!$implicit) { return; } echo tsprintf( "\n%!\n%W\n", pht('IMPLICIT COMMITS'), pht( 'Some commits reachable from the specified sources (%s) are not '. 'associated with revisions, and may not have been reviewed. These '. 'commits will be landed as though they belong to the nearest '. 'ancestor revision:', $this->getDisplaySymbols($symbols))); foreach ($implicit as $set) { $this->printCommitSet($set); } $query = pht( 'Continue with this mapping between commits and revisions?'); $this->getWorkflow() ->getPrompt('arc.land.implicit') ->setQuery($query) ->execute(); } final protected function getDisplaySymbols(array $symbols) { $display = array(); foreach ($symbols as $symbol) { $display[] = sprintf('"%s"', addcslashes($symbol->getSymbol(), '\\"')); } return implode(', ', $display); } final protected function printCommitSet(ArcanistLandCommitSet $set) { $revision_ref = $set->getRevisionRef(); echo tsprintf( "\n%s", $revision_ref->newDisplayRef()); foreach ($set->getCommits() as $commit) { $is_implicit = $commit->getIsImplicitCommit(); $display_hash = $this->getDisplayHash($commit->getHash()); $display_summary = $commit->getDisplaySummary(); if ($is_implicit) { echo tsprintf( " %s %s\n", $display_hash, $display_summary); } else { echo tsprintf( " %s %s\n", $display_hash, $display_summary); } } } final protected function loadRevisionRefs(array $commit_map) { assert_instances_of($commit_map, 'ArcanistLandCommit'); $workflow = $this->getWorkflow(); $state_refs = array(); foreach ($commit_map as $commit) { $hash = $commit->getHash(); $commit_ref = id(new ArcanistCommitRef()) ->setCommitHash($hash); $state_ref = id(new ArcanistWorkingCopyStateRef()) ->setCommitRef($commit_ref); $state_refs[$hash] = $state_ref; } $force_symbol_ref = $this->getRevisionSymbolRef(); $force_ref = null; if ($force_symbol_ref) { $workflow->loadHardpoints( $force_symbol_ref, ArcanistSymbolRef::HARDPOINT_OBJECT); $force_ref = $force_symbol_ref->getObject(); if (!$force_ref) { throw new PhutilArgumentUsageException( pht( 'Symbol "%s" does not identify a valid revision.', $force_symbol_ref->getSymbol())); } } $workflow->loadHardpoints( $state_refs, ArcanistWorkingCopyStateRef::HARDPOINT_REVISIONREFS); foreach ($commit_map as $commit) { $hash = $commit->getHash(); $state_ref = $state_refs[$hash]; $revision_refs = $state_ref->getRevisionRefs(); $commit->setRelatedRevisionRefs($revision_refs); } // For commits which have exactly one related revision, select it now. foreach ($commit_map as $commit) { $revision_refs = $commit->getRelatedRevisionRefs(); if (count($revision_refs) !== 1) { continue; } $revision_ref = head($revision_refs); $commit->setExplicitRevisionRef($revision_ref); } // If we have a "--revision", select that revision for any commits with // no known related revisions. // Also select that revision for any commits which have several possible // revisions including that revision. This is relatively safe and // reasonable and doesn't require a warning. if ($force_ref) { $force_phid = $force_ref->getPHID(); foreach ($commit_map as $commit) { if ($commit->getExplicitRevisionRef()) { continue; } $revision_refs = $commit->getRelatedRevisionRefs(); if ($revision_refs) { $revision_refs = mpull($revision_refs, null, 'getPHID'); if (!isset($revision_refs[$force_phid])) { continue; } } $commit->setExplicitRevisionRef($force_ref); } } // If we have a "--revision", identify any commits which it is not yet // selected for. These are commits which are not associated with the // identified revision but are associated with one or more other revisions. if ($force_ref) { $force_phid = $force_ref->getPHID(); $confirm_force = array(); foreach ($commit_map as $key => $commit) { $revision_ref = $commit->getExplicitRevisionRef(); if (!$revision_ref) { continue; } if ($revision_ref->getPHID() === $force_phid) { continue; } $confirm_force[] = $commit; } if ($confirm_force) { // TODO: Make this more clear. // TODO: Show all the commits. throw new PhutilArgumentUsageException( pht( 'TODO: You are forcing a revision, but commits are associated '. 'with some other revision. Are you REALLY sure you want to land '. - 'ALL these commits wiht a different unrelated revision???')); + 'ALL these commits with a different unrelated revision???')); } foreach ($confirm_force as $commit) { $commit->setExplicitRevisionRef($force_ref); } } // Finally, raise an error if we're left with ambiguous revisions. This // happens when we have no "--revision" and some commits in the range // that are associated with more than one revision. $ambiguous = array(); foreach ($commit_map as $commit) { if ($commit->getExplicitRevisionRef()) { continue; } if (!$commit->getRelatedRevisionRefs()) { continue; } $ambiguous[] = $commit; } if ($ambiguous) { foreach ($ambiguous as $commit) { $symbols = $commit->getIndirectSymbols(); $raw_symbols = mpull($symbols, 'getSymbol'); $symbol_list = implode(', ', $raw_symbols); $display_hash = $this->getDisplayHash($hash); $revision_refs = $commit->getRelatedRevisionRefs(); // TODO: Include "use 'arc look --type commit abc' to figure out why" // once that works? // TODO: We could print all the ambiguous commits. // TODO: Suggest "--pick" as a remedy once it exists? echo tsprintf( "\n%!\n%W\n\n", pht('AMBIGUOUS REVISION'), pht( 'The revision associated with commit "%s" (an ancestor of: %s) '. 'is ambiguous. These %s revision(s) are associated with the '. 'commit:', $display_hash, implode(', ', $raw_symbols), phutil_count($revision_refs))); foreach ($revision_refs as $revision_ref) { echo tsprintf( '%s', $revision_ref->newDisplayRef()); } echo tsprintf("\n"); throw new PhutilArgumentUsageException( pht( 'Revision for commit "%s" is ambiguous. Use "--revision" to force '. 'selection of a particular revision.', $display_hash)); } } // NOTE: We may exit this method with commits that are still unassociated. // These will be handled later by the "implicit commits" mechanism. } final protected function getDisplayHash($hash) { // TODO: This should be on the API object. return substr($hash, 0, 12); } final protected function confirmCommits( $into_commit, array $symbols, array $commit_map) { $commit_count = count($commit_map); if (!$commit_count) { $message = pht( 'There are no commits reachable from the specified sources (%s) '. 'which are not already present in the state you are merging '. 'into ("%s"), so nothing can land.', $this->getDisplaySymbols($symbols), $this->getDisplayHash($into_commit)); echo tsprintf( "\n%!\n%W\n\n", pht('NOTHING TO LAND'), $message); throw new PhutilArgumentUsageException( pht('There are no commits to land.')); } // Reverse the commit list so that it's oldest-first, since this is the // order we'll use to show revisions. $commit_map = array_reverse($commit_map, true); $warn_limit = $this->getWorkflow()->getLargeWorkingSetLimit(); $show_limit = 5; if ($commit_count > $warn_limit) { if ($into_commit === null) { $message = pht( 'There are %s commit(s) reachable from the specified sources (%s). '. 'You are landing into the empty state, so all of these commits '. 'will land:', new PhutilNumber($commit_count), $this->getDisplaySymbols($symbols)); } else { $message = pht( 'There are %s commit(s) reachable from the specified sources (%s) '. 'that are not present in the repository state you are merging '. 'into ("%s"). All of these commits will land:', new PhutilNumber($commit_count), $this->getDisplaySymbols($symbols), $this->getDisplayHash($into_commit)); } echo tsprintf( "\n%!\n%W\n", pht('LARGE WORKING SET'), $message); $display_commits = array_merge( array_slice($commit_map, 0, $show_limit), array(null), array_slice($commit_map, -$show_limit)); echo tsprintf("\n"); foreach ($display_commits as $commit) { if ($commit === null) { echo tsprintf( " %s\n", pht( '< ... %s more commits ... >', new PhutilNumber($commit_count - ($show_limit * 2)))); } else { echo tsprintf( " %s %s\n", $this->getDisplayHash($commit->getHash()), $commit->getDisplaySummary()); } } $query = pht( 'Land %s commit(s)?', new PhutilNumber($commit_count)); $this->getWorkflow() ->getPrompt('arc.land.large-working-set') ->setQuery($query) ->execute(); } // Build the commit objects into a tree. foreach ($commit_map as $commit_hash => $commit) { $parent_map = array(); foreach ($commit->getParents() as $parent) { if (isset($commit_map[$parent])) { $parent_map[$parent] = $commit_map[$parent]; } } $commit->setParentCommits($parent_map); } // Identify the commits which are heads (have no children). $child_map = array(); foreach ($commit_map as $commit_hash => $commit) { foreach ($commit->getParents() as $parent) { $child_map[$parent][$commit_hash] = $commit; } } foreach ($commit_map as $commit_hash => $commit) { if (isset($child_map[$commit_hash])) { continue; } $commit->setIsHeadCommit(true); } return $commit_map; } public function execute() { $api = $this->getRepositoryAPI(); $log = $this->getLogEngine(); $this->validateArguments(); $raw_symbols = $this->getSourceRefs(); if (!$raw_symbols) { $raw_symbols = $this->getDefaultSymbols(); } $symbols = array(); foreach ($raw_symbols as $raw_symbol) { $symbols[] = id(new ArcanistLandSymbol()) ->setSymbol($raw_symbol); } $this->resolveSymbols($symbols); $onto_remote = $this->selectOntoRemote($symbols); $this->setOntoRemote($onto_remote); $onto_refs = $this->selectOntoRefs($symbols); $this->confirmOntoRefs($onto_refs); $this->setOntoRefs($onto_refs); $this->selectIntoRemote(); $this->selectIntoRef(); $into_commit = $this->selectIntoCommit(); $commit_map = $this->selectCommits($into_commit, $symbols); $this->loadRevisionRefs($commit_map); // TODO: It's possible we have a list of commits which includes disjoint // groups of commits associated with the same revision, or groups of // commits which do not form a range. We should test that here, since we // can't land commit groups which are not a single contiguous range. $revision_groups = array(); foreach ($commit_map as $commit_hash => $commit) { $revision_ref = $commit->getRevisionRef(); if (!$revision_ref) { echo tsprintf( "\n%!\n%W\n\n", pht('UNKNOWN REVISION'), pht( 'Unable to determine which revision is associated with commit '. '"%s". Use "arc diff" to create or update a revision with this '. 'commit, or "--revision" to force selection of a particular '. 'revision.', $this->getDisplayHash($commit_hash))); throw new PhutilArgumentUsageException( pht( 'Unable to determine revision for commit "%s".', $this->getDisplayHash($commit_hash))); } $revision_groups[$revision_ref->getPHID()][] = $commit; } $commit_heads = array(); foreach ($commit_map as $commit) { if ($commit->getIsHeadCommit()) { $commit_heads[] = $commit; } } $revision_order = array(); foreach ($commit_heads as $head) { foreach ($head->getAncestorRevisionPHIDs() as $phid) { $revision_order[$phid] = true; } } $revision_groups = array_select_keys( $revision_groups, array_keys($revision_order)); $sets = array(); foreach ($revision_groups as $revision_phid => $group) { $revision_ref = head($group)->getRevisionRef(); $set = id(new ArcanistLandCommitSet()) ->setRevisionRef($revision_ref) ->setCommits($group); $sets[$revision_phid] = $set; } $sets = $this->filterCommitSets($sets); if (!$this->getShouldPreview()) { $this->confirmImplicitCommits($sets, $symbols); } $log->writeStatus( pht('LANDING'), pht('These changes will land:')); foreach ($sets as $set) { $this->printCommitSet($set); } if ($this->getShouldPreview()) { $log->writeStatus( pht('PREVIEW'), pht('Completed preview of land operation.')); return; } $query = pht('Land these changes?'); $this->getWorkflow() ->getPrompt('arc.land.confirm') ->setQuery($query) ->execute(); $this->confirmRevisions($sets); $workflow = $this->getWorkflow(); $is_incremental = $this->getIsIncremental(); $is_hold = $this->getShouldHold(); $is_keep = $this->getShouldKeep(); $local_state = $api->newLocalState() ->setWorkflow($workflow) ->saveLocalState(); $this->setLocalState($local_state); $seen_into = array(); try { $last_key = last_key($sets); $need_cascade = array(); $need_prune = array(); foreach ($sets as $set_key => $set) { // Add these first, so we don't add them multiple times if we need // to retry a push. $need_prune[] = $set; $need_cascade[] = $set; while (true) { $into_commit = $this->executeMerge($set, $into_commit); $this->setHasUnpushedChanges(true); if ($is_hold) { $should_push = false; } else if ($is_incremental) { $should_push = true; } else { $is_last = ($set_key === $last_key); $should_push = $is_last; } if ($should_push) { try { $this->pushChange($into_commit); $this->setHasUnpushedChanges(false); } catch (Exception $ex) { // TODO: If the push fails, fetch and retry if the remote ref // has moved ahead of us. if ($this->getIntoLocal()) { $can_retry = false; } else if ($this->getIntoEmpty()) { $can_retry = false; } else if ($this->getIntoRemote() !== $this->getOntoRemote()) { $can_retry = false; } else { $can_retry = false; } if ($can_retry) { // New commit state here $into_commit = '..'; continue; } throw $ex; } if ($need_cascade) { // NOTE: We cascade each set we've pushed, but we're going to // cascade them from most recent to least recent. This way, // branches which descend from more recent changes only cascade // once, directly in to the correct state. $need_cascade = array_reverse($need_cascade); foreach ($need_cascade as $cascade_set) { $this->cascadeState($set, $into_commit); } $need_cascade = array(); } if (!$is_keep) { $this->pruneBranches($need_prune); $need_prune = array(); } } break; } } if ($is_hold) { $this->didHoldChanges($into_commit); $local_state->discardLocalState(); } else { // TODO: Restore this. // $this->getWorkflow()->askForRepositoryUpdate(); $this->reconcileLocalState($into_commit, $local_state); $log->writeSuccess( pht('DONE'), pht('Landed changes.')); } } catch (Exception $ex) { $local_state->restoreLocalState(); throw $ex; } catch (Throwable $ex) { $local_state->restoreLocalState(); throw $ex; } } protected function validateArguments() { $log = $this->getLogEngine(); $into_local = $this->getIntoLocalArgument(); $into_empty = $this->getIntoEmptyArgument(); $into_remote = $this->getIntoRemoteArgument(); $into_count = 0; if ($into_remote !== null) { $into_count++; } if ($into_local) { $into_count++; } if ($into_empty) { $into_count++; } if ($into_count > 1) { throw new PhutilArgumentUsageException( pht( 'Arguments "--into-local", "--into-remote", and "--into-empty" '. 'are mutually exclusive.')); } $into = $this->getIntoArgument(); if ($into && $into_empty) { throw new PhutilArgumentUsageException( pht( 'Arguments "--into" and "--into-empty" are mutually exclusive.')); } $strategy = $this->selectMergeStrategy(); $this->setStrategy($strategy); $is_pick = $this->getPickArgument(); if ($is_pick && !$this->isSquashStrategy()) { throw new PhutilArgumentUsageException( pht( 'You can not "--pick" changes under the "merge" strategy.')); } // Build the symbol ref here (which validates the format of the symbol), // but don't load the object until later on when we're sure we actually // need it, since loading it requires a relatively expensive Conduit call. $revision_symbol = $this->getRevisionSymbol(); if ($revision_symbol) { $symbol_ref = id(new ArcanistRevisionSymbolRef()) ->setSymbol($revision_symbol); $this->setRevisionSymbolRef($symbol_ref); } // NOTE: When a user provides: "--hold" or "--preview"; and "--incremental" // or various combinations of remote flags, the flags affecting push/remote // behavior have no effect. // These combinations are allowed to support adding "--preview" or "--hold" // to any command to run the same command with fewer side effects. } abstract protected function getDefaultSymbols(); abstract protected function resolveSymbols(array $symbols); abstract protected function selectOntoRemote(array $symbols); abstract protected function selectOntoRefs(array $symbols); abstract protected function confirmOntoRefs(array $onto_refs); abstract protected function selectIntoRemote(); abstract protected function selectIntoRef(); abstract protected function selectIntoCommit(); abstract protected function selectCommits($into_commit, array $symbols); abstract protected function executeMerge( ArcanistLandCommitSet $set, $into_commit); abstract protected function pushChange($into_commit); abstract protected function cascadeState( ArcanistLandCommitSet $set, $into_commit); protected function isSquashStrategy() { return ($this->getStrategy() === 'squash'); } abstract protected function pruneBranches(array $sets); abstract protected function reconcileLocalState( $into_commit, ArcanistRepositoryLocalState $state); abstract protected function didHoldChanges($into_commit); private function selectMergeStrategy() { $log = $this->getLogEngine(); $supported_strategies = array( 'merge', 'squash', ); $supported_strategies = array_fuse($supported_strategies); $strategy_list = implode(', ', $supported_strategies); $strategy = $this->getStrategyArgument(); if ($strategy !== null) { if (!isset($supported_strategies[$strategy])) { throw new PhutilArgumentUsageException( pht( 'Merge strategy "%s" specified with "--strategy" is unknown. '. 'Supported merge strategies are: %s.', $strategy, $strategy_list)); } $log->writeStatus( pht('STRATEGY'), pht( 'Merging with "%s" strategy, selected with "--strategy".', $strategy)); return $strategy; } $strategy = $this->getStrategyFromConfiguration(); if ($strategy !== null) { if (!isset($supported_strategies[$strategy])) { throw new PhutilArgumentUsageException( pht( 'Merge strategy "%s" specified in "%s" configuration is '. 'unknown. Supported merge strategies are: %s.', $strategy, $strategy_list)); } $log->writeStatus( pht('STRATEGY'), pht( 'Merging with "%s" strategy, configured with "%s".', $strategy, $this->getStrategyConfigurationKey())); return $strategy; } $strategy = 'squash'; $log->writeStatus( pht('STRATEGY'), pht( 'Merging with "%s" strategy, the default strategy.', $strategy)); return $strategy; } private function filterCommitSets(array $sets) { assert_instances_of($sets, 'ArcanistLandCommitSet'); $log = $this->getLogEngine(); // If some of the ancestor revisions are already closed, and the user did // not specifically indicate that we should land them, and we are using // a "squash" strategy, discard those sets. if ($this->isSquashStrategy()) { $discard = array(); foreach ($sets as $key => $set) { $revision_ref = $set->getRevisionRef(); if (!$revision_ref->isClosed()) { continue; } if ($set->hasDirectSymbols()) { continue; } $discard[] = $set; unset($sets[$key]); } if ($discard) { echo tsprintf( "\n%!\n%W\n", pht('DISCARDING ANCESTORS'), pht( 'Some ancestor commits are associated with revisions that have '. 'already been closed. These changes will be skipped:')); foreach ($discard as $set) { $this->printCommitSet($set); } echo tsprintf("\n"); } } // TODO: Some of the revisions we've identified may be mapped to an // outdated set of commits. We should look in local branches for a better // set of commits, and try to confirm that the state we're about to land // is the current state in Differential. $is_pick = $this->getPickArgument(); if ($is_pick) { foreach ($sets as $key => $set) { if ($set->hasDirectSymbols()) { $set->setIsPick(true); continue; } unset($sets[$key]); } } return $sets; } final protected function newPassthruCommand($pattern /* , ... */) { $workflow = $this->getWorkflow(); $argv = func_get_args(); $api = $this->getRepositoryAPI(); $passthru = call_user_func_array( array($api, 'newPassthru'), $argv); $command = $workflow->newCommand($passthru) ->setResolveOnError(true); return $command; } final protected function newPassthru($pattern /* , ... */) { $argv = func_get_args(); $command = call_user_func_array( array($this, 'newPassthruCommand'), $argv); return $command->execute(); } } diff --git a/src/ref/build/ArcanistBuildBuildplanHardpointQuery.php b/src/ref/build/ArcanistBuildBuildplanHardpointQuery.php index 3491c210..8e386527 100644 --- a/src/ref/build/ArcanistBuildBuildplanHardpointQuery.php +++ b/src/ref/build/ArcanistBuildBuildplanHardpointQuery.php @@ -1,44 +1,44 @@ yieldConduitSearch( 'harbormaster.buildplan.search', array( 'phids' => $plan_phids, ))); $plan_refs = array(); foreach ($plans as $plan) { $plan_ref = ArcanistBuildPlanRef::newFromConduit($plan); $plan_refs[] = $plan_ref; } - $plan_refs = mpull($plan_refs, 'getPHID'); + $plan_refs = mpull($plan_refs, null, 'getPHID'); $results = array(); foreach ($refs as $key => $build_ref) { $plan_phid = $build_ref->getBuildPlanPHID(); $plan = idx($plan_refs, $plan_phid); $results[$key] = $plan; } yield $this->yieldMap($results); } } diff --git a/src/ref/buildable/ArcanistBuildableRef.php b/src/ref/buildable/ArcanistBuildableRef.php index e9b376cc..bf19ebd1 100644 --- a/src/ref/buildable/ArcanistBuildableRef.php +++ b/src/ref/buildable/ArcanistBuildableRef.php @@ -1,63 +1,69 @@ newTemplateHardpoint( self::HARDPOINT_BUILDREFS, $object_list), ); } public function getRefDisplayName() { return pht('Buildable "%s"', $this->getMonogram()); } public static function newFromConduit(array $parameters) { $ref = new self(); $ref->parameters = $parameters; return $ref; } public function getID() { return idx($this->parameters, 'id'); } public function getPHID() { return idx($this->parameters, 'phid'); } - public function getName() { - return idxv($this->parameters, array('fields', 'name')); - } - public function getObjectPHID() { return idxv($this->parameters, array('fields', 'objectPHID')); } public function getMonogram() { return 'B'.$this->getID(); } public function getDisplayRefObjectName() { return $this->getMonogram(); } public function getDisplayRefTitle() { - return $this->getName(); + return pht('Buildable %d', $this->getID()); } public function getBuildRefs() { return $this->getHardpoint(self::HARDPOINT_BUILDREFS); } + public function getURI() { + $uri = idxv($this->parameters, array('fields', 'uri')); + + if ($uri === null) { + $uri = '/'.$this->getMonogram(); + } + + return $uri; + } + } diff --git a/src/workflow/ArcanistWorkflow.php b/src/workflow/ArcanistWorkflow.php index d50e8780..c6098ddb 100644 --- a/src/workflow/ArcanistWorkflow.php +++ b/src/workflow/ArcanistWorkflow.php @@ -1,2482 +1,2482 @@ toolset = $toolset; return $this; } final public function getToolset() { return $this->toolset; } final public function setRuntime(ArcanistRuntime $runtime) { $this->runtime = $runtime; return $this; } final public function getRuntime() { return $this->runtime; } final public function setConfigurationEngine( ArcanistConfigurationEngine $engine) { $this->configurationEngine = $engine; return $this; } final public function getConfigurationEngine() { return $this->configurationEngine; } final public function setConfigurationSourceList( ArcanistConfigurationSourceList $list) { $this->configurationSourceList = $list; return $this; } final public function getConfigurationSourceList() { return $this->configurationSourceList; } public function newPhutilWorkflow() { $arguments = $this->getWorkflowArguments(); assert_instances_of($arguments, 'ArcanistWorkflowArgument'); $specs = mpull($arguments, 'getPhutilSpecification'); $phutil_workflow = id(new ArcanistPhutilWorkflow()) ->setName($this->getWorkflowName()) ->setWorkflow($this) ->setArguments($specs); $information = $this->getWorkflowInformation(); if ($information !== null) { if (!($information instanceof ArcanistWorkflowInformation)) { throw new Exception( pht( 'Expected workflow ("%s", of class "%s") to return an '. '"ArcanistWorkflowInformation" object from call to '. '"getWorkflowInformation()", got %s.', $this->getWorkflowName(), get_class($this), phutil_describe_type($information))); } } if ($information) { $synopsis = $information->getSynopsis(); if (strlen($synopsis)) { $phutil_workflow->setSynopsis($synopsis); } $examples = $information->getExamples(); if ($examples) { $examples = implode("\n", $examples); $phutil_workflow->setExamples($examples); } $help = $information->getHelp(); if (strlen($help)) { // Unwrap linebreaks in the help text so we don't get weird formatting. $help = preg_replace("/(?<=\S)\n(?=\S)/", ' ', $help); $phutil_workflow->setHelp($help); } } return $phutil_workflow; } final public function newLegacyPhutilWorkflow() { $phutil_workflow = id(new ArcanistPhutilWorkflow()) ->setName($this->getWorkflowName()); $arguments = $this->getArguments(); $specs = array(); foreach ($arguments as $key => $argument) { if ($key == '*') { $key = $argument; $argument = array( 'wildcard' => true, ); } unset($argument['paramtype']); unset($argument['supports']); unset($argument['nosupport']); unset($argument['passthru']); unset($argument['conflict']); $spec = array( 'name' => $key, ) + $argument; $specs[] = $spec; } $phutil_workflow->setArguments($specs); $synopses = $this->getCommandSynopses(); $phutil_workflow->setSynopsis($synopses); $help = $this->getCommandHelp(); if (strlen($help)) { $phutil_workflow->setHelp($help); } return $phutil_workflow; } final protected function newWorkflowArgument($key) { return id(new ArcanistWorkflowArgument()) ->setKey($key); } final protected function newWorkflowInformation() { return new ArcanistWorkflowInformation(); } final public function executeWorkflow(PhutilArgumentParser $args) { $runtime = $this->getRuntime(); $this->arguments = $args; $caught = null; $runtime->pushWorkflow($this); try { $err = $this->runWorkflow($args); } catch (Exception $ex) { $caught = $ex; } try { $this->runWorkflowCleanup(); } catch (Exception $ex) { phlog($ex); } $runtime->popWorkflow(); if ($caught) { throw $caught; } return $err; } final public function getLogEngine() { return $this->getRuntime()->getLogEngine(); } protected function runWorkflowCleanup() { // TOOLSETS: Do we need this? return; } public function __construct() {} public function run() { throw new PhutilMethodNotImplementedException(); } /** * Finalizes any cleanup operations that need to occur regardless of * whether the command succeeded or failed. */ public function finalize() { $this->finalizeWorkingCopy(); } /** * Return the command used to invoke this workflow from the command like, * e.g. "help" for @{class:ArcanistHelpWorkflow}. * * @return string The command a user types to invoke this workflow. */ abstract public function getWorkflowName(); /** * Return console formatted string with all command synopses. * * @return string 6-space indented list of available command synopses. */ public function getCommandSynopses() { return array(); } /** * Return console formatted string with command help printed in `arc help`. * * @return string 10-space indented help to use the command. */ public function getCommandHelp() { return null; } public function supportsToolset(ArcanistToolset $toolset) { return false; } /* -( Conduit )------------------------------------------------------------ */ /** * Set the URI which the workflow will open a conduit connection to when * @{method:establishConduit} is called. Arcanist makes an effort to set * this by default for all workflows (by reading ##.arcconfig## and/or the * value of ##--conduit-uri##) even if they don't need Conduit, so a workflow * can generally upgrade into a conduit workflow later by just calling * @{method:establishConduit}. * * You generally should not need to call this method unless you are * specifically overriding the default URI. It is normally sufficient to * just invoke @{method:establishConduit}. * * NOTE: You can not call this after a conduit has been established. * * @param string The URI to open a conduit to when @{method:establishConduit} * is called. * @return this * @task conduit */ final public function setConduitURI($conduit_uri) { if ($this->conduit) { throw new Exception( pht( 'You can not change the Conduit URI after a '. 'conduit is already open.')); } $this->conduitURI = $conduit_uri; return $this; } /** * Returns the URI the conduit connection within the workflow uses. * * @return string * @task conduit */ final public function getConduitURI() { return $this->conduitURI; } /** * Open a conduit channel to the server which was previously configured by * calling @{method:setConduitURI}. Arcanist will do this automatically if * the workflow returns ##true## from @{method:requiresConduit}, or you can * later upgrade a workflow and build a conduit by invoking it manually. * * You must establish a conduit before you can make conduit calls. * * NOTE: You must call @{method:setConduitURI} before you can call this * method. * * @return this * @task conduit */ final public function establishConduit() { if ($this->conduit) { return $this; } if (!$this->conduitURI) { throw new Exception( pht( 'You must specify a Conduit URI with %s before you can '. 'establish a conduit.', 'setConduitURI()')); } $this->conduit = new ConduitClient($this->conduitURI); if ($this->conduitTimeout) { $this->conduit->setTimeout($this->conduitTimeout); } return $this; } final public function getConfigFromAnySource($key) { $source_list = $this->getConfigurationSourceList(); if ($source_list) { $value_list = $source_list->getStorageValueList($key); if ($value_list) { return last($value_list)->getValue(); } return null; } return $this->configurationManager->getConfigFromAnySource($key); } /** * Set credentials which will be used to authenticate against Conduit. These * credentials can then be used to establish an authenticated connection to * conduit by calling @{method:authenticateConduit}. Arcanist sets some * defaults for all workflows regardless of whether or not they return true * from @{method:requireAuthentication}, based on the ##~/.arcrc## and * ##.arcconf## files if they are present. Thus, you can generally upgrade a * workflow which does not require authentication into an authenticated * workflow by later invoking @{method:requireAuthentication}. You should not * normally need to call this method unless you are specifically overriding * the defaults. * * NOTE: You can not call this method after calling * @{method:authenticateConduit}. * * @param dict A credential dictionary, see @{method:authenticateConduit}. * @return this * @task conduit */ final public function setConduitCredentials(array $credentials) { if ($this->isConduitAuthenticated()) { throw new Exception( pht('You may not set new credentials after authenticating conduit.')); } $this->conduitCredentials = $credentials; return $this; } /** * Get the protocol version the client should identify with. * * @return int Version the client should claim to be. * @task conduit */ final public function getConduitVersion() { return 6; } /** * Open and authenticate a conduit connection to a Phabricator server using * provided credentials. Normally, Arcanist does this for you automatically * when you return true from @{method:requiresAuthentication}, but you can * also upgrade an existing workflow to one with an authenticated conduit * by invoking this method manually. * * You must authenticate the conduit before you can make authenticated conduit * calls (almost all calls require authentication). * * This method uses credentials provided via @{method:setConduitCredentials} * to authenticate to the server: * * - ##user## (required) The username to authenticate with. * - ##certificate## (required) The Conduit certificate to use. * - ##description## (optional) Description of the invoking command. * * Successful authentication allows you to call @{method:getUserPHID} and * @{method:getUserName}, as well as use the client you access with * @{method:getConduit} to make authenticated calls. * * NOTE: You must call @{method:setConduitURI} and * @{method:setConduitCredentials} before you invoke this method. * * @return this * @task conduit */ final public function authenticateConduit() { if ($this->isConduitAuthenticated()) { return $this; } $this->establishConduit(); $credentials = $this->conduitCredentials; try { if (!$credentials) { throw new Exception( pht( 'Set conduit credentials with %s before authenticating conduit!', 'setConduitCredentials()')); } // If we have `token`, this server supports the simpler, new-style // token-based authentication. Use that instead of all the certificate // stuff. $token = idx($credentials, 'token'); if (strlen($token)) { $conduit = $this->getConduit(); $conduit->setConduitToken($token); try { $result = $this->getConduit()->callMethodSynchronous( 'user.whoami', array()); $this->userName = $result['userName']; $this->userPHID = $result['phid']; $this->conduitAuthenticated = true; return $this; } catch (Exception $ex) { $conduit->setConduitToken(null); throw $ex; } } if (empty($credentials['user'])) { throw new ConduitClientException( 'ERR-INVALID-USER', pht('Empty user in credentials.')); } if (empty($credentials['certificate'])) { throw new ConduitClientException( 'ERR-NO-CERTIFICATE', pht('Empty certificate in credentials.')); } $description = idx($credentials, 'description', ''); $user = $credentials['user']; $certificate = $credentials['certificate']; $connection = $this->getConduit()->callMethodSynchronous( 'conduit.connect', array( 'client' => 'arc', 'clientVersion' => $this->getConduitVersion(), 'clientDescription' => php_uname('n').':'.$description, 'user' => $user, 'certificate' => $certificate, 'host' => $this->conduitURI, )); } catch (ConduitClientException $ex) { if ($ex->getErrorCode() == 'ERR-NO-CERTIFICATE' || $ex->getErrorCode() == 'ERR-INVALID-USER' || $ex->getErrorCode() == 'ERR-INVALID-AUTH') { $conduit_uri = $this->conduitURI; $message = phutil_console_format( "\n%s\n\n %s\n\n%s\n%s", pht('YOU NEED TO __INSTALL A CERTIFICATE__ TO LOGIN TO PHABRICATOR'), pht('To do this, run: **%s**', 'arc install-certificate'), pht("The server '%s' rejected your request:", $conduit_uri), $ex->getMessage()); throw new ArcanistUsageException($message); } else if ($ex->getErrorCode() == 'NEW-ARC-VERSION') { // Cleverly disguise this as being AWESOME!!! echo phutil_console_format("**%s**\n\n", pht('New Version Available!')); echo phutil_console_wrap($ex->getMessage()); echo "\n\n"; echo pht('In most cases, arc can be upgraded automatically.')."\n"; $ok = phutil_console_confirm( pht('Upgrade arc now?'), $default_no = false); if (!$ok) { throw $ex; } $root = dirname(phutil_get_library_root('arcanist')); chdir($root); $err = phutil_passthru('%s upgrade', $root.'/bin/arc'); if (!$err) { echo "\n".pht('Try running your arc command again.')."\n"; } exit(1); } else { throw $ex; } } $this->userName = $user; $this->userPHID = $connection['userPHID']; $this->conduitAuthenticated = true; return $this; } /** * @return bool True if conduit is authenticated, false otherwise. * @task conduit */ final protected function isConduitAuthenticated() { return (bool)$this->conduitAuthenticated; } /** * Override this to return true if your workflow requires a conduit channel. * Arc will build the channel for you before your workflow executes. This * implies that you only need an unauthenticated channel; if you need * authentication, override @{method:requiresAuthentication}. * * @return bool True if arc should build a conduit channel before running * the workflow. * @task conduit */ public function requiresConduit() { return false; } /** * Override this to return true if your workflow requires an authenticated * conduit channel. This implies that it requires a conduit. Arc will build * and authenticate the channel for you before the workflow executes. * * @return bool True if arc should build an authenticated conduit channel * before running the workflow. * @task conduit */ public function requiresAuthentication() { return false; } /** * Returns the PHID for the user once they've authenticated via Conduit. * * @return phid Authenticated user PHID. * @task conduit */ final public function getUserPHID() { if (!$this->userPHID) { $workflow = get_class($this); throw new Exception( pht( "This workflow ('%s') requires authentication, override ". "%s to return true.", $workflow, 'requiresAuthentication()')); } return $this->userPHID; } /** * Return the username for the user once they've authenticated via Conduit. * * @return string Authenticated username. * @task conduit */ final public function getUserName() { return $this->userName; } /** * Get the established @{class@libphutil:ConduitClient} in order to make * Conduit method calls. Before the client is available it must be connected, * either implicitly by making @{method:requireConduit} or * @{method:requireAuthentication} return true, or explicitly by calling * @{method:establishConduit} or @{method:authenticateConduit}. * * @return @{class@libphutil:ConduitClient} Live conduit client. * @task conduit */ final public function getConduit() { if (!$this->conduit) { $workflow = get_class($this); throw new Exception( pht( "This workflow ('%s') requires a Conduit, override ". "%s to return true.", $workflow, 'requiresConduit()')); } return $this->conduit; } final public function setArcanistConfiguration( ArcanistConfiguration $arcanist_configuration) { $this->arcanistConfiguration = $arcanist_configuration; return $this; } final public function getArcanistConfiguration() { return $this->arcanistConfiguration; } final public function setConfigurationManager( ArcanistConfigurationManager $arcanist_configuration_manager) { $this->configurationManager = $arcanist_configuration_manager; return $this; } final public function getConfigurationManager() { return $this->configurationManager; } public function requiresWorkingCopy() { return false; } public function desiresWorkingCopy() { return false; } public function requiresRepositoryAPI() { return false; } public function desiresRepositoryAPI() { return false; } final public function setCommand($command) { $this->command = $command; return $this; } final public function getCommand() { return $this->command; } public function getArguments() { return array(); } final public function setWorkingDirectory($working_directory) { $this->workingDirectory = $working_directory; return $this; } final public function getWorkingDirectory() { return $this->workingDirectory; } final private function setParentWorkflow($parent_workflow) { $this->parentWorkflow = $parent_workflow; return $this; } final protected function getParentWorkflow() { return $this->parentWorkflow; } final public function buildChildWorkflow($command, array $argv) { $arc_config = $this->getArcanistConfiguration(); $workflow = $arc_config->buildWorkflow($command); $workflow->setParentWorkflow($this); $workflow->setConduitEngine($this->getConduitEngine()); $workflow->setCommand($command); $workflow->setConfigurationManager($this->getConfigurationManager()); if ($this->repositoryAPI) { $workflow->setRepositoryAPI($this->repositoryAPI); } if ($this->userPHID) { $workflow->userPHID = $this->getUserPHID(); $workflow->userName = $this->getUserName(); } if ($this->conduit) { $workflow->conduit = $this->conduit; $workflow->setConduitCredentials($this->conduitCredentials); $workflow->conduitAuthenticated = $this->conduitAuthenticated; } $workflow->setArcanistConfiguration($arc_config); $workflow->parseArguments(array_values($argv)); return $workflow; } final public function getArgument($key, $default = null) { // TOOLSETS: Remove this legacy code. if (is_array($this->arguments)) { return idx($this->arguments, $key, $default); } return $this->arguments->getArg($key); } final public function getCompleteArgumentSpecification() { $spec = $this->getArguments(); $arc_config = $this->getArcanistConfiguration(); $command = $this->getCommand(); $spec += $arc_config->getCustomArgumentsForCommand($command); return $spec; } final public function parseArguments(array $args) { $spec = $this->getCompleteArgumentSpecification(); $dict = array(); $more_key = null; if (!empty($spec['*'])) { $more_key = $spec['*']; unset($spec['*']); $dict[$more_key] = array(); } $short_to_long_map = array(); foreach ($spec as $long => $options) { if (!empty($options['short'])) { $short_to_long_map[$options['short']] = $long; } } foreach ($spec as $long => $options) { if (!empty($options['repeat'])) { $dict[$long] = array(); } } $more = array(); $size = count($args); for ($ii = 0; $ii < $size; $ii++) { $arg = $args[$ii]; $arg_name = null; $arg_key = null; if ($arg == '--') { $more = array_merge( $more, array_slice($args, $ii + 1)); break; } else if (!strncmp($arg, '--', 2)) { $arg_key = substr($arg, 2); $parts = explode('=', $arg_key, 2); if (count($parts) == 2) { list($arg_key, $val) = $parts; array_splice($args, $ii, 1, array('--'.$arg_key, $val)); $size++; } if (!array_key_exists($arg_key, $spec)) { $corrected = PhutilArgumentSpellingCorrector::newFlagCorrector() ->correctSpelling($arg_key, array_keys($spec)); if (count($corrected) == 1) { PhutilConsole::getConsole()->writeErr( pht( "(Assuming '%s' is the British spelling of '%s'.)", '--'.$arg_key, '--'.head($corrected))."\n"); $arg_key = head($corrected); } else { throw new ArcanistUsageException( pht( "Unknown argument '%s'. Try '%s'.", $arg_key, 'arc help')); } } } else if (!strncmp($arg, '-', 1)) { $arg_key = substr($arg, 1); if (empty($short_to_long_map[$arg_key])) { throw new ArcanistUsageException( pht( "Unknown argument '%s'. Try '%s'.", $arg_key, 'arc help')); } $arg_key = $short_to_long_map[$arg_key]; } else { $more[] = $arg; continue; } $options = $spec[$arg_key]; if (empty($options['param'])) { $dict[$arg_key] = true; } else { if ($ii == $size - 1) { throw new ArcanistUsageException( pht( "Option '%s' requires a parameter.", $arg)); } if (!empty($options['repeat'])) { $dict[$arg_key][] = $args[$ii + 1]; } else { $dict[$arg_key] = $args[$ii + 1]; } $ii++; } } if ($more) { if ($more_key) { $dict[$more_key] = $more; } else { $example = reset($more); throw new ArcanistUsageException( pht( "Unrecognized argument '%s'. Try '%s'.", $example, 'arc help')); } } foreach ($dict as $key => $value) { if (empty($spec[$key]['conflicts'])) { continue; } foreach ($spec[$key]['conflicts'] as $conflict => $more) { if (isset($dict[$conflict])) { if ($more) { $more = ': '.$more; } else { $more = '.'; } // TODO: We'll always display these as long-form, when the user might // have typed them as short form. throw new ArcanistUsageException( pht( "Arguments '%s' and '%s' are mutually exclusive", "--{$key}", "--{$conflict}").$more); } } } $this->arguments = $dict; $this->didParseArguments(); return $this; } protected function didParseArguments() { // Override this to customize workflow argument behavior. } final public function getWorkingCopy() { $configuration_engine = $this->getConfigurationEngine(); // TOOLSETS: Remove this once all workflows are toolset workflows. if (!$configuration_engine) { throw new Exception( pht( 'This workflow has not yet been updated to Toolsets and can '. 'not retrieve a modern WorkingCopy object. Use '. '"getWorkingCopyIdentity()" to retrieve a previous-generation '. 'object.')); } return $configuration_engine->getWorkingCopy(); } final public function getWorkingCopyIdentity() { $configuration_engine = $this->getConfigurationEngine(); if ($configuration_engine) { $working_copy = $configuration_engine->getWorkingCopy(); $working_path = $working_copy->getWorkingDirectory(); return ArcanistWorkingCopyIdentity::newFromPath($working_path); } $working_copy = $this->getConfigurationManager()->getWorkingCopyIdentity(); if (!$working_copy) { $workflow = get_class($this); throw new Exception( pht( "This workflow ('%s') requires a working copy, override ". "%s to return true.", $workflow, 'requiresWorkingCopy()')); } return $working_copy; } final public function setRepositoryAPI($api) { $this->repositoryAPI = $api; return $this; } final public function hasRepositoryAPI() { try { return (bool)$this->getRepositoryAPI(); } catch (Exception $ex) { return false; } } final public function getRepositoryAPI() { $configuration_engine = $this->getConfigurationEngine(); if ($configuration_engine) { $working_copy = $configuration_engine->getWorkingCopy(); return $working_copy->getRepositoryAPI(); } if (!$this->repositoryAPI) { $workflow = get_class($this); throw new Exception( pht( "This workflow ('%s') requires a Repository API, override ". "%s to return true.", $workflow, 'requiresRepositoryAPI()')); } return $this->repositoryAPI; } final protected function shouldRequireCleanUntrackedFiles() { return empty($this->arguments['allow-untracked']); } final public function setCommitMode($mode) { $this->commitMode = $mode; return $this; } final public function finalizeWorkingCopy() { if ($this->stashed) { $api = $this->getRepositoryAPI(); $api->unstashChanges(); echo pht('Restored stashed changes to the working directory.')."\n"; } } final public function requireCleanWorkingCopy() { $api = $this->getRepositoryAPI(); $must_commit = array(); $working_copy_desc = phutil_console_format( " %s: __%s__\n\n", pht('Working copy'), $api->getPath()); // NOTE: this is a subversion-only concept. $incomplete = $api->getIncompleteChanges(); if ($incomplete) { throw new ArcanistUsageException( sprintf( "%s\n\n%s %s\n %s\n\n%s", pht( "You have incompletely checked out directories in this working ". "copy. Fix them before proceeding.'"), $working_copy_desc, pht('Incomplete directories in working copy:'), implode("\n ", $incomplete), pht( "You can fix these paths by running '%s' on them.", 'svn update'))); } $conflicts = $api->getMergeConflicts(); if ($conflicts) { throw new ArcanistUsageException( sprintf( "%s\n\n%s %s\n %s", pht( 'You have merge conflicts in this working copy. Resolve merge '. 'conflicts before proceeding.'), $working_copy_desc, pht('Conflicts in working copy:'), implode("\n ", $conflicts))); } $missing = $api->getMissingChanges(); if ($missing) { throw new ArcanistUsageException( sprintf( "%s\n\n%s %s\n %s\n", pht( 'You have missing files in this working copy. Revert or formally '. 'remove them (with `%s`) before proceeding.', 'svn rm'), $working_copy_desc, pht('Missing files in working copy:'), implode("\n ", $missing))); } $externals = $api->getDirtyExternalChanges(); // TODO: This state can exist in Subversion, but it is currently handled // elsewhere. It should probably be handled here, eventually. if ($api instanceof ArcanistSubversionAPI) { $externals = array(); } if ($externals) { $message = pht( '%s submodule(s) have uncommitted or untracked changes:', new PhutilNumber(count($externals))); $prompt = pht( 'Ignore the changes to these %s submodule(s) and continue?', new PhutilNumber(count($externals))); $list = id(new PhutilConsoleList()) ->setWrap(false) ->addItems($externals); id(new PhutilConsoleBlock()) ->addParagraph($message) ->addList($list) ->draw(); $ok = phutil_console_confirm($prompt, $default_no = false); if (!$ok) { throw new ArcanistUserAbortException(); } } $uncommitted = $api->getUncommittedChanges(); $unstaged = $api->getUnstagedChanges(); // We already dealt with externals. $unstaged = array_diff($unstaged, $externals); // We only want files which are purely uncommitted. $uncommitted = array_diff($uncommitted, $unstaged); $uncommitted = array_diff($uncommitted, $externals); $untracked = $api->getUntrackedChanges(); if (!$this->shouldRequireCleanUntrackedFiles()) { $untracked = array(); } if ($untracked) { echo sprintf( "%s\n\n%s", pht('You have untracked files in this working copy.'), $working_copy_desc); if ($api instanceof ArcanistGitAPI) { $hint = pht( '(To ignore these %s change(s), add them to "%s".)', phutil_count($untracked), '.git/info/exclude'); } else if ($api instanceof ArcanistSubversionAPI) { $hint = pht( '(To ignore these %s change(s), add them to "%s".)', phutil_count($untracked), 'svn:ignore'); } else if ($api instanceof ArcanistMercurialAPI) { $hint = pht( '(To ignore these %s change(s), add them to "%s".)', phutil_count($untracked), '.hgignore'); } $untracked_list = " ".implode("\n ", $untracked); echo sprintf( " %s\n %s\n%s", pht('Untracked changes in working copy:'), $hint, $untracked_list); $prompt = pht( 'Ignore these %s untracked file(s) and continue?', phutil_count($untracked)); if (!phutil_console_confirm($prompt)) { throw new ArcanistUserAbortException(); } } $should_commit = false; if ($unstaged || $uncommitted) { // NOTE: We're running this because it builds a cache and can take a // perceptible amount of time to arrive at an answer, but we don't want // to pause in the middle of printing the output below. $this->getShouldAmend(); echo sprintf( "%s\n\n%s", pht('You have uncommitted changes in this working copy.'), $working_copy_desc); $lists = array(); if ($unstaged) { $unstaged_list = " ".implode("\n ", $unstaged); $lists[] = sprintf( " %s\n%s", pht('Unstaged changes in working copy:'), $unstaged_list); } if ($uncommitted) { $uncommitted_list = " ".implode("\n ", $uncommitted); $lists[] = sprintf( "%s\n%s", pht('Uncommitted changes in working copy:'), $uncommitted_list); } echo implode("\n\n", $lists)."\n"; $all_uncommitted = array_merge($unstaged, $uncommitted); if ($this->askForAdd($all_uncommitted)) { if ($unstaged) { $api->addToCommit($unstaged); } $should_commit = true; } else { $permit_autostash = $this->getConfigFromAnySource('arc.autostash'); if ($permit_autostash && $api->canStashChanges()) { echo pht( 'Stashing uncommitted changes. (You can restore them with `%s`).', 'git stash pop')."\n"; $api->stashChanges(); $this->stashed = true; } else { throw new ArcanistUsageException( pht( 'You can not continue with uncommitted changes. '. 'Commit or discard them before proceeding.')); } } } if ($should_commit) { if ($this->getShouldAmend()) { $commit = head($api->getLocalCommitInformation()); $api->amendCommit($commit['message']); } else if ($api->supportsLocalCommits()) { $template = sprintf( "\n\n# %s\n#\n# %s\n#\n", pht('Enter a commit message.'), pht('Changes:')); $paths = array_merge($uncommitted, $unstaged); $paths = array_unique($paths); sort($paths); foreach ($paths as $path) { $template .= "# ".$path."\n"; } $commit_message = $this->newInteractiveEditor($template) ->setName(pht('commit-message')) ->editInteractively(); if ($commit_message === $template) { throw new ArcanistUsageException( pht('You must provide a commit message.')); } $commit_message = ArcanistCommentRemover::removeComments( $commit_message); if (!strlen($commit_message)) { throw new ArcanistUsageException( pht('You must provide a nonempty commit message.')); } $api->doCommit($commit_message); } } } private function getShouldAmend() { if ($this->shouldAmend === null) { $this->shouldAmend = $this->calculateShouldAmend(); } return $this->shouldAmend; } private function calculateShouldAmend() { $api = $this->getRepositoryAPI(); if ($this->isHistoryImmutable() || !$api->supportsAmend()) { return false; } $commits = $api->getLocalCommitInformation(); if (!$commits) { return false; } $commit = reset($commits); $message = ArcanistDifferentialCommitMessage::newFromRawCorpus( $commit['message']); if ($message->getGitSVNBaseRevision()) { return false; } if ($api->getAuthor() != $commit['author']) { return false; } if ($message->getRevisionID() && $this->getArgument('create')) { return false; } // TODO: Check commits since tracking branch. If empty then return false. // Don't amend the current commit if it has already been published. $repository = $this->loadProjectRepository(); if ($repository) { $repo_id = $repository['id']; $commit_hash = $commit['commit']; $callsign = idx($repository, 'callsign'); if ($callsign) { // The server might be too old to support the new style commit names, // so prefer the old way $commit_name = "r{$callsign}{$commit_hash}"; } else { $commit_name = "R{$repo_id}:{$commit_hash}"; } $result = $this->getConduit()->callMethodSynchronous( 'diffusion.querycommits', array('names' => array($commit_name))); $known_commit = idx($result['identifierMap'], $commit_name); if ($known_commit) { return false; } } if (!$message->getRevisionID()) { return true; } $in_working_copy = $api->loadWorkingCopyDifferentialRevisions( $this->getConduit(), array( 'authors' => array($this->getUserPHID()), 'status' => 'status-open', )); if ($in_working_copy) { return true; } return false; } private function askForAdd(array $files) { if ($this->commitMode == self::COMMIT_DISABLE) { return false; } if ($this->commitMode == self::COMMIT_ENABLE) { return true; } $prompt = $this->getAskForAddPrompt($files); return phutil_console_confirm($prompt); } private function getAskForAddPrompt(array $files) { if ($this->getShouldAmend()) { $prompt = pht( 'Do you want to amend these %s change(s) to the current commit?', phutil_count($files)); } else { $prompt = pht( 'Do you want to create a new commit with these %s change(s)?', phutil_count($files)); } return $prompt; } final protected function loadDiffBundleFromConduit( ConduitClient $conduit, $diff_id) { return $this->loadBundleFromConduit( $conduit, array( 'ids' => array($diff_id), )); } final protected function loadRevisionBundleFromConduit( ConduitClient $conduit, $revision_id) { return $this->loadBundleFromConduit( $conduit, array( 'revisionIDs' => array($revision_id), )); } final private function loadBundleFromConduit( ConduitClient $conduit, $params) { $future = $conduit->callMethod('differential.querydiffs', $params); $diff = head($future->resolve()); if ($diff == null) { throw new Exception( phutil_console_wrap( pht("The diff or revision you specified is either invalid or you ". "don't have permission to view it.")) ); } $changes = array(); foreach ($diff['changes'] as $changedict) { $changes[] = ArcanistDiffChange::newFromDictionary($changedict); } $bundle = ArcanistBundle::newFromChanges($changes); $bundle->setConduit($conduit); // since the conduit method has changes, assume that these fields // could be unset $bundle->setBaseRevision(idx($diff, 'sourceControlBaseRevision')); $bundle->setRevisionID(idx($diff, 'revisionID')); $bundle->setAuthorName(idx($diff, 'authorName')); $bundle->setAuthorEmail(idx($diff, 'authorEmail')); return $bundle; } /** * Return a list of lines changed by the current diff, or ##null## if the * change list is meaningless (for example, because the path is a directory * or binary file). * * @param string Path within the repository. * @param string Change selection mode (see ArcanistDiffHunk). * @return list|null List of changed line numbers, or null to indicate that * the path is not a line-oriented text file. */ final protected function getChangedLines($path, $mode) { $repository_api = $this->getRepositoryAPI(); $full_path = $repository_api->getPath($path); if (is_dir($full_path)) { return null; } if (!file_exists($full_path)) { return null; } $change = $this->getChange($path); if ($change->getFileType() !== ArcanistDiffChangeType::FILE_TEXT) { return null; } $lines = $change->getChangedLines($mode); return array_keys($lines); } final protected function getChange($path) { $repository_api = $this->getRepositoryAPI(); // TODO: Very gross $is_git = ($repository_api instanceof ArcanistGitAPI); $is_hg = ($repository_api instanceof ArcanistMercurialAPI); $is_svn = ($repository_api instanceof ArcanistSubversionAPI); if ($is_svn) { // NOTE: In SVN, we don't currently support a "get all local changes" // operation, so special case it. if (empty($this->changeCache[$path])) { $diff = $repository_api->getRawDiffText($path); $parser = $this->newDiffParser(); $changes = $parser->parseDiff($diff); if (count($changes) != 1) { throw new Exception(pht('Expected exactly one change.')); } $this->changeCache[$path] = reset($changes); } } else if ($is_git || $is_hg) { if (empty($this->changeCache)) { $changes = $repository_api->getAllLocalChanges(); foreach ($changes as $change) { $this->changeCache[$change->getCurrentPath()] = $change; } } } else { throw new Exception(pht('Missing VCS support.')); } if (empty($this->changeCache[$path])) { if ($is_git || $is_hg) { // This can legitimately occur under git/hg if you make a change, // "git/hg commit" it, and then revert the change in the working copy // and run "arc lint". $change = new ArcanistDiffChange(); $change->setCurrentPath($path); return $change; } else { throw new Exception( pht( "Trying to get change for unchanged path '%s'!", $path)); } } return $this->changeCache[$path]; } final public function willRunWorkflow() { $spec = $this->getCompleteArgumentSpecification(); foreach ($this->arguments as $arg => $value) { if (empty($spec[$arg])) { continue; } $options = $spec[$arg]; if (!empty($options['supports'])) { $system_name = $this->getRepositoryAPI()->getSourceControlSystemName(); if (!in_array($system_name, $options['supports'])) { $extended_info = null; if (!empty($options['nosupport'][$system_name])) { $extended_info = ' '.$options['nosupport'][$system_name]; } throw new ArcanistUsageException( pht( "Option '%s' is not supported under %s.", "--{$arg}", $system_name). $extended_info); } } } } final protected function normalizeRevisionID($revision_id) { return preg_replace('/^D/i', '', $revision_id); } protected function shouldShellComplete() { return true; } protected function getShellCompletions(array $argv) { return array(); } public function getSupportedRevisionControlSystems() { return array('git', 'hg', 'svn'); } final protected function getPassthruArgumentsAsMap($command) { $map = array(); foreach ($this->getCompleteArgumentSpecification() as $key => $spec) { if (!empty($spec['passthru'][$command])) { if (isset($this->arguments[$key])) { $map[$key] = $this->arguments[$key]; } } } return $map; } final protected function getPassthruArgumentsAsArgv($command) { $spec = $this->getCompleteArgumentSpecification(); $map = $this->getPassthruArgumentsAsMap($command); $argv = array(); foreach ($map as $key => $value) { $argv[] = '--'.$key; if (!empty($spec[$key]['param'])) { $argv[] = $value; } } return $argv; } /** * Write a message to stderr so that '--json' flags or stdout which is meant * to be piped somewhere aren't disrupted. * * @param string Message to write to stderr. * @return void */ final protected function writeStatusMessage($msg) { fwrite(STDERR, $msg); } final public function writeInfo($title, $message) { $this->writeStatusMessage( phutil_console_format( "** %s ** %s\n", $title, $message)); } final public function writeWarn($title, $message) { $this->writeStatusMessage( phutil_console_format( "** %s ** %s\n", $title, $message)); } final public function writeOkay($title, $message) { $this->writeStatusMessage( phutil_console_format( "** %s ** %s\n", $title, $message)); } final protected function isHistoryImmutable() { $repository_api = $this->getRepositoryAPI(); $config = $this->getConfigFromAnySource('history.immutable'); if ($config !== null) { return $config; } return $repository_api->isHistoryDefaultImmutable(); } /** * Workflows like 'lint' and 'unit' operate on a list of working copy paths. * The user can either specify the paths explicitly ("a.js b.php"), or by * specifying a revision ("--rev a3f10f1f") to select all paths modified * since that revision, or by omitting both and letting arc choose the * default relative revision. * * This method takes the user's selections and returns the paths that the * workflow should act upon. * * @param list List of explicitly provided paths. * @param string|null Revision name, if provided. * @param mask Mask of ArcanistRepositoryAPI flags to exclude. * Defaults to ArcanistRepositoryAPI::FLAG_UNTRACKED. * @return list List of paths the workflow should act on. */ final protected function selectPathsForWorkflow( array $paths, $rev, $omit_mask = null) { if ($omit_mask === null) { $omit_mask = ArcanistRepositoryAPI::FLAG_UNTRACKED; } if ($paths) { $working_copy = $this->getWorkingCopyIdentity(); foreach ($paths as $key => $path) { $full_path = Filesystem::resolvePath($path); if (!Filesystem::pathExists($full_path)) { throw new ArcanistUsageException( pht( "Path '%s' does not exist!", $path)); } $relative_path = Filesystem::readablePath( $full_path, $working_copy->getProjectRoot()); $paths[$key] = $relative_path; } } else { $repository_api = $this->getRepositoryAPI(); if ($rev) { $this->parseBaseCommitArgument(array($rev)); } $paths = $repository_api->getWorkingCopyStatus(); foreach ($paths as $path => $flags) { if ($flags & $omit_mask) { unset($paths[$path]); } } $paths = array_keys($paths); } return array_values($paths); } final protected function renderRevisionList(array $revisions) { $list = array(); foreach ($revisions as $revision) { $list[] = ' - D'.$revision['id'].': '.$revision['title']."\n"; } return implode('', $list); } /* -( Scratch Files )------------------------------------------------------ */ /** * Try to read a scratch file, if it exists and is readable. * * @param string Scratch file name. * @return mixed String for file contents, or false for failure. * @task scratch */ final protected function readScratchFile($path) { if (!$this->repositoryAPI) { return false; } return $this->getRepositoryAPI()->readScratchFile($path); } /** * Try to read a scratch JSON file, if it exists and is readable. * * @param string Scratch file name. * @return array Empty array for failure. * @task scratch */ final protected function readScratchJSONFile($path) { $file = $this->readScratchFile($path); if (!$file) { return array(); } return phutil_json_decode($file); } /** * Try to write a scratch file, if there's somewhere to put it and we can * write there. * * @param string Scratch file name to write. * @param string Data to write. * @return bool True on success, false on failure. * @task scratch */ final protected function writeScratchFile($path, $data) { if (!$this->repositoryAPI) { return false; } return $this->getRepositoryAPI()->writeScratchFile($path, $data); } /** * Try to write a scratch JSON file, if there's somewhere to put it and we can * write there. * * @param string Scratch file name to write. * @param array Data to write. * @return bool True on success, false on failure. * @task scratch */ final protected function writeScratchJSONFile($path, array $data) { return $this->writeScratchFile($path, json_encode($data)); } /** * Try to remove a scratch file. * * @param string Scratch file name to remove. * @return bool True if the file was removed successfully. * @task scratch */ final protected function removeScratchFile($path) { if (!$this->repositoryAPI) { return false; } return $this->getRepositoryAPI()->removeScratchFile($path); } /** * Get a human-readable description of the scratch file location. * * @param string Scratch file name. * @return mixed String, or false on failure. * @task scratch */ final protected function getReadableScratchFilePath($path) { if (!$this->repositoryAPI) { return false; } return $this->getRepositoryAPI()->getReadableScratchFilePath($path); } /** * Get the path to a scratch file, if possible. * * @param string Scratch file name. * @return mixed File path, or false on failure. * @task scratch */ final protected function getScratchFilePath($path) { if (!$this->repositoryAPI) { return false; } return $this->getRepositoryAPI()->getScratchFilePath($path); } final protected function getRepositoryEncoding() { return nonempty( idx($this->loadProjectRepository(), 'encoding'), 'UTF-8'); } final protected function loadProjectRepository() { list($info, $reasons) = $this->loadRepositoryInformation(); return coalesce($info, array()); } final protected function newInteractiveEditor($text) { $editor = new PhutilInteractiveEditor($text); $preferred = $this->getConfigFromAnySource('editor'); if ($preferred) { $editor->setPreferredEditor($preferred); } return $editor; } final protected function newDiffParser() { $parser = new ArcanistDiffParser(); if ($this->repositoryAPI) { $parser->setRepositoryAPI($this->getRepositoryAPI()); } $parser->setWriteDiffOnFailure(true); return $parser; } final protected function resolveCall(ConduitFuture $method) { try { return $method->resolve(); } catch (ConduitClientException $ex) { if ($ex->getErrorCode() == 'ERR-CONDUIT-CALL') { echo phutil_console_wrap( pht( 'This feature requires a newer version of Phabricator. Please '. 'update it using these instructions: %s', 'https://secure.phabricator.com/book/phabricator/article/'. 'upgrading/')."\n\n"); } throw $ex; } } final protected function dispatchEvent($type, array $data) { $data += array( 'workflow' => $this, ); $event = new PhutilEvent($type, $data); PhutilEventEngine::dispatchEvent($event); return $event; } final public function parseBaseCommitArgument(array $argv) { if (!count($argv)) { return; } $api = $this->getRepositoryAPI(); if (!$api->supportsCommitRanges()) { throw new ArcanistUsageException( pht('This version control system does not support commit ranges.')); } if (count($argv) > 1) { throw new ArcanistUsageException( pht( 'Specify exactly one base commit. The end of the commit range is '. 'always the working copy state.')); } $api->setBaseCommit(head($argv)); return $this; } final protected function getRepositoryVersion() { if (!$this->repositoryVersion) { $api = $this->getRepositoryAPI(); $commit = $api->getSourceControlBaseRevision(); $versions = array('' => $commit); foreach ($api->getChangedFiles($commit) as $path => $mask) { $versions[$path] = (Filesystem::pathExists($path) ? md5_file($path) : ''); } $this->repositoryVersion = md5(json_encode($versions)); } return $this->repositoryVersion; } /* -( Phabricator Repositories )------------------------------------------- */ /** * Get the PHID of the Phabricator repository this working copy corresponds * to. Returns `null` if no repository can be identified. * * @return phid|null Repository PHID, or null if no repository can be * identified. * * @task phabrep */ final protected function getRepositoryPHID() { return idx($this->getRepositoryInformation(), 'phid'); } /** * Get the name of the Phabricator repository this working copy * corresponds to. Returns `null` if no repository can be identified. * * @return string|null Repository name, or null if no repository can be * identified. * * @task phabrep */ final protected function getRepositoryName() { return idx($this->getRepositoryInformation(), 'name'); } /** * Get the URI of the Phabricator repository this working copy * corresponds to. Returns `null` if no repository can be identified. * * @return string|null Repository URI, or null if no repository can be * identified. * * @task phabrep */ final protected function getRepositoryURI() { return idx($this->getRepositoryInformation(), 'uri'); } final protected function getRepositoryStagingConfiguration() { return idx($this->getRepositoryInformation(), 'staging'); } /** * Get human-readable reasoning explaining how `arc` evaluated which * Phabricator repository corresponds to this working copy. Used by * `arc which` to explain the process to users. * * @return list Human-readable explanation of the repository * association process. * * @task phabrep */ final protected function getRepositoryReasons() { $this->getRepositoryInformation(); return $this->repositoryReasons; } /** * @task phabrep */ private function getRepositoryInformation() { if ($this->repositoryInfo === null) { list($info, $reasons) = $this->loadRepositoryInformation(); $this->repositoryInfo = nonempty($info, array()); $this->repositoryReasons = $reasons; } return $this->repositoryInfo; } /** * @task phabrep */ private function loadRepositoryInformation() { list($query, $reasons) = $this->getRepositoryQuery(); if (!$query) { return array(null, $reasons); } try { $method = 'repository.query'; $results = $this->getConduitEngine()->newCall($method, $query) ->resolve(); } catch (ConduitClientException $ex) { if ($ex->getErrorCode() == 'ERR-CONDUIT-CALL') { $reasons[] = pht( 'This version of Arcanist is more recent than the version of '. 'Phabricator you are connecting to: the Phabricator install is '. 'out of date and does not have support for identifying '. 'repositories by callsign or URI. Update Phabricator to enable '. 'these features.'); return array(null, $reasons); } throw $ex; } $result = null; if (!$results) { $reasons[] = pht( 'No repositories matched the query. Check that your configuration '. 'is correct, or use "%s" to select a repository explicitly.', 'repository.callsign'); } else if (count($results) > 1) { $reasons[] = pht( 'Multiple repostories (%s) matched the query. You can use the '. '"%s" configuration to select the one you want.', implode(', ', ipull($results, 'callsign')), 'repository.callsign'); } else { $result = head($results); $reasons[] = pht('Found a unique matching repository.'); } return array($result, $reasons); } /** * @task phabrep */ private function getRepositoryQuery() { $reasons = array(); $callsign = $this->getConfigFromAnySource('repository.callsign'); if ($callsign) { $query = array( 'callsigns' => array($callsign), ); $reasons[] = pht( 'Configuration value "%s" is set to "%s".', 'repository.callsign', $callsign); return array($query, $reasons); } else { $reasons[] = pht( 'Configuration value "%s" is empty.', 'repository.callsign'); } $uuid = $this->getRepositoryAPI()->getRepositoryUUID(); if ($uuid !== null) { $query = array( 'uuids' => array($uuid), ); $reasons[] = pht( 'The UUID for this working copy is "%s".', $uuid); return array($query, $reasons); } else { $reasons[] = pht( 'This repository has no VCS UUID (this is normal for git/hg).'); } // TODO: Swap this for a RemoteRefQuery. $remote_uri = $this->getRepositoryAPI()->getRemoteURI(); if ($remote_uri !== null) { $query = array( 'remoteURIs' => array($remote_uri), ); $reasons[] = pht( 'The remote URI for this working copy is "%s".', $remote_uri); return array($query, $reasons); } else { $reasons[] = pht( 'Unable to determine the remote URI for this repository.'); } return array(null, $reasons); } /** * Build a new lint engine for the current working copy. * * Optionally, you can pass an explicit engine class name to build an engine * of a particular class. Normally this is used to implement an `--engine` * flag from the CLI. * * @param string Optional explicit engine class name. * @return ArcanistLintEngine Constructed engine. */ protected function newLintEngine($engine_class = null) { $working_copy = $this->getWorkingCopyIdentity(); $config = $this->getConfigurationManager(); if (!$engine_class) { $engine_class = $config->getConfigFromAnySource('lint.engine'); } if (!$engine_class) { if (Filesystem::pathExists($working_copy->getProjectPath('.arclint'))) { $engine_class = 'ArcanistConfigurationDrivenLintEngine'; } } if (!$engine_class) { throw new ArcanistNoEngineException( pht( "No lint engine is configured for this project. Create an '%s' ". "file, or configure an advanced engine with '%s' in '%s'.", '.arclint', 'lint.engine', '.arcconfig')); } $base_class = 'ArcanistLintEngine'; if (!class_exists($engine_class) || !is_subclass_of($engine_class, $base_class)) { throw new ArcanistUsageException( pht( 'Configured lint engine "%s" is not a subclass of "%s", but must be.', $engine_class, $base_class)); } $engine = newv($engine_class, array()) ->setWorkingCopy($working_copy) ->setConfigurationManager($config); return $engine; } /** * Build a new unit test engine for the current working copy. * * Optionally, you can pass an explicit engine class name to build an engine * of a particular class. Normally this is used to implement an `--engine` * flag from the CLI. * * @param string Optional explicit engine class name. * @return ArcanistUnitTestEngine Constructed engine. */ protected function newUnitTestEngine($engine_class = null) { $working_copy = $this->getWorkingCopyIdentity(); $config = $this->getConfigurationManager(); if (!$engine_class) { $engine_class = $config->getConfigFromAnySource('unit.engine'); } if (!$engine_class) { if (Filesystem::pathExists($working_copy->getProjectPath('.arcunit'))) { $engine_class = 'ArcanistConfigurationDrivenUnitTestEngine'; } } if (!$engine_class) { throw new ArcanistNoEngineException( pht( "No unit test engine is configured for this project. Create an ". "'%s' file, or configure an advanced engine with '%s' in '%s'.", '.arcunit', 'unit.engine', '.arcconfig')); } $base_class = 'ArcanistUnitTestEngine'; if (!class_exists($engine_class) || !is_subclass_of($engine_class, $base_class)) { throw new ArcanistUsageException( pht( 'Configured unit test engine "%s" is not a subclass of "%s", '. 'but must be.', $engine_class, $base_class)); } $engine = newv($engine_class, array()) ->setWorkingCopy($working_copy) ->setConfigurationManager($config); return $engine; } protected function openURIsInBrowser(array $uris) { $browser = $this->getBrowserCommand(); // The "browser" may actually be a list of arguments. if (!is_array($browser)) { $browser = array($browser); } foreach ($uris as $uri) { $err = phutil_passthru('%LR %R', $browser, $uri); if ($err) { throw new ArcanistUsageException( pht( 'Failed to open URI "%s" in browser ("%s"). '. 'Check your "browser" config option.', $uri, implode(' ', $browser))); } } } private function getBrowserCommand() { $config = $this->getConfigFromAnySource('browser'); if ($config) { return $config; } if (phutil_is_windows()) { // See T13504. We now use "bypass_shell", so "start" alone is no longer // a valid binary to invoke directly. return array( 'cmd', '/c', 'start', ); } $candidates = array( 'sensible-browser' => array('sensible-browser'), 'xdg-open' => array('xdg-open'), 'open' => array('open', '--'), ); // NOTE: The "open" command works well on OS X, but on many Linuxes "open" // exists and is not a browser. For now, we're just looking for other // commands first, but we might want to be smarter about selecting "open" // only on OS X. foreach ($candidates as $cmd => $argv) { if (Filesystem::binaryExists($cmd)) { return $argv; } } throw new ArcanistUsageException( pht( "Unable to find a browser command to run. Set '%s' in your ". "Arcanist config to specify a command to use.", 'browser')); } /** * Ask Phabricator to update the current repository as soon as possible. * * Calling this method after pushing commits allows Phabricator to discover * the commits more quickly, so the system overall is more responsive. * * @return void */ protected function askForRepositoryUpdate() { // If we know which repository we're in, try to tell Phabricator that we // pushed commits to it so it can update. This hint can help pull updates // more quickly, especially in rarely-used repositories. if ($this->getRepositoryPHID()) { try { $this->getConduit()->callMethodSynchronous( 'diffusion.looksoon', array( 'repositories' => array($this->getRepositoryPHID()), )); } catch (ConduitClientException $ex) { // If we hit an exception, just ignore it. Likely, we are running // against a Phabricator which is too old to support this method. // Since this hint is purely advisory, it doesn't matter if it has // no effect. } } } protected function getModernLintDictionary(array $map) { $map = $this->getModernCommonDictionary($map); return $map; } protected function getModernUnitDictionary(array $map) { $map = $this->getModernCommonDictionary($map); $details = idx($map, 'userData'); if (strlen($details)) { $map['details'] = (string)$details; } unset($map['userData']); return $map; } private function getModernCommonDictionary(array $map) { foreach ($map as $key => $value) { if ($value === null) { unset($map[$key]); } } return $map; } final public function setConduitEngine( ArcanistConduitEngine $conduit_engine) { $this->conduitEngine = $conduit_engine; return $this; } final public function getConduitEngine() { return $this->conduitEngine; } final public function getRepositoryRef() { $configuration_engine = $this->getConfigurationEngine(); if ($configuration_engine) { // This is a toolset workflow and can always build a repository ref. } else { if (!$this->getConfigurationManager()->getWorkingCopyIdentity()) { return null; } if (!$this->repositoryAPI) { return null; } } if (!$this->repositoryRef) { $ref = id(new ArcanistRepositoryRef()) ->setPHID($this->getRepositoryPHID()) ->setBrowseURI($this->getRepositoryURI()); $this->repositoryRef = $ref; } return $this->repositoryRef; } final public function getToolsetKey() { return $this->getToolset()->getToolsetKey(); } final public function getConfig($key) { return $this->getConfigurationSourceList()->getConfig($key); } public function canHandleSignal($signo) { return false; } public function handleSignal($signo) { return; } final public function newCommand(PhutilExecutableFuture $future) { return id(new ArcanistCommand()) ->setLogEngine($this->getLogEngine()) ->setExecutableFuture($future); } final public function loadHardpoints( $objects, $requests) { return $this->getRuntime()->loadHardpoints($objects, $requests); } protected function newPrompts() { return array(); } protected function newPrompt($key) { return id(new ArcanistPrompt()) ->setWorkflow($this) ->setKey($key); } public function hasPrompt($key) { $map = $this->getPromptMap(); return isset($map[$key]); } public function getPromptMap() { if ($this->promptMap === null) { $prompts = $this->newPrompts(); assert_instances_of($prompts, 'ArcanistPrompt'); // TODO: Move this somewhere modular. $prompts[] = $this->newPrompt('arc.state.stash') ->setDescription( pht( 'Prompts the user to stash changes and continue when the '. 'working copy has untracked, uncommitted, or unstaged '. 'changes.')); // TODO: Swap to ArrayCheck? $map = array(); foreach ($prompts as $prompt) { $key = $prompt->getKey(); if (isset($map[$key])) { throw new Exception( pht( 'Workflow ("%s") generates two prompts with the same '. 'key ("%s"). Each prompt a workflow generates must have a '. 'unique key.', get_class($this), $key)); } $map[$key] = $prompt; } $this->promptMap = $map; } return $this->promptMap; } final public function getPrompt($key) { $map = $this->getPromptMap(); $prompt = idx($map, $key); if (!$prompt) { throw new Exception( pht( 'Workflow ("%s") is requesting a prompt ("%s") but it did not '. 'generate any prompt with that name in "newPrompts()".', get_class($this), $key)); } return clone $prompt; } final protected function getSymbolEngine() { return $this->getRuntime()->getSymbolEngine(); } final protected function getViewer() { return $this->getRuntime()->getViewer(); } final protected function readStdin() { $log = $this->getLogEngine(); $log->writeWaitingForInput(); // NOTE: We can't just "file_get_contents()" here because signals don't // interrupt it. If the user types "^C", we want to interrupt the read. $raw_handle = fopen('php://stdin', 'rb'); $stdin = new PhutilSocketChannel($raw_handle); while ($stdin->update()) { PhutilChannel::waitForAny(array($stdin)); } return $stdin->read(); } - protected function getAbsoluteURI($raw_uri) { + final public function getAbsoluteURI($raw_uri) { // TODO: "ArcanistRevisionRef", at least, may return a relative URI. // If we get a relative URI, guess the correct absolute URI based on // the Conduit URI. This might not be correct for Conduit over SSH. $raw_uri = new PhutilURI($raw_uri); if (!strlen($raw_uri->getDomain())) { $base_uri = $this->getConduitEngine() ->getConduitURI(); $raw_uri = id(new PhutilURI($base_uri)) ->setPath($raw_uri->getPath()); } $raw_uri = phutil_string_cast($raw_uri); return $raw_uri; } final public function writeToPager($corpus) { $is_tty = (function_exists('posix_isatty') && posix_isatty(STDOUT)); if (!$is_tty) { echo $corpus; } else { $pager = $this->getConfig('pager'); if (!$pager) { $pager = array('less', '-R', '--'); } // Try to show the content through a pager. $err = id(new PhutilExecPassthru('%Ls', $pager)) ->write($corpus) ->resolve(); // If the pager exits with an error, print the content normally. if ($err) { echo $corpus; } } return $this; } }