0.0.2 • Published 1 year ago

git-hooks-toolkit v0.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Git Hooks Toolkit

Git Hooks Toolkit is a lightweight library to simplify the use of git hooks in a collaborative workflow.
Git Hooks Toolkit can be used on any git repository and comes with a few added features like branch name, file name and commit message linting.

Installation

NPX

Git Hooks Toolkit can be automatically installed with npx.

npx git-hooks-toolkit --init=maven

Git Hooks Toolkit needs to be wired on each collaborator local repository. Otherwise the hooks won't be automatically executed.

npx git-hooks-toolkit --wire

Manual installation

  • Copy the .git-hooks-toolkit folder in your project.
  • Run the installation script install.sh.
  • Create your config file from the documentation or use a predefined template.
    The installation script needs to be run on each collaborator local repository.

Usage

To configure different scripts to be run you need to place a git-hooks.yml in the .git-hooks-toolkit folder or at the root of your project. You can find configuration templates in the templates folder.

To bypass the hooks for a specific commit add --no-verify to your git command. Example: git commit -m 'yolo' --no-verify
More information on which git command support the --no-verify argument on Git

Set a pre-commit hook

pre_commit: npm run lint && npm run test

The pre-commit hook is run first, before you even type in a commit message. It’s used to inspect the snapshot that’s about to be committed, to see if you’ve forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code. Git

Set a pre-push hook

pre_push: npm run lint && npm run test

The pre-push hook runs during git push, after the remote refs have been updated but before any objects have been transferred. You can use it to validate a set of ref updates before a push occurs. Git

Prevent commits to branches

no_commit_to: 
  branches:
    - master
    - develop
  message: 'Committing to the following branches is not allowed : master, develop'

Lint branch names

branchname_lint:
  regex: '^(develop|main|master|((feature|hotfix)/[a-zA-Z0-9\_]+))$'
  message: 'Commit aborted: branch name does not follow naming convention.'

Lint file names

filename_lint: 
  regex: '^[a-zA-Z\_\.\-]+$'
  message: 'Non-alphanumeric file names are not allowed.'

Lint commit messages

commit_lint: 
  regex: '^(add|fix|remove): .*'
  message: 'Commit message must contain a verb (ex: \"add: awesome feature\").'

Uninstall

NPX

To completely remove git-hooks-toolkit run:

npx git-hooks-toolkit --uninstall

To disable your custom git hooks in the current git repository but keep the git-hooks-toolkit config run:

npx git-hooks-toolkit --unwire

Manual

To completely remove git-hooks-toolkit run the script uninstall.sh.

To disable your custom git hooks in the current git repository but keep the git-hooks-toolkit config run the command git config --unset core.hooksPath.

Alternatives

Husky: https://github.com/typicode/husky

Pre-commit: https://github.com/pre-commit/pre-commit

0.0.2

1 year ago

0.0.1

1 year ago