https://github.com/piyook/validate-branch-name
A simple bash script to validate local git branch name
https://github.com/piyook/validate-branch-name
bash-script git husky
Last synced: about 2 months ago
JSON representation
A simple bash script to validate local git branch name
- Host: GitHub
- URL: https://github.com/piyook/validate-branch-name
- Owner: piyook
- Created: 2024-12-10T10:09:31.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-11T14:06:07.000Z (over 1 year ago)
- Last Synced: 2025-02-10T03:29:32.809Z (over 1 year ago)
- Topics: bash-script, git, husky
- Language: Shell
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Validate Git Branch Name
A simple bash script to validate local git branch name without the need to install packages or dependencies.
## Using
Copy validate-branch-name.sh bash script to project root and add the following to package.json
```bash
"validate-branch-name": "bash validate-branch-name.sh"
```
The script can be run from the command line with
```
npm run validate-branch-name
```
Combine with Husky to check branch names before committing or pushing to GitHub.
For example in '.husky/pre-push':
```bash
npm run validate-branch-name
npm audit --omit=dev
```
**Alternatively**, the validation script can be copied and pasted directly into the relevant husky hook script and executed directly.
The default acceptable branch pattern is:
```bash
feat|fix|hotfix|release|chore/your-branch-name
```
So **'feat/test-branch'** will pass validation but **'test-branch'** will fail.
Note that **'main'** and **'dev'** branch names bypass checks as they are 'standard' branch names.
## Customisation
The RegEx can be modified to fit any branch naming practice that is being used.
Replace default REGEX value with custom regex (some examples below).
Replace default STANDARD_BRANCHES value with names of standard branches names that avoid regex checks (E.g 'main|working')
```
REGEX='^(feat|fix|hotfix|release|chore)/.'
STANDARD_BRANCHES='main|dev'
```
Examples:
- branch name has to start with 'feature/', 'patch/', 'hotfix/' or 'release/' E.g 'hotfix/urgent'
```
REGEX='^(feat|patch|hotfix|release)/.+'
```
- branch name needs to be in the format 'feat/JIRA-1234'
```
REGEX='(feat|release|hotfix)/(JIRA-\d+)'
```
- branch name needs to be in the format 'feature/branch-name' OR include JIRA's code like 'feat/JIRA-1234/branch-name'
```
REGEX='(feat|release|hotfix)/(JIRA-\d+/)?[a-z-]+'
```
## Error Message
Change or update the error message if custom regex is used. E.g
```
ERROR_MESSAGE="branch name needs to be in the format 'feat/JIRA-1234'"
```