tarmolov / git-hooks-js Goto Github PK
View Code? Open in Web Editor NEWA tool to manage and run project git hooks
A tool to manage and run project git hooks
Symlinks are useful if you have a task you want to have performed during different hooks. They let you keep the code in a single file and use it in all the right places.
I've added a pull request that corrects this: #45
$ npm install git-hooks --save-dev
> [email protected] postinstall c:\home\cis-dashboard\web-api\frontend\node_modules\git-hooks
> ./bin/git-hooks --install
>
> '.' is not recognized as an internal or external command,
> operable program or batch file.
In my opinion, precommit-hook config more usefull, and we can transfer all configuration to package.json too. For example, it can looks like that:
"hooks": {
"pre-commit": [
{
"test": "/*\.js$/",
"script": ["lint", "test"]
},
{
"test": "/*\.css$/",
"script": ["csscomb"]
},
...
]
}
@tarmolov, what do you think about that?
At git-hooks.js#L188, args
may be undefined
, thus spawn
considers that no option object is passed, ignoring {stdio: 'inherit'}
: that would explain why scripts logs disappear.
Making a PR to solve this issue.
I deleted by accident the .git/hooks
folder. What would be the best way to restore it?
(moved from discussion in #30)
Currently this project has code to check if a hook file is included in .gitignore and not run it if that is the case. This was inspired by a problem where a vim .[filename].swp
file was executed while developing a git hook: #12
This looses the ability to have local git hooks that you might want to deliberately ignore because you don't want to commit them, but you do want to run them on all your commits, and you do want to use the git-hooks-js folder structure because you have other hooks that you do want committed.
The vim swap file is not executable, and starts with a .
character. Either of these reasons should be enough to not run the file. Perhaps a warning could be displayed on such files, something like "not running hook named .foobar.swp as it starts with a ." and / or "not running hook named .foobar.swp as it is not executable".
When running the CLI script with --install
, it backs up the preexisting Git hooks in a hooks.old
directory. The reverse happens when running it with --uninstall
, but it can be run twice to delete the preexisting hooks. This should be changed so that it can only be uninstalled once.
Hi
I'm talking about this https://github.com/tarmolov/git-hooks-js/blob/master/lib/git-hooks.js#L53-L54
It produces code like
require('../../../../../../....../node_modules/git-hooks/lib/git-hooks').run .....
inside git hook files (e.g. .git/hooks/post-merge
).
But, I'm building cli tool which relies on git-hooks
inside. Problem occurs if repo changes it's location, so relative path becomes broken. I'd would suggest to change relative path with absolute. I can prepare PR if you interested in it.
Unless you can suggest anything else?
Thanks
Since I've upgraded to 1.1.6
I'm getting the following error
[GIT-HOOKS ERROR] Incorrect value of args option
I've downgraded to 1.1.5
and everything is back to normal.
I get a bunch of "no such file or directory" errors whenever I try any git commands after installing.
Hi!
I had added the git-hook and it wasn't working. The reason was *.swp file generated by vim.
I was going to create PR but saw the tests and it considered as normal case. But maybe it's a good idea to handle it somehow. E.g. add a list with exceptional extensions or analyse execute permission for the file.
What do you think?
In git submodules .git
is a file pointing to the "real" git-dir instead of a directory.
The current implementation of getClosestGitPath does not take this case into account and the githooks --install
command fails.
One possible solution is to use git rev-parse --git-dir
to get the git directory for the cwd. I implemented that solution naively in my fork: https://github.com/Kampfgnom/git-hooks-js
The problem is: The tests expect a broken implementation and no longer pass with the given solution.
In the context of my current project I do not have the time to investigate any further and fix the issue, which is why I am posting my fork "as is".
This looks like a promising package, but I ran into a problem getting started. I have a repo where node lives at www/node
. When I add git-hooks to www/node/package.json
it installs successfully, but the hooks can't find node.
$ .git/hooks/pre-commit
Cannot find git-hooks. Did you install it?
I think this is similar to observing/pre-commit#27 βΒ and as noted there it gets more complex if there's more than one copy of node in the repo. For example I might have admin/node
along with www/node
, and I want git-hooks to run for both, each with its own separate .githooks
directories.
Currently if some subfolder (for storing something helpful) exists inside hook folder, it will be interpreted and executed as hook leading to that hook will be failed:
$ mkdir -p .githooks/pre-rebase/test
$ .git/hooks/pre-rebase
[GIT-HOOKS ERROR] Cannot execute hook: <project_dir>/.githooks/pre-rebase/test. Please check file permissions.
I think that would be great if result of readDirSync
will be filtered before running hook.
how to get file names and message text in pre-commit hook?
After reading the readme.md, I just don't get what this module is for...
Why use .githooks
in project root? Why not $GIT_DIR/templates/hooks/*
or $GIT_DIR/.githooksjs/*
?
It's always better to follow conventions and structures.
same as SBoudrias/Inquirer.js#518,
spawn(hookName, args, {stdio: 'inherit'}) will finish, not wait for user input(by inquirer.js).
my code is:
// .githooks/pre-commit/hello.js
#!/usr/bin/env node
console.log('start select ...');
'use strict';
var inquirer = require('inquirer');
var config = require('../../config/var-app');
var fs = require('fs');
var path = require('path');
inquirer
.prompt([
{
type: 'list',
name: 'appid',
message: 'What mini application do you want?',
choices: config.APPS
}
])
.then(answers => {
var appId = answers.appid;
var filePath = path.resolve(__dirname, '../../config/var-app.js');
console.log('appid: ' + appId);
fs.readFile(filePath, 'utf8', function(err, data) {
if (err) return console.log(err);
var res = data.replace(/\$appid\s*=\s*\'[^']*\'/g, "$appid = '" + appId + "'");
fs.writeFile(filePath, res, 'utf8', function(err) {
if (err) return console.log(err);
console.log('select mini application finished!')
});
});
});
entry code is:
git add -A && git commit -m "test" // this will skip user input; but use 'node ./.githooks/pre-commit/hello.js', everything is ok.
Currenty it's simply assumed that there is a .git/hooks directory, but the proper way to do this is to lookup the right path:
git rev-parse --git-path hooks
This would then also properly support linked worktrees. What should be considered there, is that the hooks might be already installed, since I have to install node modules in every worktree.
I changed my project folder and path to .git/hooks
grew with one path segment.
Now sources of every file in .git/hooks
are outdated, there is a line with wrong path:
require('/home/george/workspace/document-template/node_modules/git-hooks/lib/git-hooks').run(__filename, process.argv.slice(2), function (code, error) {
When I trigger my hook with git commit
I get
GIT-HOOKS ERROR] Cannot find module '/home/george/workspace/document-template/node_modules/git-hooks/lib/git-hooks'
[GIT-HOOKS ERROR] Please reinstall git-hooks to fix this error
[GIT-HOOKS ERROR] Cannot find module '/home/george/workspace/document-template/node_modules/git-hooks/lib/git-hooks'
[GIT-HOOKS ERROR] Please reinstall git-hooks to fix this error
[GIT-HOOKS ERROR] Cannot find module '/home/george/workspace/document-template/node_modules/git-hooks/lib/git-hooks'
[GIT-HOOKS ERROR] Please reinstall git-hooks to fix this error
[GIT-HOOKS ERROR] Cannot find module '/home/george/workspace/document-template/node_modules/git-hooks/lib/git-hooks'
[GIT-HOOKS ERROR] Please reinstall git-hooks to fix this error
I run:
Nothing helped
What to do?
git-hooks
postinstall script fails from time to time because of missing dependency.
The issues is fixed in npm@3
but the majority of clients use npm@2
.
Change introduced in pull request #21 broke some hooks, like pre-rebase (and I believe others which do not receive data via stdin)
Git sends nothing to those hooks and and they are waiting for 'end' stream event forever.
That makes module unusable
no issue
We check the hook names against the output of git check-ignore
, but git check-ignore
does some nominal path normalisation, so any non-trivial path will not match the output.
Furthermore, we only ever filter against the first file in the list of ignored files provided by the output, so at most one hook will ever be filtered.
I ran into this issue while fixing the tests for Windows. The test only verifies that gitignored files which also have the wrong permissions are not executed. Since hooks with the wrong permissions will never be executed anyways, that eclipsed ability to spot the issue with not executing gitignored files.
Will create a PR to resolve the issue
I just followed the turtorial to test, but it fails, and keep said 'Cannot find git-hooks. Did you install it?'.
My env is Windows 7 + node 0.12.7
Thanks.
After the latest update I'm getting this error:
[GIT-HOOKS ERROR] callback is not a function
/Users/marcello/Desktop/react-mypages/node_modules/git-hooks/lib/git-hooks.js:163
callback(1, e);
^
TypeError: callback is not a function
at runHooks (/Users/marcello/Desktop/react-mypages/node_modules/git-hooks/lib/git-hooks.js:163:9)
at /Users/marcello/Desktop/react-mypages/node_modules/git-hooks/lib/git-hooks.js:131:17
at /Users/marcello/Desktop/react-mypages/node_modules/git-hooks/lib/git-hooks.js:36:9
at ChildProcess.exithandler (child_process.js:213:5)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:877:16)
at Socket.<anonymous> (internal/child_process.js:334:11)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
I currently have two githooks:
#!/bin/bash
source .githooks/config.sh
h1 "GIT COMMIT-MSG HOOK"
BRANCH_NAME="$(branchName)"
BRANCH_PREFIX="$(branchPrefix)"
if [ $BRANCH_NAME = "master" ] || [ $BRANCH_NAME = "HEAD" ]
then
success "Committing $(cat $1) on master"
echo $1
exit 0
elif [ -n "$BRANCH_PREFIX" ]
then
PREFIXED_MSG="${BRANCH_PREFIX}: $(cat $1)"
success "Committing ${PREFIXED_MSG} on ${BRANCH_NAME}"
echo $PREFIXED_MSG > "$1"
exit 0
else
error "The name of your branch ($BRANCH_NAME) does not follow the conventions. To ignore this run git commit with -n or with --no-verify."
exit 1
fi
#!/bin/bash
source .githooks/config.sh
h1 "GIT PRE-COMMIT HOOK"
scripts=(stylelint jslint test)
for script in "${scripts[@]}"
do
h1 "RUNNING: npm run $script"
npm run $script
if [ $? -ne 0 ]; then
error "There was a problem running: $script. To ignore this, run git commit with -n or with --no-verify."
exit 1
else
success "npm run $script was executed successfully"
fi
done
Is it possible to install git-hooks globally?
$ npm install -g git-hooks
The primary purpose is to support repositories that do not contain Node.js modules. We want the ability to check our shared commit-hooks into source control, but if I install the library globally, and run git-hooks --install
on a newly cloned repo, I get the following message:
git-hooks already installed
It appears that the git-hooks
working directory is the location of the executable, which is in /usr/local/bin
, rather than the node_modules/
folder of the current repo.
release 1.1.1 broke our git hooks. We have a pre-commit and a commit-msg hook.
The pre-commit runs, but doesn't create any terminal output anymore.
The commit-msg should validate the commit message, I'm not sure if that works, but after everything is done, the commit wasn't created.
There are no errors, so I don't know why this is happening. I reverted back to 1.1.0 and everything is working again.
[GIT-HOOKS ERROR] Cannot find module '....
node_modulesgit-hookslib/git-hooks'
the path delimiter of the generated hook files is error:
.git/hooks/post-commit:
#!/usr/bin/env node
try {
require('..\..\node_modules\git-hooks\lib/git-hooks').run(__filename, process.argv[2], function (code) {
process.exit(code);
});
} catch (e) {
console.error('[GIT-HOOKS ERROR] ' + e.message);
}
First of all, great job. This module has been key in improving our team's QA.
I and many Dev's I work with have moved away from Git on the command line in favour of a Git GUI (Github Desktop, Tower, Kaleidoscope etc) and it's at this point purists generally scoff at the idea π but the fact is (for us at least), passed merging and branching it becomes archaic to do anything more complex and productivity is improved through these tools and that's what really matters to us.
But unfortunately, several GUI's I've tried on macOS don't play nicely this library and it's because each generated hook script is set to #!/usr/bin/env node
which results in the error 'env: node not found' as the client isn't aware of the env path that has been set for the current user session.
I've manually updated each generated hook script to #!/usr/bin/env /usr/local/bin/node
which works.
I appreciate there may be pitfalls for automatically detecting the full path to the node bin e.g. which node
returns my NVM managed node bin /Users/me/.nvm/versions/node/v7.9.0/bin/node
, which could easily change in the future. Is there a .gitignore'able way to explicitly specify the node path as part of config perhaps?
Thanks
EDIT: tried various workarounds such as setting the PATH globally (e.g. /etc/profile) but some GUI's don't honour this.
https://github.com/tarmolov/git-hooks-js/blob/master/lib/git-hooks.js#L131
Error: spawn P:\some-project\.githooks\pre-commit\lint ENOENT
result of executing in terminal P:\some-project\.githooks\pre-commit\lint
- "bash: command not found"
bash P:\some-project\.githooks\pre-commit\lint
- works
i.e. with such code modifying all works var hook = spawn('bash', [hooks.shift()], {stdio: 'inherit'});
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.