@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Move more discovery responsibilities into DiscoveryEngine

Summary:
Ref T4327. This moves the last pieces of discovery responsibility out of the PullLocal daemon and into the DiscoveryEngine.

(This makes it easier to discover repositories in unit tests in the future, since we don't need to build a PullLocal daemon and can just build a DiscoveryEngine.)

Test Plan: Ran `phd debug pulllocal`. Ran `repostory discover`.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T4327

Differential Revision: https://secure.phabricator.com/D7997

+102 -109
+2 -104
src/applications/repository/daemon/PhabricatorRepositoryPullLocalDaemon.php
··· 29 29 final class PhabricatorRepositoryPullLocalDaemon 30 30 extends PhabricatorDaemon { 31 31 32 - private $repair; 33 32 private $discoveryEngines = array(); 34 - 35 - public function setRepair($repair) { 36 - $this->repair = $repair; 37 - return $this; 38 - } 39 33 40 34 41 35 /* -( Pulling Repositories )----------------------------------------------- */ ··· 225 219 $refs = $this->getDiscoveryEngine($repository) 226 220 ->discoverCommits(); 227 221 228 - foreach ($refs as $ref) { 229 - $this->recordCommit( 230 - $repository, 231 - $ref->getIdentifier(), 232 - $ref->getEpoch(), 233 - $ref->getCanCloseImmediately()); 234 - } 235 - 236 222 $this->checkIfRepositoryIsFullyImported($repository); 237 223 238 224 try { ··· 262 248 $id = $repository->getID(); 263 249 if (empty($this->discoveryEngines[$id])) { 264 250 $engine = id(new PhabricatorRepositoryDiscoveryEngine()) 265 - ->setRepository($repository) 266 - ->setVerbose($this->getVerbose()) 267 - ->setRepairMode($this->repair); 251 + ->setRepository($repository) 252 + ->setVerbose($this->getVerbose()); 268 253 269 254 $this->discoveryEngines[$id] = $engine; 270 255 } 271 256 return $this->discoveryEngines[$id]; 272 - } 273 - 274 - private function recordCommit( 275 - PhabricatorRepository $repository, 276 - $commit_identifier, 277 - $epoch, 278 - $close_immediately) { 279 - 280 - $commit = new PhabricatorRepositoryCommit(); 281 - $commit->setRepositoryID($repository->getID()); 282 - $commit->setCommitIdentifier($commit_identifier); 283 - $commit->setEpoch($epoch); 284 - if ($close_immediately) { 285 - $commit->setImportStatus(PhabricatorRepositoryCommit::IMPORTED_CLOSEABLE); 286 - } 287 - 288 - $data = new PhabricatorRepositoryCommitData(); 289 - 290 - try { 291 - $commit->openTransaction(); 292 - $commit->save(); 293 - $data->setCommitID($commit->getID()); 294 - $data->save(); 295 - $commit->saveTransaction(); 296 - 297 - $this->insertTask($repository, $commit); 298 - 299 - queryfx( 300 - $repository->establishConnection('w'), 301 - 'INSERT INTO %T (repositoryID, size, lastCommitID, epoch) 302 - VALUES (%d, 1, %d, %d) 303 - ON DUPLICATE KEY UPDATE 304 - size = size + 1, 305 - lastCommitID = 306 - IF(VALUES(epoch) > epoch, VALUES(lastCommitID), lastCommitID), 307 - epoch = IF(VALUES(epoch) > epoch, VALUES(epoch), epoch)', 308 - PhabricatorRepository::TABLE_SUMMARY, 309 - $repository->getID(), 310 - $commit->getID(), 311 - $epoch); 312 - 313 - if ($this->repair) { 314 - // Normally, the query should throw a duplicate key exception. If we 315 - // reach this in repair mode, we've actually performed a repair. 316 - $this->log("Repaired commit '{$commit_identifier}'."); 317 - } 318 - 319 - PhutilEventEngine::dispatchEvent( 320 - new PhabricatorEvent( 321 - PhabricatorEventType::TYPE_DIFFUSION_DIDDISCOVERCOMMIT, 322 - array( 323 - 'repository' => $repository, 324 - 'commit' => $commit, 325 - ))); 326 - 327 - } catch (AphrontQueryDuplicateKeyException $ex) { 328 - $commit->killTransaction(); 329 - // Ignore. This can happen because we discover the same new commit 330 - // more than once when looking at history, or because of races or 331 - // data inconsistency or cosmic radiation; in any case, we're still 332 - // in a good state if we ignore the failure. 333 - } 334 - } 335 - 336 - private function insertTask( 337 - PhabricatorRepository $repository, 338 - PhabricatorRepositoryCommit $commit, 339 - $data = array()) { 340 - 341 - $vcs = $repository->getVersionControlSystem(); 342 - switch ($vcs) { 343 - case PhabricatorRepositoryType::REPOSITORY_TYPE_GIT: 344 - $class = 'PhabricatorRepositoryGitCommitMessageParserWorker'; 345 - break; 346 - case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN: 347 - $class = 'PhabricatorRepositorySvnCommitMessageParserWorker'; 348 - break; 349 - case PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL: 350 - $class = 'PhabricatorRepositoryMercurialCommitMessageParserWorker'; 351 - break; 352 - default: 353 - throw new Exception("Unknown repository type '{$vcs}'!"); 354 - } 355 - 356 - $data['commitID'] = $commit->getID(); 357 - 358 - PhabricatorWorker::scheduleTask($class, $data); 359 257 } 360 258 361 259 private function checkIfRepositoryIsFullyImported(
+95 -1
src/applications/repository/engine/PhabricatorRepositoryDiscoveryEngine.php
··· 50 50 throw new Exception("Unknown VCS '{$vcs}'!"); 51 51 } 52 52 53 - // Mark discovered commits in the cache. 53 + // Record discovered commits and mark them in the cache. 54 54 foreach ($refs as $ref) { 55 + $this->recordCommit( 56 + $repository, 57 + $ref->getIdentifier(), 58 + $ref->getEpoch(), 59 + $ref->getCanCloseImmediately()); 60 + 55 61 $this->commitCache[$ref->getIdentifier()] = true; 56 62 } 57 63 ··· 451 457 } 452 458 453 459 return array_merge($head_branches, $tail_branches); 460 + } 461 + 462 + 463 + private function recordCommit( 464 + PhabricatorRepository $repository, 465 + $commit_identifier, 466 + $epoch, 467 + $close_immediately) { 468 + 469 + $commit = new PhabricatorRepositoryCommit(); 470 + $commit->setRepositoryID($repository->getID()); 471 + $commit->setCommitIdentifier($commit_identifier); 472 + $commit->setEpoch($epoch); 473 + if ($close_immediately) { 474 + $commit->setImportStatus(PhabricatorRepositoryCommit::IMPORTED_CLOSEABLE); 475 + } 476 + 477 + $data = new PhabricatorRepositoryCommitData(); 478 + 479 + try { 480 + $commit->openTransaction(); 481 + $commit->save(); 482 + $data->setCommitID($commit->getID()); 483 + $data->save(); 484 + $commit->saveTransaction(); 485 + 486 + $this->insertTask($repository, $commit); 487 + 488 + queryfx( 489 + $repository->establishConnection('w'), 490 + 'INSERT INTO %T (repositoryID, size, lastCommitID, epoch) 491 + VALUES (%d, 1, %d, %d) 492 + ON DUPLICATE KEY UPDATE 493 + size = size + 1, 494 + lastCommitID = 495 + IF(VALUES(epoch) > epoch, VALUES(lastCommitID), lastCommitID), 496 + epoch = IF(VALUES(epoch) > epoch, VALUES(epoch), epoch)', 497 + PhabricatorRepository::TABLE_SUMMARY, 498 + $repository->getID(), 499 + $commit->getID(), 500 + $epoch); 501 + 502 + if ($this->repairMode) { 503 + // Normally, the query should throw a duplicate key exception. If we 504 + // reach this in repair mode, we've actually performed a repair. 505 + $this->log(pht('Repaired commit "%s".', $commit_identifier)); 506 + } 507 + 508 + PhutilEventEngine::dispatchEvent( 509 + new PhabricatorEvent( 510 + PhabricatorEventType::TYPE_DIFFUSION_DIDDISCOVERCOMMIT, 511 + array( 512 + 'repository' => $repository, 513 + 'commit' => $commit, 514 + ))); 515 + 516 + } catch (AphrontQueryDuplicateKeyException $ex) { 517 + $commit->killTransaction(); 518 + // Ignore. This can happen because we discover the same new commit 519 + // more than once when looking at history, or because of races or 520 + // data inconsistency or cosmic radiation; in any case, we're still 521 + // in a good state if we ignore the failure. 522 + } 523 + } 524 + 525 + private function insertTask( 526 + PhabricatorRepository $repository, 527 + PhabricatorRepositoryCommit $commit, 528 + $data = array()) { 529 + 530 + $vcs = $repository->getVersionControlSystem(); 531 + switch ($vcs) { 532 + case PhabricatorRepositoryType::REPOSITORY_TYPE_GIT: 533 + $class = 'PhabricatorRepositoryGitCommitMessageParserWorker'; 534 + break; 535 + case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN: 536 + $class = 'PhabricatorRepositorySvnCommitMessageParserWorker'; 537 + break; 538 + case PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL: 539 + $class = 'PhabricatorRepositoryMercurialCommitMessageParserWorker'; 540 + break; 541 + default: 542 + throw new Exception("Unknown repository type '{$vcs}'!"); 543 + } 544 + 545 + $data['commitID'] = $commit->getID(); 546 + 547 + PhabricatorWorker::scheduleTask($class, $data); 454 548 } 455 549 456 550 }
+5 -4
src/applications/repository/management/PhabricatorRepositoryManagementDiscoverWorkflow.php
··· 38 38 foreach ($repos as $repo) { 39 39 $console->writeOut("Discovering '%s'...\n", $repo->getCallsign()); 40 40 41 - $daemon = new PhabricatorRepositoryPullLocalDaemon(array()); 42 - $daemon->setVerbose($args->getArg('verbose')); 43 - $daemon->setRepair($args->getArg('repair')); 44 - $daemon->discoverRepository($repo); 41 + id(new PhabricatorRepositoryDiscoveryEngine()) 42 + ->setRepository($repo) 43 + ->setVerbose($args->getArg('verbose')) 44 + ->setRepairMode($args->getArg('repair')) 45 + ->discoverCommits(); 45 46 } 46 47 47 48 $console->writeOut("Done.\n");