diff --git a/lua/git/cmd.lua b/lua/git/cmd.lua index 8bc79fd..91b94e4 100644 --- a/lua/git/cmd.lua +++ b/lua/git/cmd.lua @@ -189,11 +189,7 @@ end ---@param args string[] function M.run(args) - local path = vim.api.nvim_buf_get_name(0) - if path == "" then - path = vim.fn.getcwd() - end - local _, worktree = repo.resolve(path) + local _, worktree = repo.resolve_cwd() if not worktree then log.warning("not in a git repository") return diff --git a/lua/git/commit.lua b/lua/git/commit.lua index f5956f7..4c07382 100644 --- a/lua/git/commit.lua +++ b/lua/git/commit.lua @@ -6,11 +6,7 @@ local M = {} ---@param opts { amend: boolean? }? function M.commit(opts) local amend = opts and opts.amend or false - local path = vim.api.nvim_buf_get_name(0) - if path == "" then - path = vim.fn.getcwd() - end - local gitdir, worktree = repo.resolve(path) + local gitdir, worktree = repo.resolve_cwd() if not gitdir or not worktree then log.warning("not in a git repository") return diff --git a/lua/git/log_win.lua b/lua/git/log_win.lua index 176c279..ac4f321 100644 --- a/lua/git/log_win.lua +++ b/lua/git/log_win.lua @@ -7,11 +7,7 @@ local M = {} local LOG_FORMAT = "%h %ad {%an}%d %s" function M.show() - local path = vim.api.nvim_buf_get_name(0) - if path == "" then - path = vim.fn.getcwd() - end - local _, worktree = repo.resolve(path) + local _, worktree = repo.resolve_cwd() if not worktree then log.warning("not in a git repository") return diff --git a/lua/git/repo.lua b/lua/git/repo.lua index e844930..8f6d521 100644 --- a/lua/git/repo.lua +++ b/lua/git/repo.lua @@ -83,6 +83,18 @@ local function resolve(path) return vim.fs.normalize(gitdir), worktree end +---Resolve the gitdir/worktree from the current buffer's file path, falling +---back to `vim.fn.getcwd()` when the buffer is unnamed. Returns nil for +---both when not inside a git repo. +---@return string?, string? gitdir, worktree +local function resolve_cwd() + local path = vim.api.nvim_buf_get_name(0) + if path == "" then + path = vim.fn.getcwd() + end + return resolve(path) +end + ---@class ow.Git.Repo ---@field gitdir string ---@field worktree string @@ -312,6 +324,7 @@ return { indicator = indicator, refresh_buf = refresh_buf, resolve = resolve, + resolve_cwd = resolve_cwd, rev_parse = rev_parse, stop_all = stop_all, unregister = unregister, diff --git a/lua/git/status_win.lua b/lua/git/status_win.lua index 1ff2565..fc20181 100644 --- a/lua/git/status_win.lua +++ b/lua/git/status_win.lua @@ -842,11 +842,7 @@ function M.toggle() vim.api.nvim_win_close(sidebar_win, false) return end - local path = vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf()) - if path == "" then - path = vim.fn.getcwd() - end - local _, worktree = repo.resolve(path) + local _, worktree = repo.resolve_cwd() if not worktree then log.warning("not in a git repository") return