Get Hooked

on Git Hooks

Fishing hooks
Omni Adams
http://wtfshiznit.blogspot.com/2011/04/wtf-hoff.html
http://edaltorfer.com/blog/wp-content/uploads/2011/08/WhatWouldYouSayYouDoHere.png
http://www.saalonmuyo.com/wp-content/uploads/2012/08/ghostbusters-gatekeeper.jpeg
http://images5.fanpop.com/image/photos/25200000/Dexter-plastic-sheets-dexter-25239677-1511-911.jpg
http://themoviedoc.tumblr.com/post/4990263114/destiny
Clone
Checkout
Post-checkout
Merge
Post-merge
Add
Commit
Post-commit
Pre-commit
Prepare-commit-message
Commit-msg
Push
Pull
Rebase
Pre-rebase
Work
Receive
Pre-receive
Post-receive
Update
Update
http://blog.swabr.com/wp-content/uploads/2012/10/no-email.jpg
Overview map
Clone
Checkout
Post-checkout
Merge
Post-merge
Add
Commit
Post-commit
Pre-commit
Prepare-commit-message
Commit-msg
Push
Pull
Rebase
Pre-rebase
Work
Receive
Pre-receive
Post-receive
Update
Update
http://lifeboat.com/ex/great.power.great.responsibility
Clone
Checkout
Post-checkout
Merge
Post-merge
Add
Commit
Post-commit
Pre-commit
Prepare-commit-message
Commit-msg
Push
Pull
Rebase
Pre-rebase
Work
Receive
Pre-receive
Post-receive
Update
Update
http://imgur.com/gallery/lxHqF
Clone
Checkout
Post-checkout
Merge
Post-merge
Add
Commit
Post-commit
Pre-commit
Prepare-commit-message
Commit-msg
Push
Pull
Rebase
Pre-rebase
Work
Receive
Pre-receive
Post-receive
Update
Update
http://chuva-inc.com/sites/default/files/blog_posts/356merge-conflicts.jpg
Clone
Checkout
Post-checkout
Merge
Post-merge
Add
Commit
Post-commit
Pre-commit
Prepare-commit-message
Commit-msg
Push
Pull
Rebase
Pre-rebase
Work
Receive
Pre-receive
Post-receive
Update
Update
http://thelottolife.com/storage/ruprecht.jpg?__SQUARESPACE_CACHEVERSION=1335481235961
Clone
Checkout
Post-checkout
Merge
Post-merge
Add
Commit
Post-commit
Pre-commit
Prepare-commit-message
Commit-msg
Push
Pull
Rebase
Pre-rebase
Work
Receive
Pre-receive
Post-receive
Update
Update
http://cheezburger.com/4404460544
Clone
Checkout
Post-checkout
Merge
Post-merge
Add
Commit
Post-commit
Pre-commit
Prepare-commit-message
Commit-msg
Push
Pull
Rebase
Pre-rebase
Work
Receive
Pre-receive
Post-receive
Update
Update
http://tomshone.blogspot.com/2010/04/horror-movies-in-broad-daylight.html
Clone
Checkout
Post-checkout
Merge
Post-merge
Add
Commit
Post-commit
Pre-commit
Prepare-commit-message
Commit-msg
Push
Pull
Rebase
Pre-rebase
Work
Receive
Pre-receive
Post-receive
Update
Update
http://portlandfoodcoma.blogspot.com/2011_04_01_archive.html
Clone
Checkout
Post-checkout
Merge
Post-merge
Add
Commit
Post-commit
Pre-commit
Prepare-commit-message
Commit-msg
Push
Pull
Rebase
Pre-rebase
Work
Receive
Pre-receive
Post-receive
Update
Update
http://skiffleboom.wordpress.com/2011/12/14/walter-sobchak-motivational-poster-over-the-line-skiffleboom-com/
Clone
Checkout
Post-checkout
Merge
Post-merge
Add
Commit
Post-commit
Pre-commit
Prepare-commit-message
Commit-msg
Push
Pull
Rebase
Pre-rebase
Work
Receive
Pre-receive
Post-receive
Update
Update
http://static.guim.co.uk/sys-images/Guardian/Pix/pictures/2009/9/15/1253005192978/Patrick-Swayze-in-Road-Ho-026.jpg
Clone
Checkout
Post-checkout
Merge
Post-merge
Add
Commit
Post-commit
Pre-commit
Prepare-commit-message
Commit-msg
Push
Pull
Rebase
Pre-rebase
Work
Receive
Pre-receive
Post-receive
Update
Update
http://images.fanpop.com/images/image_uploads/Edward-in-Fight-Club-edward-norton-562432_1600_900.jpg
Clone
Checkout
Post-checkout
Merge
Post-merge
Add
Commit
Post-commit
Pre-commit
Prepare-commit-message
Commit-msg
Push
Pull
Rebase
Pre-rebase
Work
Receive
Pre-receive
Post-receive
Update
Update
http://rollinspsychtips.blogspot.com/2012/06/get-rid-of-mini-me-algorithm.html
now
that
you're
hooked
background
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error:
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error:
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To /Users/Omni/code/hooks
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '/Users/Omni/code/hooks'
http://knowyourmeme.com/photos/2626
    $ mkdir hooks-bare
    $ cd hooks-bare
    $ git init --bare
    Initialized empty Git repository in /Users/Omni/Code/hooks-bare/
    $ ls
    HEAD         description  info/        refs/
    config       hooks/       objects/
    $ mkdir hooks
    $ cd hooks
    $ git init
    Initialized empty Git repository in /Users/Omni/Code/hooks/.git/
    hooks(000) /$ ls
    hooks(000) /$ ls -A
    .git/
    hooks(000) /$ ls -A .git
    HEAD         description  info/        refs/
    config       hooks/       objects/
    hooks(000) /$ ls .git/hooks
    applypatch-msg.sample*     pre-commit.sample*
    commit-msg.sample*         pre-rebase.sample*
    post-update.sample*        prepare-commit-msg.sample*
    pre-applypatch.sample*     update.sample*
    
    hooks(000) /$ cat > .git/hooks/pre-commit
    echo 'You FAIL'
    exit 1^D
    hooks(000) /$ chmod +x .git/hooks/pre-commit
    hooks(000) /$ touch foo
    hooks(100) /$ git add foo
    hooks(100) /$ git status -s
    A  foo
    hooks(100) /$ git commit
    You FAIL
    hooks(100) /$ git status -s
    A  foo
