Git Product home page Git Product logo

git-hooks-js's People

Contributors

cinderblock avatar getsnoopy avatar maxlath avatar rjmunro avatar ruslan-polutsygan avatar shdwjk avatar tarmolov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

git-hooks-js's Issues

Simplify configuration

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?

Restoring .git/hooks

I deleted by accident the .git/hooks folder. What would be the best way to restore it?

Hooks should not be ignored by .gitignore

(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".

Running --uninstall twice removes the preexisting hooks

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.

Relative path to git-hooks

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

tmp files in hook's directory

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?

getClosestGitPath does not work in git submodules

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".

Support node in a subdirectory of git repo

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.

Existence of subfolders follows to error

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.

purpose?

After reading the readme.md, I just don't get what this module is for...

use $GIT_DIR to store the hooks

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.

spawn(hookName, args, {stdio: 'inherit'}) will skip user input by inquirer.js

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.

Properly lookup git hooks directory

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.

How to reinstall git-hooks with yarn?

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:

  1. yarn
  2. deleted yarn.lock and called yarn
  3. yarn install --check-files
  4. yarn install --force

Nothing helped
What to do?

Passing stdin to all hooks broke some of them completely

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

.gitignored files are still executed

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

[GIT-HOOKS ERROR] callback is not a function

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:

commit-msg

#!/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

pre-commit

#!/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

Install git-hooks globally

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.

1.1.1 is broken

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.

the git-hooks can not work on windows(mingw)

[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);
}

Git GUI clients: 'env: node' not found

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.