Giter Club home page Giter Club logo

gitbranchvalidator's Introduction

License: MIT npm version last version release date Jest:coverage

Synopsis

GitBranchValidator is an open source git branch name validator library.

Motivation

The goal of this library is to provide an easy way to check if the current branch name is valid before pushing it to the remote.

It's a good way to help developers keep their branch names clean on the repository and facilitate the workflow of a project with strict naming rules.

The default pattern follows the principles described here.

You can also set your own rules using custom patterns.

You can validate commit messages as well with GitCommitValidator

Installation

This library is written in Node.js. You must install Node.js and npm in order to run it.

npm

$ npm i @dwtechs/gitbranchvalidator -g

Yarn

$ yarn add @dwtechs/gitbranchvalidator -g

Locally

Alternatively, if you are planning to use GitBranchValidator within your npm package only or with npx you can install it as a devDependency in your project.

$ npm i @dwtechs/gitbranchvalidator --save-dev
$ yarn add @dwtechs/gitbranchvalidator --dev

Usage

This library is meant to be used in "pre-commit", "commit-msg" or/and "pre-push" Git hook. Thus it is not bound to Javasrcipt applications only and can be used by any git repository.

You can learn more about Git hooks and how ti cutomize them in the Git Manual

Because hooks aren’t transferred with a clone of a project, you must distribute these scripts some other way and then have your users copy them to their .git/hooks directory and make them executable. You can distribute these hooks within the project or in a separate project, but Git will not set them up automatically for you.

Command line

$ gbvalidator

Alternatively if you did not install GitBranchValidator globally you can use npx.

$ npx gbvalidator

Or as an npm script in your package.json.

{
  "scripts": {
    "commit": "gbvalidator"
  }
}

Patterns

  • Default : ^(feat|fix|test|doc)/[A-Z0-9-#]{2,25}/[a-z0-9_-.]{3,40}$
  • Release : ^release/v[a-z0-9+-.]{3,25}$

The patterns follow the principles described here.

You can use your own custom patterns by adding an optional regexp :

$ cd <git-project>
$ gbvalidator --patterns "^(feat|fix)\\/([a-z0-9_#-\\.\\/]){3,50}$"

You can use several patterns if needed :

$ gbvalidator --patterns "pattern1" "pattern2" "pattern3"

If one of them is valid, the branch name will be valid.

Message

You can customize the end of the error message :

$ gbvalidator --message "You can learn more about branch name conventions of this project on https://dwtechs.github.io/efficient-git/branch/"

Prompt

If the branch name is not valid, the process will be blocked by default. You can use the option "--continue" to prompt the user instead.

$ gbvalidator --continue

In this case the user will be prompted about the invalid branch. He will be able to keep going if he choose so.

Git hooks are not run in an interactive environment. So this option will fail if used in a Git hook.

Help

You can access the documentation by passing the "help" parameter :

$ gbvalidator --help

Workflow integration

Validate branch name on pre-push hook by adding the following code in the .git/hooks/pre-push file :

gbvalidator

Or with tools like Husky In the package.json file :

{
  "husky": {
    "hooks": {
      "pre-push": "gbvalidator"
    }
  }
}

You can do it with any other tool, or work on Git hooks directly.

You can use GitCommitValidator in the "commit-msg" hook as well :

gbvalidator && gcvalidator -src "$1"

You can find git hooks examples in the ./hooks/ folder. To install them just paste them in the .git/hooks/ folder of your repositories.

options

Option Alias Type description
--patterns -p string Use a custom regex. You can send multiple patterns
--message -m string Add a custom message at the end of the error message
--continue -c string Prompt the user if the branch name is not valid, instead of stopping the process. Does not work if used in Git hooks
--help -h boolean Learn about library usage

Contributors

GitBranchValidator is still in development and I would be glad to get all the help you can provide for this project. To contribute please read contributor.md for detailed installation guide.

License

MIT

Stack

Purpose Choice Motivation
repository Github hosting for software development version control using Git
package manager npm default node.js package manager
unit testing Jest delightful testing with a focus on simplicity

Dependencies

Name version
@dwtechs/checkhard 2.19.0
command-line-args 5.2.1
command-line-usage 6.1.3
current-git-branch 1.1.0
prompts 2.4.2

gitbranchvalidator's People

Contributors

lcluber avatar

Watchers

 avatar

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.