Skip to content

Commit

Permalink
feat: generate repo's homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
ruifm committed Jul 6, 2021
1 parent 9123be8 commit 6a59e9c
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 91 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ In your `init.lua` or in a lua-here-doc in your `init.vim`:
require"gitlinker".setup()
```

### buffer url with (optional line range)

**By default, the following mappings are defined:**

- `<leader>gy` for normal and visual mode
Expand Down Expand Up @@ -93,6 +95,20 @@ looking for is `require"gitlinker".get_buf_range_url(mode, user_opts)` where:
vim.api.nvim_set_keymap('v', '<leader>gb', ':lua require"gitlinker".get_buf_range_url("v", {action_callback = require"gitlinker.actions".open_in_browser})<cr>')
```

### Repo home page url

For convenience, the function
`require"gitlinker".get_buf_range_url(mode, user_opts)` allows one to generate
the url for the repository homepage. You can map it like so:

``` lua
vim.api.nvim_set_keymap('n', '<leader>gY', '<cmd>lua require"gitlinker".get_repo_url()<cr>', {silent = true})
vim.api.nvim_set_keymap('n', '<leader>gB', '<cmd>lua require"gitlinker".get_repo_url({action_callback = require"gitlinker.actions".open_in_browser})<cr>', {silent = true})
```

And use `<leader>gY` to copy the repo's homepage to your clipboard or
`<leader>gB` to open it in your browser.

## Configuration

To configure `gitlinker.nvim`, call `require"gitlinker".setup(config)` in your
Expand Down
17 changes: 17 additions & 0 deletions doc/gitlinker.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ In your `init.lua` or in a `:lua-heredoc` in your `init.vim`:
>
require"gitlinker".setup()
<
==============================================================================
buffer url *gitlinker-buf-url*

By default, the following mapping is defined:

Expand Down Expand Up @@ -89,6 +91,21 @@ looking for is `require"gitlinker".get_buf_range_url(mode, user_opts)` where:
vim.api.nvim_set_keymap('v', '<leader>gb', ':lua require"gitlinker".get_buf_range_url("v", {action_callback = require"gitlinker.actions".open_in_browser})<cr>'))
<

==============================================================================
repo url *gitlinker-repo-url*

For convenience, the function
`require"gitlinker".get_buf_range_url(mode, user_opts)` allows one to generate
the url for the repository homepage. You can map it like so:

>
vim.api.nvim_set_keymap('n', '<leader>gY', '<cmd>lua require"gitlinker".get_repo_url()<cr>', {silent = true})
vim.api.nvim_set_keymap('n', '<leader>gB', '<cmd>lua require"gitlinker".get_repo_url({action_callback = require"gitlinker.actions".open_in_browser})<cr>', {silent = true})
<

And use `<leader>gY` to copy the repo's homepage to your clipboard or
`<leader>gB` to open it in your browser.

==============================================================================
Configuration *gitlinker-config*

Expand Down
47 changes: 39 additions & 8 deletions lua/gitlinker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ function M.setup(config)
end
end

local function get_url_data(mode, user_opts)
local remote = user_opts.remote or git.get_branch_remote()
local function get_repo_url_data(remote)
remote = remote or git.get_branch_remote()
if not remote then
return nil
end
Expand All @@ -54,6 +54,15 @@ local function get_url_data(mode, user_opts)
if not repo or vim.tbl_isempty(repo) then
return nil
end
return repo
end

local function get_buf_range_url_data(mode, user_opts)
local remote = user_opts.remote or git.get_branch_remote()
local repo_url_data = get_repo_url_data(remote)
if not repo_url_data then
return nil
end

local buf_repo_path = buffer.get_relative_path(git.get_git_root())

Expand All @@ -67,15 +76,12 @@ local function get_url_data(mode, user_opts)
user_opts.add_current_line_on_normal_mode
)

return {
host = repo.host,
repo = repo.path,
port = repo.port,
return vim.tbl_extend("force", repo_url_data, {
rev = rev,
file = buf_repo_path,
lstart = range.lstart,
lend = range.lend,
}
})
end

--- Retrieves the url for the selected buffer range
Expand All @@ -93,7 +99,7 @@ end
function M.get_buf_range_url(mode, user_opts)
user_opts = vim.tbl_deep_extend("force", opts.get(), user_opts or {})

local url_data = get_url_data(mode, user_opts)
local url_data = get_buf_range_url_data(mode, user_opts)
if not url_data then
return nil
end
Expand All @@ -115,4 +121,29 @@ function M.get_buf_range_url(mode, user_opts)
return url
end

function M.get_repo_url(user_opts)
user_opts = vim.tbl_deep_extend("force", opts.get(), user_opts or {})

local repo_url_data = get_repo_url_data(user_opts.remote)
if not repo_url_data then
return nil
end

local matching_callback = M.hosts.get_matching_callback(repo_url_data.host)
if not matching_callback then
return nil
end

local url = matching_callback(repo_url_data)

if user_opts.action_callback then
user_opts.action_callback(url)
end
if user_opts.print_url then
print(url)
end

return url
end

return M
2 changes: 1 addition & 1 deletion lua/gitlinker/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ local function parse_uri(uri, errs)
port = nil
end

return { host = host, port = port, path = repo_path }
return { host = host, port = port, repo = repo_path }
end

local function is_file_compatible_with_revspec(buf_repo_path, revspec, errs)
Expand Down
169 changes: 87 additions & 82 deletions lua/gitlinker/hosts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,108 +5,112 @@ function M.get_base_https_url(url_data)
if url_data.port then
url = url .. ":" .. url_data.port
end
return url .. "/"
return url .. "/" .. url_data.repo
end

