Skip to content

Instantly share code, notes, and snippets.

@dprevite
Last active August 29, 2015 14:17
Show Gist options
  • Save dprevite/5a5aabb9b7708b0a9548 to your computer and use it in GitHub Desktop.
Save dprevite/5a5aabb9b7708b0a9548 to your computer and use it in GitHub Desktop.
Search computer for all git repos and add lolcommits hook to all the post-commit files
<?php
/**
* Search forward starting from end minus needle length characters
*
* @param string $haystack The string to search within
* @param string $needle The string to search for
*
* @return boolean
**/
function endsWith($haystack, $needle) {
return $needle === '' || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== FALSE);
}
$contents = '#!/bin/sh' . PHP_EOL . '/Users/dprevite/Dropbox/lolcommits/post-commit' . PHP_EOL;
// Get the home directory
$homeDir = `echo ~`;
$homeDir = trim($homeDir);
// Find all the existing post commit files
echo 'Finding all of the git repositories in your home directory, ' . $homeDir . PHP_EOL;
$matches = `locate .git/hooks`;
$matches = explode(PHP_EOL, trim($matches));
$directories = [];
// Loop through each match and get an array of hook directories
foreach ($matches as $directory) {
$directory = dirname($directory);
if (!endsWith($directory, '/hooks')) {
$directory .= '/hooks';
}
$directories[] = $directory;
}
// We only want 1 entry per directory
$directories = array_unique($directories);
// Loop through the directories creating hooks
foreach ($directories as $directory) {
// We don't want to mess up composer directories
if (strpos($directory, '/vendor/') !== FALSE) {
echo 'Skipping ' . $directory . ' because it is a vendor directory.' . PHP_EOL;
continue;
}
// Weird thing for coderobot.io storage dirs
if (strpos($directory, '/storage/') !== FALSE) {
echo 'Skipping ' . $directory . ' because it is a storage directory.' . PHP_EOL;
continue;
}
// Only update stuff in the users home directory
if (strpos($directory, $homeDir) === FALSE) {
echo 'Skipping ' . $directory . ' because it isn\'t in the users home directory.' . PHP_EOL;
continue;
}
echo 'Checking for hook in ' . $directory . '...' . PHP_EOL;
if (!file_exists($directory . '/post-commit')) {
echo 'No post commit file, creating' . PHP_EOL;
touch($directory . '/post-commit');
chmod($directory . '/post-commit', 0777);
}
echo 'Putting hooks into ' . $directory . '/post-commit' . PHP_EOL;
file_put_contents($directory . '/post-commit', $contents);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment