Skip to content

Commit 2281cab

Browse files
CKolkeychrishrb
andauthored
Add handler for git commit hash (#15)
* Add handler for git commit hash * Handle both long (40) char hashes and short (7) char hashes. * Handle commit hashes of any length between 7 and 40 characters. * Add spec for commit handler * Run stylua * Update test/spec/gx/handlers/commit_handler_spec.lua Co-authored-by: Christoph <52382992+chrishrb@users.noreply.github.com> * Update test/spec/gx/handlers/commit_handler_spec.lua Co-authored-by: Christoph <52382992+chrishrb@users.noreply.github.com> --------- Co-authored-by: Christoph <52382992+chrishrb@users.noreply.github.com>
1 parent 991b6ac commit 2281cab

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

lua/gx/handler.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local package_json_handler = require("gx.handlers.package_json")
33
local plugin_handler = require("gx.handlers.plugin")
44
local url_handler = require("gx.handlers.url")
55
local github_handler = require("gx.handlers.github")
6+
local commit_handler = require("gx.handlers.commit")
67
local markdown_handler = require("gx.handlers.markdown")
78

89
local M = {}
@@ -28,6 +29,7 @@ function M.get_url(mode, line, activated_handlers)
2829
add_handler(handlers, package_json_handler, activated_handlers.package_json)
2930
add_handler(handlers, plugin_handler, activated_handlers.plugin)
3031
add_handler(handlers, github_handler, activated_handlers.github)
32+
add_handler(handlers, commit_handler, activated_handlers.github)
3133
add_handler(handlers, markdown_handler, true)
3234
add_handler(handlers, url_handler, true)
3335
-- ###

lua/gx/handlers/commit.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
local helper = require("gx.helper")
2+
local git = require("gx.git")
3+
4+
local M = {}
5+
6+
-- every filetype and filename
7+
M.filetype = nil
8+
M.filename = nil
9+
10+
-- navigate to github url for commit
11+
function M.handle(mode, line)
12+
local pattern = "(%x%x%x%x%x%x%x+)"
13+
local commit_hash = helper.find(line, mode, pattern)
14+
if not commit_hash or #commit_hash > 40 then
15+
return
16+
end
17+
local git_url = git.get_remote_url()
18+
if not git_url then
19+
return
20+
end
21+
return git_url .. "/commit/" .. commit_hash
22+
end
23+
24+
return M
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
local handler = require("gx.handlers.commit")
2+
3+
describe("commit_handler_does_work", function()
4+
it("doesn't see a hash with < 7 characters", function()
5+
assert.is_nil(handler.handle("v", "1a2b3c"))
6+
end)
7+
8+
it("does see a hash with 7 characters", function()
9+
assert.equals(
10+
"https://github.com/chrishrb/gx.nvim/commit/1a2b3c4",
11+
handler.handle("v", "1a2b3c4")
12+
)
13+
end)
14+
15+
it("does see a hash with 10 characters", function()
16+
assert.equals(
17+
"https://github.com/chrishrb/gx.nvim/commit/1a2b3c4d5f",
18+
handler.handle("v", "1a2b3c4d5f")
19+
)
20+
end)
21+
22+
it("does see a hash with 40 characters", function()
23+
local hash = "1a2b3c4d5f1a2b3c4d5f1a2b3c4d5f1a2b3c4d5f"
24+
assert.equals("https://github.com/chrishrb/gx.nvim/commit/" .. hash, handler.handle("v", hash))
25+
end)
26+
27+
it("doesn't see a hash with > 40 characters", function()
28+
assert.is_nil(handler.handle("v", "1a2b3c4d5f1a2b3c4d5f1a2b3c4d5f1a2b3c4d5fa"))
29+
end)
30+
end)

0 commit comments

Comments
 (0)