Skip to content

Commit ca5ba0b

Browse files
committed
feat(commitMsg): new config commit.subject.autoFormat
1 parent 0b5f997 commit ca5ba0b

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ require("tinygit").setup {
146146
insert = { confirm = "<C-CR>" },
147147
},
148148
subject = {
149+
-- automatically apply formatting to the subject line
150+
autoFormat = function(subject) ---@type nil|fun(subject: string): string
151+
subject = subject:gsub("%.$", "") -- remove trailing dot https://commitlint.js.org/reference/rules.html#body-full-stop
152+
return subject
153+
end,
154+
155+
-- disallow commits that do not use an allowed type
149156
enforceType = false,
150157
-- stylua: ignore
151158
types = {

lua/tinygit/commands/commit/msg-input.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@ local function setupKeymaps(confirmationCallback)
4949

5050
local function confirm()
5151
-- TITLE
52-
local commitTitle = vim.trim(vim.api.nvim_buf_get_lines(bufnr, 0, 1, false)[1])
53-
:gsub("%.$", "") -- no trailing dot https://commitlint.js.org/reference/rules.html#body-full-stop
54-
if #commitTitle > MAX_TITLE_LEN then
55-
warn("Title is too long.")
52+
local commitSubject = vim.trim(vim.api.nvim_buf_get_lines(bufnr, 0, 1, false)[1])
53+
if conf.subject.autoFormat then commitSubject = conf.subject.autoFormat(commitSubject) end
54+
if #commitSubject > MAX_TITLE_LEN then
55+
warn("Subject is too long.")
5656
return
5757
end
58-
if #commitTitle == 0 then
59-
warn("Title is empty.")
58+
if #commitSubject == 0 then
59+
warn("Subject is empty.")
6060
return
6161
end
6262
if conf.subject.enforceType then
63-
local firstWord = commitTitle:match("^%w+")
63+
local firstWord = commitSubject:match("^%w+")
6464
if not vim.tbl_contains(conf.subject.types, firstWord) then
6565
local msg = "Not using a type allowed by the config `commit.subject.types`. "
6666
.. "(Alternatively, you can also disable `commit.subject.enforceType`.)"
@@ -89,7 +89,7 @@ local function setupKeymaps(confirmationCallback)
8989
state.abortedCommitMsg[cwd] = nil
9090

9191
-- confirm and close
92-
confirmationCallback(commitTitle, commitBody)
92+
confirmationCallback(commitSubject, commitBody)
9393
vim.cmd.bwipeout(bufnr)
9494
end
9595

lua/tinygit/config.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@ local defaultConfig = {
3434
insert = { confirm = "<C-CR>" },
3535
},
3636
subject = {
37-
enforceType = false, -- disallow commits that do not use an allowed type
37+
-- automatically apply formatting to the subject line
38+
autoFormat = function(subject) ---@type nil|fun(subject: string): string
39+
subject = subject:gsub("%.$", "") -- remove trailing dot https://commitlint.js.org/reference/rules.html#body-full-stop
40+
return subject
41+
end,
42+
43+
-- disallow commits that do not use an allowed type
44+
enforceType = false,
3845
-- stylua: ignore
3946
types = {
4047
"fix", "feat", "chore", "docs", "refactor", "build", "test",

0 commit comments

Comments
 (0)