diff --git a/resources/sql/autopatches/20180510.repo_identity.big_migrate.php b/resources/sql/autopatches/20180510.repo_identity.big_migrate.php new file mode 100644 --- /dev/null +++ b/resources/sql/autopatches/20180510.repo_identity.big_migrate.php @@ -0,0 +1,59 @@ +setViewer(PhabricatorUser::getOmnipotentUser()) + ->needCommitData(true); +$iterator = new PhabricatorQueryIterator($query); + +foreach ($iterator as $commit) { + $data = $commit->getCommitData(); + $author_name = $data->getAuthorName(); + $author_identity = get_identity_for_commit($commit, $author_name); + + $commit->setAuthorIdentityPHID($author_identity->getPHID()); + $data->setCommitDetail( + 'authorIdentityPHID', $author_identity->getPHID()); + + $committer_name = $data->getCommitDetail('committer', null); + if ($committer_name) { + $committer_identity = get_identity_for_commit($commit, $committer_name); + + $commit->setCommitterIdentityPHID($committer_identity->getPHID()); + $data->setCommitDetail( + 'committerIdentityPHID', $committer_identity->getPHID()); + } + + $commit->save(); + $data->save(); +} + + +function get_identity_for_commit( + PhabricatorRepositoryCommit $commit, $identity_name) { + + static $seen = array(); + $identity_key = PhabricatorHash::digestForIndex($identity_name); + if (empty($seen[$identity_key])) { + try { + $user_phid = id(new DiffusionResolveUserQuery()) + ->withCommit($commit) + ->withName($identity_name) + ->execute(); + + $identity = id(new PhabricatorRepositoryIdentity()) + ->setAuthorPHID($commit->getPHID()) + ->setIdentityName($identity_name) + ->setAutomaticGuessedUserPHID($user_phid) + ->save(); + } catch (AphrontDuplicateKeyQueryException $ex) { + // Somehow this identity already exists? + $identity = id(new PhabricatorRepositoryIdentityQuery()) + ->setViewer(PhabricatorUser::getOmnipotentUser()) + ->withIdentityNames(array($identity_name)) + ->executeOne(); + } + $seen[$identity_key] = $identity; + } + + return $seen[$identity_key]; +}