2015-02-03 18:54:02 +01:00
|
|
|
#!/usr/bin/env php
|
2013-12-03 00:45:36 +01:00
|
|
|
<?php
|
|
|
|
|
2015-02-03 18:54:02 +01:00
|
|
|
// NOTE: This script will sometimes emit a warning like this on startup:
|
Prepare SSH connections for proxying
Summary:
Ref T7034.
In a cluster environment, when a user connects with a VCS request over SSH (like `git pull`), the receiving server may need to proxy it to a server which can actually satisfy the request.
In order to proxy the request, we need to know which repository the user is interested in accessing.
Split the SSH workflow into two steps:
# First, identify the repository.
# Then, execute the operation.
In the future, this will allow us to put a possible "proxy the whole thing somewhere else" step in the middle, mirroring the behavior of Conduit.
This is trivially easy in `git` and `hg`. Both identify the repository on the commmand line.
This is fiendishly complex in `svn`, for the same reasons that hosting SVN was hard in the first place. Specifically:
- The client doesn't tell us what it's after.
- To get it to tell us, we have to send it a server capabilities string //first//.
- We can't just start an `svnserve` process and read the repository out after a little while, because we may need to proxy the request once we figure out the repository.
- We can't consume the client protocol frame that tells us what the client wants, because when we start the real server request it won't know what the client is after if it never receives that frame.
- On the other hand, we must consume the second copy of the server protocol frame that would be sent to the client, or they'll get two "HELLO" messages and not know what to do.
The approach here is straightforward, but the implementation is not trivial. Roughly:
- Start `svnserve`, read the "hello" frame from it.
- Kill `svnserve`.
- Send the "hello" to the client.
- Wait for the client to send us "I want repository X".
- Save the message it sent us in the "peekBuffer".
- Return "this is a request for repository X", so we can proxy it.
Then, to continue the request:
- Start the real `svnserve`.
- Read the "hello" frame from it and throw it away.
- Write the data in the "peekBuffer" to it, as though we'd just received it from the client.
- State of the world is normal again, so we can continue.
Also fixed some other issues:
- SVN could choke if `repository.default-local-path` contained extra slashes.
- PHP might emit some complaints when executing the commit hook; silence those.
Test Plan: Pushed and pulled repositories in SVN, Mercurial and Git.
Reviewers: btrahan
Reviewed By: btrahan
Subscribers: epriestley
Maniphest Tasks: T7034
Differential Revision: https://secure.phabricator.com/D11541
2015-01-28 19:18:07 +01:00
|
|
|
//
|
|
|
|
// No entry for terminal type "unknown";
|
|
|
|
// using dumb terminal settings.
|
|
|
|
//
|
2015-02-03 18:54:02 +01:00
|
|
|
// This can be fixed by adding "TERM=dumb" to the shebang line, but doing so
|
|
|
|
// causes some systems to hang mysteriously. See T7119.
|
Prepare SSH connections for proxying
Summary:
Ref T7034.
In a cluster environment, when a user connects with a VCS request over SSH (like `git pull`), the receiving server may need to proxy it to a server which can actually satisfy the request.
In order to proxy the request, we need to know which repository the user is interested in accessing.
Split the SSH workflow into two steps:
# First, identify the repository.
# Then, execute the operation.
In the future, this will allow us to put a possible "proxy the whole thing somewhere else" step in the middle, mirroring the behavior of Conduit.
This is trivially easy in `git` and `hg`. Both identify the repository on the commmand line.
This is fiendishly complex in `svn`, for the same reasons that hosting SVN was hard in the first place. Specifically:
- The client doesn't tell us what it's after.
- To get it to tell us, we have to send it a server capabilities string //first//.
- We can't just start an `svnserve` process and read the repository out after a little while, because we may need to proxy the request once we figure out the repository.
- We can't consume the client protocol frame that tells us what the client wants, because when we start the real server request it won't know what the client is after if it never receives that frame.
- On the other hand, we must consume the second copy of the server protocol frame that would be sent to the client, or they'll get two "HELLO" messages and not know what to do.
The approach here is straightforward, but the implementation is not trivial. Roughly:
- Start `svnserve`, read the "hello" frame from it.
- Kill `svnserve`.
- Send the "hello" to the client.
- Wait for the client to send us "I want repository X".
- Save the message it sent us in the "peekBuffer".
- Return "this is a request for repository X", so we can proxy it.
Then, to continue the request:
- Start the real `svnserve`.
- Read the "hello" frame from it and throw it away.
- Write the data in the "peekBuffer" to it, as though we'd just received it from the client.
- State of the world is normal again, so we can continue.
Also fixed some other issues:
- SVN could choke if `repository.default-local-path` contained extra slashes.
- PHP might emit some complaints when executing the commit hook; silence those.
Test Plan: Pushed and pulled repositories in SVN, Mercurial and Git.
Reviewers: btrahan
Reviewed By: btrahan
Subscribers: epriestley
Maniphest Tasks: T7034
Differential Revision: https://secure.phabricator.com/D11541
2015-01-28 19:18:07 +01:00
|
|
|
|
2015-01-27 23:51:48 +01:00
|
|
|
// 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.
|
|
|
|
|
2018-11-06 23:51:08 +01:00
|
|
|
$hook_start = microtime(true);
|
|
|
|
|
2015-01-27 23:51:48 +01:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-03 00:45:36 +01:00
|
|
|
$root = dirname(dirname(dirname(__FILE__)));
|
|
|
|
require_once $root.'/scripts/__init_script__.php';
|
|
|
|
|
|
|
|
if ($argc < 2) {
|
2016-02-18 01:45:22 +01:00
|
|
|
throw new Exception(pht('usage: commit-hook <repository>'));
|
2013-12-03 00:45:36 +01:00
|
|
|
}
|
|
|
|
|
2018-11-06 23:51:08 +01:00
|
|
|
$engine = id(new DiffusionCommitHookEngine())
|
|
|
|
->setStartTime($hook_start);
|
2013-12-03 00:45:55 +01:00
|
|
|
|
2013-12-03 00:45:36 +01:00
|
|
|
$repository = id(new PhabricatorRepositoryQuery())
|
2013-12-03 00:45:55 +01:00
|
|
|
->setViewer(PhabricatorUser::getOmnipotentUser())
|
2016-02-18 01:45:22 +01:00
|
|
|
->withIdentifiers(array($argv[1]))
|
2014-01-03 21:24:28 +01:00
|
|
|
->needProjectPHIDs(true)
|
2013-12-03 00:45:36 +01:00
|
|
|
->executeOne();
|
|
|
|
|
|
|
|
if (!$repository) {
|
2014-01-09 19:35:31 +01:00
|
|
|
throw new Exception(pht('No such repository "%s"!', $argv[1]));
|
2013-12-03 00:45:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$repository->isHosted()) {
|
2017-07-31 22:21:58 +02:00
|
|
|
// In Mercurial, the "pretxnchangegroup" hook fires for both pulls and
|
|
|
|
// pushes. Normally we only install the hook for hosted repositories, but
|
|
|
|
// if a hosted repository is later converted into an observed repository we
|
|
|
|
// can end up with an observed repository that has the hook installed.
|
|
|
|
// If we're running hooks from an observed repository, just exit without
|
|
|
|
// taking action. For more discussion, see PHI24.
|
|
|
|
return 0;
|
2013-12-03 00:45:36 +01:00
|
|
|
}
|
|
|
|
|
2013-12-03 00:45:55 +01:00
|
|
|
$engine->setRepository($repository);
|
|
|
|
|
2016-06-24 17:48:06 +02:00
|
|
|
$args = new PhutilArgumentParser($argv);
|
|
|
|
$args->parsePartial(
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'name' => 'hook-mode',
|
|
|
|
'param' => 'mode',
|
|
|
|
'help' => pht('Hook execution mode.'),
|
|
|
|
),
|
|
|
|
));
|
|
|
|
|
|
|
|
$argv = array_merge(
|
|
|
|
array($argv[0]),
|
|
|
|
$args->getUnconsumedArgumentVector());
|
2013-12-03 00:45:55 +01:00
|
|
|
|
|
|
|
// Figure out which user is writing the commit.
|
2016-06-24 17:48:06 +02:00
|
|
|
$hook_mode = $args->getArg('hook-mode');
|
|
|
|
if ($hook_mode !== null) {
|
|
|
|
$known_modes = array(
|
|
|
|
'svn-revprop' => true,
|
|
|
|
);
|
2013-12-03 00:45:55 +01:00
|
|
|
|
2016-06-24 17:48:06 +02:00
|
|
|
if (empty($known_modes[$hook_mode])) {
|
|
|
|
throw new Exception(
|
|
|
|
pht(
|
|
|
|
'Invalid Hook Mode: This hook was invoked in "%s" mode, but this '.
|
|
|
|
'is not a recognized hook mode. Valid modes are: %s.',
|
|
|
|
$hook_mode,
|
|
|
|
implode(', ', array_keys($known_modes))));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$is_svnrevprop = ($hook_mode == 'svn-revprop');
|
|
|
|
|
|
|
|
if ($is_svnrevprop) {
|
|
|
|
// For now, we let these through if the repository allows dangerous changes
|
|
|
|
// and prevent them if it doesn't. See T11208 for discussion.
|
|
|
|
|
|
|
|
$revprop_key = $argv[5];
|
|
|
|
|
|
|
|
if ($repository->shouldAllowDangerousChanges()) {
|
|
|
|
$err = 0;
|
|
|
|
} else {
|
|
|
|
$err = 1;
|
|
|
|
|
|
|
|
$console = PhutilConsole::getConsole();
|
|
|
|
$console->writeErr(
|
|
|
|
pht(
|
|
|
|
"DANGEROUS CHANGE: Dangerous change protection is enabled for this ".
|
|
|
|
"repository, so you can not change revision properties (you are ".
|
|
|
|
"attempting to edit \"%s\").\n".
|
|
|
|
"Edit the repository configuration before making dangerous changes.",
|
|
|
|
$revprop_key));
|
|
|
|
}
|
|
|
|
|
|
|
|
exit($err);
|
|
|
|
} else if ($repository->isGit() || $repository->isHg()) {
|
2013-12-05 20:59:22 +01:00
|
|
|
$username = getenv(DiffusionCommitHookEngine::ENV_USER);
|
2023-04-14 22:08:15 +02:00
|
|
|
if ($username !== false) {
|
|
|
|
if (!phutil_nonempty_string($username)) {
|
|
|
|
throw new Exception(
|
|
|
|
pht(
|
|
|
|
'No Direct Pushes: You are pushing directly to a hosted repository. '.
|
|
|
|
'This will not work. See "No Direct Pushes" in the documentation '.
|
|
|
|
'for more information.'));
|
|
|
|
}
|
2013-12-03 00:45:55 +01:00
|
|
|
}
|
2013-12-03 00:46:03 +01:00
|
|
|
|
2013-12-17 17:34:30 +01:00
|
|
|
if ($repository->isHg()) {
|
|
|
|
// We respond to several different hooks in Mercurial.
|
|
|
|
$engine->setMercurialHook($argv[2]);
|
|
|
|
}
|
2013-12-03 00:46:03 +01:00
|
|
|
|
2013-12-03 00:45:55 +01:00
|
|
|
} else if ($repository->isSVN()) {
|
|
|
|
// NOTE: In Subversion, the entire environment gets wiped so we can't read
|
2013-12-05 20:59:22 +01:00
|
|
|
// DiffusionCommitHookEngine::ENV_USER. Instead, we've set "--tunnel-user" to
|
|
|
|
// specify the correct user; read this user out of the commit log.
|
2013-12-03 00:45:55 +01:00
|
|
|
|
|
|
|
if ($argc < 4) {
|
2016-02-18 01:45:22 +01:00
|
|
|
throw new Exception(pht('usage: commit-hook <repository> <repo> <txn>'));
|
2013-12-03 00:45:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$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 {
|
2013-12-03 00:46:03 +01:00
|
|
|
throw new Exception(pht('Unknown repository type.'));
|
2013-12-03 00:45:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$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.
|
|
|
|
|
2013-12-03 00:46:03 +01:00
|
|
|
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!'));
|
|
|
|
}
|
2013-12-03 00:45:36 +01:00
|
|
|
}
|
|
|
|
|
2013-12-03 00:45:55 +01:00
|
|
|
$engine->setStdin($stdin);
|
2014-01-03 21:26:10 +01:00
|
|
|
$engine->setOriginalArgv(array_slice($argv, 2));
|
2013-12-03 00:45:36 +01:00
|
|
|
|
2013-12-05 20:59:22 +01:00
|
|
|
$remote_address = getenv(DiffusionCommitHookEngine::ENV_REMOTE_ADDRESS);
|
2023-04-14 22:08:15 +02:00
|
|
|
if ($remote_address !== false) {
|
|
|
|
if (phutil_nonempty_string($remote_address)) {
|
|
|
|
$engine->setRemoteAddress($remote_address);
|
|
|
|
}
|
2013-12-05 20:59:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$remote_protocol = getenv(DiffusionCommitHookEngine::ENV_REMOTE_PROTOCOL);
|
2023-04-14 22:08:15 +02:00
|
|
|
if ($remote_protocol !== false) {
|
|
|
|
if (phutil_nonempty_string($remote_protocol)) {
|
|
|
|
$engine->setRemoteProtocol($remote_protocol);
|
|
|
|
}
|
2013-12-05 20:59:22 +01:00
|
|
|
}
|
|
|
|
|
2018-03-22 20:42:08 +01:00
|
|
|
$request_identifier = getenv(DiffusionCommitHookEngine::ENV_REQUEST);
|
2023-04-14 22:08:15 +02:00
|
|
|
if ($request_identifier !== false) {
|
|
|
|
if (phutil_nonempty_string($request_identifier)) {
|
|
|
|
$engine->setRequestIdentifier($request_identifier);
|
|
|
|
}
|
2018-03-22 20:42:08 +01:00
|
|
|
}
|
|
|
|
|
2013-12-03 19:28:39 +01:00
|
|
|
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 * * * |
|
|
|
|
+---------------------------------------------------------------+
|
2018-11-06 13:52:59 +01:00
|
|
|
\
|
|
|
|
\ ^ /^
|
|
|
|
\ / \ // \
|
|
|
|
\ |\___/| / \// .\
|
|
|
|
\ /V V \__ / // | \ \ *----*
|
|
|
|
/ / \/_/ // | \ \ \ |
|
|
|
|
@___@` \/_ // | \ \ \/\ \
|
|
|
|
0/0/| \/_ // | \ \ \ \
|
|
|
|
0/0/0/0/| \/// | \ \ | |
|
|
|
|
0/0/0/0/0/_|_ / ( // | \ _\ | /
|
|
|
|
0/0/0/0/0/0/`/,_ _ _/ ) ; -. | _ _\.-~ / /
|
|
|
|
,-} _ *-.|.-~-. .~ ~
|
|
|
|
* \__/ `/\ / ~-. _ .-~ /
|
|
|
|
\____(Oo) *. } { /
|
|
|
|
( (..) .----~-.\ \-` .~
|
|
|
|
//___\\\\ \ DENIED! ///.----..< \ _ -~
|
|
|
|
// \\\\ ///-._ _ _ _ _ _ _{^ - - - - ~
|
2013-12-03 19:28:39 +01:00
|
|
|
|
|
|
|
EOTXT
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$console->writeErr("%s\n\n", $preamble);
|
|
|
|
$console->writeErr("%s\n\n", $ex->getMessage());
|
|
|
|
$err = 1;
|
|
|
|
}
|
2013-12-03 00:45:36 +01:00
|
|
|
|
|
|
|
exit($err);
|