isak102 / telescope-git-file-history.nvim

Open/preview contents of the current file at a specific commit, without using git checkout
MIT License
67 stars 4 forks source link

Please add possibility to open in browser for azure too :) #3

Closed faridsaid20 closed 6 months ago

faridsaid20 commented 6 months ago

This is working solution for our azure and github. Something like this would be nice if you could add :)


                local function is_https_url(url)
            return url:match("^https://")
        end

        local function get_repo_url()
            local repo_url = vim.fn.system("git remote get-url $(git remote)")

            if not is_https_url(repo_url) then
                repo_url = repo_url:gsub(":", "/")
                repo_url = repo_url:gsub("git@", "https://")
            end
            repo_url = repo_url:gsub("%.git", "")
            repo_url = repo_url:gsub("[^/]+@dev", "dev")

            return repo_url
        end
        local function url_encode(str)
            if str then
                str = string.gsub(str, "\n", "\r\n")
                str = string.gsub(str, "([^%w %.])", function(c)
                    return string.format("%%%02X", string.byte(c))
                end)
                str = string.gsub(str, " ", "+")
            end
            return str
        end

        local function get_file_at_commit_url(repo_url, hash, path)
            repo_url = repo_url:gsub("\n", "")
            hash = hash:gsub("\n", "")
            path = path:gsub("\n", "")

            local relative_path = vim.fn.system("git ls-files --full-name " .. path):gsub("\n", "")

            if repo_url:match("dev.azure.com") then
                return repo_url .. "?path=" .. url_encode(relative_path) .. "&version=GC" .. hash .. "&_a=contents"
            else
                return repo_url .. "/blob/" .. hash .. "/" .. path
            end
        end

As a bonus it would be nice to have a possibility to choose what we want to open in browser. Just file at that commit or whole commit with changes added. This is simple implementation where the url is commit changes in azure.

        local function get_file_changes_at_commit_url(repo_url, hash, path)
            repo_url = repo_url:gsub("\n", "")
            hash = hash:gsub("\n", "")
            path = path:gsub("\n", "")

            local branch = vim.fn.system("git rev-parse --abbrev-ref HEAD"):gsub("\n", "")

            local relative_path = vim.fn.system("git ls-files --full-name " .. path):gsub("\n", "")

            if repo_url:match("dev.azure.com") then
                -- Modify the URL structure for Azure DevOps
                return repo_url
                    .. "/commit/"
                    .. hash
                    .. "?refName=refs%2Fheads%2F"
                    .. url_encode(branch)
                    .. "&path=%2F"
                    .. url_encode(relative_path)
                    .. "&_a=compare"
            else
                return repo_url .. "/blob/" .. hash .. "/" .. path
            end
        end
isak102 commented 6 months ago

I created a new pull request (#4) which handles your first problem. @faridsaid20 could you please try it out and see if it works? Personally I don't use azure so I can't try it right now.

For your second problem, I will look into it and see how I want to handle it. Might add a configuration option for that

faridsaid20 commented 6 months ago

I created a new pull request (#4) which handles your first problem. @faridsaid20 could you please try it out and see if it works? Personally I don't use azure so I can't try it right now.

For your second problem, I will look into it and see how I want to handle it. Might add a configuration option for that

Thank you! Added a comment, will work when it's fixed!

isak102 commented 6 months ago

Where did you comment? I can't find it

faridsaid20 commented 6 months ago

Where did you comment? I can't find it

https://github.com/isak102/telescope-git-file-history.nvim/pull/4

isak102 commented 6 months ago

I think you forgot to publish it because there is no comment on that PR

faridsaid20 commented 6 months ago

I think you forgot to publish it because there is no comment on that PR

Thanks, I don't use github usually, had no idea that you need to publish your comments o.O

isak102 commented 6 months ago

No worries, fixed it and merged it now.

I will leave this issue open as the second part is not resolved yet, I think I will add a separate action to open the whole commit instead of the specific file.

Edit: Nvm, created a separate issue for that (#5)