From 6716d4f6ae17ca606799d9a89bedc3929828a15b Mon Sep 17 00:00:00 2001 From: epriestley Date: Fri, 22 Jan 2021 13:53:52 -0800 Subject: [PATCH] Separate "shouldPublishRef()" from "isPermanentRef()" and set "IMPORTED_PERMANENT" more narrowly Summary: Ref T13591. Currently, the "IMPORTED_PERMANENT" flag (previously "IMPORTED_CLOSEABLE", until D21514) flag is set by using the result of "shouldPublishRef()". This method returns the wrong value for the flag when there is a repository-level reason not to publish the ref (most commonly, because the repository is currently importing). Although it's correct that commits should not be published in an importing repository, that's already handled in the "PublishWorker" by testing "shouldPublishCommit()". The "IMPORTED_PERMANENT" flag should only reflect whether a commit is reachable from a permanent ref or not. - Move the relevant logic to a new method in Publisher. - Fill "IMPORTED_PERMANENT" narrowly from "isPermanentRef()", rather than broadly from "shouldPublishRef()". - Deduplicate some logic in "PhabricatorRepositoryRefEngine" which has the same intent as the logic in the Publisher. Test Plan: - Ran discovery on a new repository, saw permanent commits marked as permanent from the beginning. - See later changes in this patch series for additional testing. Maniphest Tasks: T13591 Differential Revision: https://secure.phabricator.com/D21515 --- .../PhabricatorRepositoryDiscoveryEngine.php | 10 ++--- .../engine/PhabricatorRepositoryRefEngine.php | 26 ++++++------- .../query/PhabricatorRepositoryPublisher.php | 39 +++++++++++++------ .../PhabricatorRepositoryRefCursor.php | 6 +++ 4 files changed, 49 insertions(+), 32 deletions(-) diff --git a/src/applications/repository/engine/PhabricatorRepositoryDiscoveryEngine.php b/src/applications/repository/engine/PhabricatorRepositoryDiscoveryEngine.php index 1c27e38e99..dedb9de93a 100644 --- a/src/applications/repository/engine/PhabricatorRepositoryDiscoveryEngine.php +++ b/src/applications/repository/engine/PhabricatorRepositoryDiscoveryEngine.php @@ -189,7 +189,7 @@ final class PhabricatorRepositoryDiscoveryEngine $head_refs = $this->discoverStreamAncestry( new PhabricatorGitGraphStream($repository, $commit), $commit, - $publisher->shouldPublishRef($ref)); + $publisher->isPermanentRef($ref)); $this->didDiscoverRefs($head_refs); @@ -507,9 +507,9 @@ final class PhabricatorRepositoryDiscoveryEngine } /** - * Sort branches so we process permanent branches first. This makes the - * whole import process a little cheaper, since we can publish these commits - * the first time through rather than catching them in the refs step. + * Sort refs so we process permanent refs first. This makes the whole import + * process a little cheaper, since we can publish these commits the first + * time through rather than catching them in the refs step. * * @task internal * @@ -523,7 +523,7 @@ final class PhabricatorRepositoryDiscoveryEngine $head_refs = array(); $tail_refs = array(); foreach ($refs as $ref) { - if ($publisher->shouldPublishRef($ref)) { + if ($publisher->isPermanentRef($ref)) { $head_refs[] = $ref; } else { $tail_refs[] = $ref; diff --git a/src/applications/repository/engine/PhabricatorRepositoryRefEngine.php b/src/applications/repository/engine/PhabricatorRepositoryRefEngine.php index 18dbf754f9..390e6e569d 100644 --- a/src/applications/repository/engine/PhabricatorRepositoryRefEngine.php +++ b/src/applications/repository/engine/PhabricatorRepositoryRefEngine.php @@ -100,7 +100,7 @@ final class PhabricatorRepositoryRefEngine $this->setPermanentFlagOnCommits($this->permanentCommits); } - $save_cursors = $this->getCursorsForUpdate($all_cursors); + $save_cursors = $this->getCursorsForUpdate($repository, $all_cursors); if ($this->newPositions || $this->deadPositions || $save_cursors) { $repository->openTransaction(); @@ -121,17 +121,19 @@ final class PhabricatorRepositoryRefEngine } } - private function getCursorsForUpdate(array $cursors) { + private function getCursorsForUpdate( + PhabricatorRepository $repository, + array $cursors) { assert_instances_of($cursors, 'PhabricatorRepositoryRefCursor'); + $publisher = $repository->newPublisher(); + $results = array(); foreach ($cursors as $cursor) { - $ref_type = $cursor->getRefType(); - $ref_name = $cursor->getRefName(); - - $is_permanent = $this->isPermanentRef($ref_type, $ref_name); + $diffusion_ref = $cursor->newDiffusionRepositoryRef(); + $is_permanent = $publisher->isPermanentRef($diffusion_ref); if ($is_permanent == $cursor->getIsPermanent()) { continue; } @@ -259,6 +261,7 @@ final class PhabricatorRepositoryRefEngine $ref_type, array $all_closing_heads) { $repository = $this->getRepository(); + $publisher = $repository->newPublisher(); // NOTE: Mercurial branches may have multiple branch heads; this logic // is complex primarily to account for that. @@ -341,7 +344,8 @@ final class PhabricatorRepositoryRefEngine $this->markPositionNew($new_position); } - if ($this->isPermanentRef($ref_type, $name)) { + $diffusion_ref = head($refs)->newDiffusionRepositoryRef(); + if ($publisher->isPermanentRef($diffusion_ref)) { // See T13284. If this cursor was already marked as permanent, we // only need to publish the newly created ref positions. However, if @@ -404,14 +408,6 @@ final class PhabricatorRepositoryRefEngine } } - private function isPermanentRef($ref_type, $ref_name) { - if ($ref_type !== PhabricatorRepositoryRefCursor::TYPE_BRANCH) { - return false; - } - - return $this->getRepository()->isBranchPermanentRef($ref_name); - } - /** * Find all ancestors of a new closing branch head which are not ancestors * of any old closing branch head. diff --git a/src/applications/repository/query/PhabricatorRepositoryPublisher.php b/src/applications/repository/query/PhabricatorRepositoryPublisher.php index ad374bd034..fae1b07f00 100644 --- a/src/applications/repository/query/PhabricatorRepositoryPublisher.php +++ b/src/applications/repository/query/PhabricatorRepositoryPublisher.php @@ -38,6 +38,10 @@ final class PhabricatorRepositoryPublisher return !$this->getCommitHoldReasons($commit); } + public function isPermanentRef(DiffusionRepositoryRef $ref) { + return !$this->getRefImpermanentReasons($ref); + } + /* -( Hold Reasons )------------------------------------------------------- */ public function getRepositoryHoldReasons() { @@ -59,18 +63,8 @@ final class PhabricatorRepositoryPublisher $repository = $this->getRepository(); $reasons = $this->getRepositoryHoldReasons(); - if (!$ref->isBranch()) { - $reasons[] = self::HOLD_REF_NOT_BRANCH; - } else { - $branch_name = $ref->getShortName(); - - if (!$repository->shouldTrackBranch($branch_name)) { - $reasons[] = self::HOLD_UNTRACKED; - } - - if (!$repository->isBranchPermanentRef($branch_name)) { - $reasons[] = self::HOLD_NOT_PERMANENT_REF; - } + foreach ($this->getRefImpermanentReasons($ref) as $reason) { + $reasons[] = $reason; } return $reasons; @@ -89,4 +83,25 @@ final class PhabricatorRepositoryPublisher return $reasons; } + public function getRefImpermanentReasons(DiffusionRepositoryRef $ref) { + $repository = $this->getRepository(); + $reasons = array(); + + if (!$ref->isBranch()) { + $reasons[] = self::HOLD_REF_NOT_BRANCH; + } else { + $branch_name = $ref->getShortName(); + + if (!$repository->shouldTrackBranch($branch_name)) { + $reasons[] = self::HOLD_UNTRACKED; + } + + if (!$repository->isBranchPermanentRef($branch_name)) { + $reasons[] = self::HOLD_NOT_PERMANENT_REF; + } + } + + return $reasons; + } + } diff --git a/src/applications/repository/storage/PhabricatorRepositoryRefCursor.php b/src/applications/repository/storage/PhabricatorRepositoryRefCursor.php index 91261f2b93..e661e94295 100644 --- a/src/applications/repository/storage/PhabricatorRepositoryRefCursor.php +++ b/src/applications/repository/storage/PhabricatorRepositoryRefCursor.php @@ -88,6 +88,12 @@ final class PhabricatorRepositoryRefCursor return mpull($this->getPositions(), 'getCommitIdentifier'); } + public function newDiffusionRepositoryRef() { + return id(new DiffusionRepositoryRef()) + ->setRefType($this->getRefType()) + ->setShortName($this->getRefName()); + } + /* -( PhabricatorPolicyInterface )----------------------------------------- */