4 * Pushes a repository to its mirrors.
6 final class PhabricatorRepositoryMirrorEngine
7 extends PhabricatorRepositoryEngine
{
9 public function pushToMirrors() {
10 $viewer = $this->getViewer();
11 $repository = $this->getRepository();
13 if (!$repository->canMirror()) {
17 if (PhabricatorEnv
::getEnvConfig('phabricator.silent')) {
19 pht('This software is running in silent mode; declining to mirror.'));
23 $uris = id(new PhabricatorRepositoryURIQuery())
25 ->withRepositories(array($repository))
28 $io_mirror = PhabricatorRepositoryURI
::IO_MIRROR
;
30 $exceptions = array();
31 foreach ($uris as $mirror) {
32 if ($mirror->getIsDisabled()) {
36 $io_type = $mirror->getEffectiveIOType();
37 if ($io_type != $io_mirror) {
42 $this->pushRepositoryToMirror($repository, $mirror);
43 } catch (Exception
$ex) {
49 throw new PhutilAggregateException(
51 'Exceptions occurred while mirroring the "%s" repository.',
52 $repository->getDisplayName()),
57 private function pushRepositoryToMirror(
58 PhabricatorRepository
$repository,
59 PhabricatorRepositoryURI
$mirror_uri) {
63 'Pushing to remote "%s"...',
64 $mirror_uri->getEffectiveURI()));
66 if ($repository->isGit()) {
67 $this->pushToGitRepository($repository, $mirror_uri);
68 } else if ($repository->isHg()) {
69 $this->pushToHgRepository($repository, $mirror_uri);
71 throw new Exception(pht('Unsupported VCS!'));
75 private function pushToGitRepository(
76 PhabricatorRepository
$repository,
77 PhabricatorRepositoryURI
$mirror_uri) {
79 // See T5965. Test if we have any refs to mirror. If we have nothing, git
80 // will exit with an error ("No refs in common and none specified; ...")
81 // when we run "git push --mirror".
83 // If we don't have any refs, we just bail out. (This is arguably sort of
84 // the wrong behavior: to mirror an empty repository faithfully we should
85 // delete everything in the remote.)
87 list($stdout) = $repository->execxLocalCommand(
88 'for-each-ref --count 1 --');
89 if (!strlen($stdout)) {
94 'push --verbose --mirror -- %P',
95 $mirror_uri->getURIEnvelope(),
98 $future = $mirror_uri->newCommandEngine()
103 ->setCWD($repository->getLocalPath())
107 private function pushToHgRepository(
108 PhabricatorRepository
$repository,
109 PhabricatorRepositoryURI
$mirror_uri) {
112 'push --verbose --rev tip -- %P',
113 $mirror_uri->getURIEnvelope(),
116 $future = $mirror_uri->newCommandEngine()
122 ->setCWD($repository->getLocalPath())
124 } catch (CommandException
$ex) {
125 if (preg_match('/no changes found/', $ex->getStdout())) {
126 // mercurial says nothing changed, but that's good