GithubHelp home page GithubHelp logo

git-hooks's Introduction

@vercel/git-hooks

No nonsense Git hook management.

Usage

Install this module, preferably as a dev-dependency:

yarn add --dev @vercel/git-hooks

That's it. You can now use the module in two ways:

{
  "scripts": {
    "git-pre-commit": "eslint"
  }
}

The above will run a single command line, just like running npm run git-pre-commit or yarn git-pre-commit, every time you git commit.

Alternatively, if you'd like to run several scripts in succession upon a hook, you may define a git top-level property and specify an array of scripts to run:

{
  "git": {
    "pre-commit": "lint"
  }
}

or

{
  "git": {
    "pre-commit": ["lint", "test"]
  }
}

Note that any "scripts" hooks supplant any corresponding "git" hooks. That is to say, if you define both a {"scripts": {"git-pre-commit": "..."}} hook and a {"git": {"pre-commit": []}} hook, the hook in "scripts" will be the only hook that is executed.

Why? There are hundreds of these.

  • No dependencies
  • Supports NPM, Yarn, <insert package manager> - this package will detect and use whatever package manager you installed it with*
  • Tiny footprint - two script files and a couple of symlinks
  • Existing hook / anti-overwrite checks are very reliable since two proprietary scripts are added and all of 'our' hooks are just symlinks to those, so there's virtually no way the uninstall script will mistake a pre-existing hook for its own

*Caveat: The package manager needs to be npm compliant in terms of environment variables. Worst case, define the environment variables npm_node_execpath (node binary) and npm_execpath (package manager entry point) as environment variables prior to installing.

License

Copyright Β© 2021 by Vercel, Inc.

Released under the MIT License.

git-hooks's People

Contributors

leerob avatar qix- avatar rauchg avatar rdmurphy 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  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

git-hooks's Issues

hooks scripts stop working when `yarn` is update

It looks yarn path is hardcoded so that when it's updated and removed by like brew, scripts stop working.
I think it's the same for node which the path is hardcoded.
Is there are an easy way to update the scripts?

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Mono repo support?

With a structure like this:

.
β”œβ”€β”€ .git
β”œβ”€β”€ api/
β”‚   └── package.json
└── app/
    β”œβ”€β”€ hasLintError.js
    └── package.json

In app/package.json, using @zeit/git-hooks with lint-staged has no effect.

Checking the .git/hooks directory, there are only .sample files in there.

Does that mean @zeit/git-hooks does not work for monorepo packages?

Tags a "0" onto the end of the command.

When I created a command it tags a "0" onto the end of the command. For a temporary fix I just added echo to the end of my command so it doesn't fail.

Example
unset ZEIT_GITHOOKS_RUNNING ; if npx git-branch-is master ; then npm version patch && git push && git push --tags ; fi ; echo

cross OS usage

On Windows it's possible to install the package using an admin console (since .git folders are read-only for normal users). Otherwise symlink creation will fail due to missing permissions (.cjs files -however- are created, so creating and removing symlinks seems to need more permissions, than file creation):
Error: EPERM: operation not permitted, symlink './_do_hook.cjs' -> '[path\to\hooks]\applypatch-msg' (install.js:122:5)

Result in hooks with proper credentials (admin) will be e.g.:

06.09.2021  05:09    <DIR>          .
06.09.2021  05:09    <DIR>          ..
06.09.2021  05:09    <SYMLINK>      applypatch-msg [.\_do_hook.cjs]
01.09.2021  18:18               478 applypatch-msg.sample
06.09.2021  05:09    <SYMLINK>      commit-msg [.\_do_hook.cjs]
01.09.2021  18:18               896 commit-msg.sample
06.09.2021  05:09    <SYMLINK>      post-applypatch [.\_do_hook.cjs]
06.09.2021  05:09    <SYMLINK>      post-checkout [.\_do_hook.cjs]
...

But uninstall is failing on removing symlinks due to different os path separator (baked into symlink string during creation).
(codepointer)

β–³  @vercel/git-hooks: hook 'applypatch-msg' appears to be a user-defined hook; skipping: [project\path]\.git\hooks\applypatch-msg

fs.readlinkSync(hookPath) gives '.\\_do_hook.cjs' ('.\\_do_hook' before #7)

Solution could be: adding different path sepatators as character class to RegExp literal [\\/]

const isOneOfOurs = fs.lstatSync(hookPath).isSymbolicLink() && fs.readlinkSync(hookPath).match(/\.[\\/]_do_hook(\.cjs)?/);

or (better readable): using os path separator from path module without RegExp

const isOneOfOurs = fs.lstatSync(hookPath).isSymbolicLink() && [`.${path.sep}_do_hook`, `.${path.sep}_do_hook.cjs`].includes(fs.readlinkSync(hookPath));

btw. trying to include dynamic path separator into RegExp constructor string is a bit cumbersome due to a lack of proper build-in escaping for RegExp...\

new RegExp("."+needToRegExpEscape(path.sep)+"_do_hook(\\.cjs)?")

Would be nice to see a patch on this soon. And maybe you can mention this troubleshooting (admin console on Windows) in the README. Also, I had to guess where to put the config JSON, would be nice to mention the package.json in the README as well.

kind regards.

_detect_package_hooks throws error with "type": "module" in package.json

Hello! I believe because the _detect_package_hooks script is ran in the same context as the package its installed in, it now errors when @zeit/git-hooks is used in a project that sets type to module in its package.json.

internal/process/esm_loader.js:74
    internalBinding('errors').triggerUncaughtException(
                              ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension "" for /Scrubbed/project/folder/.git/hooks/_detect_package_hooks
    at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:71:15)
    at Loader.getFormat (internal/modules/esm/loader.js:102:42)
    at Loader.getModuleJob (internal/modules/esm/loader.js:231:31)
    at async Loader.import (internal/modules/esm/loader.js:165:17)
    at async Object.loadESM (internal/process/esm_loader.js:68:5) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

When I do not set type to module it runs as expected.

I was able to "fix" this locally by forcing that script to be ran as a .cjs file. This required renaming _detect_package_hooks to _detect_package_hooks.cjs and altering the hook (in this case pre-commit) to call it with that extension. Obviously not the most backward-compatible of fixes, however. (Although we're very close to v10 being EOL.)

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.