Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lua/gitlinker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ local function get_buf_range_url_data(mode, user_opts)
end

return vim.tbl_extend("force", repo_url_data, {
rev = rev,
rev = repo_url_data.rev or rev,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we wouldn't have to do this if the key was called branch. Then we can embed both the branch name and the rev sha in the url data that is passed to callbacks.

Which is good because then it is up to the callbacks to decide what they want to do (either use the branch name or the rev sha).

file = buf_repo_path,
lstart = range.lstart,
lend = range.lend,
Expand Down
31 changes: 31 additions & 0 deletions lua/gitlinker/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,31 @@ function M.get_closest_remote_compatible_rev(remote)
return nil
end

local function get_remote_branch(remote)
local errs = {
string.format("Failed to retrieve remote branch for remote '%s'", remote),
}
local symRef
local p = job:new({
command = "git",
args = { "symbolic-ref", "-q", "HEAD" },
})
p:after_success(function(j)
symRef = j:result()[1]
end)
p:sync()
local trackingBranch
local p = job:new({
command = 'git',
args = { 'for-each-ref', '--format=%(upstream:short)', symRef },
})
p:after_success(function(j)
trackingBranch = (j:result()[1]):match(remote .. '/(.+)$')
end)
p:sync()
return trackingBranch or 'origin/master'
end

function M.get_repo_data(remote)
local errs = {
string.format("Failed to retrieve repo data for remote '%s'", remote),
Expand All @@ -255,6 +280,12 @@ function M.get_repo_data(remote)
if not repo or vim.tbl_isempty(repo) then
vim.notify(table.concat(errs), vim.log.levels.Error)
end

local branch = get_remote_branch(remote)
if branch then
repo.rev = branch
end
Comment on lines +284 to +287
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for conditionality here, always set it, even if nil, it will be checked later anyways 😉

Suggested change
local branch = get_remote_branch(remote)
if branch then
repo.rev = branch
end
repo.branch = get_remote_branch(remote)


return repo
Comment on lines +284 to 289
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why call the key rev when it's always a branch name or nil? I would prefer repo.branch

end

Expand Down
5 changes: 4 additions & 1 deletion lua/gitlinker/hosts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ end
--- Constructs a github style url
function M.get_github_type_url(url_data)
local url = M.get_base_https_url(url_data)
if not url_data.file or not url_data.rev then
if not url_data.file and not url_data.rev then
return url
end
if not url_data.file and url_data.rev then
return url .. "/tree/" .. url_data.rev
end
url = url .. "/blob/" .. url_data.rev .. "/" .. url_data.file

if not url_data.lstart then
Expand Down