Skip to content

Commit

Permalink
git: improve remote detection
Browse files Browse the repository at this point in the history
The previous version had a few issues I got bitten by:

* it's on gitea/gitlab/github not necessary to have `.git` as suffix for
  a remote, i.e.

      git@github.com:org/repo

  is perfectly fine and because I wasn't that, all of my remotes were
  dismissed by this plugin.

* For forked repos I usually adhere to the convention to name my own
  fork's remote `origin` while the original repo's origin is called
  `upstream`[1]. This also means that if an issue is referenced in a
  file, it refers to the upstream's issue tracker.

  Accordingly I patched this part to prefer the upstream remote over the
  origin remote (if it exists).

  This is just a very bare-bone setup missing a few more important
  things:

  * on github at least you can also specify owner#issuename for issues
    in forks. This is still completely ignored.

  * the prefer remote X over Y should be at least configurable, perhaps
    it should be possible to do this on a per-project level.

  For me, the current state is good-enough to use and I figured I'd
  contribute my changes back here even though the patch is not perfect,
  perhaps they're useful as a base to improve this nice plugin.

[1] https://docs.github.com/en/get-started/quickstart/fork-a-repo
  • Loading branch information
Ma27 committed Jul 19, 2023
1 parent 9f7a264 commit 23b2274
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions lua/gx/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ local shell = require("gx.shell")

M = {}

local function parse_git_output(result)
if not result or table.getn(result) < 1 then
local function parse_git_output(result, remote)
if not result then
return
end

local _, _, domain, repository = string.find(result[1], "^origin\t.*git@(.*%..*):(.*/.*).git")
local _, _, domain, repository = string.find(result, "^" .. remote .. "\t.*git@(.*%..*):(.*/.*) ")

if domain and repository then
domain = domain:gsub("%.git", "")
return "https://" .. domain .. "/" .. repository
end

local _, _, url = string.find(result[1], "origin\t(.*)%s")
local _, _, url = string.find(result, remote .. "\t(.*)%s")
if url then
return url
end
Expand All @@ -28,7 +30,16 @@ function M.get_remote_url()
return
end

local url = parse_git_output(result)
local url
for _, remote in ipairs({"upstream", "origin"}) do
for _, line in ipairs(result) do
url = parse_git_output(line, remote)
if url then
goto loopend
end
end
end
::loopend::
if not url then
notfier.warn("No remote git repository found!")
end
Expand Down

0 comments on commit 23b2274

Please sign in to comment.