Skip to content

Instantly share code, notes, and snippets.

@dprevite
Last active August 29, 2015 14:17

Revisions

  1. dprevite revised this gist Apr 16, 2015. 1 changed file with 30 additions and 5 deletions.
    35 changes: 30 additions & 5 deletions update_post_commits.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,17 @@
    <?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
    @@ -7,14 +20,20 @@

    // 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 = `locate .git/hooks`;
    $matches = explode(PHP_EOL, trim($matches));

    $directories = [];

    // Loop through each match and get an array of directories
    // Loop through each match and get an array of hook directories
    foreach ($matches as $directory) {
    $directories[] = dirname($directory);
    $directory = dirname($directory);

    if (!endsWith($directory, '/hooks')) {
    $directory .= '/hooks';
    }

    $directories[] = $directory;
    }

    // We only want 1 entry per directory
    @@ -24,16 +43,22 @@
    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 PHP_EOL . PHP_EOL . PHP_EOL;

    echo 'Checking for hook in ' . $directory . '...' . PHP_EOL;
    if (!file_exists($directory . '/post-commit')) {
    echo 'No post commit file, creating' . PHP_EOL;
  2. dprevite revised this gist Mar 29, 2015. 1 changed file with 38 additions and 10 deletions.
    48 changes: 38 additions & 10 deletions update_post_commits.php
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,46 @@
    <?php
    $contents = '#!/bin/sh' . PHP_EOL . './Users/dprevite/Dropbox/lolcommits/post-commit' . PHP_EOL;
    $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
    $gitPostCommitFiles = `locate post-commit`;
    $gitPostCommitFiles = explode(PHP_EOL, trim($gitPostCommitFiles));
    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 directories
    foreach ($matches as $directory) {
    $directories[] = dirname($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) {
    continue;
    }

    // Only update stuff in the users home directory
    if (strpos($directory, $homeDir) === FALSE) {
    continue;
    }

    echo PHP_EOL . PHP_EOL . PHP_EOL;

    // Loop through each match
    foreach ($gitPostCommitFiles as $file) {
    if (!file_exists(dirname($file) . '/post-commit')) {
    echo 'Checking for hook in ' . $directory . '...' . PHP_EOL;
    if (!file_exists($directory . '/post-commit')) {
    echo 'No post commit file, creating' . PHP_EOL;
    touch(dirname($file) . '/post-commit');
    chmod(dirname($file) . '/post-commit', 0777);
    touch($directory . '/post-commit');
    chmod($directory . '/post-commit', 0777);
    }

    echo 'Putting hooks into ' . dirname($file) . '/post-commit' . PHP_EOL;
    file_put_contents(dirname($file) . '/post-commit', $contents);
    echo 'Putting hooks into ' . $directory . '/post-commit' . PHP_EOL;
    file_put_contents($directory . '/post-commit', $contents);
    }
  3. dprevite created this gist Mar 29, 2015.
    18 changes: 18 additions & 0 deletions update_post_commits.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    <?php
    $contents = '#!/bin/sh' . PHP_EOL . './Users/dprevite/Dropbox/lolcommits/post-commit' . PHP_EOL;

    // Find all the existing post commit files
    $gitPostCommitFiles = `locate post-commit`;
    $gitPostCommitFiles = explode(PHP_EOL, trim($gitPostCommitFiles));

    // Loop through each match
    foreach ($gitPostCommitFiles as $file) {
    if (!file_exists(dirname($file) . '/post-commit')) {
    echo 'No post commit file, creating' . PHP_EOL;
    touch(dirname($file) . '/post-commit');
    chmod(dirname($file) . '/post-commit', 0777);
    }

    echo 'Putting hooks into ' . dirname($file) . '/post-commit' . PHP_EOL;
    file_put_contents(dirname($file) . '/post-commit', $contents);
    }