Skip to content

Commit d09dd23

Browse files
author
epriestley
committed
Actually push to mirrors
Summary: Fixes T4038. Push repositories to mirrors. Test Plan: Created a functional mirror of a local: https://github.com/epriestley/poems Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T4038 Differential Revision: https://secure.phabricator.com/D7633
1 parent 4b91c4f commit d09dd23

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

scripts/ssh/ssh-connect.php

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,6 @@
77
$root = dirname(dirname(dirname(__FILE__)));
88
require_once $root.'/scripts/__init_script__.php';
99

10-
$target_name = getenv('PHABRICATOR_SSH_TARGET');
11-
if (!$target_name) {
12-
throw new Exception(pht("No 'PHABRICATOR_SSH_TARGET' in environment!"));
13-
}
14-
15-
$viewer = PhabricatorUser::getOmnipotentUser();
16-
17-
$repository = id(new PhabricatorRepositoryQuery())
18-
->setViewer($viewer)
19-
->withCallsigns(array($target_name))
20-
->executeOne();
21-
if (!$repository) {
22-
throw new Exception(pht('No repository with callsign "%s"!', $target_name));
23-
}
24-
2510
$pattern = array();
2611
$arguments = array();
2712

@@ -30,8 +15,9 @@
3015
$pattern[] = '-o';
3116
$pattern[] = 'StrictHostKeyChecking=no';
3217

33-
$credential_phid = $repository->getCredentialPHID();
18+
$credential_phid = getenv('PHABRICATOR_CREDENTIAL');
3419
if ($credential_phid) {
20+
$viewer = PhabricatorUser::getOmnipotentUser();
3521
$key = PassphraseSSHKey::loadFromPHID($credential_phid, $viewer);
3622

3723
$pattern[] = '-l %P';

src/applications/repository/daemon/PhabricatorRepositoryPullLocalDaemon.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,15 @@ public function discoverRepository(PhabricatorRepository $repository) {
252252

253253
$this->checkIfRepositoryIsFullyImported($repository);
254254

255+
try {
256+
$this->pushToMirrors($repository);
257+
} catch (Exception $ex) {
258+
// TODO: We should report these into the UI properly, but for
259+
// now just complain. These errors are much less severe than
260+
// pull errors.
261+
phlog($ex);
262+
}
263+
255264
if ($refs !== null) {
256265
return (bool)count($refs);
257266
} else {
@@ -802,4 +811,43 @@ private static function executeGitNormalizePath($path) {
802811
}
803812

804813

814+
private function pushToMirrors(PhabricatorRepository $repository) {
815+
if (!$repository->canMirror()) {
816+
return;
817+
}
818+
819+
$mirrors = id(new PhabricatorRepositoryMirrorQuery())
820+
->setViewer($this->getViewer())
821+
->withRepositoryPHIDs(array($repository->getPHID()))
822+
->execute();
823+
824+
// TODO: This is a little bit janky, but we don't have first-class
825+
// infrastructure for running remote commands against an arbitrary remote
826+
// right now. Just make an emphemeral copy of the repository and muck with
827+
// it a little bit. In the medium term, we should pull this command stuff
828+
// out and use it here and for "Land to ...".
829+
830+
$proxy = clone $repository;
831+
$proxy->makeEphemeral();
832+
833+
$proxy->setDetail('hosting-enabled', false);
834+
foreach ($mirrors as $mirror) {
835+
$proxy->setDetail('remote-uri', $mirror->getRemoteURI());
836+
$proxy->setCredentialPHID($mirror->getCredentialPHID());
837+
838+
$this->log(pht('Pushing to remote "%s"...', $mirror->getRemoteURI()));
839+
840+
if (!$proxy->isGit()) {
841+
throw new Exception('Unsupported VCS!');
842+
}
843+
844+
$future = $proxy->getRemoteCommandFuture(
845+
'push --verbose --mirror -- %s',
846+
$proxy->getRemoteURI());
847+
848+
$future
849+
->setCWD($proxy->getLocalPath())
850+
->resolvex();
851+
}
852+
}
805853
}

src/applications/repository/storage/PhabricatorRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ private function getRemoteCommandEnvironment() {
337337
if ($this->shouldUseSSH()) {
338338
// NOTE: This is read by `bin/ssh-connect`, and tells it which credentials
339339
// to use.
340-
$env['PHABRICATOR_SSH_TARGET'] = $this->getCallsign();
340+
$env['PHABRICATOR_CREDENTIAL'] = $this->getCredentialPHID();
341341
switch ($this->getVersionControlSystem()) {
342342
case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN:
343343
// Force SVN to use `bin/ssh-connect`.

0 commit comments

Comments
 (0)