npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, πŸ‘‹, I’m Ryan HefnerΒ  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you πŸ™

Β© 2025 – Pkg Stats / Ryan Hefner

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.

Readme

commit-from-branch

⚠️ Package Migration Notice This package was previously published as @253eosam/commit-from-branch. Please use commit-from-branch instead. The scoped package is deprecated.

NPM Version NPM Downloads GitHub Release License

CI Status GitHub Pages Node.js TypeScript

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

πŸš€ Live Demo - Try the interactive demo to see how commit message templating works!

πŸ“¦ Installation

npm install commit-from-branch --save-dev

Note: This package requires Husky v9 as a peer dependency. npm will automatically warn you if it's not installed.

πŸš€ Quick Start

  1. Install Husky v9 (if not already installed):
npm install --save-dev husky@^9.0.0
npx husky init
  1. Setup the hook:
npx cfb init
  1. Configure in package.json (optional):
{
  "commitFromBranch": {
    "format": "[${ticket}] ${msg}",
    "fallbackFormat": "[${seg0}] ${msg}",
    "includePattern": ["feature/*", "bugfix/*"]
  }
}
  1. 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

  1. Hook Integration: Integrates with Husky's prepare-commit-msg hook
  2. Branch Detection: Gets current branch name using git rev-parse --abbrev-ref HEAD
  3. Pattern Matching: Checks if branch matches include patterns and isn't excluded
  4. Ticket Extraction: Uses regex /([A-Z]+-\d+)/i to find Jira-style tickets
  5. Template Rendering: Replaces placeholders with actual values
  6. Message Formatting: Updates commit message file with formatted result

πŸ“‹ Requirements

  • Node.js >= 16
  • Git repository
  • Husky v9 (peer dependency)

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/my-feature
  3. Make your changes and add tests
  4. Run the build: npm run build
  5. Commit your changes: git commit -m "add my feature"
  6. Push to the branch: git push origin feature/my-feature
  7. Submit a pull request

πŸ”— Links

Package & Releases

Development & Community

Documentation

πŸ“„ License

MIT Β© 253eosam


⬆ Back to Top

Made with ❀️ by 253eosam