From 23b22742ae5f9de17e4fe646139feaf6aebf48b2 Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Wed, 19 Jul 2023 19:30:35 +0200 Subject: [PATCH] git: improve remote detection 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 --- lua/gx/git.lua | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/lua/gx/git.lua b/lua/gx/git.lua index 3c2d6b5..6f98e09 100644 --- a/lua/gx/git.lua +++ b/lua/gx/git.lua @@ -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 @@ -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