2011-01-10 00:22:25 +01:00
|
|
|
<?php
|
|
|
|
|
2011-02-19 20:36:08 +01:00
|
|
|
/**
|
|
|
|
* Installable as a git pre-receive hook.
|
|
|
|
*
|
|
|
|
* @group workflow
|
|
|
|
*/
|
2012-01-31 21:07:05 +01:00
|
|
|
final class ArcanistGitHookPreReceiveWorkflow extends ArcanistBaseWorkflow {
|
2011-01-10 00:22:25 +01:00
|
|
|
|
Make Arcanist workflow names explicit
Summary:
Currently, adding a new workflow requires you to override ArcanistConfiguration, which is messy. Instead, just load everything that extends ArcanistBaseWorkflow.
Remove all the rules tying workflow names to class names through arcane incantations.
This has a very small performance cost in that we need to load every Workflow class every time now, but we don't hit __init__ and such anymore and it was pretty negligible on my machine (98ms vs 104ms or something).
Test Plan: Ran "arc help", "arc which", "arc diff", etc.
Reviewers: edward, vrana, btrahan
Reviewed By: edward
CC: aran, zeeg
Differential Revision: https://secure.phabricator.com/D3691
2012-10-17 17:35:03 +02:00
|
|
|
public function getWorkflowName() {
|
|
|
|
return 'git-hook-pre-receive';
|
|
|
|
}
|
|
|
|
|
2012-03-05 19:02:37 +01:00
|
|
|
public function getCommandSynopses() {
|
2011-01-10 00:22:25 +01:00
|
|
|
return phutil_console_format(<<<EOTEXT
|
|
|
|
**git-hook-pre-receive**
|
2012-03-05 19:02:37 +01:00
|
|
|
EOTEXT
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCommandHelp() {
|
|
|
|
return phutil_console_format(<<<EOTEXT
|
2011-01-10 00:22:25 +01:00
|
|
|
Supports: git
|
2011-01-12 10:49:48 +01:00
|
|
|
You can install this as a git pre-receive hook.
|
2011-01-10 00:22:25 +01:00
|
|
|
EOTEXT
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getArguments() {
|
|
|
|
return array(
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requiresConduit() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requiresWorkingCopy() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-15 05:00:11 +01:00
|
|
|
public function shouldShellComplete() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-10 00:22:25 +01:00
|
|
|
public function run() {
|
|
|
|
$working_copy = $this->getWorkingCopy();
|
|
|
|
if (!$working_copy->getProjectID()) {
|
|
|
|
throw new ArcanistUsageException(
|
|
|
|
"You have installed a git pre-receive hook in a remote without an ".
|
|
|
|
".arcconfig.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Git repositories have special rules in pre-receive hooks. We need to
|
|
|
|
// construct the API against the .git directory instead of the project
|
|
|
|
// root or commands don't work properly.
|
|
|
|
$repository_api = ArcanistGitAPI::newHookAPI($_SERVER['PWD']);
|
|
|
|
|
|
|
|
$root = $working_copy->getProjectRoot();
|
|
|
|
|
|
|
|
$parser = new ArcanistDiffParser();
|
|
|
|
|
|
|
|
$mark_revisions = array();
|
|
|
|
|
|
|
|
$stdin = file_get_contents('php://stdin');
|
|
|
|
$commits = array_filter(explode("\n", $stdin));
|
|
|
|
foreach ($commits as $commit) {
|
|
|
|
list($old_ref, $new_ref, $refname) = explode(' ', $commit);
|
|
|
|
|
|
|
|
list($log) = execx(
|
|
|
|
'(cd %s && git log -n1 %s)',
|
|
|
|
$repository_api->getPath(),
|
|
|
|
$new_ref);
|
|
|
|
$message_log = reset($parser->parseDiff($log));
|
|
|
|
$message = ArcanistDifferentialCommitMessage::newFromRawCorpus(
|
|
|
|
$message_log->getMetadata('message'));
|
|
|
|
|
|
|
|
$revision_id = $message->getRevisionID();
|
|
|
|
if ($revision_id) {
|
|
|
|
$mark_revisions[] = $revision_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Do commit message junk.
|
|
|
|
|
|
|
|
$info = $repository_api->getPreReceiveHookStatus($old_ref, $new_ref);
|
|
|
|
$paths = ipull($info, 'mask');
|
|
|
|
$frefs = ipull($info, 'ref');
|
|
|
|
$data = array();
|
|
|
|
foreach ($paths as $path => $mask) {
|
|
|
|
list($stdout) = execx(
|
|
|
|
'(cd %s && git cat-file blob %s)',
|
|
|
|
$repository_api->getPath(),
|
|
|
|
$frefs[$path]);
|
|
|
|
$data[$path] = $stdout;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Do commit content junk.
|
|
|
|
|
|
|
|
$commit_name = $new_ref;
|
|
|
|
if ($revision_id) {
|
|
|
|
$commit_name = 'D'.$revision_id.' ('.$commit_name.')';
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "[arc pre-receive] {$commit_name} OK...\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$conduit = $this->getConduit();
|
|
|
|
|
|
|
|
$futures = array();
|
|
|
|
foreach ($mark_revisions as $revision_id) {
|
|
|
|
$futures[] = $conduit->callMethod(
|
2012-05-07 17:16:14 +02:00
|
|
|
'differential.close',
|
2011-01-10 00:22:25 +01:00
|
|
|
array(
|
2012-05-07 17:16:14 +02:00
|
|
|
'revisionID' => $revision_id,
|
2011-01-10 00:22:25 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
Futures($futures)->resolveAll();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|