From ead948402e2f91cbb1851930602305135dc3091c Mon Sep 17 00:00:00 2001 From: Thomas Hallock Date: Sat, 23 Jul 2016 12:01:57 -0700 Subject: [PATCH 01/18] cross-spawn@4 (#77) --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index f166b47..8ac7b58 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,8 @@ "homepage": "https://github.com/observing/pre-commit", "license": "MIT", "dependencies": { - "cross-spawn": "2.0.x", + "cross-spawn": "^4.0.0", + "spawn-sync": "^1.0.15", "which": "1.2.x" }, "devDependencies": { From 7154dee93f2c38ef46f807f21cc9ce73848d7396 Mon Sep 17 00:00:00 2001 From: Luan Muniz Date: Mon, 25 Jul 2016 06:03:29 -0300 Subject: [PATCH 02/18] Update dep to remove warn about cross-spaw (#76) * Update dep to remove warn about cross-spaw * fix backwards compatibility From 3cf4cf781fe0e3fe362b408ed43590007803c340 Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Thu, 17 Nov 2016 09:25:17 -0800 Subject: [PATCH 03/18] chore(package): update dependencies (#85) https://greenkeeper.io/ --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 8ac7b58..0d0eac6 100644 --- a/package.json +++ b/package.json @@ -30,14 +30,14 @@ "homepage": "https://github.com/observing/pre-commit", "license": "MIT", "dependencies": { - "cross-spawn": "^4.0.0", + "cross-spawn": "^5.0.1", "spawn-sync": "^1.0.15", "which": "1.2.x" }, "devDependencies": { "assume": "1.4.x", "istanbul": "0.4.x", - "mocha": "2.4.x", + "mocha": "~3.1.2", "pre-commit": "git://github.com/observing/pre-commit.git" } } From 4167676bb1cb9b0addef81bf889da60951ba505d Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Sun, 27 Nov 2016 03:04:38 +0100 Subject: [PATCH 04/18] chore(package): update mocha to version 3.2.0 (#86) https://greenkeeper.io/ --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0d0eac6..1f61ee7 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "devDependencies": { "assume": "1.4.x", "istanbul": "0.4.x", - "mocha": "~3.1.2", + "mocha": "~3.2.0", "pre-commit": "git://github.com/observing/pre-commit.git" } } From a9c973224a299a03aef66b5fd1a3b5d566edfee6 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Sun, 27 Nov 2016 03:04:47 +0100 Subject: [PATCH 05/18] solves the windows issue (with symlinks) (#84) * solved the windows issue with symlinks Instead of symlinking to the hook in node-modules/pre-commit, this injects a new pre-commit hook (in .git/hooks) that takes care of launching the hook in node-modules/pre-commit. In addition it stashes the unstaged changes and applies them again after running the pre-commit hook, so that you only run your pre-commit scripts on the commit actual codebase. * removed stashing Stashing automatically is actually a bad idea, it's better to for the user to have control over doing it or not (manually then). --- install.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/install.js b/install.js index 2aa0dcb..b903bcc 100644 --- a/install.js +++ b/install.js @@ -5,7 +5,7 @@ // var fs = require('fs') , path = require('path') - , spawn = require('cross-spawn') + , os = require('os') , hook = path.join(__dirname, 'hook') , root = path.resolve(__dirname, '..', '..') , exists = fs.existsSync || path.existsSync; @@ -47,15 +47,28 @@ if (exists(precommit) && !fs.lstatSync(precommit).isSymbolicLink()) { try { fs.unlinkSync(precommit); } catch (e) {} +// Create generic precommit hook that launches this modules hook (as well +// as stashing - unstashing the unstaged changes) +// TODO: we could keep launching the old pre-commit scripts +var hookRelativeUnixPath = hook.replace(root, '.'); +if(os.platform() === 'win32') { + hookRelativeUnixPath = hookRelativeUnixPath.replace(/[\\\/]+/g, '/'); +} +var precommitContent = '#!/bin/bash' + os.EOL + + hookRelativeUnixPath + os.EOL + + 'RESULT=$?' + os.EOL + + '[ $RESULT -ne 0 ] && exit 1' + os.EOL + + 'exit 0' + os.EOL; + // // It could be that we do not have rights to this folder which could cause the // installation of this module to completely fail. We should just output the // error instead destroying the whole npm install process. // -try { fs.symlinkSync(path.relative(hooks, hook), precommit, 'file'); } +try { fs.writeFileSync(precommit, precommitContent); } catch (e) { console.error('pre-commit:'); - console.error('pre-commit: Failed to symlink the hook file in your .git/hooks folder because:'); + console.error('pre-commit: Failed to create the hook file in your .git/hooks folder because:'); console.error('pre-commit: '+ e.message); console.error('pre-commit: The hook was not installed.'); console.error('pre-commit:'); From 1689e3e87ca2c73dc75a260e2fba472a345b5d5b Mon Sep 17 00:00:00 2001 From: Arnout Kazemier Date: Fri, 9 Dec 2016 13:07:44 +0100 Subject: [PATCH 06/18] [dist] 1.2.0 - Better windows support --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1f61ee7..3ffa2c5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pre-commit", - "version": "1.1.3", + "version": "1.2.0", "description": "Automatically install pre-commit hooks for your npm modules.", "main": "index.js", "scripts": { From 3ecafe09d222c6f97b67fb2f1224f23e094a8d82 Mon Sep 17 00:00:00 2001 From: Arnout Kazemier Date: Mon, 12 Dec 2016 01:13:01 +0100 Subject: [PATCH 07/18] [fix] chmod 0777 the newly created file; Fixes #88 --- install.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/install.js b/install.js index b903bcc..3d2e69e 100644 --- a/install.js +++ b/install.js @@ -51,9 +51,11 @@ catch (e) {} // as stashing - unstashing the unstaged changes) // TODO: we could keep launching the old pre-commit scripts var hookRelativeUnixPath = hook.replace(root, '.'); + if(os.platform() === 'win32') { hookRelativeUnixPath = hookRelativeUnixPath.replace(/[\\\/]+/g, '/'); } + var precommitContent = '#!/bin/bash' + os.EOL + hookRelativeUnixPath + os.EOL + 'RESULT=$?' + os.EOL @@ -73,3 +75,11 @@ catch (e) { console.error('pre-commit: The hook was not installed.'); console.error('pre-commit:'); } + +try { fs.chmodSync(precommit, 777); } +catch (e) { + console.error('pre-commit:'); + console.error('pre-commit: chmod 0777 the pre-commit file in your .git/hooks folder because:'); + console.error('pre-commit: '+ e.message); + console.error('pre-commit:'); +} From 7d6feb19648e8d60cab50917b42f08f12d89482e Mon Sep 17 00:00:00 2001 From: Arnout Kazemier Date: Mon, 12 Dec 2016 01:17:56 +0100 Subject: [PATCH 08/18] [dist] 1.2.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3ffa2c5..e090ece 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pre-commit", - "version": "1.2.0", + "version": "1.2.1", "description": "Automatically install pre-commit hooks for your npm modules.", "main": "index.js", "scripts": { From 675560c16d5d5fe81f419c526b210dc56759e853 Mon Sep 17 00:00:00 2001 From: josegal Date: Tue, 13 Dec 2016 13:52:44 +0200 Subject: [PATCH 09/18] Use string instead of int 777 in fs.chmodSync so it gets correctly interpreted as octal (#90) --- install.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install.js b/install.js index 3d2e69e..e761ba9 100644 --- a/install.js +++ b/install.js @@ -76,7 +76,7 @@ catch (e) { console.error('pre-commit:'); } -try { fs.chmodSync(precommit, 777); } +try { fs.chmodSync(precommit, '777'); } catch (e) { console.error('pre-commit:'); console.error('pre-commit: chmod 0777 the pre-commit file in your .git/hooks folder because:'); From bf393adbf5de842b6286dc1d12d024fb2784d1f5 Mon Sep 17 00:00:00 2001 From: Arnout Kazemier Date: Tue, 13 Dec 2016 12:56:32 +0100 Subject: [PATCH 10/18] [dist] 1.2.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e090ece..28ba177 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pre-commit", - "version": "1.2.1", + "version": "1.2.2", "description": "Automatically install pre-commit hooks for your npm modules.", "main": "index.js", "scripts": { From f55b66b9c75b73a903f5b77c7c511a13e0c7edff Mon Sep 17 00:00:00 2001 From: "Brian R. Bondy" Date: Wed, 21 Dec 2016 09:43:12 -0500 Subject: [PATCH 11/18] Fix pre-commit template (#80) Without this, it actually looks for a file with quotes around it on macOS and probably other platforms too. The path is already escaped because it is part of an args array. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index a20646d..89f5a9d 100644 --- a/index.js +++ b/index.js @@ -198,7 +198,7 @@ Hook.prototype.initialize = function initialize() { // execute. // if (this.config.template) { - this.exec(this.git, ['config', 'commit.template', '"'+ this.config.template +'"']); + this.exec(this.git, ['config', 'commit.template', this.config.template]); } if (!this.config.run) return this.log(Hook.log.run, 0); From a7c824781bca0b17e846878e2a727fd20fedb80f Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Mon, 3 Apr 2017 19:46:26 +0200 Subject: [PATCH 12/18] chore(package): update assume to version 1.5.0 (#98) https://greenkeeper.io/ --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 28ba177..72cdaeb 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "which": "1.2.x" }, "devDependencies": { - "assume": "1.4.x", + "assume": "~1.5.0", "istanbul": "0.4.x", "mocha": "~3.2.0", "pre-commit": "git://github.com/observing/pre-commit.git" From 66b95fd83859406b38859280dda1569c214ac760 Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Mon, 24 Apr 2017 16:03:33 +0200 Subject: [PATCH 13/18] chore(package): update mocha to version 3.3.0 (#101) https://greenkeeper.io/ --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 72cdaeb..dc1ff70 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "devDependencies": { "assume": "~1.5.0", "istanbul": "0.4.x", - "mocha": "~3.2.0", + "mocha": "~3.3.0", "pre-commit": "git://github.com/observing/pre-commit.git" } } From 5eb7b9bf35e9d0322564ea2e28fc8c640590f3f1 Mon Sep 17 00:00:00 2001 From: Brendan Warkentin Date: Fri, 5 May 2017 03:44:33 -0600 Subject: [PATCH 14/18] Support for git repos as submodules (#96) --- install.js | 15 +++++++++++++++ uninstall.js | 21 ++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/install.js b/install.js index e761ba9..ba9d65b 100644 --- a/install.js +++ b/install.js @@ -20,6 +20,21 @@ var git = path.resolve(root, '.git') , hooks = path.resolve(git, 'hooks') , precommit = path.resolve(hooks, 'pre-commit'); +// +// Resolve git directory for submodules +// +if (exists(git) && fs.lstatSync(git).isFile()) { + var gitinfo = fs.readFileSync(git).toString() + , gitdirmatch = /gitdir: (.+)/.exec(gitinfo) + , gitdir = gitdirmatch.length == 2 ? gitdirmatch[1] : null; + + if (gitdir !== null) { + git = path.resolve(root, gitdir); + hooks = path.resolve(git, 'hooks'); + precommit = path.resolve(hooks, 'pre-commit'); + } +} + // // Bail out if we don't have an `.git` directory as the hooks will not get // triggered. If we do have directory create a hooks folder if it doesn't exist. diff --git a/uninstall.js b/uninstall.js index a0d2fd0..7c47903 100644 --- a/uninstall.js +++ b/uninstall.js @@ -3,7 +3,26 @@ var fs = require('fs') , path = require('path') , exists = fs.existsSync || path.existsSync - , precommit = path.resolve(__dirname, '../..', '.git', 'hooks', 'pre-commit'); + , root = path.resolve(__dirname, '..', '..') + , git = path.resolve(root, '.git'); + +// +// Resolve git directory for submodules +// +if (exists(git) && fs.lstatSync(git).isFile()) { + var gitinfo = fs.readFileSync(git).toString() + , gitdirmatch = /gitdir: (.+)/.exec(gitinfo) + , gitdir = gitdirmatch.length == 2 ? gitdirmatch[1] : null; + + if (gitdir !== null) { + git = path.resolve(root, gitdir); + } +} + +// +// Location of pre-commit hook, if it exists +// +var precommit = path.resolve(git, 'hooks', 'pre-commit'); // // Bail out if we don't have pre-commit file, it might be removed manually. From 77bf620de7f7e8cdfbc260524d86c49def4e4333 Mon Sep 17 00:00:00 2001 From: Alexander Sokolov Date: Fri, 30 Jun 2017 16:02:29 +0300 Subject: [PATCH 15/18] Add ability to find .git folder on upper levels (#100) --- install.js | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/install.js b/install.js index ba9d65b..77f090c 100644 --- a/install.js +++ b/install.js @@ -16,9 +16,31 @@ var fs = require('fs') // `pre-commit` file. The path needs to be absolute in order for the symlinking // to work correctly. // -var git = path.resolve(root, '.git') - , hooks = path.resolve(git, 'hooks') - , precommit = path.resolve(hooks, 'pre-commit'); + +var git = getGitFolderPath(root); + +// Function to recursively finding .git folder +function getGitFolderPath(currentPath) { + var git = path.resolve(currentPath, '.git') + + if (!exists(git) || !fs.lstatSync(git).isDirectory()) { + console.log('pre-commit:'); + console.log('pre-commit: Not found .git folder in', git); + + var newPath = path.resolve(currentPath, '..'); + + // Stop if we on top folder + if (currentPath === newPath) { + return null; + } + + return getGitFolderPath(newPath); + } + + console.log('pre-commit:'); + console.log('pre-commit: Found .git folder in', git); + return git; +} // // Resolve git directory for submodules @@ -39,7 +61,15 @@ if (exists(git) && fs.lstatSync(git).isFile()) { // Bail out if we don't have an `.git` directory as the hooks will not get // triggered. If we do have directory create a hooks folder if it doesn't exist. // -if (!exists(git) || !fs.lstatSync(git).isDirectory()) return; +if (!git) { + console.log('pre-commit:'); + console.log('pre-commit: Not found any .git folder for installing pre-commit hook'); + return; +} + +var hooks = path.resolve(git, 'hooks') + , precommit = path.resolve(hooks, 'pre-commit'); + if (!exists(hooks)) fs.mkdirSync(hooks); // From f25888ffc67d21beaa1572b53ab6704a67266b64 Mon Sep 17 00:00:00 2001 From: Martin Wijk Date: Fri, 30 Jun 2017 15:02:53 +0200 Subject: [PATCH 16/18] Change shebang to #!/usr/bin/env bash for better portability (#105) --- hook | 2 +- install.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/hook b/hook index 62436f3..28a7f38 100755 --- a/hook +++ b/hook @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash HAS_NODE=`which node 2> /dev/null || which nodejs 2> /dev/null || which iojs 2> /dev/null` diff --git a/install.js b/install.js index 77f090c..e24fc0d 100644 --- a/install.js +++ b/install.js @@ -101,7 +101,7 @@ if(os.platform() === 'win32') { hookRelativeUnixPath = hookRelativeUnixPath.replace(/[\\\/]+/g, '/'); } -var precommitContent = '#!/bin/bash' + os.EOL +var precommitContent = '#!/usr/bin/env bash' + os.EOL + hookRelativeUnixPath + os.EOL + 'RESULT=$?' + os.EOL + '[ $RESULT -ne 0 ] && exit 1' + os.EOL From 84aa9eac11634d8fe6053fa7946dee5b77f09581 Mon Sep 17 00:00:00 2001 From: Filip Barakovski Date: Tue, 20 Nov 2018 01:16:39 +0100 Subject: [PATCH 17/18] Changes 'side affects' to 'side effects' (#141) --- hook | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hook b/hook index 28a7f38..1bf23b8 100755 --- a/hook +++ b/hook @@ -38,7 +38,7 @@ elif [[ -x "$LOCAL" ]]; then fi # -# Add --dry-run cli flag support so we can execute this hook without side affects +# Add --dry-run cli flag support so we can execute this hook without side effects # and see if it works in the current environment # if [[ $* == *--dry-run* ]]; then From a84bdc87aabf79493343a366872ab204a62b1613 Mon Sep 17 00:00:00 2001 From: Ace Young <2482343+aceyoung9@users.noreply.github.com> Date: Tue, 4 Jun 2019 06:50:52 -0400 Subject: [PATCH 18/18] Update link to official npm docs (#143) --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 137671e..16f6b2e 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ the existing `pre-commit` file in your `.git/hooks` folder. Existing `pre-commit` will try to run your `npm test` command in the root of the git repository by default unless it's the default value that is set by the `npm -init` script. +init` script. But `pre-commit` is not limited to just running your `npm test`'s during the commit hook. It's also capable of running every other script that you've @@ -116,7 +116,7 @@ or as `"pre-commit.{key}` key properties in the `package.json`: It's all the same. Different styles so use what matches your project. To learn more about the scripts, please read the official `npm` documentation: -https://npmjs.org/doc/scripts.html +https://docs.npmjs.com/misc/scripts And to learn more about git hooks read: