diff --git a/lua/git/cmd.lua b/lua/git/cmd.lua index eca1cfb..3b3fd68 100644 --- a/lua/git/cmd.lua +++ b/lua/git/cmd.lua @@ -175,7 +175,7 @@ end ---@param args string[] function M.run(args) - local _, worktree = repo.resolve_cwd() + local _, worktree = repo.current_repo() if not worktree then util.warning("not in a git repository") return diff --git a/lua/git/commit.lua b/lua/git/commit.lua index e580655..ea88b06 100644 --- a/lua/git/commit.lua +++ b/lua/git/commit.lua @@ -7,7 +7,7 @@ local M = {} ---@param opts { amend: boolean? }? function M.commit(opts) local amend = opts and opts.amend or false - local _, worktree = repo.resolve_cwd() + local _, worktree = repo.current_repo() if not worktree then util.warning("not in a git repository") return diff --git a/lua/git/diff.lua b/lua/git/diff.lua index bec9b4f..77e355f 100644 --- a/lua/git/diff.lua +++ b/lua/git/diff.lua @@ -62,7 +62,7 @@ end ---@param buf integer ---@param rev ow.Git.Revision local function uri_split(opts, buf, rev) - local worktree = vim.b[buf].git_worktree or select(2, repo.resolve_cwd()) + local worktree = vim.b[buf].git_worktree or select(2, repo.current_repo()) if not worktree then util.warning("git URI buffer has no worktree") return @@ -154,16 +154,13 @@ function M.split(opts) util.warning("cannot diff this buffer (not a worktree file)") return end - local _, worktree = repo.resolve(cur_path) + local _, worktree, cur_path = repo.resolve(cur_path) if not worktree then util.warning("not in a git repository") return end + ---@cast cur_path -nil local rel = vim.fs.relpath(worktree, cur_path) - if not rel then - util.warning("file is outside the worktree") - return - end local rev if not opts.rev then diff --git a/lua/git/log.lua b/lua/git/log.lua index 256545e..7200094 100644 --- a/lua/git/log.lua +++ b/lua/git/log.lua @@ -92,7 +92,7 @@ M.opt_parsers = { ---@param opts ow.Git.LogOpts? function M.open(opts) opts = opts or {} - local _, worktree = repo.resolve_cwd() + local _, worktree = repo.current_repo() if not worktree then util.warning("not in a git repository") return diff --git a/lua/git/object.lua b/lua/git/object.lua index 6e18c1b..7c14c56 100644 --- a/lua/git/object.lua +++ b/lua/git/object.lua @@ -149,7 +149,7 @@ function M.read_uri(buf) end local rev_str = rev:format() - local worktree = vim.b[buf].git_worktree or select(2, repo.resolve_cwd()) + local worktree = vim.b[buf].git_worktree or select(2, repo.current_repo()) if not worktree then util.error("git BufReadCmd %s: cannot resolve worktree", name) return diff --git a/lua/git/repo.lua b/lua/git/repo.lua index 49edb0b..ae3900c 100644 --- a/lua/git/repo.lua +++ b/lua/git/repo.lua @@ -5,7 +5,9 @@ local M = {} ---@param path string ---@return string? gitdir ---@return string? worktree +---@return string? path function M.resolve(path) + path = vim.fn.resolve(path) local found = vim.fs.find(".git", { upward = true, path = path })[1] if not found then return nil @@ -16,7 +18,7 @@ function M.resolve(path) return nil end if stat.type == "directory" then - return found, worktree + return found, worktree, path end local f = io.open(found, "r") if not f then @@ -32,17 +34,18 @@ function M.resolve(path) if not gitdir:match("^/") then gitdir = vim.fs.joinpath(worktree, gitdir) end - return vim.fs.normalize(gitdir), worktree + return vim.fs.normalize(gitdir), worktree, path end ---@return string? gitdir ---@return string? worktree -function M.resolve_cwd() +function M.current_repo() local path = vim.api.nvim_buf_get_name(0) if path == "" or path:match("^%a+://") then path = vim.fn.getcwd() end - return M.resolve(path) + local gitdir, worktree, _ = M.resolve(path) + return gitdir, worktree end ---@param path string @@ -94,7 +97,7 @@ end ---@param arg_lead string ---@return string[] function M.complete_rev(arg_lead) - local _, worktree = M.resolve_cwd() + local _, worktree = M.current_repo() if not worktree then return {} end diff --git a/lua/git/sidebar.lua b/lua/git/sidebar.lua index 6591db4..9aa7d30 100644 --- a/lua/git/sidebar.lua +++ b/lua/git/sidebar.lua @@ -837,10 +837,11 @@ local function open(worktree) return end - local gitdir = repo.resolve(worktree) + local gitdir, worktree = repo.resolve(worktree) if not gitdir then return end + ---@cast worktree -nil local previous_win = vim.api.nvim_get_current_win() local bufnr, win = util.new_scratch({ split = "left" }) @@ -915,7 +916,7 @@ function M.toggle() vim.api.nvim_win_close(sidebar_win, false) return end - local _, worktree = repo.resolve_cwd() + local _, worktree = repo.current_repo() if not worktree then util.warning("not in a git repository") return diff --git a/lua/git/watcher.lua b/lua/git/watcher.lua index 8df1027..b77a5bd 100644 --- a/lua/git/watcher.lua +++ b/lua/git/watcher.lua @@ -94,7 +94,8 @@ local function do_refresh(r) if not vim.api.nvim_buf_is_valid(buf) then r.buffers[buf] = nil else - local status = statuses[vim.api.nvim_buf_get_name(buf)] + local status = + statuses[vim.fn.resolve(vim.api.nvim_buf_get_name(buf))] if vim.b[buf].git_status ~= status then vim.b[buf].git_status = status dirty = true