Add 'hook.d/' directories to SVN and Git repositories for custom hooks
Summary:
Fixes T4189. Ref T4151. Allows repositories to have additional custom hooks for operations which can't be expressed with Herald (one such operation is lint).
This adds only local hook directories, since they're easier to use with existing hooks than global directories. I might add global directories eventually.
This doesn't support Mercurial since we have no demand for it and it's more complicated (we lose compatibility and power by just dropping a `hooks.d/` somewhere).
Test Plan:
- Pulled hosted SVN and Git repos to verify the hook directories generate correctly.
- Added a variety of hooks to the hook directories (echo + pass, fail).
- Pushed commits and verified the hooks fired (output expected info, or failed).
- Verified push log reflected the correct error code ("3", external) and detail ("nope.sh") when rejecting.
Reviewers: btrahan
Reviewed By: btrahan
CC: aran
Maniphest Tasks: T4151, T4189
Differential Revision: https://secure.phabricator.com/D7884
This commit is contained in:
@@ -19,6 +19,7 @@ final class DiffusionCommitHookEngine extends Phobject {
|
||||
private $viewer;
|
||||
private $repository;
|
||||
private $stdin;
|
||||
private $originalArgv;
|
||||
private $subversionTransaction;
|
||||
private $subversionRepository;
|
||||
private $remoteAddress;
|
||||
@@ -84,6 +85,15 @@ final class DiffusionCommitHookEngine extends Phobject {
|
||||
return $this->stdin;
|
||||
}
|
||||
|
||||
public function setOriginalArgv(array $original_argv) {
|
||||
$this->originalArgv = $original_argv;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getOriginalArgv() {
|
||||
return $this->originalArgv;
|
||||
}
|
||||
|
||||
public function setRepository(PhabricatorRepository $repository) {
|
||||
$this->repository = $repository;
|
||||
return $this;
|
||||
@@ -141,7 +151,8 @@ final class DiffusionCommitHookEngine extends Phobject {
|
||||
|
||||
$this->applyHeraldContentRules($content_updates, $all_updates);
|
||||
|
||||
// TODO: Fire external hooks.
|
||||
// Run custom scripts in `hook.d/` directories.
|
||||
$this->applyCustomHooks($all_updates);
|
||||
|
||||
// If we make it this far, we're accepting these changes. Mark all the
|
||||
// logs as accepted.
|
||||
@@ -551,6 +562,74 @@ final class DiffusionCommitHookEngine extends Phobject {
|
||||
return $content_updates;
|
||||
}
|
||||
|
||||
/* -( Custom )------------------------------------------------------------- */
|
||||
|
||||
private function applyCustomHooks(array $updates) {
|
||||
$args = $this->getOriginalArgv();
|
||||
$stdin = $this->getStdin();
|
||||
$console = PhutilConsole::getConsole();
|
||||
|
||||
$env = array(
|
||||
'PHABRICATOR_REPOSITORY' => $this->getRepository()->getCallsign(),
|
||||
self::ENV_USER => $this->getViewer()->getUsername(),
|
||||
self::ENV_REMOTE_PROTOCOL => $this->getRemoteProtocol(),
|
||||
self::ENV_REMOTE_ADDRESS => $this->getRemoteAddress(),
|
||||
);
|
||||
|
||||
$directories = $this->getRepository()->getHookDirectories();
|
||||
foreach ($directories as $directory) {
|
||||
$hooks = $this->getExecutablesInDirectory($directory);
|
||||
sort($hooks);
|
||||
foreach ($hooks as $hook) {
|
||||
// NOTE: We're explicitly running the hooks in sequential order to
|
||||
// make this more predictable.
|
||||
$future = id(new ExecFuture('%s %Ls', $hook, $args))
|
||||
->setEnv($env, $wipe_process_env = false)
|
||||
->write($stdin);
|
||||
|
||||
list($err, $stdout, $stderr) = $future->resolve();
|
||||
if (!$err) {
|
||||
// This hook ran OK, but echo its output in case there was something
|
||||
// informative.
|
||||
$console->writeOut("%s", $stdout);
|
||||
$console->writeErr("%s", $stderr);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Mark everything as rejected by this hook.
|
||||
foreach ($updates as $update) {
|
||||
$update->setRejectCode(
|
||||
PhabricatorRepositoryPushLog::REJECT_EXTERNAL);
|
||||
$update->setRejectDetails(basename($hook));
|
||||
}
|
||||
|
||||
throw new DiffusionCommitHookRejectException(
|
||||
pht(
|
||||
"This push was rejected by custom hook script '%s':\n\n%s%s",
|
||||
basename($hook),
|
||||
$stdout,
|
||||
$stderr));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function getExecutablesInDirectory($directory) {
|
||||
$executables = array();
|
||||
|
||||
if (!Filesystem::pathExists($directory)) {
|
||||
return $executables;
|
||||
}
|
||||
|
||||
foreach (Filesystem::listDirectory($directory) as $path) {
|
||||
$full_path = $directory.DIRECTORY_SEPARATOR.$path;
|
||||
if (is_executable($full_path)) {
|
||||
$executables[] = $full_path;
|
||||
}
|
||||
}
|
||||
|
||||
return $executables;
|
||||
}
|
||||
|
||||
|
||||
/* -( Mercurial )---------------------------------------------------------- */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user