refactor(git): centralise the git:// scheme behind util.uri / util.parse_uri

This commit is contained in:
2026-04-29 09:56:32 +02:00
parent 590651e9d8
commit 9d938b228b
4 changed files with 25 additions and 10 deletions
+1 -1
View File
@@ -221,7 +221,7 @@ function M.split(opts)
local cur_buf = vim.api.nvim_get_current_buf() local cur_buf = vim.api.nvim_get_current_buf()
local cur_path = vim.api.nvim_buf_get_name(cur_buf) local cur_path = vim.api.nvim_buf_get_name(cur_buf)
local cur_revspec = cur_path:match("^git://(.+)$") local cur_revspec = util.parse_uri(cur_path)
if cur_revspec then if cur_revspec then
return uri_split(opts, cur_buf, cur_revspec) return uri_split(opts, cur_buf, cur_revspec)
end end
+6 -7
View File
@@ -152,8 +152,7 @@ local pending_content = {}
---@param content string? ---@param content string?
---@return integer ---@return integer
function M.buf_for(worktree, revspec, content) function M.buf_for(worktree, revspec, content)
local name = "git://" .. revspec local buf = vim.fn.bufadd(util.uri(revspec))
local buf = vim.fn.bufadd(name)
vim.b[buf].git_worktree = worktree vim.b[buf].git_worktree = worktree
if content then if content then
pending_content[buf] = content pending_content[buf] = content
@@ -170,7 +169,7 @@ end
---@param buf integer ---@param buf integer
function M.read_uri(buf) function M.read_uri(buf)
local name = vim.api.nvim_buf_get_name(buf) local name = vim.api.nvim_buf_get_name(buf)
local revspec = name:match("^git://(.+)$") local revspec = util.parse_uri(name)
if not revspec then if not revspec then
return return
end end
@@ -230,7 +229,7 @@ local function blob_buf(worktree, blob, path, ref)
local revspec = ref .. ":" .. path local revspec = ref .. ":" .. path
if is_zero(blob) then if is_zero(blob) then
return diff.empty_buf({ return diff.empty_buf({
name = "git://" .. revspec, name = util.uri(revspec),
bufhidden = "hide", bufhidden = "hide",
}) })
end end
@@ -311,7 +310,7 @@ end
function M.open_commit(worktree, ref, opts) function M.open_commit(worktree, ref, opts)
local split = opts and opts.split local split = opts and opts.split
local sha = repo.rev_parse(worktree, ref, true) or ref local sha = repo.rev_parse(worktree, ref, true) or ref
local name = "git://" .. sha local name = util.uri(sha)
local existing = vim.fn.bufnr(name) local existing = vim.fn.bufnr(name)
if existing ~= -1 and vim.api.nvim_buf_is_loaded(existing) then if existing ~= -1 and vim.api.nvim_buf_is_loaded(existing) then
if split == false then if split == false then
@@ -376,7 +375,7 @@ function M.open_object(worktree, ref, opts)
local commit_ref, path = ref:match("^(.-):(.+)$") local commit_ref, path = ref:match("^(.-):(.+)$")
if commit_ref then if commit_ref then
local sha = repo.rev_parse(worktree, commit_ref, true) or commit_ref local sha = repo.rev_parse(worktree, commit_ref, true) or commit_ref
open_uri(worktree, "git://" .. sha .. ":" .. path, sha, opts) open_uri(worktree, util.uri(sha .. ":" .. path), sha, opts)
return return
end end
@@ -398,7 +397,7 @@ function M.open_object(worktree, ref, opts)
-- `filetype.add` pattern doesn't match; default to `git` so -- `filetype.add` pattern doesn't match; default to `git` so
-- tree / tag header lines syntax-highlight. -- tree / tag header lines syntax-highlight.
local sha = repo.rev_parse(worktree, ref, true) or ref local sha = repo.rev_parse(worktree, ref, true) or ref
open_uri(worktree, "git://" .. sha, sha, opts, "git") open_uri(worktree, util.uri(sha), sha, opts, "git")
end end
---@return boolean dispatched true if the cursor was on an actionable line ---@return boolean dispatched true if the cursor was on an actionable line
+2 -2
View File
@@ -485,7 +485,7 @@ end
local function head_pane(worktree, path) local function head_pane(worktree, path)
return { return {
buf = object.buf_for(worktree, "HEAD:" .. path), buf = object.buf_for(worktree, "HEAD:" .. path),
name = "git://HEAD:" .. path, name = util.uri("HEAD:" .. path),
} }
end end
@@ -526,7 +526,7 @@ local function index_pane(s, entry)
end end
return { return {
buf = object.buf_for(s.worktree, ":0:" .. entry.path), buf = object.buf_for(s.worktree, ":0:" .. entry.path),
name = "git://:0:" .. entry.path, name = util.uri(":0:" .. entry.path),
} }
end end
+16
View File
@@ -1,5 +1,21 @@
local M = {} local M = {}
local URI_PREFIX = "git://"
---@param revspec string
---@return string
function M.uri(revspec)
return URI_PREFIX .. revspec
end
---Extract the revspec from a `git://<revspec>` buffer name. Returns
---nil if the name doesn't carry the scheme.
---@param name string
---@return string?
function M.parse_uri(name)
return name:match("^" .. URI_PREFIX .. "(.+)$")
end
---@class ow.Git.ParsedRevspec ---@class ow.Git.ParsedRevspec
---@field stage 0|1|2|3? index stage when the revspec is `:<path>` / `:0:<path>` / `:N:<path>`; nil otherwise ---@field stage 0|1|2|3? index stage when the revspec is `:<path>` / `:0:<path>` / `:N:<path>`; nil otherwise
---@field path string? path component when the revspec carries one; nil for bare object refs ---@field path string? path component when the revspec carries one; nil for bare object refs