Skip to content

Instantly share code, notes, and snippets.

@cgmartin
Forked from domenic/README.md
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save cgmartin/fe9eaeadd023a52b37c5 to your computer and use it in GitHub Desktop.

Select an option

Save cgmartin/fe9eaeadd023a52b37c5 to your computer and use it in GitHub Desktop.

Revisions

  1. cgmartin revised this gist Jun 7, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions git-hooks∕pre-commit
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,7 @@ npm.load(function (err) {
    npm.commands.test(function(err) {
    if (err) failed('test', err);

    // Chain additional commands...
    //npm.commands['run-script'](['foo'], function(err) {
    // if (err) failed('foo', err);
    //});
  2. cgmartin revised this gist Jun 7, 2015. 3 changed files with 5 additions and 6 deletions.
    11 changes: 5 additions & 6 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,13 @@
    Here's how this works:

    * Include a `git_hooks/` directory in your project, with these two files (plus other hooks if you want, written in a similar style).
    * Add `"npm"` to your `devDependencies` in `package.json`, so that the `pre-commit` hook can do its magic.
    * Add `test` and `lint` scripts to your `package.json`, e.g.
    * Include a `git-hooks/` directory in your project, with these two files (plus other hooks if you want, written in a similar style).
    * Ensure the files under the `git-hooks/` directory are executable: `chmod 755 git-hooks/*`
    * Add `test` script to your `package.json`, e.g.

    ```json
    "scripts": {
    "test": "mocha",
    "lint": "jshint ./lib --show-non-errors"
    "test": "mocha"
    }
    ```

    * Whenever you clone the repository, run `node git_hooks/link.js` to symlink the hooks stored in the repository, in `git_hooks`, to the hidden `.git/hooks` directory.
    * Whenever you clone the repository, run `git-hooks/install-hooks` to symlink the hooks stored in the repository, in `git-hooks`, to the hidden `.git/hooks` directory.
    File renamed without changes.
    File renamed without changes.
  3. cgmartin revised this gist Jun 7, 2015. 2 changed files with 13 additions and 15 deletions.
    1 change: 0 additions & 1 deletion git_hooks∕link.js → git_hooks∕install-hooks
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    #!/usr/bin/env node

    var fs = require("fs");
    var path = require("path");
    var gitHookDir = path.resolve(__dirname, '..', '.git', 'hooks');
    27 changes: 13 additions & 14 deletions git_hooks∕pre-commit
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,20 @@
    #!/usr/bin/env node

    var npm = require("npm");
    var npm = require('npm');

    npm.load(function (err) {
    if (err) {
    throw err;
    }
    if (err) throw err;

    npm.commands.test(function (err) {
    if (err) {
    process.exit(1);
    }
    // run `npm test`
    npm.commands.test(function(err) {
    if (err) failed('test', err);

    npm.commands["run-script"](["lint"], function (err) {
    if (err) {
    process.exit(1);
    }
    });
    //npm.commands['run-script'](['foo'], function(err) {
    // if (err) failed('foo', err);
    //});
    });
    });

    function failed(msg, err) {
    console.error('\npre-commit failure:', msg, err);
    process.exit(1);
    }
  4. cgmartin revised this gist Jun 7, 2015. 1 changed file with 9 additions and 6 deletions.
    15 changes: 9 additions & 6 deletions git_hooks∕link.js
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,20 @@
    #!/usr/bin/env node

    var fs = require("fs");
    var path = require("path");
    var gitHookDir = path.resolve(__dirname, '..', '.git', 'hooks');

    ["applypatch-msg", "commit-msg", "post-commit", "post-receive", "post-update", "pre-applypatch", "pre-commit",
    "prepare-commit-msg", "pre-rebase", "update"].forEach(function (hook) {
    var hookInSourceControl = path.resolve(__dirname, hook);

    if (path.existsSync(hookInSourceControl)) {
    var hookInHiddenDirectory = path.resolve(__dirname, "..", ".git", "hooks", hook);
    var hookInSourceControl = path.resolve(__dirname, hook);
    if (fs.existsSync(hookInSourceControl)) {
    var hookInHiddenDirectory = path.resolve(gitHookDir, hook);

    if (path.existsSync(hookInHiddenDirectory)) {
    if (fs.existsSync(hookInHiddenDirectory)) {
    fs.unlinkSync(hookInHiddenDirectory);
    }

    fs.linkSync(hookInSourceControl, hookInHiddenDirectory);
    fs.symlinkSync(path.relative(gitHookDir, hookInSourceControl), hookInHiddenDirectory);
    }
    });
    });
  5. @domenic domenic revised this gist Mar 29, 2012. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions git_hooks∕pre-commit
    Original file line number Diff line number Diff line change
    @@ -11,10 +11,11 @@ npm.load(function (err) {
    if (err) {
    process.exit(1);
    }
    });
    npm.commands["run-script"](["lint"], function (err) {
    if (err) {
    process.exit(1);
    }

    npm.commands["run-script"](["lint"], function (err) {
    if (err) {
    process.exit(1);
    }
    });
    });
    });
  6. @domenic domenic revised this gist Mar 29, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions git_hooks∕link.js
    Original file line number Diff line number Diff line change
    @@ -3,10 +3,10 @@ var path = require("path");

    ["applypatch-msg", "commit-msg", "post-commit", "post-receive", "post-update", "pre-applypatch", "pre-commit",
    "prepare-commit-msg", "pre-rebase", "update"].forEach(function (hook) {
    var hookInSourceControl = path.resolve(__dirname, "pre-commit");
    var hookInSourceControl = path.resolve(__dirname, hook);

    if (path.existsSync(hookInSourceControl)) {
    var hookInHiddenDirectory = path.resolve(__dirname, "..", ".git", "hooks", "pre-commit");
    var hookInHiddenDirectory = path.resolve(__dirname, "..", ".git", "hooks", hook);

    if (path.existsSync(hookInHiddenDirectory)) {
    fs.unlinkSync(hookInHiddenDirectory);
  7. @domenic domenic created this gist Mar 29, 2012.
    14 changes: 14 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    Here's how this works:

    * Include a `git_hooks/` directory in your project, with these two files (plus other hooks if you want, written in a similar style).
    * Add `"npm"` to your `devDependencies` in `package.json`, so that the `pre-commit` hook can do its magic.
    * Add `test` and `lint` scripts to your `package.json`, e.g.

    ```json
    "scripts": {
    "test": "mocha",
    "lint": "jshint ./lib --show-non-errors"
    }
    ```

    * Whenever you clone the repository, run `node git_hooks/link.js` to symlink the hooks stored in the repository, in `git_hooks`, to the hidden `.git/hooks` directory.
    17 changes: 17 additions & 0 deletions git_hooks∕link.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    var fs = require("fs");
    var path = require("path");

    ["applypatch-msg", "commit-msg", "post-commit", "post-receive", "post-update", "pre-applypatch", "pre-commit",
    "prepare-commit-msg", "pre-rebase", "update"].forEach(function (hook) {
    var hookInSourceControl = path.resolve(__dirname, "pre-commit");

    if (path.existsSync(hookInSourceControl)) {
    var hookInHiddenDirectory = path.resolve(__dirname, "..", ".git", "hooks", "pre-commit");

    if (path.existsSync(hookInHiddenDirectory)) {
    fs.unlinkSync(hookInHiddenDirectory);
    }

    fs.linkSync(hookInSourceControl, hookInHiddenDirectory);
    }
    });
    20 changes: 20 additions & 0 deletions git_hooks∕pre-commit
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    #!/usr/bin/env node

    var npm = require("npm");

    npm.load(function (err) {
    if (err) {
    throw err;
    }

    npm.commands.test(function (err) {
    if (err) {
    process.exit(1);
    }
    });
    npm.commands["run-script"](["lint"], function (err) {
    if (err) {
    process.exit(1);
    }
    });
    });