refactor(git): extract repo.resolve_cwd helper

This commit is contained in:
2026-04-27 13:41:39 +02:00
parent 4b8d551b0d
commit b4c166e86c
5 changed files with 17 additions and 20 deletions
+1 -5
View File
@@ -189,11 +189,7 @@ end
---@param args string[] ---@param args string[]
function M.run(args) function M.run(args)
local path = vim.api.nvim_buf_get_name(0) local _, worktree = repo.resolve_cwd()
if path == "" then
path = vim.fn.getcwd()
end
local _, worktree = repo.resolve(path)
if not worktree then if not worktree then
log.warning("not in a git repository") log.warning("not in a git repository")
return return
+1 -5
View File
@@ -6,11 +6,7 @@ local M = {}
---@param opts { amend: boolean? }? ---@param opts { amend: boolean? }?
function M.commit(opts) function M.commit(opts)
local amend = opts and opts.amend or false local amend = opts and opts.amend or false
local path = vim.api.nvim_buf_get_name(0) local gitdir, worktree = repo.resolve_cwd()
if path == "" then
path = vim.fn.getcwd()
end
local gitdir, worktree = repo.resolve(path)
if not gitdir or not worktree then if not gitdir or not worktree then
log.warning("not in a git repository") log.warning("not in a git repository")
return return
+1 -5
View File
@@ -7,11 +7,7 @@ local M = {}
local LOG_FORMAT = "%h %ad {%an}%d %s" local LOG_FORMAT = "%h %ad {%an}%d %s"
function M.show() function M.show()
local path = vim.api.nvim_buf_get_name(0) local _, worktree = repo.resolve_cwd()
if path == "" then
path = vim.fn.getcwd()
end
local _, worktree = repo.resolve(path)
if not worktree then if not worktree then
log.warning("not in a git repository") log.warning("not in a git repository")
return return
+13
View File
@@ -83,6 +83,18 @@ local function resolve(path)
return vim.fs.normalize(gitdir), worktree return vim.fs.normalize(gitdir), worktree
end 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 ---@class ow.Git.Repo
---@field gitdir string ---@field gitdir string
---@field worktree string ---@field worktree string
@@ -312,6 +324,7 @@ return {
indicator = indicator, indicator = indicator,
refresh_buf = refresh_buf, refresh_buf = refresh_buf,
resolve = resolve, resolve = resolve,
resolve_cwd = resolve_cwd,
rev_parse = rev_parse, rev_parse = rev_parse,
stop_all = stop_all, stop_all = stop_all,
unregister = unregister, unregister = unregister,
+1 -5
View File
@@ -842,11 +842,7 @@ function M.toggle()
vim.api.nvim_win_close(sidebar_win, false) vim.api.nvim_win_close(sidebar_win, false)
return return
end end
local path = vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf()) local _, worktree = repo.resolve_cwd()
if path == "" then
path = vim.fn.getcwd()
end
local _, worktree = repo.resolve(path)
if not worktree then if not worktree then
log.warning("not in a git repository") log.warning("not in a git repository")
return return