From 85080514b688bd21801cb1bc83220ff061c4bce1 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Wed, 4 Feb 2026 22:23:09 -0500 Subject: [PATCH] feat(git): add index and working tree content retrieval Adds functions for accessing git index content and working tree files: - get_index_content() retrieves file from staging area via :0:path - get_working_content() reads file directly from disk - file_exists_in_index() checks if file is staged - file_exists_at_revision() checks if file exists at given revision --- lua/diffs/git.lua | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/lua/diffs/git.lua b/lua/diffs/git.lua index c7632a2..7695283 100644 --- a/lua/diffs/git.lua +++ b/lua/diffs/git.lua @@ -45,4 +45,69 @@ function M.get_relative_path(filepath) return vim.fn.fnamemodify(filepath, ':.') end +---@param filepath string +---@return string[]?, string? +function M.get_index_content(filepath) + local repo_root = M.get_repo_root(filepath) + if not repo_root then + return nil, 'not in a git repository' + end + + local rel_path = M.get_relative_path(filepath) + if not rel_path then + return nil, 'could not determine relative path' + end + + local result = vim.fn.systemlist({ 'git', '-C', repo_root, 'show', ':0:' .. rel_path }) + if vim.v.shell_error ~= 0 then + return nil, 'file not in index' + end + return result, nil +end + +---@param filepath string +---@return string[]?, string? +function M.get_working_content(filepath) + if vim.fn.filereadable(filepath) ~= 1 then + return nil, 'file not readable' + end + local lines = vim.fn.readfile(filepath) + return lines, nil +end + +---@param filepath string +---@return boolean +function M.file_exists_in_index(filepath) + local repo_root = M.get_repo_root(filepath) + if not repo_root then + return false + end + + local rel_path = M.get_relative_path(filepath) + if not rel_path then + return false + end + + vim.fn.system({ 'git', '-C', repo_root, 'ls-files', '--stage', '--', rel_path }) + return vim.v.shell_error == 0 +end + +---@param revision string +---@param filepath string +---@return boolean +function M.file_exists_at_revision(revision, filepath) + local repo_root = M.get_repo_root(filepath) + if not repo_root then + return false + end + + local rel_path = M.get_relative_path(filepath) + if not rel_path then + return false + end + + vim.fn.system({ 'git', '-C', repo_root, 'cat-file', '-e', revision .. ':' .. rel_path }) + return vim.v.shell_error == 0 +end + return M