--- Constructs a github style url
function M.get_github_type_url(url_data)
local url = M.get_base_https_url(url_data)
.. url_data.repo
.. "/blob/"
.. url_data.rev
.. "/"
.. url_data.file
if url_data.lstart then
url = url .. "#L" .. url_data.lstart
if url_data.lend then
url = url .. "-L" .. url_data.lend
end
if not url_data.file or not url_data.rev then
return url
end
url = url .. "/blob/" .. url_data.rev .. "/" .. url_data.file

if not url_data.lstart then
return url
end
url = url .. "#L" .. url_data.lstart
if url_data.lend then
url = url .. "-L" .. url_data.lend
end
return url
end

--- Constructs a gitea style url
function M.get_gitea_type_url(url_data)
local url = M.get_base_https_url(url_data)
.. url_data.repo
.. "/src/commit/"
.. url_data.rev
.. "/"
.. url_data.file
if url_data.lstart then
url = url .. "#L" .. url_data.lstart
if url_data.lend then
url = url .. "-L" .. url_data.lend
end
if not url_data.file or not url_data.rev then
return url
end
url = url .. "/src/commit/" .. url_data.rev .. "/" .. url_data.file

if not url_data.lstart then
return url
end
url = url .. "#L" .. url_data.lstart
if url_data.lend then
url = url .. "-L" .. url_data.lend
end
return url
end

--- Constructs a gitlab style url
function M.get_gitlab_type_url(url_data)
local url = M.get_base_https_url(url_data)
.. url_data.repo
.. "/-/blob/"
.. url_data.rev
.. "/"
.. url_data.file
if url_data.lstart then
url = url .. "#L" .. url_data.lstart
if url_data.lend then
url = url .. "-" .. url_data.lend
end
if not url_data.file or not url_data.rev then
return url
end
url = url .. "/-/blob/" .. url_data.rev .. "/" .. url_data.file

if not url_data.lstart then
return url
end
url = url .. "#L" .. url_data.lstart
if url_data.lend then
url = url .. "-" .. url_data.lend
end
return url
end

--- Constructs a bitbucket style url
function M.get_bitbucket_type_url(url_data)
local url = M.get_base_https_url(url_data)
.. url_data.repo
.. "/src/"
.. url_data.rev
.. "/"
.. url_data.file
if url_data.lstart then
url = url .. "#lines-" .. url_data.lstart
if url_data.lend then
url = url .. ":" .. url_data.lend
end
if not url_data.file or not url_data.rev then
return url
end
url = url .. "/src/" .. url_data.rev .. "/" .. url_data.file

if not url_data.lstart then
return url
end
url = url .. "#lines-" .. url_data.lstart
if url_data.lend then
url = url .. ":" .. url_data.lend
end

return url
end

--- Constructs a gogs style url
function M.get_gogs_type_url(url_data)
local url = M.get_base_https_url(url_data)
.. url_data.repo
.. "/src/"
.. url_data.rev
.. "/"
.. url_data.file
if url_data.lstart then
url = url .. "#L" .. url_data.lstart
if url_data.lend then
url = url .. "-L" .. url_data.lend
end
if not url_data.file or not url_data.rev then
return url
end
url = url .. "/src/" .. url_data.rev .. "/" .. url_data.file

if not url_data.lstart then
return url
end
url = url .. "#L" .. url_data.lstart
if url_data.lend then
url = url .. "-L" .. url_data.lend
end

return url
end

--- Constructs a cgit style url
function M.get_cgit_type_url(url_data)
local repo = ""
if url_data.repo then
repo = url_data.repo .. ".git/"
end
local url = "https://"
.. url_data.host
.. "/cgit/"
.. repo
.. "tree/"
.. url_data.file
.. "?id="
.. url_data.rev
url_data.repo = url_data.repo .. ".git/"
end

local url = "https://" .. url_data.host
if url_data.port then
url = url .. ":" .. url_data.port
end
url = url .. "/tree/" .. url_data.file .. "?id=" .. url_data.rev
if url_data.lstart then
url = url .. "#n" .. url_data.lstart
end
Expand All @@ -116,28 +120,30 @@ end
--- Constructs a sourcehut style url
function M.get_srht_type_url(url_data)
local url = M.get_base_https_url(url_data)
.. url_data.repo
.. "/tree/"
.. url_data.rev
.. "/item/"
.. url_data.file
if url_data.lstart then
url = url .. "#L" .. url_data.lstart
if url_data.lend then
url = url .. "-" .. url_data.lend
end
if not url_data.file or not url_data.rev then
return url
end
url = url .. "/tree/" .. url_data.rev .. "/item/" .. url_data.file

if not url_data.lstart then
return url
end
url = url .. "#L" .. url_data.lstart
if url_data.lend then
url = url .. "-" .. url_data.lend
end

return url
end

--- Constructs a launchpad style url
function M.get_launchpad_type_url(url_data)
local url = M.get_base_https_url(url_data)
.. url_data.repo
.. "/tree/"
.. url_data.file
.. "?id="
.. url_data.rev
if not url_data.file or not url_data.rev then
return url
end
url = url .. "/tree/" .. url_data.file .. "?id=" .. url_data.rev

if url_data.lstart then
url = url .. "#n" .. url_data.lstart
end
Expand All @@ -147,11 +153,10 @@ end
--- Constructs a repo.or.cz style url
function M.get_repoorcz_type_url(url_data)
local url = M.get_base_https_url(url_data)
.. url_data.repo
.. "/blob/"
.. url_data.rev
.. ":/"
.. url_data.file
if not url_data.file or not url_data.rev then
return url
end
url = url .. "/blob/" .. url_data.rev .. ":/" .. url_data.file
if url_data.lstart then
url = url .. "#l" .. url_data.lstart
end
Expand Down

0 comments on commit 6a59e9c

Please sign in to comment.