1.0.2 • Published 2 years ago

@stinobe/git-tools v1.0.2

Weekly downloads
-
License
none
Repository
github
Last release
2 years ago

Git Helpers

A tool to help validate commit messages before committing & branch names before pushing them.
The tool will not run in CI thanks to ci-info

To be used with Husky

Installation

npm install @stinobe/git-tools

Setup

Assuming you haven't installed and/or configured Husky.

Add precommit and prepublish scripts in package.json

# Set script for commits
npm set-script precommit "stinobe-commit-message"

# Set script for branches
npm set-script prepush "stinobe-branch-name"

If Husky not yet installed

# Install Husky
npm install husky --save-dev

# Enable Git hook
npx husky install

# Automatically enable Git hooks after install
npm set-script prepare "husky install"

Add previously created NPM scripts to Husky

# Inform Husky of pre commit script
npx husky add .husky/pre-commit "npm run precommit"

# Inform Husky of pre publish script
npx husky add .husky/pre-publish "npm run prepublish"

Configuration

A default configuration is built in, but you can overwrite those by a .gittoolrc , .gittool.json or by adding a gittool property in the package.json file (in order of hierarchy).

Configuration exists of Commit message configuration and Branch name configuration.

{
  "commits": [],
  "branches": {}
}

All settings are optional

Commit message configuration

Commit settings exists of an array containing differenc configuration for different branch names

PropertyTypeDescription
branchesRegExpRegex to check if rules apply on current branch
validationRegExpRegex where commit message will be tested against
examplesstring[]Will be printed out in the console when validation fails

How the default commit message settings look like

{
  "commits": [
    {
      "branches": /.*/,
      "validation": /([Cc]lose[sd]?|[Ff]ix(e[sd])?|[Rr]esolve[sd]?) #[1-9]+\d*/,
      "examples": [
        "Commit message contains one of:",
        "\tclose #[issue-number]",
        "\tcloses #[issue-number]",
        "\tclosed #[issue-number]",
        "\tfix #[issue-number]",
        "\tfixes #[issue-number]",
        "\tfixed #[issue-number]",
        "\tresolve #[issue-number]",
        "\tresolves #[issue-number]",
        "\tresolved #[issue-number]"
      ]
    }
  ],
  "branches": {}
}

branches property can be an empty string or be left out but will cause a log in the output.

Branch name configuration

PropertyTypeDescription
validationRegExp[]List of regular expressions with allowed branch names
examplesstring[]List of example branch names, will be logged if validation fails

How the default branch name settings look like

{
  "commit": [],
  "branches": {
    "validation": [
      /^(feature|bug|docs|hotfix)\/(\d+)-[a-z-]+/,
      /^wip\/[a-z1-9]+[a-z0-9]*/
    ],
    "examples": [
      "feature/[issue_number]-[subject]",
      "bug/[issue_number]-[subject]",
      "docs/[issue_number]-[subject]",
      "hotfix/[issue_number]-[subject]"
    ]
  }
}
1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago