diff --git a/resources/sql/autopatches/20180618.repo_identity.migrate.php b/resources/sql/autopatches/20180618.repo_identity.migrate.php new file mode 100644 --- /dev/null +++ b/resources/sql/autopatches/20180618.repo_identity.migrate.php @@ -0,0 +1,77 @@ +setViewer(PhabricatorUser::getOmnipotentUser()) + ->needCommitData(true); + +$iterator = new PhabricatorQueryIterator($query); +foreach ($iterator as $commit) { + $needs_update = false; + + $data = $commit->getCommitData(); + $author_name = $data->getAuthorName(); + + $author_identity = get_identity_for_commit( + $commit, + $author_name); + + $author_phid = $commit->getAuthorIdentityPHID(); + $identity_phid = $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_phid = $commit->getCommitterIdentityPHID(); + if (strlen($committer_name)) { + $committer_identity = get_identity_for_commit( + $commit, + $committer_name); + $identity_phid = $committer_identity->getPHID(); + } else { + $identity_phid = null; + } + + if ($committer_phid !== $identity_phid) { + $commit->setCommitterIdentityPHID($identity_phid); + $data->setCommitDetail('committerIdentityPHID', $identity_phid); + $needs_update = true; + } + + if ($needs_update) { + $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]; +}