Differential D19484 Diff 46613 src/applications/repository/management/PhabricatorRepositoryManagementRebuildIdentitiesWorkflow.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/repository/management/PhabricatorRepositoryManagementRebuildIdentitiesWorkflow.php
Show All 27 Lines | public function execute(PhutilArgumentParser $args) { | ||||
$all = $args->getArg('all'); | $all = $args->getArg('all'); | ||||
$repositories = $args->getArg('repositories'); | $repositories = $args->getArg('repositories'); | ||||
if ($all xor empty($repositories)) { | if ($all xor empty($repositories)) { | ||||
throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
pht('Specify --all or a list of repositories, but not both.')); | pht('Specify --all or a list of repositories, but not both.')); | ||||
} | } | ||||
$query = id(new DiffusionCommitQuery()) | $query = id(new DiffusionCommitQuery()) | ||||
->setViewer(PhabricatorUser::getOmnipotentUser()) | ->setViewer(PhabricatorUser::getOmnipotentUser()) | ||||
->needCommitData(true); | ->needCommitData(true); | ||||
amckinley: I was thinking we could speed this up instead by searching for commits without identities, but… | |||||
if ($repositories) { | if ($repositories) { | ||||
$repos = $this->loadRepositories($args, 'repositories'); | $repos = $this->loadRepositories($args, 'repositories'); | ||||
$query->withRepositoryIDs(mpull($repos, 'getID')); | $query->withRepositoryIDs(mpull($repos, 'getID')); | ||||
} | } | ||||
$iterator = new PhabricatorQueryIterator($query); | $iterator = new PhabricatorQueryIterator($query); | ||||
foreach ($iterator as $commit) { | foreach ($iterator as $commit) { | ||||
$needs_update = false; | |||||
$data = $commit->getCommitData(); | $data = $commit->getCommitData(); | ||||
$author_name = $data->getAuthorName(); | $author_name = $data->getAuthorName(); | ||||
$author_identity = $this->getIdentityForCommit( | $author_identity = $this->getIdentityForCommit( | ||||
$commit, $author_name); | $commit, | ||||
$author_name); | |||||
$commit->setAuthorIdentityPHID($author_identity->getPHID()); | $author_phid = $commit->getAuthorIdentityPHID(); | ||||
$data->setCommitDetail( | $identity_phid = $author_identity->getPHID(); | ||||
'authorIdentityPHID', $author_identity->getPHID()); | if ($author_phid !== $identity_phid) { | ||||
$commit->setAuthorIdentityPHID($identity_phid); | |||||
$data->setCommitDetail('authorIdentityPHID', $identity_phid); | |||||
$needs_update = true; | |||||
} | |||||
$committer_name = $data->getCommitDetail('committer', null); | $committer_name = $data->getCommitDetail('committer', null); | ||||
if ($committer_name) { | $committer_phid = $commit->getCommitterIdentityPHID(); | ||||
if (strlen($committer_name)) { | |||||
$committer_identity = $this->getIdentityForCommit( | $committer_identity = $this->getIdentityForCommit( | ||||
$commit, $committer_name); | $commit, | ||||
$committer_name); | |||||
$identity_phid = $committer_identity->getPHID(); | |||||
} else { | |||||
$identity_phid = null; | |||||
} | |||||
Not Done Inline ActionsI'm convinced that reusing $identity_phid here is ok, but it took me a second. amckinley: I'm convinced that reusing `$identity_phid` here is ok, but it took me a second. | |||||
$commit->setCommitterIdentityPHID($committer_identity->getPHID()); | if ($committer_phid !== $identity_phid) { | ||||
$data->setCommitDetail( | $commit->setCommitterIdentityPHID($identity_phid); | ||||
'committerIdentityPHID', $committer_identity->getPHID()); | $data->setCommitDetail('committerIdentityPHID', $identity_phid); | ||||
$needs_update = true; | |||||
} | } | ||||
if ($needs_update) { | |||||
$commit->save(); | $commit->save(); | ||||
$data->save(); | $data->save(); | ||||
echo tsprintf( | |||||
"Rebuilt identities for %s.\n", | |||||
$commit->getDisplayName()); | |||||
} else { | |||||
echo tsprintf( | |||||
"No changes for %s.\n", | |||||
$commit->getDisplayName()); | |||||
} | |||||
} | } | ||||
Not Done Inline Actions...and presumably doing all these writes is the expensive part of the operation anyway. amckinley: ...and presumably doing all these writes is the expensive part of the operation anyway. | |||||
} | } | ||||
private function getIdentityForCommit( | private function getIdentityForCommit( | ||||
PhabricatorRepositoryCommit $commit, $identity_name) { | PhabricatorRepositoryCommit $commit, $identity_name) { | ||||
static $seen = array(); | static $seen = array(); | ||||
$identity_key = PhabricatorHash::digestForIndex($identity_name); | $identity_key = PhabricatorHash::digestForIndex($identity_name); | ||||
Show All 25 Lines |
I was thinking we could speed this up instead by searching for commits without identities, but presumably users might want to actually "rebuild all identities", as advertised...