observing / pre-commit Goto Github PK
View Code? Open in Web Editor NEWAutomatically installs a git pre-commit script in your git repository which runs your `npm test` on pre-commit
License: MIT License
Automatically installs a git pre-commit script in your git repository which runs your `npm test` on pre-commit
License: MIT License
It would be really handy if pre-commit would also run scripts when repositories were used as submodules
http://stackoverflow.com/questions/10848191/git-submodule-commit-hooks
`git rev-parse --git-dir`/hooks
I have no idea how difficult this would be to implement.
Here is my single task config.
"scripts": {
"eslint": "eslint $(git diff-index --cached HEAD --name-only --diff-filter ACMR | egrep '.js$')"
},
"pre-commit": ["eslint"],
"precommit.silent": true
It works well even if I did not change any javascript file.
And below is my multi tasks config.
"scripts": {
"eslint": "eslint $(git diff-index --cached HEAD --name-only --diff-filter ACMR | egrep '.js$')",
"scss-lint": "scss-lint $(git diff-index --cached HEAD --name-only --diff-filter ACMR | egrep '.s(a|c)ss$')"
},
"pre-commit": ["eslint", "scss-lint"],
"precommit.silent": true
Let's say I just modified a sass file and commit it .
eslint [options] file.js [file.js] [dir]
Basic configuration:
-c, --config path::String Use configuration from this file or shareable
config
--no-eslintrc Disable use of configuration from .eslintrc
--env [String] Specify environments
--ext [String] Specify JavaScript file extensions - default: .js
--global [String] Define global variables
--parser String Specify the parser to be used - default: espree
--parser-options Object Specify parser options
Caching:
--cache Only check changed files - default: false
--cache-file path::String Path to the cache file. Deprecated: use
--cache-location - default: .eslintcache
--cache-location path::String Path to the cache file or directory
Specifying rules and plugins:
--rulesdir [path::String] Use additional rules from this directory
--plugin [String] Specify plugins
--rule Object Specify rules
Ignoring files:
--ignore-path path::String Specify path of ignore file
--no-ignore Disable use of .eslintignore
--ignore-pattern [String] Pattern of files to ignore (in addition to those
in .eslintignore)
Using stdin:
--stdin Lint code provided on <STDIN> - default: false
--stdin-filename String Specify filename to process STDIN as
Handling warnings:
--quiet Report errors only - default: false
--max-warnings Int Number of warnings to trigger nonzero exit code
- default: -1
Output:
-o, --output-file path::String Specify file to write report to
-f, --format String Use a specific output format - default: stylish
--no-color Disable color in piped output
Miscellaneous:
--init Run config initialization wizard - default: false
--fix Automatically fix problems
--debug Output debugging information
-h, --help Show help
-v, --version Outputs the version number
--no-inline-config Allow comments to change eslint config/rules
--print-config Print the configuration to be used
client/styles/food/food-box.sass:11 [E] Syntax Error: Invalid CSS after " border: none": expected "{", was ";"
This's what I got. It contains eslint
output. I mean if you run eslint
without any arguments, You will get those.
For the single task config, if I do not modify any javascript file, eslint
will run with empty arguments, but it won't output those stuff, So I guess pre-commit
handle that for me .
What about multi task config ? is it a bug or what ?
I have a pre-commit hook that asks a few questions to the committer prior to running the rest of the standard commit hooks (those not requiring user input). When I attempt to commit, the script that gather user input starts, prints out some chunk of the prompt and then exits. I tried to track this down and see why and it appears that the 'close' event on the spawned process is getting called earlier than I would expect on the user-input-expecting hook AKA almost immediately after printing the first prompt it emits a 'close' event on the spawned process. This causes 'again' to be called unexpectedly. I need to find out why that is happening. I had the same issue with the readline module and the prompt module. Maybe it is how user prompting works generally?
My question is: Is requesting user input and waiting on it before continuing on to the next hook not a feature we want to support or is it bad practice to ask for user input during a hook? I would like to make it possible, but just wanted to see some opinions on this.
Hello,
I have a multi-technology / single-repository project, something like that :
The problem is : the install.js doesn't find the .git directory, because it isn't in frontend directory but 1 level up. I don't know how to detect accuratly the git root, but maybe we can specify it manually in package.json, along side pre-commit scripts ?
I've been using this project for a while but am stuck with v0.0.11. My default OS is Windows (sad, I know), so to make things less weird I use Vagrant for development.
After 248d18c it's not possible to install pre-commit
because of this:
vagrant@vagrant-ubuntu-trusty-64:~/cha/api/code$ npm install --save-dev pre-commit --no-bin-links
/
> [email protected] install /home/vagrant/cha/api/code/node_modules/pre-commit
> node install.js
pre-commit:
pre-commit: Detected an existing git pre-commit hook
pre-commit: Old pre-commit hook backuped to pre-commit.old
pre-commit:
pre-commit:
pre-commit: Failed to symlink the hook file in your .git/hooks folder because:
pre-commit: EPROTO: protocol error, symlink '/home/vagrant/cha/api/code/node_modules/pre-commit/hook' -> '/home/vagrant/cha/api/code/.git/hooks/pre-co
mmit'
pre-commit: The hook was not installed.
pre-commit:
[email protected] node_modules/pre-commit
└── [email protected]
There are ways to enable symlinking from Vagrant on Windows, but they're all hacks and really, really painful.
What do you guys think about supporting copying again (in case symlink creation fails)? If you agree, I can send a PR to support this :)
Thanks!
I only want to pre-commit my pretest (linting) step, and exclude test. How can I do this?
I'm getting the following error on Windows when I try to use this package:
.git/hooks/pre-commit: line 11: [: /c/Program: binary operator expected pre-commit:
This is line 11 from the referenced file:
if [ -z $HAS_NODE ] && [ -f ~/.bash_profile ]; then
Can you provide any help?
Package version 1.0.10
Node version: 0.10.31
NPM version 2.13.2
Windows 7
node
isn't always in the PATH for GUI clients.
Helps test only prospective commit.
$ npm test # runs jshint
$ echo "// pass" > pass.js
$ echo "err()" > fail.js
$ git add pass.js
$ npm test # should fail on fail.js
$ git commit -m "should pass"
npm WARN deprecated [email protected]: cross-spawn no longer requires a build toolchain, use it instead!
Is there any way to only run the hook (jshint in my case) on the files being committed? It's quite slow on my large repo
Some IDEs that do commits will break if git commit outputs random NPM related output.
We could fix this in IDEs but its simpler to fix here.
I'm not sure if this is a bug with pre-commit
, a bug with snazzy
or both. So I'm filing it here and copying @feross
The following "project" will commit without error despite failing the linting step:
package.json
{
"scripts": {
"lint": "snazzy",
"foo": "exit 0"
},
"precommit": [
"lint",
"foo"
],
"devDependencies": {
"pre-commit": "^1.2.2",
"snazzy": "^6.0.0",
"standard": "^10.0.0"
}
}
index.js
'use strict'
function invalid() {
return 1
} // note missing newline EOF
If you replace "lint": "snazzy"
with "lint": "standard"
then the commit will fail as it should.
.git/hooks/pre-commit: line 3: /Volumes/Users/aaron/.bash_profile: No such file or directory
As the subject states, when I update to the latest version, the pre-commit no longer happens when doing a commit.
The pre-commit file and pre-commit.old show no differences.
I attempted to uninstall this from a project I'm working on (we're over 100 tests and switching branches became a process). Once uninstalled, I started seeing this in my commit message:
.git/hooks/pre-commit: line 2: ./node_modules/pre-commit/hook: No such file or directory
I had to re-install the package and add this to my package.json to get the project functional again:
"pre-commit": {
"silent": true,
"run": []
}
I fully accept that the problem could be me, but if anyone else has experienced this I'd love to see a better solution
Only check staged files when trying to commit
I've been pulling my hair out trying to make sense of this for someone I'm working with who is having this problem. It's breaking their workflow.
No such file or directory 2: ./node_modules/pre-commit/hook
: integer expression expected: [: 127
: numeric argument required 5:
I've tried Windows machines near me, and VMs and don't have the problem.
This happens in cmd, powershell, MinGW, WebStorm
.
Any ideas?
If I run git commit ...
from the command line which causes a "node script" to spin up a Docker container and mount the volumes in it which subsequently runs the hooks in the container I get the error cannot enable tty mode on non tty input
.
I'm getting the following error while trying to install on Windows:
> [email protected] install c:\path\to\project\node_modules\pre-commit
> node install.js
fs.js:741
return binding.symlink(preprocessSymlinkDestination(destination, type),
^
Error: EPERM, operation not permitted 'c:\path\to\project\node_modules\pre-commit\hook'
at Object.fs.symlinkSync (fs.js:741:18)
at Object.<anonymous> (c:\path\to\project\node_modules\pre-commit\install.js:49:4)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
When I use git add -p
or even git add some files
it will run my tests before the commit however the tests pass because of the unstaged changes I have locally.
If I were to stash before running npm test
then the tests would have failed.
Is it the responsibility of pre-commit
to ensure that the tests pass based on only the staged changes or is that the users responsibility ?
would be nice for non node people if there was a list of suggested node tools for common tasks:
I installed pre-commit into an Ember cli project, but it complains about receiving a non 0 exit code after passing tests. Any ideas?
First raised here:
Pretty much a duplicate of #43
pre-commit:
pre-commit: Failed to symlink the hook file in your .git/hooks folder because:
pre-commit: EPERM: operation not permitted, symlink '..\..\node_modules\pre-commit\hook' -> 'C:\path\to\glory\.git\hooks\pre-commit'
pre-commit: The hook was not installed.
pre-commit:
However, I'd like to share some more info: https://en.wikipedia.org/wiki/NTFS_symbolic_link
Ideally, we could request elevation something like https://www.npmjs.com/package/windows-elevate or call a native function.
Thanks!
Hi!
My package.json looks like this:
"scripts": {
"lint-changed": "eslint $(git diff --name-only | grep -e \\.js$)"
},
"pre-commit": ["lint-changed"]
Running npm run-script lint-changed
works as expected, but git commit
returns this:
$ git commit -a
eslint [options] file.js [file.js] [dir]
Options:
[..]
What am I doing wrong?
Hello, thanks for good module!
But one thing is really needed.
For example, I have lint script on npm, that run linting gulp lint task. And in this task I can get list of all changed files from last commit through gulp-git
to lint only them like this
gulp.task('git-diff', callback => {
git.exec({args: `diff-index --name-only HEAD | grep .*\\.js | grep -v json`, quiet: true}, (err, stdout) => {
.....
});
});
But if I commit only some files this task anyway will take every changed, i.e. some files that I don't want to commit right now may have broken lint rules (for examples this files in deep development in that moment).
It would be great, if pre-commit
passed list of files to npm script, for example if we have
"scripts": {
"lint": "gulp lint"
},
"pre-commit": [
"lint"
]
And pre-commit
can call npm run lint -- --file path1 [--file path2 [--file path3]]
where pathN
is path to commiting files. In this case I can read in gulp array of passed files and lint only them.
For example, when running npm version patch
or doing a bower version update which makes a commit and tag.
This fails on windows with the error:
events.js:72
throw er; // Unhandled 'error' event
^
Error: spawn ENOENT
at errnoException (child_process.js:1001:11)
at Process.ChildProcess._handle.onexit (child_process.js:792:34)
because you can't use spawn
to invoke npm on windows.
maybe this help https://www.npmjs.com/package/cross-spawn
I just installed pre-commit for a project which contains a space in the path name (e.g. /projects/example path
). npm test
itself runs fine, but when I try to commit, I get the following error:
$ git commit -m "package.json: add pre-commit"
module.js:338
throw err;
^
Error: Cannot find module '/projects/example'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:286:25)
at Function.Module.runMain (module.js:471:10)
at startup (node.js:117:18)
at node.js:948:3
What could be done to make sure build files created by pre-commit are added to the stage?
Is this something that can be in pre-commit, or is there something I can do on my end?
For instance I could just add a command to the package.json scripts that stages the built files on pre commit.
I would like to only run this on commits directly to master. Can we add this to the configuration options?
Posted in wrong repo...sorry
npm install --save-dev pre-commit
/
> [email protected] postinstall c:\git\iron-node\node_modules\pre-commit\node_modules\cross-spawn\node_modules\spawn-sync
> node postinstall
> [email protected] install c:\git\iron-node\node_modules\pre-commit
> node install.js
pre-commit:
pre-commit: Failed to symlink the hook file in your .git/hooks folder because:
pre-commit: EPERM, operation not permitted 'c:\git\iron-node\.git\hooks\pre-commit'
pre-commit: The hook was not installed.
pre-commit:
[email protected] node_modules\pre-commit
├── [email protected] ([email protected])
└── [email protected] ([email protected], [email protected])
I love the fact that pre-commit
ties directly into package.json
scripts
from an ergonomics perspective.
However, I think it'd be a good idea to avoid the npm sub-process altogether, for performance reasons. You could, for example, look up the scripts yourself and run them with the ./node_modules/.bin
prefix.
Every commit will pay a penalty of ~380ms per pre-commit
script defined. See investigation by @vjeux: prettier/prettier#443
Since pre-commit
is now the recommended way to set up prettier, I think this is very much a worthwhile optimization that'll save a lot of time to many people.
Hello, I am trying to make run your precommit hook on this environment:
When I run git commit the test pass successfully but then nothing happens.
As an experiment, I tried the template
option.
It did not work (apparently due to #80)
After removing the template
option, my pre-commit hook still failed.
fatal: could not read '".commit-template"': No such file or directory
I had to manually edit my .git/config
to fix this.
I can't think of a nice way to handle this, and honestly the more I think about it, I am not sure if having this option in your project really makes that much sense to begin with (but of course, thats your prerogative)
Thanks for your great work!
I only see 1.0.5
a9c9732#diff-f16acefe4b6553580c43edab685f50f3 creates a file that does not have +x
set on Unix systems, making it not executable.
My loading of NVM on local dev machine is in .bashrc
file and if i try to commit from Phpstorm it doesnt find the node executable
I am using the zsh
shell with oh-my-zsh. I also often use the SourceTree client. Lastly, I'm using react-boilerplate which uses pre-commit
.
When I commit from the command line (zsh shell), everything works ok. But I just started getting errors when committing from inside the SourceTree client (which worked fine yesterday):
git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree commit -q -F /var/folders/wt/bj_21zvn7518qvf6bfxtt3w40000gn/T/SourceTreeTemp.IWP5H8
/Users/tauren/.oh-my-zsh/oh-my-zsh.sh: line 12: autoload: command not found
/Users/tauren/.oh-my-zsh/oh-my-zsh.sh: line 31: syntax error near unexpected token `('
/Users/tauren/.oh-my-zsh/oh-my-zsh.sh: line 31: `for config_file ($ZSH/lib/*.zsh); do'
/Users/tauren/.zshrc: line 95: brew: command not found
/Users/tauren/.zshrc: line 95: /nvm.sh: No such file or directory
zsh-syntax-highlighting: error: not compatible with NO_FUNCTION_ARGZERO
./node_modules/pre-commit/hook: line 49: : command not found
./node_modules/pre-commit/hook: line 49: : command not found
Completed with errors, see above
I've been using this setup for quite some time without issues, and I'm not quite sure what changed. The most recent thing I did was blow away node_modules
in my react-boilerplate
project and run npm install
again. I did update to the latest SourceTree a few days ago, but I'm pretty sure I've committed using it since then. I tried updating to [email protected]
from 1.1.3
, but the same thing happens.
As with any oh-my-zsh setup, my ~/.zshrc
file sources ~/.oh-my-zsh/oh-my-zsh.sh
. This file executes autoload
which is a ZSH shell command and blows up. It looks like pre-commit
is running in a bash shell, so the command wouldn't be available.
But what I don't understand how it was ever working... This error just started, and my OMZ script has had autoload
in it forever.
Is there something I can do to fix this? Can pre-commit
take measures to run with the correct shell? Anything else I should do?
e.g. pre-push
. It seems silly to have separate projects (e.g. https://www.npmjs.com/package/prepush-hook) for each different hook.
If you are developing using nvm, sourcing the bash profile will change which node version are you targeting. This means that if you are developing a native addon against iojs and your default is 0.10, everything falls apart.
A quick solution for this might be checking if node is already present, and if so do not source. I will send a PR with this fix.
Side note, can you please bump the MINOR or at least the MAJOR when you change the hook? It broke a lot of stuff for the way I work.
Is there a way to configure this to only run the commit hooks on master?
I started with the following in my package.json
"pre-commit": [
"jshint app.js"
],
This resulted in the following when I commit:
pre-commit:
pre-commit: We've failed to pass the specified git pre-commit hooks as the `jshint app.js`
pre-commit: hook returned an exit code (1). If you're feeling adventurous you can
pre-commit: skip the git pre-commit hooks by adding the following flags to your commit:
pre-commit:
pre-commit: git commit -n (or --no-verify)
pre-commit:
pre-commit: This is ill-advised since the commit is broken.
pre-commit:
Running jshint from the command line returns 0. So under some expert advice I changed my package.json to
"pre-commit": [
"true"
],
and it reports the same error as before except it says true returned exit code 1. So something is obviously wrong or misconfigured. Any help to troubleshoot?
I am running OS X 10.11.4 and the following:
node v5.5.0
npm 3.5.3
pre-commit 1.1.2
same behavior occurs using Fish shell and bash
Something similar to https://github.com/typicode/husky/blob/master/bin/uninstall.js
That way npm rm pre-commit
will uninstall the hook.
Hi
when i try to install the new version v1.1.0, it fails on row 26 of install.js
if (shelly.exec(hook +' --dry-run', { silent: true }).code !== 0) {..}
If i change silent to false and run node install.js from pre-commit folder, it gives me
"...\node_modules\pre-commit\hook" non è riconosciuto come comando interno o esterno, un programma eseguibile o un file batch.
pre-commit:
pre-commit: The --dry-run of the pre-commit hook failed to execute.
pre-commit:
pre-commit: The hook was not installed.
pre-commit:
because windows cant run
./hook --dry-run
Instead i must use
sh ./hook --dry-run
sh.exe is the emulator on windows of unix shell installed with msysgit (git for windows) for example
Hi! Thanks for amazing solution of hooks implementation for node.js. But could you add support for cases when .git placed in parent directory relative to package.json?
Is there a reason that #!/bin/bash
is used here
Line 1 in 7154dee
#!/usr/bin/env bash
which would avoid the errorerror: cannot run .git/hooks/pre-commit: No such file or directory
that users could get who does not have bash in /bin.
Has there been any discussion around this?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.