Skip to content

Commit f094393

Browse files
committed
feat!: use builtin vim.net.request instead of curl, needs nvim 0.12
1 parent d108d5c commit f094393

3 files changed

Lines changed: 79 additions & 65 deletions

File tree

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,16 @@ Bundle of commands focused on swift and streamlined git operations.
7575

7676
## Installation
7777
**Requirements** <!-- rumdl-disable-line MD036 -->
78-
- nvim 0.10+
78+
- nvim 0.12+
7979
- A plugin implementing `vim.ui.select`, such as:
8080
- [snacks.picker](http://github.com/folke/snacks.nvim)
8181
- [mini.pick](http://github.com/echasnovski/mini.pick)
8282
- [telescope.nvim](https://github.com/nvim-telescope/telescope.nvim) with
83-
[telescope-ui-select](https://github.com/nvim-telescope/telescope-ui-select.nvim)
83+
[telescope-ui-select](https://github.com/nvim-telescope/telescope-ui-select.nvim)
8484
- [fzf-lua](https://github.com/ibhagwan/fzf-lua)
8585
- For interactive staging:
8686
[telescope.nvim](https://github.com/nvim-telescope/telescope.nvim). (For
8787
`snacks.nvim`, the `git_diff` picker allows interactive staging.)
88-
- For GitHub-related commands: `curl`
8988
- *Recommended*: Treesitter parser for syntax highlighting `TSInstall
9089
gitcommit`.
9190

@@ -349,7 +348,6 @@ require("tinygit").undoLastCommitOrAmend()
349348
### GitHub interaction
350349

351350
#### Search issues & PRs
352-
- All GitHub interaction commands require `curl`.
353351

354352
```lua
355353
-- state: all|closed|open (default: all)

lua/tinygit/commands/github.lua

Lines changed: 72 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -81,77 +81,90 @@ function M.issuesAndPrs(opts)
8181
if u.notInGitRepo() then return end
8282
local repo = M.getGithubRemote()
8383
if not repo then return end
84-
if vim.fn.executable("curl") == 0 then
85-
u.notify("`curl` cannot be found.", "warn")
86-
return
87-
end
8884

8985
local defaultOpts = { state = "all", type = "all" }
9086
opts = vim.tbl_deep_extend("force", defaultOpts, opts or {})
9187

9288
-- DOCS https://docs.github.com/en/free-pro-team@latest/rest/issues/issues?apiVersion=2022-11-28#list-repository-issues
9389
local baseUrl = ("https://api.github.com/repos/%s/issues"):format(repo)
9490
local rawJsonUrl = baseUrl .. ("?per_page=100&state=%s&sort=updated"):format(opts.state)
95-
local rawJSON = vim.system({ "curl", "-sL", rawJsonUrl }):wait().stdout or ""
96-
local issues = vim.json.decode(rawJSON)
97-
if not issues then
98-
u.notify("Failed to fetch issues.", "warn")
99-
return
100-
end
101-
if issues and opts.type ~= "all" then
102-
issues = vim.tbl_filter(function(issue)
103-
local isPR = issue.pull_request ~= nil
104-
local isRightKind = (isPR and opts.type == "pr") or (not isPR and opts.type == "issue")
105-
return isRightKind
106-
end, issues)
107-
end
91+
vim.net.request(
92+
rawJsonUrl,
93+
{},
94+
vim.schedule_wrap(function(err, res)
95+
if err then
96+
u.notify(("Failed to fetch issues: %s"):format(err), "warn")
97+
return
98+
end
99+
local issues = vim.json.decode(res.body)
100+
if not issues then
101+
u.notify("Failed to decode GitHub response.", "warn")
102+
return
103+
end
108104

109-
if #issues == 0 then
110-
local state = opts.state == "all" and "" or opts.state .. " "
111-
local type = opts.type == "all" and "issues or PRs " or opts.type .. "s "
112-
local msg = ("There are no %s%sfor this repo."):format(state, type)
113-
u.notify(msg, "warn")
114-
return
115-
end
105+
if opts.type ~= "all" then
106+
issues = vim.tbl_filter(function(issue)
107+
local isPR = issue.pull_request ~= nil
108+
local isRightKind = (isPR and opts.type == "pr")
109+
or (not isPR and opts.type == "issue")
110+
return isRightKind
111+
end, issues)
112+
end
116113

117-
local type = opts.type == "all" and "Issue/PR" or opts.type
118-
local mainIcon = require("tinygit.config").config.appearance.mainIcon
119-
local prompt = vim.trim(("%s Select %s (%s)"):format(mainIcon, type, opts.state))
120-
local onChoice = function(choice) vim.ui.open(choice.html_url) end
121-
local stylingFunc = function()
122-
local highlights = require("tinygit.shared.highlights")
123-
highlights.commitType()
124-
highlights.inlineCodeAndIssueNumbers()
125-
vim.fn.matchadd("DiagnosticError", [[\v[Bb]ug]])
126-
vim.fn.matchadd("DiagnosticInfo", [[\v[Ff]eature [Rr]equest|FR]])
127-
vim.fn.matchadd("Comment", [[\vby [A-Za-z0-9-]+\s*$]])
128-
end
129-
local function issueListFormatter(issue)
130-
local icons = require("tinygit.config").config.github.icons
131-
local icon
132-
if issue.pull_request then
133-
if issue.draft then
134-
icon = icons.draftPR
135-
elseif issue.state == "open" then
136-
icon = icons.openPR
137-
elseif issue.pull_request.merged_at then
138-
icon = icons.mergedPR
139-
else
140-
icon = icons.closedPR
114+
if #issues == 0 then
115+
local state = opts.state == "all" and "" or opts.state .. " "
116+
local type = opts.type == "all" and "issues or PRs " or opts.type .. "s "
117+
local msg = ("There are no %s%sfor this repo."):format(state, type)
118+
u.notify(msg, "warn")
119+
return
141120
end
142-
else
143-
if issue.state == "open" then
144-
icon = icons.openIssue
145-
elseif issue.state_reason == "completed" then
146-
icon = icons.closedIssue
147-
elseif issue.state_reason == "not_planned" then
148-
icon = icons.notPlannedIssue
121+
122+
local type = opts.type == "all" and "Issue/PR" or opts.type
123+
local mainIcon = require("tinygit.config").config.appearance.mainIcon
124+
local prompt = vim.trim(("%s Select %s (%s)"):format(mainIcon, type, opts.state))
125+
local onChoice = function(choice) vim.ui.open(choice.html_url) end
126+
local stylingFunc = function()
127+
local highlights = require("tinygit.shared.highlights")
128+
highlights.commitType()
129+
highlights.inlineCodeAndIssueNumbers()
130+
vim.fn.matchadd("DiagnosticError", [[\v[Bb]ug]])
131+
vim.fn.matchadd("DiagnosticInfo", [[\v[Ff]eature [Rr]equest|FR]])
132+
vim.fn.matchadd("Comment", [[\vby [A-Za-z0-9-]+\s*$]])
133+
end
134+
local function issueListFormatter(issue)
135+
local icons = require("tinygit.config").config.github.icons
136+
local icon
137+
if issue.pull_request then
138+
if issue.draft then
139+
icon = icons.draftPR
140+
elseif issue.state == "open" then
141+
icon = icons.openPR
142+
elseif issue.pull_request.merged_at then
143+
icon = icons.mergedPR
144+
else
145+
icon = icons.closedPR
146+
end
147+
else
148+
if issue.state == "open" then
149+
icon = icons.openIssue
150+
elseif issue.state_reason == "completed" then
151+
icon = icons.closedIssue
152+
elseif issue.state_reason == "not_planned" then
153+
icon = icons.notPlannedIssue
154+
end
155+
end
156+
return ("%s #%s %s by %s"):format(icon, issue.number, issue.title, issue.user.login)
149157
end
150-
end
151-
return ("%s #%s %s by %s"):format(icon, issue.number, issue.title, issue.user.login)
152-
end
153158

154-
require("tinygit.shared.picker").pick(prompt, issues, issueListFormatter, stylingFunc, onChoice)
159+
require("tinygit.shared.picker").pick(
160+
prompt,
161+
issues,
162+
issueListFormatter,
163+
stylingFunc,
164+
onChoice
165+
)
166+
end)
167+
)
155168
end
156169

157170
function M.openIssueUnderCursor()

lua/tinygit/init.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
local version = vim.version()
2-
if version.major == 0 and version.minor < 10 then
3-
vim.notify("tinygit requires at least nvim 0.10.", vim.log.levels.WARN)
2+
if version.major == 0 and version.minor < 12 then
3+
local msg = "nvim-scissors requires at least nvim 0.12.\n"
4+
.. "The latest commit supporting nvim 0.11 is d108d5c."
5+
vim.notify(msg, vim.log.levels.WARN)
46
return
57
end
68
--------------------------------------------------------------------------------
9+
710
local M = {}
811

912
---@param userConfig? Tinygit.Config

0 commit comments

Comments
 (0)