Skip to content

Commit

Permalink
feat(smartCommit): option to open references issue afterwards
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Sep 27, 2023
1 parent 18864a8 commit 34bbeff
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ Optionally, install the Treesitter parser for git commits for some syntax highli

```lua
-- Open a commit popup. If there are no staged changes, stage all changes (`git add -A`) before the commit.
-- Right now, only supports the commit subject line. Optionally runs `git push` afterwards.
-- Right now, only supports the commit subject line. Optionally runs `git push` afterwards or opens references issues in the browser.
-- 💡 Use gitsigns.nvim's `add_hunk` command to conveniently stage changes.
-- 💡 To use vim commands in the input field, set dressing.nvim's `insert_only` to `false`.
require("tinygit").smartCommit({ push = false }) -- options default to `false`
require("tinygit").smartCommit({ push = false, openReferencedIssue = false }) -- options default to `false`

-- Quick amends.
-- `amendOnlyMsg` just opens the commit popup to change the last commit message, and does not stage any changes.
Expand Down
17 changes: 13 additions & 4 deletions lua/tinygit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ local function setGitCommitAppearance()
})
end

local function getRepo()
return fn.system("git remote -v | head -n1"):match(":.*%."):sub(2, -2)
end

--------------------------------------------------------------------------------

---@param opts? { forcePush?: boolean }
Expand Down Expand Up @@ -251,7 +255,7 @@ end
---If there are staged changes, commit them.
---If there aren't, add all changes (`git add -A`) and then commit.
---@param prefillMsg? string
---@param opts? { push?: boolean }
---@param opts? { push?: boolean, openReferencedIssue?: boolean }
function M.smartCommit(opts, prefillMsg)
if notInGitRepo() then return end

Expand Down Expand Up @@ -279,6 +283,12 @@ function M.smartCommit(opts, prefillMsg)
local notifyText = table.concat(body, "\n" .. notifySeperator .. "\n")
notify(notifyText, "info", "Smart-Commit")

if opts.openReferencedIssue then
local issueReferenced = processedMsg:match("#(%d+)")
local url = ("https://github.com/%s/issues/%s"):format(getRepo(), issueReferenced)
openUrl(url)
end

if opts.push then M.push { pullBefore = true } end
end)
end
Expand Down Expand Up @@ -406,11 +416,10 @@ end
---@param userOpts? { state?: string, type?: string }
function M.issuesAndPrs(userOpts)
if notInGitRepo() then return end
if not userOpts then userOpts = {} end
local defaultOpts = { state = "all", type = "all" }
local opts = vim.tbl_deep_extend("force", defaultOpts, userOpts)
local opts = vim.tbl_deep_extend("force", defaultOpts, userOpts or {})

local repo = fn.system("git remote -v | head -n1"):match(":.*%."):sub(2, -2)
local repo = getRepo()

-- DOCS https://docs.github.com/en/free-pro-team@latest/rest/issues/issues?apiVersion=2022-11-28#list-repository-issues
local rawJsonUrl = ("https://api.github.com/repos/%s/issues?per_page=100&state=%s&sort=updated"):format(
Expand Down
1 change: 0 additions & 1 deletion lua/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
---@field maxLen number
---@field mediumLen number
---@field emptyFillIn string
---@field openReferencesIssues boolean
---@field enforceConvCommits enforceConvCommitsConfig

---@class enforceConvCommitsConfig
Expand Down

0 comments on commit 34bbeff

Please sign in to comment.