Skip to content

Commit 101e5b9

Browse files
committed
refactor: githubUrl code
1 parent f33a7fb commit 101e5b9

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,12 @@ require("tinygit").openIssueUnderCursor()
196196
```
197197

198198
**GitHub URL**
199-
Opens the current file at GitHub in the browser and copy the URL to the system
200-
clipboard.
201-
- Normal mode: open the current file or repo.
202-
- Visual mode: open selected lines.
199+
Creates a permalink to the current file/lines at GitHub. The link is opened in
200+
the browser and copied to the system clipboard.
201+
202+
- Normal mode: current file
203+
- Visual mode: selected lines
204+
- Using `"repo"` as argument, will just the repo URL instead
203205

204206
```lua
205207
-- file|repo (default: file)

lua/tinygit/commands/github.lua

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,25 @@ function M.githubUrl(what)
3434
local pathInRepoEncoded = pathInRepo:gsub("%s+", "%%20")
3535

3636
local repo = M.getGithubRemote()
37-
if not repo then return end
37+
if not repo then return end -- not on github
3838
local hash = u.syncShellCmd { "git", "rev-parse", "HEAD" }
39-
local branch = u.syncShellCmd { "git", "branch", "--show-current" }
40-
-- if on branch, uses branch, if detached HEAD, falls back to HEAD's hash
41-
if branch == "" then branch = hash end
42-
43-
local mode = vim.fn.mode()
4439
local url = "https://github.com/" .. repo
40+
local location = ""
4541

46-
if what == "file" and mode == "n" then
47-
url = url .. ("/blob/%s/%s"):format(hash, pathInRepoEncoded)
48-
elseif what == "file" and mode:find("[Vv]") then
42+
local mode = vim.fn.mode()
43+
if what == "file" and mode:find("[Vv]") then
4944
vim.cmd.normal { mode, bang = true } -- leave visual mode, so marks are set
5045
local startLn = vim.api.nvim_buf_get_mark(0, "<")[1]
5146
local endLn = vim.api.nvim_buf_get_mark(0, ">")[1]
52-
local location
5347
if startLn == endLn then -- one-line-selection
54-
location = "#L" .. tostring(startLn)
48+
location = "#L" .. startLn
5549
elseif startLn < endLn then
56-
location = "#L" .. tostring(startLn) .. "-L" .. tostring(endLn)
50+
location = "#L" .. startLn .. "-L" .. endLn
5751
else
58-
location = "#L" .. tostring(endLn) .. "-L" .. tostring(startLn)
52+
location = "#L" .. endLn .. "-L" .. startLn
5953
end
54+
end
55+
if what == "file" then
6056
url = url .. ("/blob/%s/%s%s"):format(hash, pathInRepoEncoded, location)
6157
end
6258

0 commit comments

Comments
 (0)