http://commons.wikimedia.org/wiki/File:Navel_lint_ball.jpg
    #!/usr/bin/env php
    <?php
    echo 'You FAIL';
    exit(1);
    function getCurrentHash() {
        exec('git rev-parse --verify HEAD', $unused, $ret);

        if (0 !== $ret) {
            return '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
        }

        return 'HEAD';
    }
    function getChangedFiles($hash) {
        $cmd = 'git diff-index --cached --name-status ' . $hash . ' | '
            . 'grep -E "^(A|M)" | '
            . 'cut -f2';
        exec($cmd, $files);
        return $files;
    }
    function lintFiles($files) {
        $errors = array();
        foreach ($files as $file) {
            if (false === strpos($file, '.php'))
                continue;
            $file = escapeshellarg($file);
            exec('php -l ' . $file . ' 2>/dev/null', $output, $ret);
            if (!$ret)
                continue;
            $errors = array_merge($errors, $output);
        }
        return $errors;
    }
    echo 'Poking your belly for lint...', PHP_EOL;
    $hash = getCurrentHash();
    $files = getChangedFiles($hash);
    $errors = lintFiles($files);
    if (!empty($errors)) {
        echo 'Ugh, that\'s gross:', PHP_EOL . '    ',
            implode(PHP_EOL . '    ', $errors), PHP_EOL;
        exit(1);
    }
    echo 'Ahh... nice and clean.', PHP_EOL;
    exit(0);
    hooks(200) /$ git commit
    Poking your belly for lint...
    Ugh, that's gross:

        Parse error: syntax error, unexpected '<' in broken.php on line 3

        Errors parsing broken.php
    
    hooks(200) /$ git commit
    Poking your belly for lint...
    Ahh... nice and clean.
    
    function getCommitInfo() {
        $args = file_get_contents('php://stdin');
        $argv = explode(' ', $args);
        list($old, $new, $branch) = $argv;
        $branch = trim(str_replace('refs/heads/', null, $branch));
        return array($old, $new, $branch);
    }
    
    function getCommitInfo() {
        $args = file_get_contents('php://stdin');
        list($old, $new, $branch) = explode(' ', $args);
        $branch = trim(str_replace('refs/heads/', null, $branch));
        return array($old, $new, $branch);
    }
    
    function getChangedFiles($hashes) {
        $hash = escapeshellarg($hash);
        $files = shell_exec('git show --pretty="format:" --name-only '
            . $hashes);
        $files = explode(PHP_EOL, trim($files));
        $files = array_unique($files);
        foreach ($files as $key => $file) {
            if ('' == $file || false === strpos($file, '.php')) {
                unset($files[$key]);
            }
        }
        return $files;
    }
    function getTempDirectory() {
        $tmpfile = tempnam(sys_get_temp_dir(), '');
        unlink($tmpfile);
        mkdir($tmpfile);
        return $tmpfile;
    }
    function loadFilesFromGit($hash, $tmpDir, $files) {
        foreach ($files as $filename) {
            $id = shell_exec('git diff-index --cached ' . $hash . ' '
                . '| grep ' . $filename);
            if ('' == $id) continue;
            $id = explode(' ', $id);
            $id = $id[2];
            $file = shell_exec('git cat-file blob ' . $id);
            $filename = $tmpDir . DIRECTORY_SEPARATOR . $filename;
            if (!file_exists(dirname($filename)))
                mkdir(dirname($filename), 0777, true);
            file_put_contents($filename, $file);
        }
    }
    function lint($files, $tmpDir) {
        $errors = array();
        foreach ($files as $file) {
            $file = $tmpDir . DIRECTORY_SEPARATOR . $file;
            if (false === strpos($file, '.php') || !file_exists($file))
                continue;
            exec('php -l ' . $file . ' 2>/dev/null', $output, $ret);
            if (!$ret)
                continue;
            $errors = array_merge($errors, $output);
        }
        return $errors;
    }
    echo 'Poking your belly for lint...', PHP_EOL;
    list($old, $new, $branch) = getCommitInfo();
    $combinedHash = $old . '..' . $new;
    $tmpDir = getTempDirectory();
    $files = getChangedFiles($combinedHash);
    loadFilesFromGit($new, $tmpDir, $files);
    $errors = lint($files, $tmpDir);
    if (!empty($errors)) {
        echo 'Ugh, that\'s gross:', PHP_EOL . '    ',
            implode(PHP_EOL . '    ', $errors), PHP_EOL;
        exit(1);
    }
    echo 'Ahh... nice and clean.', PHP_EOL;
    exit(0);
http://lalaloveliza.blogspot.com/2010_08_01_archive.html
Omni Adams

/