Install pre-commit hooks in Git repositories
Summary: Ref T4189. T4189 describes most of the intent here: - When updating hosted repositories, sync a pre-commit hook into them instead of doing a `git fetch`. - The hook calls into Phabricator. The acting Phabricator user is sent via PHABRICATOR_USER in the environment. The active repository is sent via CLI. - The hook doesn't do anything useful yet; it just veifies basic parameters, does a little parsing, and exits 0 to allow the commit. Test Plan: - Performed Git pushes and pulls over SSH and HTTP. Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T4189 Differential Revision: https://secure.phabricator.com/D7682
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
final class DiffusionCommitHookEngine extends Phobject {
|
||||
|
||||
private $viewer;
|
||||
private $repository;
|
||||
private $stdin;
|
||||
|
||||
public function setStdin($stdin) {
|
||||
$this->stdin = $stdin;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getStdin() {
|
||||
return $this->stdin;
|
||||
}
|
||||
|
||||
public function setRepository(PhabricatorRepository $repository) {
|
||||
$this->repository = $repository;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getRepository() {
|
||||
return $this->repository;
|
||||
}
|
||||
|
||||
public function setViewer(PhabricatorUser $viewer) {
|
||||
$this->viewer = $viewer;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getViewer() {
|
||||
return $this->viewer;
|
||||
}
|
||||
|
||||
public function execute() {
|
||||
$type = $this->getRepository()->getVersionControlSystem();
|
||||
switch ($type) {
|
||||
case PhabricatorRepositoryType::REPOSITORY_TYPE_GIT:
|
||||
$err = $this->executeGitHook();
|
||||
break;
|
||||
default:
|
||||
throw new Exception(pht('Unsupported repository type "%s"!', $type));
|
||||
}
|
||||
|
||||
return $err;
|
||||
}
|
||||
|
||||
private function executeGitHook() {
|
||||
$updates = $this->parseGitUpdates($this->getStdin());
|
||||
|
||||
// TODO: Do useful things.
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private function parseGitUpdates($stdin) {
|
||||
$updates = array();
|
||||
|
||||
$lines = phutil_split_lines($stdin, $retain_endings = false);
|
||||
foreach ($lines as $line) {
|
||||
$parts = explode(' ', $line, 3);
|
||||
if (count($parts) != 3) {
|
||||
throw new Exception(pht('Expected "old new ref", got "%s".', $line));
|
||||
}
|
||||
$updates[] = array(
|
||||
'old' => $parts[0],
|
||||
'new' => $parts[1],
|
||||
'ref' => $parts[2],
|
||||
);
|
||||
}
|
||||
|
||||
return $updates;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user