commit-from-branch
v0.4.7
Published
π Flexible commit message templating from branch name (ticket/segments) for Husky's prepare-commit-msg hook. Automatically extracts Jira-style tickets and supports customizable templates.
Maintainers
Readme
commit-from-branch
β οΈ Package Migration Notice This package was previously published as
@253eosam/commit-from-branch. Please usecommit-from-branchinstead. The scoped package is deprecated.
Flexible commit message templating from branch name (ticket/segments) for Husky's prepare-commit-msg hook.
Automatically formats your commit messages based on your branch name, extracting Jira-style tickets (e.g., ABC-123) and supporting flexible templating with branch segments.
Features
- π― Automatic ticket extraction - Finds Jira-style tickets (ABC-123) in branch names
- π§ Flexible templating - Customizable commit message formats with placeholders
- πΏ Branch segment support - Access individual parts of your branch path
- β‘ Zero configuration - Works out of the box with sensible defaults
- πͺ Husky integration - Easy setup with Husky's prepare-commit-msg hook
- π¨ Pattern matching - Include/exclude branches with glob patterns
- π Fast & lightweight - Minimal dependencies, TypeScript-based
π Table of Contents
- Demo
- Installation
- Quick Start
- Configuration
- Template Variables
- Examples
- Debug Mode
- Programmatic Usage
- How It Works
- Requirements
- Contributing
- Links
π Demo
π Live Demo - Try the interactive demo to see how commit message templating works!
π¦ Installation
npm install commit-from-branch --save-devNote: This package requires Husky v9 as a peer dependency. npm will automatically warn you if it's not installed.
π Quick Start
- Install Husky v9 (if not already installed):
npm install --save-dev husky@^9.0.0
npx husky init- Setup the hook:
npx cfb init- Configure in package.json (optional):
{
"commitFromBranch": {
"format": "[${ticket}] ${msg}",
"fallbackFormat": "[${seg0}] ${msg}",
"includePattern": ["feature/*", "bugfix/*"]
}
}- Create a branch and commit:
git checkout -b feature/ABC-123-add-login
git add .
git commit -m "implement user authentication"
# Result: "[ABC-123] implement user authentication"βοΈ Configuration
Add configuration to your package.json:
{
"commitFromBranch": {
"format": "[${ticket}] ${msg}",
"fallbackFormat": "[${seg0}] ${msg}",
"includePattern": ["*"],
"exclude": ["merge", "squash", "revert"]
}
}Configuration Options
| Option | Type | Default | Description |
| ---------------- | -------------------- | ------------------------------- | ------------------------------------- |
| format | string | "[${ticket}] ${msg}" | Template when ticket is found |
| fallbackFormat | string | "[${seg0}] ${msg}" | Template when no ticket found |
| includePattern | string \| string[] | ["*"] | Glob patterns for branches to include |
| exclude | string[] | ["merge", "squash", "revert"] | Commit sources to exclude |
π§ Template Variables
Use these placeholders in your format templates:
| Variable | Description | Example |
| -------------------------- | --------------------------------- | ------------------------------------------- |
| ${ticket} | Extracted ticket (ABC-123 format) | ABC-123 |
| ${branch} | Full branch name | feature/ABC-123-add-login |
| ${seg0}, ${seg1}, etc. | Branch segments split by / | feature, ABC-123-add-login |
| ${segments} | All segments joined with / | feature/ABC-123-add-login |
| ${prefix:n} | First n segments joined with / | ${prefix:2} β feature/ABC-123-add-login |
| ${msg} | Original commit message | implement user authentication |
| ${body} | Full original commit body | Multi-line commit content |
π‘ Examples
Basic Ticket Extraction
# Branch: feature/ABC-123-user-login
# Commit: git commit -m "add login form"
# Result: "[ABC-123] add login form"Fallback Format (No Ticket)
# Branch: feature/user-dashboard
# Commit: git commit -m "create dashboard"
# Result: "[feature] create dashboard"Custom Formatting
{
"commitFromBranch": {
"format": "${ticket}: ${msg}",
"fallbackFormat": "${seg0}/${seg1}: ${msg}"
}
}# Branch: bugfix/payment/ABC-456-fix-checkout
# Result: "ABC-456: fix payment processing"Pattern Matching
{
"commitFromBranch": {
"includePattern": ["feature/*", "bugfix/*", "hotfix/*"],
"exclude": ["merge", "squash"]
}
}Advanced Templates
{
"commitFromBranch": {
"format": "[${ticket}] ${seg0}: ${msg}",
"fallbackFormat": "[${prefix:2}] ${msg}"
}
}π Debug Mode
Enable debug logging:
BRANCH_PREFIX_DEBUG=1 git commit -m "test message"π§ͺ Dry Run Mode
Test without modifying commit messages:
BRANCH_PREFIX_DRYRUN=1 git commit -m "test message"π Programmatic Usage
import { run } from "commit-from-branch";
// Use with custom options
const exitCode = run({
argv: ["node", "script.js", "/path/to/COMMIT_EDITMSG"],
env: process.env,
cwd: "/path/to/repo",
});import { initHusky } from "commit-from-branch/init";
// Setup Husky hook programmatically
initHusky("/path/to/repo");β‘ How It Works
- Hook Integration: Integrates with Husky's
prepare-commit-msghook - Branch Detection: Gets current branch name using
git rev-parse --abbrev-ref HEAD - Pattern Matching: Checks if branch matches include patterns and isn't excluded
- Ticket Extraction: Uses regex
/([A-Z]+-\d+)/ito find Jira-style tickets - Template Rendering: Replaces placeholders with actual values
- Message Formatting: Updates commit message file with formatted result
π Requirements
- Node.js >= 16
- Git repository
- Husky v9 (peer dependency)
π€ Contributing
- Fork the repository
- Create a feature branch:
git checkout -b feature/my-feature - Make your changes and add tests
- Run the build:
npm run build - Commit your changes:
git commit -m "add my feature" - Push to the branch:
git push origin feature/my-feature - Submit a pull request
π Links
Package & Releases
- π¦ NPM Package - Official NPM package
- π GitHub Releases - Release notes and downloads
- π Live Demo - Interactive demo
Development & Community
- π» Source Code - GitHub repository
- π Report Issues - Bug reports and feature requests
- π CI/CD Pipeline - Build status and automation
- π NPM Stats - Download statistics
Documentation
- π API Documentation - Programmatic usage guide
- βοΈ Configuration Guide - Setup and customization
- π§ Template Variables - Available placeholders
π License
MIT Β© 253eosam
Made with β€οΈ by 253eosam
