Last active
August 29, 2015 14:06
-
-
Save parsingphase/512bb5d73127aea69cda to your computer and use it in GitHub Desktop.
Script to replace all the post-commit git hooks required by a sismo setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: parsingphase | |
* Date: 19/09/14 | |
* Time: 09:29 | |
*/ | |
namespace Sismo; | |
class Project | |
{ | |
protected $slug; | |
protected $repository; | |
public function __construct($slug) | |
{ | |
$this->slug = $slug; | |
} | |
public function setRepository($repository) | |
{ | |
$this->repository = $repository; | |
} | |
/** | |
* @param mixed $slug | |
* @return $this | |
*/ | |
public function setSlug($slug) | |
{ | |
$this->slug = $slug; | |
return $this; | |
} | |
public function __call($name, $arguments) | |
{ | |
// ignore everything else | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getRepository() | |
{ | |
return $this->repository; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getSlug() | |
{ | |
return $this->slug; | |
} | |
} | |
$uid = posix_getuid(); | |
$userInfo = posix_getpwuid($uid); | |
$homeDir = $userInfo['dir']; | |
$sismoConfigFile = $homeDir . '/.sismo/config.php'; | |
$projects = require($sismoConfigFile); | |
$hostname = gethostname(); | |
$sismoScript = '/path/to/sismo/sismo.php'; // FIXME this is the only non-self-configuring bit | |
foreach ($projects as $project) { | |
/** @var Project $project */ | |
print($project->getSlug() . ': ' . $project->getRepository() . "\n"); | |
$commitHook = rtrim($project->getRepository(), '/') . '/.git/hooks/post-commit'; | |
if (file_exists($commitHook)) { | |
print("\tpresent\n"); | |
} else { | |
print("\tmissing… "); | |
$ok = file_put_contents($commitHook, createCommitHook($project->getSlug(), $sismoScript, $hostname)); | |
if ($ok !== false) { | |
print("added\n"); | |
chmod($commitHook, 0755); | |
} else { | |
print("can't add\n"); | |
} | |
} | |
} | |
/** | |
* @param $slug | |
* @param $hostname | |
* @return string | |
*/ | |
function createCommitHook($slug, $sismoScript, $hostname = null) | |
{ | |
$template = []; | |
$template[] = '#!/bin/sh'; | |
if ($hostname) { | |
$template[] = "HOSTSPEC=$hostname"; | |
} | |
$template[] = "SLUG=$slug"; | |
$template[] = ($hostname ? 'ssh -f $HOSTSPEC ' : '') . | |
'php '.$sismoScript.' --quiet build $SLUG `git log -1 HEAD --pretty="%H"`'; | |
return join("\n", $template)."\n"; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Working with code in composer, for example, means that repos can come & go quite frequently, and their post-commit hooks can get removed, breaking automated testing. This script reads a .sismo/config file (only if it's very simple) and replaces the hooks.