Skip to content

TheOrangePuff/nvim-commit-prefix

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

nvim-commit-prefix

A Neovim plugin that automatically prefixes git commit messages based on branch naming conventions.

Features

  • Automatic commit message prefixes based on branch type (feature, fix, etc.)
  • Ticket number extraction from branch names (JIRA, GitHub issues)
  • Customisable format strings
  • Support for conventional commit scopes
  • Custom rules for advanced transformations

Installation

lazy.nvim

Add to your plugin specs (e.g. lua/plugins/commit-prefix.lua):

return {
  "TheOrangePuff/nvim-commit-prefix",
  ft = "gitcommit",
  opts = {},
}

Or with custom configuration:

return {
  "TheOrangePuff/nvim-commit-prefix",
  ft = "gitcommit",
  opts = {
    format = "{ticket}: {type} - ",
    fallback_type = "chore",
  },
}

After adding the spec, run :Lazy sync to install.

packer.nvim

use {
  "TheOrangePuff/nvim-commit-prefix",
  config = function()
    require("commit-prefix").setup()
  end
}

Configuration

require("commit-prefix").setup({
  enabled = true,

  -- Branch type to commit prefix mapping
  branch_mappings = {
    feature = "feat",
    feat = "feat",
    fix = "fix",
    bugfix = "fix",
    hotfix = "fix",
    chore = "chore",
    docs = "docs",
    test = "test",
    ci = "ci",
  },

  -- Fallback when branch type is unrecognised (nil = no prefix)
  fallback_type = nil,

  -- Patterns to extract ticket numbers from branch name
  ticket_patterns = {
    "([A-Z]+-[0-9]+)",  -- JIRA style: DO-1984, PROJ-123
    "([A-Z]+_[0-9]+)",  -- Underscore style: DO_1984
    "#([0-9]+)",        -- GitHub style: #123
  },

  -- Scope configuration (off by default)
  scope = {
    enabled = false,
    value = nil,          -- Static scope value
    pattern = nil,        -- Pattern to extract scope from branch
  },

  -- Output format (placeholders: {type}, {ticket}, {scope}, {branch})
  format = "{type} {ticket}: ",

  -- Format when no ticket found
  format_no_ticket = "{type}: ",

  -- Format with scope enabled
  format_with_scope = "{type}({scope}) {ticket}: ",

  -- Custom rules (functions that receive branch_info and return additions)
  rules = {},

  -- Only prefix if line 1 is empty
  only_if_empty = true,
})

Examples

Branch: feature/DO-1984-reduce-cost

With default configuration:

feat DO-1984:

Branch: fix/PROJ-123-login-bug

fix PROJ-123:

Branch: chore/update-deps

With no ticket in the branch name:

chore:

Custom Format

require("commit-prefix").setup({
  format = "{ticket}: {type} - ",
  format_no_ticket = "{type} - ",
})

Branch: feature/DO-1984-new-feature

DO-1984: feat -

With Scope

require("commit-prefix").setup({
  scope = {
    enabled = true,
    value = "api",  -- Static scope
  },
  format_with_scope = "{type}({scope}): {ticket} ",
})

Result:

feat(api): DO-1984

Custom Rules

require("commit-prefix").setup({
  rules = {
    -- Extract scope from the part after ticket
    function(info)
      local scope = info.branch_name:match("[A-Z]+-[0-9]+%-([^%-]+)")
      return { scope = scope }
    end,
  },
  scope = { enabled = true },
})

API

require("commit-prefix").setup(opts)

Initialise the plugin with your configuration.

require("commit-prefix").apply()

Manually apply the commit prefix. Useful for custom keybindings.

require("commit-prefix").get_branch()

Get the current git branch name.

require("commit-prefix").get_branch_info()

Get parsed branch information including type, ticket, and scope.

require("commit-prefix").get_prefix()

Generate the prefix string without inserting it.

require("commit-prefix").enable() / disable() / toggle()

Control the plugin's enabled state at runtime.

How It Works

  1. When you open a git commit message (FileType gitcommit), the plugin activates
  2. It retrieves the current git branch name
  3. Parses the branch to extract:
    • Branch type (text before the first /)
    • Ticket number (using configured patterns)
    • Scope (if enabled)
  4. Maps the branch type to a commit prefix
  5. Formats the prefix string using your configured format
  6. Inserts the prefix at the beginning of line 1

Licence

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages