Skip to content

Commit f93c698

Browse files
author
epriestley
committedDec 2, 2013
Support Mercurial pretxnchangegroup hooks
Summary: Ref T4189. Fixes T2066. Mercurial has a //lot// of hooks so I'm not 100% sure this is all we need to install (we may need separate hooks for tags/bookmarks) but it should cover most of what we're after at least. Test Plan: - `bin/repository pull`'d a Mercurial repo and got a hook install. - Pushed to a Mercurial repository over SSH and HTTP, with good/bad hooks. Saw hooks fire. Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T2066, T4189 Differential Revision: https://secure.phabricator.com/D7685
1 parent 017d6cc commit f93c698

File tree

5 files changed

+65
-9
lines changed

5 files changed

+65
-9
lines changed
 

‎scripts/repository/commit_hook.php

+17-5
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,16 @@
2929

3030
// Figure out which user is writing the commit.
3131

32-
if ($repository->isGit()) {
32+
if ($repository->isGit() || $repository->isHg()) {
3333
$username = getenv('PHABRICATOR_USER');
3434
if (!strlen($username)) {
3535
throw new Exception(pht('usage: PHABRICATOR_USER should be defined!'));
3636
}
37+
38+
// TODO: If this is a Mercurial repository, the hook we're responding to
39+
// is available in $argv[2]. It's unclear if we actually need this, or if
40+
// we can block all actions we care about with just pretxnchangegroup.
41+
3742
} else if ($repository->isSVN()) {
3843
// NOTE: In Subversion, the entire environment gets wiped so we can't read
3944
// PHABRICATOR_USER. Instead, we've set "--tunnel-user" to specify the
@@ -50,7 +55,7 @@
5055

5156
$engine->setSubversionTransactionInfo($svn_txn, $svn_repo);
5257
} else {
53-
throw new Exceptiont(pht('Unknown repository type.'));
58+
throw new Exception(pht('Unknown repository type.'));
5459
}
5560

5661
$user = id(new PhabricatorPeopleQuery())
@@ -67,9 +72,16 @@
6772

6873
// Read stdin for the hook engine.
6974

70-
$stdin = @file_get_contents('php://stdin');
71-
if ($stdin === false) {
72-
throw new Exception(pht('Failed to read stdin!'));
75+
if ($repository->isHg()) {
76+
// Mercurial leaves stdin open, so we can't just read it until EOF.
77+
$stdin = '';
78+
} else {
79+
// Git and Subversion write data into stdin and then close it. Read the
80+
// data.
81+
$stdin = @file_get_contents('php://stdin');
82+
if ($stdin === false) {
83+
throw new Exception(pht('Failed to read stdin!'));
84+
}
7385
}
7486

7587
$engine->setStdin($stdin);

‎src/applications/diffusion/controller/DiffusionServeController.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -406,15 +406,19 @@ private function authenticateHTTPRepositoryUser(
406406
return $user;
407407
}
408408

409-
private function serveMercurialRequest(PhabricatorRepository $repository) {
409+
private function serveMercurialRequest(
410+
PhabricatorRepository $repository,
411+
PhabricatorUser $viewer) {
410412
$request = $this->getRequest();
411413

412414
$bin = Filesystem::resolveBinary('hg');
413415
if (!$bin) {
414416
throw new Exception("Unable to find `hg` in PATH!");
415417
}
416418

417-
$env = array();
419+
$env = array(
420+
'PHABRICATOR_USER' => $viewer->getUsername(),
421+
);
418422
$input = PhabricatorStartup::getRawInput();
419423

420424
$cmd = $request->getStr('cmd');

‎src/applications/diffusion/engine/DiffusionCommitHookEngine.php

+10
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public function execute() {
5151
case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN:
5252
$err = $this->executeSubversionHook();
5353
break;
54+
case PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL:
55+
$err = $this->executeMercurialHook();
56+
break;
5457
default:
5558
throw new Exception(pht('Unsupported repository type "%s"!', $type));
5659
}
@@ -73,6 +76,13 @@ private function executeSubversionHook() {
7376
return 0;
7477
}
7578

79+
private function executeMercurialHook() {
80+
81+
// TODO: Here, too, useful things should be done.
82+
83+
return 0;
84+
}
85+
7686
private function parseGitUpdates($stdin) {
7787
$updates = array();
7888

‎src/applications/diffusion/ssh/DiffusionSSHMercurialServeWorkflow.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ protected function executeRepositoryOperations() {
4242
$command = csprintf('hg -R %s serve --stdio', $repository->getLocalPath());
4343
$command = PhabricatorDaemon::sudoCommandAsDaemonUser($command);
4444

45-
$future = new ExecFuture('%C', $command);
45+
$future = id(new ExecFuture('%C', $command))
46+
->setEnv($this->getEnvironment());
4647

4748
$io_channel = $this->getIOChannel();
4849
$protocol_channel = new DiffusionSSHMercurialWireClientProtocolChannel(

‎src/applications/repository/engine/PhabricatorRepositoryPullEngine.php

+30-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public function pullRepository() {
8787
$this->installGitHook();
8888
} else if ($is_svn) {
8989
$this->installSubversionHook();
90+
} else if ($is_hg) {
91+
$this->installMercurialHook();
9092
} else {
9193
$this->logPull(
9294
pht(
@@ -335,7 +337,7 @@ private function executeMercurialCreate() {
335337
$path);
336338
} else {
337339
$repository->execxRemoteCommand(
338-
'clone -- %s %s',
340+
'clone --noupdate -- %s %s',
339341
$repository->getRemoteURI(),
340342
$path);
341343
}
@@ -383,6 +385,33 @@ private function executeMercurialUpdate() {
383385
}
384386

385387

388+
/**
389+
* @task hg
390+
*/
391+
private function installMercurialHook() {
392+
$repository = $this->getRepository();
393+
$path = $repository->getLocalPath().'.hg/hgrc';
394+
395+
$root = dirname(phutil_get_library_root('phabricator'));
396+
$bin = $root.'/bin/commit-hook';
397+
398+
$data = array();
399+
$data[] = '[hooks]';
400+
$data[] = csprintf(
401+
'pretxnchangegroup.phabricator = %s %s %s',
402+
$bin,
403+
$repository->getCallsign(),
404+
'pretxnchangegroup');
405+
$data[] = null;
406+
407+
$data = implode("\n", $data);
408+
409+
$this->log('%s', pht('Installing commit hook config to "%s"...', $path));
410+
411+
Filesystem::writeFile($path, $data);
412+
}
413+
414+
386415
/* -( Pulling Subversion Working Copies )---------------------------------- */
387416

388417

0 commit comments

Comments
 (0)
Failed to load comments.