@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

Add a "--dry-run" flag to "bin/repository rebuild-identities"

Summary: Ref T13444. Allow the effects of performing an identity rebuild to be previewed without committing to any changes.

Test Plan: Ran "bin/repository rebuild-identities --all-identities" with and without "--dry-run".

Maniphest Tasks: T13444

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

+84 -25
+17 -3
src/applications/diffusion/identity/DiffusionRepositoryIdentityEngine.php
··· 5 5 6 6 private $viewer; 7 7 private $sourcePHID; 8 + private $dryRun; 8 9 9 10 public function setViewer(PhabricatorUser $viewer) { 10 11 $this->viewer = $viewer; ··· 26 27 } 27 28 28 29 return $this->sourcePHID; 30 + } 31 + 32 + public function setDryRun($dry_run) { 33 + $this->dryRun = $dry_run; 34 + return $this; 35 + } 36 + 37 + public function getDryRun() { 38 + return $this->dryRun; 29 39 } 30 40 31 41 public function newResolvedIdentity($raw_identity) { ··· 88 98 89 99 $resolved_phid = $this->resolveIdentity($identity); 90 100 91 - $identity 92 - ->setAutomaticGuessedUserPHID($resolved_phid) 93 - ->save(); 101 + $identity->setAutomaticGuessedUserPHID($resolved_phid); 102 + 103 + if ($this->getDryRun()) { 104 + $identity->makeEphemeral(); 105 + } else { 106 + $identity->save(); 107 + } 94 108 95 109 return $identity; 96 110 }
+67 -22
src/applications/repository/management/PhabricatorRepositoryManagementRebuildIdentitiesWorkflow.php
··· 4 4 extends PhabricatorRepositoryManagementWorkflow { 5 5 6 6 private $identityCache = array(); 7 + private $phidCache = array(); 8 + private $dryRun; 7 9 8 10 protected function didConstruct() { 9 11 $this ··· 51 53 'repeat' => true, 52 54 'help' => pht('Rebuild identities for a raw commit string.'), 53 55 ), 56 + array( 57 + 'name' => 'dry-run', 58 + 'help' => pht('Show changes, but do not make any changes.'), 59 + ), 54 60 )); 55 61 } 56 62 ··· 58 64 $viewer = $this->getViewer(); 59 65 60 66 $rebuilt_anything = false; 61 - 62 67 63 68 $all_repositories = $args->getArg('all-repositories'); 64 69 $repositories = $args->getArg('repository'); ··· 79 84 pht( 80 85 'Flags "--all-identities" and "--raw" are not '. 81 86 'compatible.')); 87 + } 88 + 89 + $dry_run = $args->getArg('dry-run'); 90 + $this->dryRun = $dry_run; 91 + 92 + if ($this->dryRun) { 93 + $this->logWarn( 94 + pht('DRY RUN'), 95 + pht('This is a dry run, so no changes will be written.')); 82 96 } 83 97 84 98 if ($all_repositories || $repositories) { ··· 245 259 $raw_identity) { 246 260 247 261 if (!isset($this->identityCache[$raw_identity])) { 248 - $viewer = $this->getViewer(); 249 - 250 - $identity = id(new DiffusionRepositoryIdentityEngine()) 251 - ->setViewer($viewer) 262 + $identity = $this->newIdentityEngine() 252 263 ->setSourcePHID($commit->getPHID()) 253 264 ->newResolvedIdentity($raw_identity); 254 265 ··· 317 328 } 318 329 319 330 private function rebuildIdentities($identities) { 320 - $viewer = $this->getViewer(); 331 + $dry_run = $this->dryRun; 321 332 322 333 foreach ($identities as $identity) { 323 334 $raw_identity = $identity->getIdentityName(); ··· 340 351 $old_auto = $identity->getAutomaticGuessedUserPHID(); 341 352 $old_assign = $identity->getManuallySetUserPHID(); 342 353 343 - $identity = id(new DiffusionRepositoryIdentityEngine()) 344 - ->setViewer($viewer) 354 + $identity = $this->newIdentityEngine() 345 355 ->newUpdatedIdentity($identity); 346 356 347 357 $this->identityCache[$raw_identity] = $identity; ··· 358 368 pht('No changes to identity.')); 359 369 } else { 360 370 if (!$same_auto) { 361 - $this->logWarn( 362 - pht('AUTOMATIC PHID'), 363 - pht( 364 - 'Automatic user updated from "%s" to "%s".', 365 - $this->renderPHID($old_auto), 366 - $this->renderPHID($new_auto))); 371 + if ($dry_run) { 372 + $this->logWarn( 373 + pht('DETECTED PHID'), 374 + pht( 375 + '(Dry Run) Would update detected user from "%s" to "%s".', 376 + $this->renderPHID($old_auto), 377 + $this->renderPHID($new_auto))); 378 + } else { 379 + $this->logWarn( 380 + pht('DETECTED PHID'), 381 + pht( 382 + 'Detected user updated from "%s" to "%s".', 383 + $this->renderPHID($old_auto), 384 + $this->renderPHID($new_auto))); 385 + } 367 386 } 368 387 if (!$same_assign) { 369 - $this->logWarn( 370 - pht('ASSIGNED PHID'), 371 - pht( 372 - 'Assigned user updated from "%s" to "%s".', 373 - $this->renderPHID($old_assign), 374 - $this->renderPHID($new_assign))); 388 + if ($dry_run) { 389 + $this->logWarn( 390 + pht('ASSIGNED PHID'), 391 + pht( 392 + '(Dry Run) Would update assigned user from "%s" to "%s".', 393 + $this->renderPHID($old_assign), 394 + $this->renderPHID($new_assign))); 395 + } else { 396 + $this->logWarn( 397 + pht('ASSIGNED PHID'), 398 + pht( 399 + 'Assigned user updated from "%s" to "%s".', 400 + $this->renderPHID($old_assign), 401 + $this->renderPHID($new_assign))); 402 + } 375 403 } 376 404 } 377 405 } ··· 380 408 private function renderPHID($phid) { 381 409 if ($phid == null) { 382 410 return pht('NULL'); 383 - } else { 384 - return $phid; 385 411 } 412 + 413 + if (!isset($this->phidCache[$phid])) { 414 + $viewer = $this->getViewer(); 415 + $handles = $viewer->loadHandles(array($phid)); 416 + $this->phidCache[$phid] = pht( 417 + '%s <%s>', 418 + $handles[$phid]->getFullName(), 419 + $phid); 420 + } 421 + 422 + return $this->phidCache[$phid]; 423 + } 424 + 425 + private function newIdentityEngine() { 426 + $viewer = $this->getViewer(); 427 + 428 + return id(new DiffusionRepositoryIdentityEngine()) 429 + ->setViewer($viewer) 430 + ->setDryRun($this->dryRun); 386 431 } 387 432 388 433 }