mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-22 06:42:42 +01:00
d8550c114d
Summary: Fixes T7019. In a cluster environment, pushes currently fail because the commit hook can't identify the instance. For web processes, the hostname identifies the instance -- but we don't have a hostname in the hook. For CLI processes, the environment identifies the instance -- but we don't have an environment in the hook under SVN. Promote the instance identifier into the upstream and pack/unpack it explicitly for hooks. This is probably not useful for anyone but us, but the amount of special-purpose code we're introducing is very small. I poked at trying to do this in a more general way, but: - We MUST know this BEFORE we run code, so the normal subclassing stuff is useless. - I couldn't come up with any other parameter which might ever be useful to pass in. Test Plan: Used `git push` to push code through proxied HTTP, got a clean push. Reviewers: btrahan Reviewed By: btrahan Subscribers: epriestley Maniphest Tasks: T7019 Differential Revision: https://secure.phabricator.com/D11495
161 lines
5 KiB
PHP
Executable file
161 lines
5 KiB
PHP
Executable file
#!/usr/bin/env php
|
|
<?php
|
|
|
|
// Commit hooks execute in an unusual context where the environment may be
|
|
// unavailable, particularly in SVN. The first parameter to this script is
|
|
// either a bare repository identifier ("X"), or a repository identifier
|
|
// followed by an instance identifier ("X:instance"). If we have an instance
|
|
// identifier, unpack it into the environment before we start up. This allows
|
|
// subclasses of PhabricatorConfigSiteSource to read it and build an instance
|
|
// environment.
|
|
|
|
if ($argc > 1) {
|
|
$context = $argv[1];
|
|
$context = explode(':', $context, 2);
|
|
$argv[1] = $context[0];
|
|
|
|
if (count($context) > 1) {
|
|
$_ENV['PHABRICATOR_INSTANCE'] = $context[1];
|
|
putenv('PHABRICATOR_INSTANCE='.$context[1]);
|
|
}
|
|
}
|
|
|
|
$root = dirname(dirname(dirname(__FILE__)));
|
|
require_once $root.'/scripts/__init_script__.php';
|
|
|
|
if ($argc < 2) {
|
|
throw new Exception(pht('usage: commit-hook <callsign>'));
|
|
}
|
|
|
|
$engine = new DiffusionCommitHookEngine();
|
|
|
|
$repository = id(new PhabricatorRepositoryQuery())
|
|
->setViewer(PhabricatorUser::getOmnipotentUser())
|
|
->withCallsigns(array($argv[1]))
|
|
->needProjectPHIDs(true)
|
|
->executeOne();
|
|
|
|
if (!$repository) {
|
|
throw new Exception(pht('No such repository "%s"!', $argv[1]));
|
|
}
|
|
|
|
if (!$repository->isHosted()) {
|
|
// This should be redundant, but double check just in case.
|
|
throw new Exception(pht('Repository "%s" is not hosted!', $argv[1]));
|
|
}
|
|
|
|
$engine->setRepository($repository);
|
|
|
|
|
|
// Figure out which user is writing the commit.
|
|
|
|
if ($repository->isGit() || $repository->isHg()) {
|
|
$username = getenv(DiffusionCommitHookEngine::ENV_USER);
|
|
if (!strlen($username)) {
|
|
throw new Exception(
|
|
pht('usage: %s should be defined!', DiffusionCommitHookEngine::ENV_USER));
|
|
}
|
|
|
|
if ($repository->isHg()) {
|
|
// We respond to several different hooks in Mercurial.
|
|
$engine->setMercurialHook($argv[2]);
|
|
}
|
|
|
|
} else if ($repository->isSVN()) {
|
|
// NOTE: In Subversion, the entire environment gets wiped so we can't read
|
|
// DiffusionCommitHookEngine::ENV_USER. Instead, we've set "--tunnel-user" to
|
|
// specify the correct user; read this user out of the commit log.
|
|
|
|
if ($argc < 4) {
|
|
throw new Exception(pht('usage: commit-hook <callsign> <repo> <txn>'));
|
|
}
|
|
|
|
$svn_repo = $argv[2];
|
|
$svn_txn = $argv[3];
|
|
list($username) = execx('svnlook author -t %s %s', $svn_txn, $svn_repo);
|
|
$username = rtrim($username, "\n");
|
|
|
|
$engine->setSubversionTransactionInfo($svn_txn, $svn_repo);
|
|
} else {
|
|
throw new Exception(pht('Unknown repository type.'));
|
|
}
|
|
|
|
$user = id(new PhabricatorPeopleQuery())
|
|
->setViewer(PhabricatorUser::getOmnipotentUser())
|
|
->withUsernames(array($username))
|
|
->executeOne();
|
|
|
|
if (!$user) {
|
|
throw new Exception(pht('No such user "%s"!', $username));
|
|
}
|
|
|
|
$engine->setViewer($user);
|
|
|
|
|
|
// Read stdin for the hook engine.
|
|
|
|
if ($repository->isHg()) {
|
|
// Mercurial leaves stdin open, so we can't just read it until EOF.
|
|
$stdin = '';
|
|
} else {
|
|
// Git and Subversion write data into stdin and then close it. Read the
|
|
// data.
|
|
$stdin = @file_get_contents('php://stdin');
|
|
if ($stdin === false) {
|
|
throw new Exception(pht('Failed to read stdin!'));
|
|
}
|
|
}
|
|
|
|
$engine->setStdin($stdin);
|
|
$engine->setOriginalArgv(array_slice($argv, 2));
|
|
|
|
$remote_address = getenv(DiffusionCommitHookEngine::ENV_REMOTE_ADDRESS);
|
|
if (strlen($remote_address)) {
|
|
$engine->setRemoteAddress($remote_address);
|
|
}
|
|
|
|
$remote_protocol = getenv(DiffusionCommitHookEngine::ENV_REMOTE_PROTOCOL);
|
|
if (strlen($remote_protocol)) {
|
|
$engine->setRemoteProtocol($remote_protocol);
|
|
}
|
|
|
|
try {
|
|
$err = $engine->execute();
|
|
} catch (DiffusionCommitHookRejectException $ex) {
|
|
$console = PhutilConsole::getConsole();
|
|
|
|
if (PhabricatorEnv::getEnvConfig('phabricator.serious-business')) {
|
|
$preamble = pht('*** PUSH REJECTED BY COMMIT HOOK ***');
|
|
} else {
|
|
$preamble = pht(<<<EOTXT
|
|
+---------------------------------------------------------------+
|
|
| * * * PUSH REJECTED BY EVIL DRAGON BUREAUCRATS * * * |
|
|
+---------------------------------------------------------------+
|
|
\
|
|
\ ^ /^
|
|
\ / \ // \
|
|
\ |\___/| / \// .\
|
|
\ /V V \__ / // | \ \ *----*
|
|
/ / \/_/ // | \ \ \ |
|
|
@___@` \/_ // | \ \ \/\ \
|
|
0/0/| \/_ // | \ \ \ \
|
|
0/0/0/0/| \/// | \ \ | |
|
|
0/0/0/0/0/_|_ / ( // | \ _\ | /
|
|
0/0/0/0/0/0/`/,_ _ _/ ) ; -. | _ _\.-~ / /
|
|
,-} _ *-.|.-~-. .~ ~
|
|
\ \__/ `/\ / ~-. _ .-~ /
|
|
\____(Oo) *. } { /
|
|
( (--) .----~-.\ \-` .~
|
|
//__\\\\ \ DENIED! ///.----..< \ _ -~
|
|
// \\\\ ///-._ _ _ _ _ _ _{^ - - - - ~
|
|
|
|
EOTXT
|
|
);
|
|
}
|
|
|
|
$console->writeErr("%s\n\n", $preamble);
|
|
$console->writeErr("%s\n\n", $ex->getMessage());
|
|
$err = 1;
|
|
}
|
|
|
|
exit($err);
|