diff --git a/src/hardpoint/ArcanistHardpointList.php b/src/hardpoint/ArcanistHardpointList.php index bc44bdde..1b93a1d7 100644 --- a/src/hardpoint/ArcanistHardpointList.php +++ b/src/hardpoint/ArcanistHardpointList.php @@ -1,125 +1,127 @@ $hardpoint) { $key = $hardpoint->getHardpointKey(); if (!strlen($key)) { throw new Exception( pht( 'Hardpoint (at index "%s") has no hardpoint key. Each hardpoint '. 'must have a key that is unique among hardpoints on the object.', $idx)); } if (isset($map[$key])) { throw new Exception( pht( 'Hardpoint (at index "%s") has the same key ("%s") as an earlier '. 'hardpoint. Each hardpoint must have a key that is unique '. - 'among hardpoints on the object.')); + 'among hardpoints on the object.', + $idx, + $key)); } $map[$key] = $hardpoint; } $this->hardpoints = $map; return $this; } public function hasHardpoint($object, $hardpoint) { return isset($this->hardpoints[$hardpoint]); } public function hasAttachedHardpoint($object, $hardpoint) { return isset($this->attached[$hardpoint]); } public function getHardpoints() { return $this->hardpoints; } public function getHardpointDefinition($object, $hardpoint) { if (!$this->hasHardpoint($object, $hardpoint)) { throw new Exception( pht( 'Hardpoint ("%s") is not registered on this object (of type "%s") '. 'so the definition object does not exist. Hardpoints are: %s.', $hardpoint, phutil_describe_type($object), $this->getHardpointListForDisplay())); } return $this->hardpoints[$hardpoint]; } public function getHardpoint($object, $hardpoint) { if (!$this->hasHardpoint($object, $hardpoint)) { throw new Exception( pht( 'Hardpoint ("%s") is not registered on this object (of type "%s"). '. 'Hardpoints are: %s.', $hardpoint, phutil_describe_type($object), $this->getHardpointListForDisplay())); } if (!$this->hasAttachedHardpoint($object, $hardpoint)) { throw new Exception( pht( 'Hardpoint data (for hardpoint "%s") is not attached.', $hardpoint)); } return $this->data[$hardpoint]; } public function setHardpointValue($object, $hardpoint, $value) { if (!$this->hasHardpoint($object, $hardpoint)) { throw new Exception( pht( 'Hardpoint ("%s") is not registered on this object (of type "%s"). '. 'Hardpoints are: %s.', $hardpoint, phutil_describe_type($object), $this->getHardpointListforDisplay())); } $this->attached[$hardpoint] = true; $this->data[$hardpoint] = $value; } public function attachHardpoint($object, $hardpoint, $value) { if ($this->hasAttachedHardpoint($object, $hardpoint)) { throw new Exception( pht( 'Hardpoint ("%s") already has attached data.', $hardpoint)); } $this->setHardpointValue($object, $hardpoint, $value); } public function getHardpointListForDisplay() { $list = array_keys($this->hardpoints); if ($list) { sort($list); return implode(', ', $list); } return pht(''); } } diff --git a/src/land/engine/ArcanistLandEngine.php b/src/land/engine/ArcanistLandEngine.php index 1bafeb23..5722073b 100644 --- a/src/land/engine/ArcanistLandEngine.php +++ b/src/land/engine/ArcanistLandEngine.php @@ -1,1578 +1,1579 @@ 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->newRefView(); $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->newRefView()); } $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->newRefView()); } $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->newRefView(); $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->newRefView(); $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_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", pht('BUILD FAILURES'), $message); $prompt_key = 'arc.land.failed-builds'; } else if ($has_ongoing) { echo tsprintf( "%!\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'; } $workflow = $this->getWorkflow(); echo tsprintf("\n"); foreach ($build_map as $build_item) { $revision_ref = $build_item['revisionRef']; $revision_view = $revision_ref->newRefView(); $buildable_ref = $revision_ref->getBuildableRef(); $buildable_view = $buildable_ref->newRefView(); $raw_uri = $buildable_ref->getURI(); $raw_uri = $workflow->getAbsoluteURI($raw_uri); $buildable_view->setURI($raw_uri); $revision_view->addChild($buildable_view); foreach ($build_item['buildRefs'] as $build_ref) { $build_view = $build_ref->newRefView(); $buildable_view->addChild($build_view); } echo tsprintf('%s', $revision_view); echo tsprintf("\n"); } $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) { $api = $this->getRepositoryAPI(); $revision_ref = $set->getRevisionRef(); echo tsprintf( "\n%s", $revision_ref->newRefView()); foreach ($set->getCommits() as $commit) { $is_implicit = $commit->getIsImplicitCommit(); $display_hash = $api->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'); $api = $this->getRepositoryAPI(); $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 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 = $api->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->newRefView()); } 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 confirmCommits( $into_commit, array $symbols, array $commit_map) { $api = $this->getRepositoryAPI(); $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), $api->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), $api->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", $api->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.', $api->getDisplayHash($commit_hash))); throw new PhutilArgumentUsageException( pht( 'Unable to determine revision for commit "%s".', $api->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 (ArcanistLandPushFailureException $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 new PhutilArgumentUsageException( $ex->getMessage()); } 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, + $this->getStrategyConfigurationKey(), $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(); } final protected function getOntoRemoteRef() { return id(new ArcanistRemoteRef()) ->setRemoteName($this->getOntoRemote()); } } diff --git a/src/repository/api/ArcanistMercurialAPI.php b/src/repository/api/ArcanistMercurialAPI.php index cbc6b5a0..e5c2078b 100644 --- a/src/repository/api/ArcanistMercurialAPI.php +++ b/src/repository/api/ArcanistMercurialAPI.php @@ -1,1061 +1,1061 @@ getMercurialEnvironmentVariables(); $argv[0] = 'hg '.$argv[0]; $future = newv('ExecFuture', $argv) ->setEnv($env) ->setCWD($this->getPath()); return $future; } public function newPassthru($pattern /* , ... */) { $args = func_get_args(); $env = $this->getMercurialEnvironmentVariables(); $args[0] = 'hg '.$args[0]; return newv('PhutilExecPassthru', $args) ->setEnv($env) ->setCWD($this->getPath()); } public function getSourceControlSystemName() { return 'hg'; } public function getMetadataPath() { return $this->getPath('.hg'); } public function getSourceControlBaseRevision() { return $this->getCanonicalRevisionName($this->getBaseCommit()); } public function getCanonicalRevisionName($string) { list($stdout) = $this->execxLocal( 'log -l 1 --template %s -r %s --', '{node}', $string); return $stdout; } public function getSourceControlPath() { return '/'; } public function getBranchName() { if (!$this->branch) { list($stdout) = $this->execxLocal('branch'); $this->branch = trim($stdout); } return $this->branch; } protected function didReloadCommitRange() { $this->localCommitInfo = null; } protected function buildBaseCommit($symbolic_commit) { if ($symbolic_commit !== null) { try { $commit = $this->getCanonicalRevisionName( hgsprintf('ancestor(%s,.)', $symbolic_commit)); } catch (Exception $ex) { // Try it as a revset instead of a commit id try { $commit = $this->getCanonicalRevisionName( hgsprintf('ancestor(%R,.)', $symbolic_commit)); } catch (Exception $ex) { throw new ArcanistUsageException( pht( "Commit '%s' is not a valid Mercurial commit identifier.", $symbolic_commit)); } } $this->setBaseCommitExplanation( pht( 'it is the greatest common ancestor of the working directory '. 'and the commit you specified explicitly.')); return $commit; } if ($this->getBaseCommitArgumentRules() || $this->getConfigurationManager()->getConfigFromAnySource('base')) { $base = $this->resolveBaseCommit(); if (!$base) { throw new ArcanistUsageException( pht( "None of the rules in your 'base' configuration matched a valid ". "commit. Adjust rules or specify which commit you want to use ". "explicitly.")); } return $base; } list($err, $stdout) = $this->execManualLocal( 'log --branch %s -r %s --style default', $this->getBranchName(), 'draft()'); if (!$err) { $logs = ArcanistMercurialParser::parseMercurialLog($stdout); } else { // Mercurial (in some versions?) raises an error when there's nothing // outgoing. $logs = array(); } if (!$logs) { $this->setBaseCommitExplanation( pht( 'you have no outgoing commits, so arc assumes you intend to submit '. 'uncommitted changes in the working copy.')); return $this->getWorkingCopyRevision(); } $outgoing_revs = ipull($logs, 'rev'); // This is essentially an implementation of a theoretical `hg merge-base` // command. $against = $this->getWorkingCopyRevision(); while (true) { // NOTE: The "^" and "~" syntaxes were only added in hg 1.9, which is // new as of July 2011, so do this in a compatible way. Also, "hg log" // and "hg outgoing" don't necessarily show parents (even if given an // explicit template consisting of just the parents token) so we need // to separately execute "hg parents". list($stdout) = $this->execxLocal( 'parents --style default --rev %s', $against); $parents_logs = ArcanistMercurialParser::parseMercurialLog($stdout); list($p1, $p2) = array_merge($parents_logs, array(null, null)); if ($p1 && !in_array($p1['rev'], $outgoing_revs)) { $against = $p1['rev']; break; } else if ($p2 && !in_array($p2['rev'], $outgoing_revs)) { $against = $p2['rev']; break; } else if ($p1) { $against = $p1['rev']; } else { // This is the case where you have a new repository and the entire // thing is outgoing; Mercurial literally accepts "--rev null" as // meaning "diff against the empty state". $against = 'null'; break; } } if ($against == 'null') { $this->setBaseCommitExplanation( pht('this is a new repository (all changes are outgoing).')); } else { $this->setBaseCommitExplanation( pht( 'it is the first commit reachable from the working copy state '. 'which is not outgoing.')); } return $against; } public function getLocalCommitInformation() { if ($this->localCommitInfo === null) { $base_commit = $this->getBaseCommit(); list($info) = $this->execxLocal( 'log --template %s --rev %s --branch %s --', "{node}\1{rev}\1{author}\1". "{date|rfc822date}\1{branch}\1{tag}\1{parents}\1{desc}\2", hgsprintf('(%s::. - %s)', $base_commit, $base_commit), $this->getBranchName()); $logs = array_filter(explode("\2", $info)); $last_node = null; $futures = array(); $commits = array(); foreach ($logs as $log) { list($node, $rev, $full_author, $date, $branch, $tag, $parents, $desc) = explode("\1", $log, 9); list($author, $author_email) = $this->parseFullAuthor($full_author); // NOTE: If a commit has only one parent, {parents} returns empty. // If it has two parents, {parents} returns revs and short hashes, not // full hashes. Try to avoid making calls to "hg parents" because it's // relatively expensive. $commit_parents = null; if (!$parents) { if ($last_node) { $commit_parents = array($last_node); } } if (!$commit_parents) { // We didn't get a cheap hit on previous commit, so do the full-cost // "hg parents" call. We can run these in parallel, at least. $futures[$node] = $this->execFutureLocal( 'parents --template %s --rev %s', '{node}\n', $node); } $commits[$node] = array( 'author' => $author, 'time' => strtotime($date), 'branch' => $branch, 'tag' => $tag, 'commit' => $node, 'rev' => $node, // TODO: Remove eventually. 'local' => $rev, 'parents' => $commit_parents, 'summary' => head(explode("\n", $desc)), 'message' => $desc, 'authorEmail' => $author_email, ); $last_node = $node; } $futures = id(new FutureIterator($futures)) ->limit(4); foreach ($futures as $node => $future) { list($parents) = $future->resolvex(); $parents = array_filter(explode("\n", $parents)); $commits[$node]['parents'] = $parents; } // Put commits in newest-first order, to be consistent with Git and the // expected order of "hg log" and "git log" under normal circumstances. // The order of ancestors() is oldest-first. $commits = array_reverse($commits); $this->localCommitInfo = $commits; } return $this->localCommitInfo; } public function getAllFiles() { // TODO: Handle paths with newlines. $future = $this->buildLocalFuture(array('manifest')); return new LinesOfALargeExecFuture($future); } public function getChangedFiles($since_commit) { list($stdout) = $this->execxLocal( 'status --rev %s', $since_commit); return ArcanistMercurialParser::parseMercurialStatus($stdout); } public function getBlame($path) { list($stdout) = $this->execxLocal( 'annotate -u -v -c --rev %s -- %s', $this->getBaseCommit(), $path); $lines = phutil_split_lines($stdout, $retain_line_endings = true); $blame = array(); foreach ($lines as $line) { if (!strlen($line)) { continue; } $matches = null; $ok = preg_match('/^\s*([^:]+?) ([a-f0-9]{12}):/', $line, $matches); if (!$ok) { throw new Exception( pht( 'Unable to parse Mercurial blame line: %s', $line)); } $revision = $matches[2]; $author = trim($matches[1]); $blame[] = array($author, $revision); } return $blame; } protected function buildUncommittedStatus() { list($stdout) = $this->execxLocal('status'); $results = new PhutilArrayWithDefaultValue(); $working_status = ArcanistMercurialParser::parseMercurialStatus($stdout); foreach ($working_status as $path => $mask) { if (!($mask & parent::FLAG_UNTRACKED)) { // Mark tracked files as uncommitted. $mask |= self::FLAG_UNCOMMITTED; } $results[$path] |= $mask; } return $results->toArray(); } protected function buildCommitRangeStatus() { list($stdout) = $this->execxLocal( 'status --rev %s --rev tip', $this->getBaseCommit()); $results = new PhutilArrayWithDefaultValue(); $working_status = ArcanistMercurialParser::parseMercurialStatus($stdout); foreach ($working_status as $path => $mask) { $results[$path] |= $mask; } return $results->toArray(); } protected function didReloadWorkingCopy() { // Diffs are against ".", so we need to drop the cache if we change the // working copy. $this->rawDiffCache = array(); $this->branch = null; } private function getDiffOptions() { $options = array( '--git', '-U'.$this->getDiffLinesOfContext(), ); return implode(' ', $options); } public function getRawDiffText($path) { $options = $this->getDiffOptions(); $range = $this->getBaseCommit(); $raw_diff_cache_key = $options.' '.$range.' '.$path; if (idx($this->rawDiffCache, $raw_diff_cache_key)) { return idx($this->rawDiffCache, $raw_diff_cache_key); } list($stdout) = $this->execxLocal( 'diff %C --rev %s -- %s', $options, $range, $path); $this->rawDiffCache[$raw_diff_cache_key] = $stdout; return $stdout; } public function getFullMercurialDiff() { return $this->getRawDiffText(''); } public function getOriginalFileData($path) { return $this->getFileDataAtRevision($path, $this->getBaseCommit()); } public function getCurrentFileData($path) { return $this->getFileDataAtRevision( $path, $this->getWorkingCopyRevision()); } public function getBulkOriginalFileData($paths) { return $this->getBulkFileDataAtRevision($paths, $this->getBaseCommit()); } public function getBulkCurrentFileData($paths) { return $this->getBulkFileDataAtRevision( $paths, $this->getWorkingCopyRevision()); } private function getBulkFileDataAtRevision($paths, $revision) { // Calling 'hg cat' on each file individually is slow (1 second per file // on a large repo) because mercurial has to decompress and parse the // entire manifest every time. Do it in one large batch instead. // hg cat will write the file data to files in a temp directory $tmpdir = Filesystem::createTemporaryDirectory(); // Mercurial doesn't create the directories for us :( foreach ($paths as $path) { $tmppath = $tmpdir.'/'.$path; Filesystem::createDirectory(dirname($tmppath), 0755, true); } // NOTE: The "%s%%p" construction passes a literal "%p" to Mercurial, // which is a formatting directive for a repo-relative filepath. The // particulars of the construction avoid Windows escaping issues. See // PHI904. list($err, $stdout) = $this->execManualLocal( 'cat --rev %s --output %s%%p -- %Ls', $revision, $tmpdir.DIRECTORY_SEPARATOR, $paths); $filedata = array(); foreach ($paths as $path) { $tmppath = $tmpdir.'/'.$path; if (Filesystem::pathExists($tmppath)) { $filedata[$path] = Filesystem::readFile($tmppath); } } Filesystem::remove($tmpdir); return $filedata; } private function getFileDataAtRevision($path, $revision) { list($err, $stdout) = $this->execManualLocal( 'cat --rev %s -- %s', $revision, $path); if ($err) { // Assume this is "no file at revision", i.e. a deleted or added file. return null; } else { return $stdout; } } public function getWorkingCopyRevision() { return '.'; } public function isHistoryDefaultImmutable() { return true; } public function supportsAmend() { list($err, $stdout) = $this->execManualLocal('help commit'); if ($err) { return false; } else { return (strpos($stdout, 'amend') !== false); } } public function supportsCommitRanges() { return true; } public function supportsLocalCommits() { return true; } public function getBaseCommitRef() { $base_commit = $this->getBaseCommit(); if ($base_commit === 'null') { return null; } $base_message = $this->getCommitMessage($base_commit); return $this->newCommitRef() ->setCommitHash($base_commit) ->attachMessage($base_message); } public function hasLocalCommit($commit) { try { $this->getCanonicalRevisionName($commit); return true; } catch (Exception $ex) { return false; } } public function getCommitMessage($commit) { list($message) = $this->execxLocal( 'log --template={desc} --rev %s', $commit); return $message; } public function getAllLocalChanges() { $diff = $this->getFullMercurialDiff(); if (!strlen(trim($diff))) { return array(); } $parser = new ArcanistDiffParser(); return $parser->parseDiff($diff); } public function getFinalizedRevisionMessage() { return pht( "You may now push this commit upstream, as appropriate (e.g. with ". "'%s' or by printing and faxing it).", 'hg push'); } public function getCommitMessageLog() { $base_commit = $this->getBaseCommit(); list($stdout) = $this->execxLocal( 'log --template %s --rev %s --branch %s --', "{node}\1{desc}\2", hgsprintf('(%s::. - %s)', $base_commit, $base_commit), $this->getBranchName()); $map = array(); $logs = explode("\2", trim($stdout)); foreach (array_filter($logs) as $log) { list($node, $desc) = explode("\1", $log); $map[$node] = $desc; } return array_reverse($map); } public function loadWorkingCopyDifferentialRevisions( ConduitClient $conduit, array $query) { $messages = $this->getCommitMessageLog(); $parser = new ArcanistDiffParser(); // First, try to find revisions by explicit revision IDs in commit messages. $reason_map = array(); $revision_ids = array(); foreach ($messages as $node_id => $message) { $object = ArcanistDifferentialCommitMessage::newFromRawCorpus($message); if ($object->getRevisionID()) { $revision_ids[] = $object->getRevisionID(); $reason_map[$object->getRevisionID()] = $node_id; } } if ($revision_ids) { $results = $conduit->callMethodSynchronous( 'differential.query', $query + array( 'ids' => $revision_ids, )); foreach ($results as $key => $result) { $hash = substr($reason_map[$result['id']], 0, 16); $results[$key]['why'] = pht( "Commit message for '%s' has explicit 'Differential Revision'.", $hash); } return $results; } // Try to find revisions by hash. $hashes = array(); foreach ($this->getLocalCommitInformation() as $commit) { $hashes[] = array('hgcm', $commit['commit']); } if ($hashes) { // NOTE: In the case of "arc diff . --uncommitted" in a Mercurial working // copy with dirty changes, there may be no local commits. $results = $conduit->callMethodSynchronous( 'differential.query', $query + array( 'commitHashes' => $hashes, )); foreach ($results as $key => $hash) { $results[$key]['why'] = pht( 'A mercurial commit hash in the commit range is already attached '. 'to the Differential revision.'); } return $results; } return array(); } public function updateWorkingCopy() { $this->execxLocal('up'); $this->reloadWorkingCopy(); } private function getMercurialConfig($key, $default = null) { list($stdout) = $this->execxLocal('showconfig %s', $key); if ($stdout == '') { return $default; } return rtrim($stdout); } public function getAuthor() { $full_author = $this->getMercurialConfig('ui.username'); list($author, $author_email) = $this->parseFullAuthor($full_author); return $author; } /** * Parse the Mercurial author field. * * Not everyone enters their email address as a part of the username * field. Try to make it work when it's obvious. * * @param string $full_author * @return array */ protected function parseFullAuthor($full_author) { if (strpos($full_author, '@') === false) { $author = $full_author; $author_email = null; } else { $email = new PhutilEmailAddress($full_author); $author = $email->getDisplayName(); $author_email = $email->getAddress(); } return array($author, $author_email); } public function addToCommit(array $paths) { $this->execxLocal( 'addremove -- %Ls', $paths); $this->reloadWorkingCopy(); } public function doCommit($message) { $tmp_file = new TempFile(); Filesystem::writeFile($tmp_file, $message); $this->execxLocal('commit -l %s', $tmp_file); $this->reloadWorkingCopy(); } public function amendCommit($message = null) { if ($message === null) { $message = $this->getCommitMessage('.'); } $tmp_file = new TempFile(); Filesystem::writeFile($tmp_file, $message); try { $this->execxLocal( 'commit --amend -l %s', $tmp_file); } catch (CommandException $ex) { if (preg_match('/nothing changed/', $ex->getStdout())) { // NOTE: Mercurial considers it an error to make a no-op amend. Although // we generally defer to the underlying VCS to dictate behavior, this // one seems a little goofy, and we use amend as part of various // workflows under the assumption that no-op amends are fine. If this // amend failed because it's a no-op, just continue. } else { throw $ex; } } $this->reloadWorkingCopy(); } public function getCommitSummary($commit) { if ($commit == 'null') { return pht('(The Empty Void)'); } list($summary) = $this->execxLocal( 'log --template {desc} --limit 1 --rev %s', $commit); $summary = head(explode("\n", $summary)); return trim($summary); } public function resolveBaseCommitRule($rule, $source) { list($type, $name) = explode(':', $rule, 2); // NOTE: This function MUST return node hashes or symbolic commits (like // branch names or the word "tip"), not revsets. This includes ".^" and // similar, which a revset, not a symbolic commit identifier. If you return // a revset it will be escaped later and looked up literally. switch ($type) { case 'hg': $matches = null; if (preg_match('/^gca\((.+)\)$/', $name, $matches)) { list($err, $merge_base) = $this->execManualLocal( 'log --template={node} --rev %s', sprintf('ancestor(., %s)', $matches[1])); if (!$err) { $this->setBaseCommitExplanation( pht( "it is the greatest common ancestor of '%s' and %s, as ". "specified by '%s' in your %s 'base' configuration.", $matches[1], '.', $rule, $source)); return trim($merge_base); } } else { list($err, $commit) = $this->execManualLocal( 'log --template {node} --rev %s', hgsprintf('%s', $name)); if ($err) { list($err, $commit) = $this->execManualLocal( 'log --template {node} --rev %s', $name); } if (!$err) { $this->setBaseCommitExplanation( pht( "it is specified by '%s' in your %s 'base' configuration.", $rule, $source)); return trim($commit); } } break; case 'arc': switch ($name) { case 'empty': $this->setBaseCommitExplanation( pht( "you specified '%s' in your %s 'base' configuration.", $rule, $source)); return 'null'; case 'outgoing': list($err, $outgoing_base) = $this->execManualLocal( 'log --template={node} --rev %s', 'limit(reverse(ancestors(.) - outgoing()), 1)'); if (!$err) { $this->setBaseCommitExplanation( pht( "it is the first ancestor of the working copy that is not ". "outgoing, and it matched the rule %s in your %s ". "'base' configuration.", $rule, $source)); return trim($outgoing_base); } case 'amended': $text = $this->getCommitMessage('.'); $message = ArcanistDifferentialCommitMessage::newFromRawCorpus( $text); if ($message->getRevisionID()) { $this->setBaseCommitExplanation( pht( "'%s' has been amended with 'Differential Revision:', ". "as specified by '%s' in your %s 'base' configuration.", - '.'. + '.', $rule, $source)); // NOTE: This should be safe because Mercurial doesn't support // amend until 2.2. return $this->getCanonicalRevisionName('.^'); } break; case 'bookmark': $revset = 'limit('. ' sort('. ' (ancestors(.) and bookmark() - .) or'. ' (ancestors(.) - outgoing()), '. ' -rev),'. '1)'; list($err, $bookmark_base) = $this->execManualLocal( 'log --template={node} --rev %s', $revset); if (!$err) { $this->setBaseCommitExplanation( pht( "it is the first ancestor of %s that either has a bookmark, ". "or is already in the remote and it matched the rule %s in ". "your %s 'base' configuration", '.', $rule, $source)); return trim($bookmark_base); } break; case 'this': $this->setBaseCommitExplanation( pht( "you specified '%s' in your %s 'base' configuration.", $rule, $source)); return $this->getCanonicalRevisionName('.^'); default: if (preg_match('/^nodiff\((.+)\)$/', $name, $matches)) { list($results) = $this->execxLocal( 'log --template %s --rev %s', "{node}\1{desc}\2", sprintf('ancestor(.,%s)::.^', $matches[1])); $results = array_reverse(explode("\2", trim($results))); foreach ($results as $result) { if (empty($result)) { continue; } list($node, $desc) = explode("\1", $result, 2); $message = ArcanistDifferentialCommitMessage::newFromRawCorpus( $desc); if ($message->getRevisionID()) { $this->setBaseCommitExplanation( pht( "it is the first ancestor of %s that has a diff and is ". "the gca or a descendant of the gca with '%s', ". "specified by '%s' in your %s 'base' configuration.", '.', $matches[1], $rule, $source)); return $node; } } } break; } break; default: return null; } return null; } public function getSubversionInfo() { $info = array(); $base_path = null; $revision = null; list($err, $raw_info) = $this->execManualLocal('svn info'); if (!$err) { foreach (explode("\n", trim($raw_info)) as $line) { list($key, $value) = explode(': ', $line, 2); switch ($key) { case 'URL': $info['base_path'] = $value; $base_path = $value; break; case 'Repository UUID': $info['uuid'] = $value; break; case 'Revision': $revision = $value; break; default: break; } } if ($base_path && $revision) { $info['base_revision'] = $base_path.'@'.$revision; } } return $info; } public function getActiveBookmark() { $bookmark = $this->newMarkerRefQuery() ->withMarkerTypes( array( ArcanistMarkerRef::TYPE_BOOKMARK, )) ->withIsActive(true) ->executeOne(); if (!$bookmark) { return null; } return $bookmark->getName(); } public function getRemoteURI() { // TODO: Remove this method in favor of RemoteRefQuery. list($stdout) = $this->execxLocal('paths default'); $stdout = trim($stdout); if (strlen($stdout)) { return $stdout; } return null; } private function getMercurialEnvironmentVariables() { $env = array(); // Mercurial has a "defaults" feature which basically breaks automation by // allowing the user to add random flags to any command. This feature is // "deprecated" and "a bad idea" that you should "forget ... existed" // according to project lead Matt Mackall: // // http://markmail.org/message/hl3d6eprubmkkqh5 // // There is an HGPLAIN environmental variable which enables "plain mode" // and hopefully disables this stuff. $env['HGPLAIN'] = 1; return $env; } protected function newLandEngine() { return new ArcanistMercurialLandEngine(); } protected function newWorkEngine() { return new ArcanistMercurialWorkEngine(); } public function newLocalState() { return id(new ArcanistMercurialLocalState()) ->setRepositoryAPI($this); } public function willTestMercurialFeature($feature) { $this->executeMercurialFeatureTest($feature, false); return $this; } public function getMercurialFeature($feature) { return $this->executeMercurialFeatureTest($feature, true); } private function executeMercurialFeatureTest($feature, $resolve) { if (array_key_exists($feature, $this->featureResults)) { return $this->featureResults[$feature]; } if (!array_key_exists($feature, $this->featureFutures)) { $future = $this->newMercurialFeatureFuture($feature); $future->start(); $this->featureFutures[$feature] = $future; } if (!$resolve) { return; } $future = $this->featureFutures[$feature]; $result = $this->resolveMercurialFeatureFuture($feature, $future); $this->featureResults[$feature] = $result; return $result; } private function newMercurialFeatureFuture($feature) { switch ($feature) { case 'shelve': return $this->execFutureLocal( '--config extensions.shelve= shelve --help --'); case 'evolve': return $this->execFutureLocal('prune --help --'); default: throw new Exception( pht( 'Unknown Mercurial feature "%s".', $feature)); } } private function resolveMercurialFeatureFuture($feature, $future) { // By default, assume the feature is a simple capability test and the // capability is present if the feature resolves without an error. list($err) = $future->resolve(); return !$err; } protected function newSupportedMarkerTypes() { return array( ArcanistMarkerRef::TYPE_BRANCH, ArcanistMarkerRef::TYPE_BOOKMARK, ); } protected function newMarkerRefQueryTemplate() { return new ArcanistMercurialRepositoryMarkerQuery(); } protected function newRemoteRefQueryTemplate() { return new ArcanistMercurialRepositoryRemoteQuery(); } public function getMercurialExtensionArguments() { $path = phutil_get_library_root('arcanist'); $path = dirname($path); $path = $path.'/support/hg/arc-hg.py'; return array( '--config', 'extensions.arc-hg='.$path, ); } protected function newNormalizedURI($uri) { return new ArcanistRepositoryURINormalizer( ArcanistRepositoryURINormalizer::TYPE_MERCURIAL, $uri); } protected function newCommitGraphQueryTemplate() { return new ArcanistMercurialCommitGraphQuery(); } protected function newPublishedCommitHashes() { $future = $this->newFuture( 'log --rev %s --template %s', hgsprintf('parents(draft()) - draft()'), '{node}\n'); list($lines) = $future->resolve(); $lines = phutil_split_lines($lines, false); $hashes = array(); foreach ($lines as $line) { if (!strlen(trim($line))) { continue; } $hashes[] = $line; } return $hashes; } } diff --git a/src/runtime/ArcanistRuntime.php b/src/runtime/ArcanistRuntime.php index 87d574a5..35099b2c 100644 --- a/src/runtime/ArcanistRuntime.php +++ b/src/runtime/ArcanistRuntime.php @@ -1,913 +1,914 @@ checkEnvironment(); } catch (Exception $ex) { echo "CONFIGURATION ERROR\n\n"; echo $ex->getMessage(); echo "\n\n"; return 1; } PhutilTranslator::getInstance() ->setLocale(PhutilLocale::loadLocale('en_US')) ->setTranslations(PhutilTranslation::getTranslationMapForLocale('en_US')); $log = new ArcanistLogEngine(); $this->logEngine = $log; try { return $this->executeCore($argv); } catch (ArcanistConduitException $ex) { $log->writeError(pht('CONDUIT'), $ex->getMessage()); } catch (PhutilArgumentUsageException $ex) { $log->writeError(pht('USAGE EXCEPTION'), $ex->getMessage()); } catch (ArcanistUserAbortException $ex) { $log->writeError(pht('---'), $ex->getMessage()); } catch (ArcanistConduitAuthenticationException $ex) { $log->writeError($ex->getTitle(), $ex->getBody()); } return 1; } private function executeCore(array $argv) { $log = $this->getLogEngine(); $config_args = array( array( 'name' => 'library', 'param' => 'path', 'help' => pht('Load a library.'), 'repeat' => true, ), array( 'name' => 'config', 'param' => 'key=value', 'repeat' => true, 'help' => pht('Specify a runtime configuration value.'), ), array( 'name' => 'config-file', 'param' => 'path', 'repeat' => true, 'help' => pht( 'Load one or more configuration files. If this flag is provided, '. 'the system and user configuration files are ignored.'), ), ); $args = id(new PhutilArgumentParser($argv)) ->parseStandardArguments(); // If we can test whether STDIN is a TTY, and it isn't, require that "--" // appear in the argument list. This is intended to make it very hard to // write unsafe scripts on top of Arcanist. if (phutil_is_noninteractive()) { $args->setRequireArgumentTerminator(true); } $is_trace = $args->getArg('trace'); $log->setShowTraceMessages($is_trace); $log->writeTrace(pht('ARGV'), csprintf('%Ls', $argv)); // We're installing the signal handler after parsing "--trace" so that it // can emit debugging messages. This means there's a very small window at // startup where signals have no special handling, but we couldn't really // route them or do anything interesting with them anyway. $this->installSignalHandler(); $args->parsePartial($config_args, true); $config_engine = $this->loadConfiguration($args); $config = $config_engine->newConfigurationSourceList(); $this->loadLibraries($config_engine, $config, $args); // Now that we've loaded libraries, we can validate configuration. // Do this before continuing since configuration can impact other // behaviors immediately and we want to catch any issues right away. $config->setConfigOptions($config_engine->newConfigOptionsMap()); $config->validateConfiguration($this); $toolset = $this->newToolset($argv); $this->setToolset($toolset); $args->parsePartial($toolset->getToolsetArguments()); $workflows = $this->newWorkflows($toolset); $this->workflows = $workflows; $conduit_engine = $this->newConduitEngine($config, $args); $this->conduitEngine = $conduit_engine; $phutil_workflows = array(); foreach ($workflows as $key => $workflow) { $workflow ->setRuntime($this) ->setConfigurationEngine($config_engine) ->setConfigurationSourceList($config) ->setConduitEngine($conduit_engine); $phutil_workflows[$key] = $workflow->newPhutilWorkflow(); } $unconsumed_argv = $args->getUnconsumedArgumentVector(); if (!$unconsumed_argv) { // TOOLSETS: This means the user just ran "arc" or some other top-level // toolset without any workflow argument. We should give them a summary // of the toolset, a list of workflows, and a pointer to "arc help" for // more details. // A possible exception is "arc --help", which should perhaps pass // through and act like "arc help". throw new PhutilArgumentUsageException(pht('Choose a workflow!')); } $alias_effects = id(new ArcanistAliasEngine()) ->setRuntime($this) ->setToolset($toolset) ->setWorkflows($workflows) ->setConfigurationSourceList($config) ->resolveAliases($unconsumed_argv); foreach ($alias_effects as $alias_effect) { if ($alias_effect->getType() === ArcanistAliasEffect::EFFECT_SHELL) { return $this->executeShellAlias($alias_effect); } } $result_argv = $this->applyAliasEffects($alias_effects, $unconsumed_argv); $args->setUnconsumedArgumentVector($result_argv); // TOOLSETS: Some day, stop falling through to the old "arc" runtime. $help_workflows = $this->getHelpWorkflows($phutil_workflows); $args->setHelpWorkflows($help_workflows); try { return $args->parseWorkflowsFull($phutil_workflows); } catch (ArcanistMissingArgumentTerminatorException $terminator_exception) { $log->writeHint( pht('USAGE'), pht( '"%s" is being run noninteractively, but the argument list is '. 'missing "--" to indicate end of flags.', $toolset->getToolsetKey())); $log->writeHint( pht('USAGE'), pht( 'When running noninteractively, you MUST provide "--" to all '. 'commands (even if they take no arguments).')); $log->writeHint( pht('USAGE'), tsprintf( '%s <__%s__>', pht('Learn More:'), 'https://phurl.io/u/noninteractive')); throw new PhutilArgumentUsageException( pht('Missing required "--" in argument list.')); } catch (PhutilArgumentUsageException $usage_exception) { // TODO: This is very, very hacky; we're trying to let errors like // "you passed the wrong arguments" through but fall back to classic // mode if the workflow itself doesn't exist. if (!preg_match('/invalid command/i', $usage_exception->getMessage())) { throw $usage_exception; } } $arcanist_root = phutil_get_library_root('arcanist'); $arcanist_root = dirname($arcanist_root); $bin = $arcanist_root.'/scripts/arcanist.php'; $err = phutil_passthru( 'php -f %R -- %Ls', $bin, array_slice($argv, 1)); return $err; } /** * Perform some sanity checks against the possible diversity of PHP builds in * the wild, like very old versions and builds that were compiled with flags * that exclude core functionality. */ private function checkEnvironment() { // NOTE: We don't have phutil_is_windows() yet here. $is_windows = (DIRECTORY_SEPARATOR != '/'); // NOTE: There's a hard PHP version check earlier, in "init-script.php". if ($is_windows) { $need_functions = array( 'curl_init' => array('builtin-dll', 'php_curl.dll'), ); } else { $need_functions = array( 'curl_init' => array( 'text', "You need to install the cURL PHP extension, maybe with ". "'apt-get install php5-curl' or 'yum install php53-curl' or ". "something similar.", ), 'json_decode' => array('flag', '--without-json'), ); } $problems = array(); $config = null; $show_config = false; foreach ($need_functions as $fname => $resolution) { if (function_exists($fname)) { continue; } static $info; if ($info === null) { ob_start(); phpinfo(INFO_GENERAL); $info = ob_get_clean(); $matches = null; if (preg_match('/^Configure Command =>\s*(.*?)$/m', $info, $matches)) { $config = $matches[1]; } } list($what, $which) = $resolution; if ($what == 'flag' && strpos($config, $which) !== false) { $show_config = true; $problems[] = sprintf( 'The build of PHP you are running was compiled with the configure '. 'flag "%s", which means it does not support the function "%s()". '. 'This function is required for Arcanist to run. Install a standard '. 'build of PHP or rebuild it without this flag. You may also be '. 'able to build or install the relevant extension separately.', $which, $fname); continue; } if ($what == 'builtin-dll') { $problems[] = sprintf( 'The build of PHP you are running does not have the "%s" extension '. 'enabled. Edit your php.ini file and uncomment the line which '. 'reads "extension=%s".', $which, $which); continue; } if ($what == 'text') { $problems[] = $which; continue; } $problems[] = sprintf( 'The build of PHP you are running is missing the required function '. '"%s()". Rebuild PHP or install the extension which provides "%s()".', $fname, $fname); } if ($problems) { if ($show_config) { $problems[] = "PHP was built with this configure command:\n\n{$config}"; } $problems = implode("\n\n", $problems); throw new Exception($problems); } } private function loadConfiguration(PhutilArgumentParser $args) { $engine = id(new ArcanistConfigurationEngine()) ->setArguments($args); $working_copy = ArcanistWorkingCopy::newFromWorkingDirectory(getcwd()); $engine->setWorkingCopy($working_copy); $this->workingCopy = $working_copy; $working_copy ->getRepositoryAPI() ->setRuntime($this); return $engine; } private function loadLibraries( ArcanistConfigurationEngine $engine, ArcanistConfigurationSourceList $config, PhutilArgumentParser $args) { $sources = array(); $cli_libraries = $args->getArg('library'); if ($cli_libraries) { $sources = array(); foreach ($cli_libraries as $cli_library) { $sources[] = array( 'type' => 'flag', 'library-source' => $cli_library, ); } } else { $items = $config->getStorageValueList('load'); foreach ($items as $item) { foreach ($item->getValue() as $library_path) { $sources[] = array( 'type' => 'config', 'config-source' => $item->getConfigurationSource(), 'library-source' => $library_path, ); } } } foreach ($sources as $spec) { $library_source = $spec['library-source']; switch ($spec['type']) { case 'flag': $description = pht('runtime --library flag'); break; case 'config': $config_source = $spec['config-source']; $description = pht( 'Configuration (%s)', $config_source->getSourceDisplayName()); break; } $this->loadLibrary($engine, $library_source, $description); } } private function loadLibrary( ArcanistConfigurationEngine $engine, $location, $description) { // TODO: This is a legacy system that should be replaced with package // management. $log = $this->getLogEngine(); $working_copy = $engine->getWorkingCopy(); if ($working_copy) { $working_copy_root = $working_copy->getPath(); $working_directory = $working_copy->getWorkingDirectory(); } else { $working_copy_root = null; $working_directory = getcwd(); } // Try to resolve the library location. We look in several places, in // order: // // 1. Inside the working copy. This is for phutil libraries within the // project. For instance "library/src" will resolve to // "./library/src" if it exists. // 2. In the same directory as the working copy. This allows you to // check out a library alongside a working copy and reference it. // If we haven't resolved yet, "library/src" will try to resolve to // "../library/src" if it exists. // 3. Using normal libphutil resolution rules. Generally, this means // that it checks for libraries next to libphutil, then libraries // in the PHP include_path. // // Note that absolute paths will just resolve absolutely through rule (1). $resolved = false; // Check inside the working copy. This also checks absolute paths, since // they'll resolve absolute and just ignore the project root. if ($working_copy_root !== null) { $resolved_location = Filesystem::resolvePath( $location, $working_copy_root); if (Filesystem::pathExists($resolved_location)) { $location = $resolved_location; $resolved = true; } // If we didn't find anything, check alongside the working copy. if (!$resolved) { $resolved_location = Filesystem::resolvePath( $location, dirname($working_copy_root)); if (Filesystem::pathExists($resolved_location)) { $location = $resolved_location; $resolved = true; } } } // Look beside "arcanist/". This is rule (3) above. if (!$resolved) { $arcanist_root = phutil_get_library_root('arcanist'); $arcanist_root = dirname(dirname($arcanist_root)); $resolved_location = Filesystem::resolvePath( $location, $arcanist_root); if (Filesystem::pathExists($resolved_location)) { $location = $resolved_location; $resolved = true; } } $log->writeTrace( pht('LOAD'), pht('Loading library from "%s"...', $location)); $error = null; try { phutil_load_library($location); } catch (PhutilLibraryConflictException $ex) { if ($ex->getLibrary() != 'arcanist') { throw $ex; } // NOTE: If you are running `arc` against itself, we ignore the library // conflict created by loading the local `arc` library (in the current // working directory) and continue without loading it. // This means we only execute code in the `arcanist/` directory which is // associated with the binary you are running, whereas we would normally // execute local code. // This can make `arc` development slightly confusing if your setup is // especially bizarre, but it allows `arc` to be used in automation // workflows more easily. For some context, see PHI13. $executing_directory = dirname(dirname(__FILE__)); $log->writeWarn( pht('VERY META'), pht( 'You are running one copy of Arcanist (at path "%s") against '. 'another copy of Arcanist (at path "%s"). Code in the current '. 'working directory will not be loaded or executed.', $executing_directory, $working_directory)); } catch (PhutilBootloaderException $ex) { $log->writeError( pht('LIBRARY ERROR'), pht( 'Failed to load library at location "%s". This library '. 'is specified by "%s". Check that the library is up to date.', $location, $description)); $prompt = pht('Continue without loading library?'); if (!phutil_console_confirm($prompt)) { throw $ex; } } catch (Exception $ex) { $log->writeError( pht('LOAD ERROR'), pht( 'Failed to load library at location "%s". This library is '. 'specified by "%s". Check that the setting is correct and the '. 'library is located in the right place.', $location, $description)); $prompt = pht('Continue without loading library?'); if (!phutil_console_confirm($prompt)) { throw $ex; } } } private function newToolset(array $argv) { $binary = basename($argv[0]); $toolsets = ArcanistToolset::newToolsetMap(); if (!isset($toolsets[$binary])) { throw new PhutilArgumentUsageException( pht( 'Arcanist toolset "%s" is unknown. The Arcanist binary should '. 'be executed so that "argv[0]" identifies a supported toolset. '. 'Rename the binary or install the library that provides the '. 'desired toolset. Current available toolsets: %s.', $binary, implode(', ', array_keys($toolsets)))); } return $toolsets[$binary]; } private function newWorkflows(ArcanistToolset $toolset) { $workflows = id(new PhutilClassMapQuery()) ->setAncestorClass('ArcanistWorkflow') ->setContinueOnFailure(true) ->execute(); foreach ($workflows as $key => $workflow) { if (!$workflow->supportsToolset($toolset)) { unset($workflows[$key]); } } $map = array(); foreach ($workflows as $workflow) { $key = $workflow->getWorkflowName(); if (isset($map[$key])) { throw new Exception( pht( 'Two workflows ("%s" and "%s") both have the same name ("%s") '. 'and both support the current toolset ("%s", "%s"). Each '. 'workflow in a given toolset must have a unique name.', get_class($workflow), get_class($map[$key]), + $key, get_class($toolset), $toolset->getToolsetKey())); } $map[$key] = id(clone $workflow) ->setToolset($toolset); } return $map; } public function getWorkflows() { return $this->workflows; } public function getLogEngine() { return $this->logEngine; } private function applyAliasEffects(array $effects, array $argv) { assert_instances_of($effects, 'ArcanistAliasEffect'); $log = $this->getLogEngine(); $command = null; $arguments = null; foreach ($effects as $effect) { $message = $effect->getMessage(); if ($message !== null) { $log->writeHint(pht('ALIAS'), $message); } if ($effect->getCommand()) { $command = $effect->getCommand(); $arguments = $effect->getArguments(); } } if ($command !== null) { $argv = array_merge(array($command), $arguments); } return $argv; } private function installSignalHandler() { $log = $this->getLogEngine(); if (!function_exists('pcntl_signal')) { $log->writeTrace( pht('PCNTL'), pht( 'Unable to install signal handler, pcntl_signal() unavailable. '. 'Continuing without signal handling.')); return; } // NOTE: SIGHUP, SIGTERM and SIGWINCH are handled by "PhutilSignalRouter". // This logic is largely similar to the logic there, but more specific to // Arcanist workflows. pcntl_signal(SIGINT, array($this, 'routeSignal')); } public function routeSignal($signo) { switch ($signo) { case SIGINT: $this->routeInterruptSignal($signo); break; } } private function routeInterruptSignal($signo) { $log = $this->getLogEngine(); $last_interrupt = $this->lastInterruptTime; $now = microtime(true); $this->lastInterruptTime = $now; $should_exit = false; // If we received another SIGINT recently, always exit. This implements // "press ^C twice in quick succession to exit" regardless of what the // workflow may decide to do. $interval = 2; if ($last_interrupt !== null) { if ($now - $last_interrupt < $interval) { $should_exit = true; } } $handler = null; if (!$should_exit) { // Look for an interrupt handler in the current workflow stack. $stack = $this->getWorkflowStack(); foreach ($stack as $workflow) { if ($workflow->canHandleSignal($signo)) { $handler = $workflow; break; } } // If no workflow in the current execution stack can handle an interrupt // signal, just exit on the first interrupt. if (!$handler) { $should_exit = true; } } // It's common for users to ^C on prompts. Write a newline before writing // a response to the interrupt so the behavior is a little cleaner. This // also avoids lines that read "^C [ INTERRUPT ] ...". $log->writeNewline(); if ($should_exit) { $log->writeHint( pht('INTERRUPT'), pht('Interrupted by SIGINT (^C).')); exit(128 + $signo); } $log->writeHint( pht('INTERRUPT'), pht('Press ^C again to exit.')); $handler->handleSignal($signo); } public function pushWorkflow(ArcanistWorkflow $workflow) { $this->stack[] = $workflow; return $this; } public function popWorkflow() { if (!$this->stack) { throw new Exception(pht('Trying to pop an empty workflow stack!')); } return array_pop($this->stack); } public function getWorkflowStack() { return $this->stack; } public function getCurrentWorkflow() { return last($this->stack); } private function newConduitEngine( ArcanistConfigurationSourceList $config, PhutilArgumentParser $args) { try { $force_uri = $args->getArg('conduit-uri'); } catch (PhutilArgumentSpecificationException $ex) { $force_uri = null; } try { $force_token = $args->getArg('conduit-token'); } catch (PhutilArgumentSpecificationException $ex) { $force_token = null; } if ($force_uri !== null) { $conduit_uri = $force_uri; } else { $conduit_uri = $config->getConfig('phabricator.uri'); if ($conduit_uri === null) { // For now, read this older config from raw storage. There is currently // no definition of this option in the "toolsets" config list, and it // would be nice to get rid of it. $default_list = $config->getStorageValueList('default'); if ($default_list) { $conduit_uri = last($default_list)->getValue(); } } } if ($conduit_uri) { // Set the URI path to '/api/'. TODO: Originally, I contemplated letting // you deploy Phabricator somewhere other than the domain root, but ended // up never pursuing that. We should get rid of all "/api/" silliness // in things users are expected to configure. This is already happening // to some degree, e.g. "arc install-certificate" does it for you. $conduit_uri = new PhutilURI($conduit_uri); $conduit_uri->setPath('/api/'); $conduit_uri = phutil_string_cast($conduit_uri); } $engine = new ArcanistConduitEngine(); if ($conduit_uri !== null) { $engine->setConduitURI($conduit_uri); } // TODO: This isn't using "getConfig()" because we aren't defining a // real config entry for the moment. if ($force_token !== null) { $conduit_token = $force_token; } else { $hosts = array(); $hosts_list = $config->getStorageValueList('hosts'); foreach ($hosts_list as $hosts_config) { $hosts += $hosts_config->getValue(); } $host_config = idx($hosts, $conduit_uri, array()); $conduit_token = idx($host_config, 'token'); } if ($conduit_token !== null) { $engine->setConduitToken($conduit_token); } return $engine; } private function executeShellAlias(ArcanistAliasEffect $effect) { $log = $this->getLogEngine(); $message = $effect->getMessage(); if ($message !== null) { $log->writeHint(pht('SHELL ALIAS'), $message); } return phutil_passthru('%Ls', $effect->getArguments()); } public function getSymbolEngine() { if ($this->symbolEngine === null) { $this->symbolEngine = $this->newSymbolEngine(); } return $this->symbolEngine; } private function newSymbolEngine() { return id(new ArcanistSymbolEngine()) ->setWorkflow($this); } public function getHardpointEngine() { if ($this->hardpointEngine === null) { $this->hardpointEngine = $this->newHardpointEngine(); } return $this->hardpointEngine; } private function newHardpointEngine() { $engine = new ArcanistHardpointEngine(); $queries = ArcanistRuntimeHardpointQuery::getAllQueries(); foreach ($queries as $key => $query) { $queries[$key] = id(clone $query) ->setRuntime($this); } $engine->setQueries($queries); return $engine; } public function getViewer() { if (!$this->viewer) { $viewer = $this->getSymbolEngine() ->loadUserForSymbol('viewer()'); // TODO: Deal with anonymous stuff. if (!$viewer) { throw new Exception(pht('No viewer!')); } $this->viewer = $viewer; } return $this->viewer; } public function loadHardpoints($objects, $requests) { if (!is_array($objects)) { $objects = array($objects); } if (!is_array($requests)) { $requests = array($requests); } $engine = $this->getHardpointEngine(); $requests = $engine->requestHardpoints( $objects, $requests); // TODO: Wait for only the required requests. $engine->waitForRequests(array()); } public function getWorkingCopy() { return $this->workingCopy; } public function getConduitEngine() { return $this->conduitEngine; } public function setToolset($toolset) { $this->toolset = $toolset; return $this; } public function getToolset() { return $this->toolset; } private function getHelpWorkflows(array $workflows) { if ($this->getToolset()->getToolsetKey() === 'arc') { $legacy = array(); $legacy[] = new ArcanistCloseRevisionWorkflow(); $legacy[] = new ArcanistCommitWorkflow(); $legacy[] = new ArcanistCoverWorkflow(); $legacy[] = new ArcanistDiffWorkflow(); $legacy[] = new ArcanistExportWorkflow(); $legacy[] = new ArcanistGetConfigWorkflow(); $legacy[] = new ArcanistSetConfigWorkflow(); $legacy[] = new ArcanistInstallCertificateWorkflow(); $legacy[] = new ArcanistLintersWorkflow(); $legacy[] = new ArcanistLintWorkflow(); $legacy[] = new ArcanistListWorkflow(); $legacy[] = new ArcanistPatchWorkflow(); $legacy[] = new ArcanistPasteWorkflow(); $legacy[] = new ArcanistTasksWorkflow(); $legacy[] = new ArcanistTodoWorkflow(); $legacy[] = new ArcanistUnitWorkflow(); $legacy[] = new ArcanistWhichWorkflow(); foreach ($legacy as $workflow) { // If this workflow has been updated but not removed from the list // above yet, just skip it. if ($workflow instanceof ArcanistArcWorkflow) { continue; } $workflows[] = $workflow->newLegacyPhutilWorkflow(); } } return $workflows; } } diff --git a/src/toolset/ArcanistAliasEngine.php b/src/toolset/ArcanistAliasEngine.php index cba6d723..baa4ab56 100644 --- a/src/toolset/ArcanistAliasEngine.php +++ b/src/toolset/ArcanistAliasEngine.php @@ -1,282 +1,282 @@ runtime = $runtime; return $this; } public function getRuntime() { return $this->runtime; } public function setToolset(ArcanistToolset $toolset) { $this->toolset = $toolset; return $this; } public function getToolset() { return $this->toolset; } public function setWorkflows(array $workflows) { assert_instances_of($workflows, 'ArcanistWorkflow'); $this->workflows = $workflows; return $this; } public function getWorkflows() { return $this->workflows; } public function setConfigurationSourceList( ArcanistConfigurationSourceList $config) { $this->configurationSourceList = $config; return $this; } public function getConfigurationSourceList() { return $this->configurationSourceList; } public function resolveAliases(array $argv) { $aliases_key = ArcanistArcConfigurationEngineExtension::KEY_ALIASES; $source_list = $this->getConfigurationSourceList(); $aliases = $source_list->getConfig($aliases_key); $results = array(); // Identify aliases which had some kind of format or specification issue // when loading config. We could possibly do this earlier, but it's nice // to handle all the alias stuff in one place. foreach ($aliases as $key => $alias) { $exception = $alias->getException(); if (!$exception) { continue; } // This alias is not defined properly, so we're going to ignore it. unset($aliases[$key]); $results[] = $this->newEffect(ArcanistAliasEffect::EFFECT_CONFIGURATION) ->setMessage( pht( 'Configuration source ("%s") defines an invalid alias, which '. 'will be ignored: %s', - $alias->getConfigurationSource()->getSourceDisplayName()), - $exception->getMessage()); + $alias->getConfigurationSource()->getSourceDisplayName(), + $exception->getMessage())); } $command = array_shift($argv); $stack = array(); return $this->resolveAliasesForCommand( $aliases, $command, $argv, $results, $stack); } private function resolveAliasesForCommand( array $aliases, $command, array $argv, array $results, array $stack) { $toolset = $this->getToolset(); $toolset_key = $toolset->getToolsetKey(); // If we have a command which resolves to a real workflow, match it and // finish resolution. You can not overwrite a real workflow with an alias. $workflows = $this->getWorkflows(); if (isset($workflows[$command])) { $results[] = $this->newEffect(ArcanistAliasEffect::EFFECT_RESOLUTION) ->setCommand($command) ->setArguments($argv); return $results; } // Find all the aliases which match whatever the user typed, like "draft". // We look for aliases in other toolsets, too, so we can provide the user // a hint when they type "phage draft" and mean "arc draft". $matches = array(); $toolset_matches = array(); foreach ($aliases as $alias) { if ($alias->getTrigger() === $command) { $matches[] = $alias; if ($alias->getToolset() == $toolset_key) { $toolset_matches[] = $alias; } } } if (!$toolset_matches) { // If the user typed "phage draft" and meant "arc draft", give them a // hint that the alias exists somewhere else and they may have specified // the wrong toolset. foreach ($matches as $match) { $results[] = $this->newEffect(ArcanistAliasEffect::EFFECT_SUGGEST) ->setMessage( pht( 'No "%s %s" alias is defined, did you mean "%s %s"?', $toolset_key, $command, $match->getToolset(), $command)); } // If the user misspells a command (like "arc hlep") and it doesn't match // anything (no alias or workflow), we want to pass it through unmodified // and let the parser try to correct the spelling into a real workflow // later on. // However, if the user correctly types a command (like "arc draft") that // resolves at least once (so it hits a valid alias) but does not // ultimately resolve into a valid workflow, we want to treat this as a // hard failure. // This could happen if you manually defined a bad alias, or a workflow // you'd previously aliased to was removed, or you stacked aliases and // then deleted one. if ($stack) { $results[] = $this->newEffect(ArcanistAliasEffect::EFFECT_NOTFOUND) ->setMessage( pht( 'Alias resolved to "%s", but this is not a valid workflow or '. 'alias name. This alias or workflow might have previously '. 'existed and been removed.', $command)); } else { $results[] = $this->newEffect(ArcanistAliasEffect::EFFECT_RESOLUTION) ->setCommand($command) ->setArguments($argv); } return $results; } $alias = array_pop($toolset_matches); if ($toolset_matches) { $source = $alias->getConfigurationSource(); $results[] = $this->newEffect(ArcanistAliasEffect::EFFECT_IGNORED) ->setMessage( pht( 'Multiple configuration sources define an alias for "%s %s". '. 'The last definition in the most specific source ("%s") will '. 'be used.', $toolset_key, $command, $source->getSourceDisplayName())); foreach ($toolset_matches as $ignored_match) { $source = $ignored_match->getConfigurationSource(); $results[] = $this->newEffect(ArcanistAliasEffect::EFFECT_IGNORED) ->setMessage( pht( 'A definition of "%s %s" in "%s" will be ignored.', $toolset_key, $command, $source->getSourceDisplayName())); } } $alias_argv = $alias->getCommand(); $alias_command = array_shift($alias_argv); if ($alias->isShellCommandAlias()) { $shell_command = substr($alias_command, 1); $shell_argv = array_merge( array($shell_command), $alias_argv, $argv); $shell_display = csprintf('%Ls', $shell_argv); $results[] = $this->newEffect(ArcanistAliasEffect::EFFECT_SHELL) ->setMessage( pht( '%s %s -> $ %s', $toolset_key, $command, $shell_display)) ->setArguments($shell_argv); return $results; } if (isset($stack[$alias_command])) { $cycle = array_keys($stack); $cycle[] = $alias_command; $cycle = implode(' -> ', $cycle); $results[] = $this->newEffect(ArcanistAliasEffect::EFFECT_CYCLE) ->setMessage( pht( 'Alias definitions form a cycle which can not be resolved: %s.', $cycle)); return $results; } $stack[$alias_command] = true; $stack_limit = 16; if (count($stack) >= $stack_limit) { $results[] = $this->newEffect(ArcanistAliasEffect::EFFECT_STACK) ->setMessage( pht( 'Alias definitions form an unreasonably deep stack. A chain of '. 'aliases may not resolve more than %s times.', new PhutilNumber($stack_limit))); return $results; } $display_argv = (string)csprintf('%LR', $alias_argv); $results[] = $this->newEffect(ArcanistAliasEffect::EFFECT_ALIAS) ->setMessage( pht( '%s %s -> %s %s %s', $toolset_key, $command, $toolset_key, $alias_command, $display_argv)); $argv = array_merge($alias_argv, $argv); return $this->resolveAliasesForCommand( $aliases, $alias_command, $argv, $results, $stack); } protected function newEffect($effect_type) { return id(new ArcanistAliasEffect()) ->setType($effect_type); } } diff --git a/src/workflow/ArcanistCloseRevisionWorkflow.php b/src/workflow/ArcanistCloseRevisionWorkflow.php index 363b5dbe..8ed435a1 100644 --- a/src/workflow/ArcanistCloseRevisionWorkflow.php +++ b/src/workflow/ArcanistCloseRevisionWorkflow.php @@ -1,191 +1,190 @@ array( 'help' => pht( "Close only if the repository is untracked and the revision is ". "accepted. Continue even if the close can't happen. This is a soft ". - "version of '' used by other workflows.", - 'close-revision'), + "version of 'close-revision' used by other workflows."), ), 'quiet' => array( 'help' => pht('Do not print a success message.'), ), '*' => 'revision', ); } public function requiresConduit() { return true; } public function requiresAuthentication() { return true; } public function requiresRepositoryAPI() { // NOTE: Technically we only use this to generate the right message at // the end, and you can even get the wrong message (e.g., if you run // "arc close-revision D123" from a git repository, but D123 is an SVN // revision). We could be smarter about this, but it's just display fluff. return true; } public function run() { $is_finalize = $this->getArgument('finalize'); $conduit = $this->getConduit(); $revision_list = $this->getArgument('revision', array()); if (!$revision_list) { throw new ArcanistUsageException( pht( '%s requires a revision number.', 'close-revision')); } if (count($revision_list) != 1) { throw new ArcanistUsageException( pht( '%s requires exactly one revision.', 'close-revision')); } $revision_id = reset($revision_list); $revision_id = $this->normalizeRevisionID($revision_id); $revisions = $conduit->callMethodSynchronous( 'differential.query', array( 'ids' => array($revision_id), )); $revision = head($revisions); $object_name = "D{$revision_id}"; if (!$revision && !$is_finalize) { throw new ArcanistUsageException( pht( 'Revision %s does not exist.', $object_name)); } $status_accepted = ArcanistDifferentialRevisionStatus::ACCEPTED; $status_closed = ArcanistDifferentialRevisionStatus::CLOSED; if (!$is_finalize && $revision['status'] != $status_accepted) { throw new ArcanistUsageException( pht( "Revision %s can not be closed. You can only close ". "revisions which have been 'accepted'.", $object_name)); } if ($revision) { $revision_display = sprintf( '%s %s', $object_name, $revision['title']); if (!$is_finalize && $revision['authorPHID'] != $this->getUserPHID()) { $prompt = pht( 'You are not the author of revision "%s", '. 'are you sure you want to close it?', $object_name); if (!phutil_console_confirm($prompt)) { throw new ArcanistUserAbortException(); } } $actually_close = true; if ($is_finalize) { if ($this->getRepositoryPHID()) { $actually_close = false; } else if ($revision['status'] != $status_accepted) { // See T13458. The server doesn't permit a transition to "Closed" // over the API if the revision is not "Accepted". If we won't be // able to close the revision, skip the attempt and print a // message. $this->writeWarn( pht('OPEN REVISION'), pht( 'Revision "%s" is not in state "Accepted", so it will '. 'be left open.', $object_name)); $actually_close = false; } } if ($actually_close) { $this->writeInfo( pht('CLOSE'), pht( 'Closing revision "%s"...', $revision_display)); $conduit->callMethodSynchronous( 'differential.close', array( 'revisionID' => $revision_id, )); $this->writeOkay( pht('CLOSE'), pht( 'Done, closed revision.')); } } $status = $revision['status']; if ($status == $status_accepted || $status == $status_closed) { // If this has already been attached to commits, don't show the // "you can push this commit" message since we know it's been pushed // already. $is_finalized = empty($revision['commits']); } else { $is_finalized = false; } if (!$this->getArgument('quiet')) { if ($is_finalized) { $message = $this->getRepositoryAPI()->getFinalizedRevisionMessage(); echo phutil_console_wrap($message)."\n"; } else { echo pht('Done.')."\n"; } } return 0; } }