refactor(git): extract repo.resolve_cwd helper
This commit is contained in:
+1
-5
@@ -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
|
||||
|
||||
+1
-5
@@ -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
|
||||
|
||||
+1
-5
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user