diff --git a/lua/git/diff.lua b/lua/git/diff.lua index 81b3b13..aa57d8f 100644 --- a/lua/git/diff.lua +++ b/lua/git/diff.lua @@ -221,7 +221,7 @@ function M.split(opts) local cur_buf = vim.api.nvim_get_current_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 return uri_split(opts, cur_buf, cur_revspec) end diff --git a/lua/git/object.lua b/lua/git/object.lua index 80643f4..7656a4c 100644 --- a/lua/git/object.lua +++ b/lua/git/object.lua @@ -152,8 +152,7 @@ local pending_content = {} ---@param content string? ---@return integer function M.buf_for(worktree, revspec, content) - local name = "git://" .. revspec - local buf = vim.fn.bufadd(name) + local buf = vim.fn.bufadd(util.uri(revspec)) vim.b[buf].git_worktree = worktree if content then pending_content[buf] = content @@ -170,7 +169,7 @@ end ---@param buf integer function M.read_uri(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 return end @@ -230,7 +229,7 @@ local function blob_buf(worktree, blob, path, ref) local revspec = ref .. ":" .. path if is_zero(blob) then return diff.empty_buf({ - name = "git://" .. revspec, + name = util.uri(revspec), bufhidden = "hide", }) end @@ -311,7 +310,7 @@ end function M.open_commit(worktree, ref, opts) local split = opts and opts.split 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) if existing ~= -1 and vim.api.nvim_buf_is_loaded(existing) then if split == false then @@ -376,7 +375,7 @@ function M.open_object(worktree, ref, opts) local commit_ref, path = ref:match("^(.-):(.+)$") if commit_ref then 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 end @@ -398,7 +397,7 @@ function M.open_object(worktree, ref, opts) -- `filetype.add` pattern doesn't match; default to `git` so -- tree / tag header lines syntax-highlight. 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 ---@return boolean dispatched true if the cursor was on an actionable line diff --git a/lua/git/sidebar.lua b/lua/git/sidebar.lua index 1894a08..5a307c3 100644 --- a/lua/git/sidebar.lua +++ b/lua/git/sidebar.lua @@ -485,7 +485,7 @@ end local function head_pane(worktree, path) return { buf = object.buf_for(worktree, "HEAD:" .. path), - name = "git://HEAD:" .. path, + name = util.uri("HEAD:" .. path), } end @@ -526,7 +526,7 @@ local function index_pane(s, entry) end return { buf = object.buf_for(s.worktree, ":0:" .. entry.path), - name = "git://:0:" .. entry.path, + name = util.uri(":0:" .. entry.path), } end diff --git a/lua/git/util.lua b/lua/git/util.lua index 76792e7..15fa10e 100644 --- a/lua/git/util.lua +++ b/lua/git/util.lua @@ -1,5 +1,21 @@ 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://` 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 ---@field stage 0|1|2|3? index stage when the revspec is `:` / `:0:` / `:N:`; nil otherwise ---@field path string? path component when the revspec carries one; nil for bare object refs