A Neovim plugin that automatically prefixes git commit messages based on branch naming conventions.
- 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
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.
use {
"TheOrangePuff/nvim-commit-prefix",
config = function()
require("commit-prefix").setup()
end
}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,
})With default configuration:
feat DO-1984:
fix PROJ-123:
With no ticket in the branch name:
chore:
require("commit-prefix").setup({
format = "{ticket}: {type} - ",
format_no_ticket = "{type} - ",
})Branch: feature/DO-1984-new-feature
DO-1984: feat -
require("commit-prefix").setup({
scope = {
enabled = true,
value = "api", -- Static scope
},
format_with_scope = "{type}({scope}): {ticket} ",
})Result:
feat(api): DO-1984
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 },
})Initialise the plugin with your configuration.
Manually apply the commit prefix. Useful for custom keybindings.
Get the current git branch name.
Get parsed branch information including type, ticket, and scope.
Generate the prefix string without inserting it.
Control the plugin's enabled state at runtime.
- When you open a git commit message (
FileType gitcommit), the plugin activates - It retrieves the current git branch name
- Parses the branch to extract:
- Branch type (text before the first
/) - Ticket number (using configured patterns)
- Scope (if enabled)
- Branch type (text before the first
- Maps the branch type to a commit prefix
- Formats the prefix string using your configured format
- Inserts the prefix at the beginning of line 1
MIT