diff --git a/lua/nvim-claude/hooks.lua b/lua/nvim-claude/hooks.lua index 7a067227..1d8fdb15 100644 --- a/lua/nvim-claude/hooks.lua +++ b/lua/nvim-claude/hooks.lua @@ -9,68 +9,18 @@ function M.setup() vim.defer_fn(function() M.cleanup_old_commits() end, 200) + + -- Create baseline on startup if we're in a git repo + vim.defer_fn(function() + M.create_startup_baseline() + end, 500) end --- Pre-tool-use hook: Create baseline commit only if one doesn't exist +-- Pre-tool-use hook: Now just validates existing baseline function M.pre_tool_use_hook() - local utils = require 'nvim-claude.utils' - - -- Check if we're in a git repository - local git_root = utils.get_project_root() - if not git_root then - return - end - - -- Check if we already have a baseline commit - local baseline_file = '/tmp/claude-baseline-commit' - local existing_baseline = utils.read_file(baseline_file) - - if existing_baseline and existing_baseline ~= '' then - existing_baseline = existing_baseline:gsub('%s+', '') - -- Verify the baseline commit still exists - local check_cmd = string.format('cd "%s" && git rev-parse --verify %s^{commit} 2>/dev/null', git_root, existing_baseline) - local check_result, check_err = utils.exec(check_cmd) - - -- If we got a result and no error, the commit exists - if check_result and not check_err and check_result:match('^%x+') then - -- Baseline is valid, keep using it - M.baseline_commit = existing_baseline - return - end - end - - -- Create new baseline commit - local timestamp = os.time() - local commit_msg = string.format('claude-baseline-%d', timestamp) - - -- Stage all current changes (including untracked files) - local add_cmd = string.format('cd "%s" && git add -A', git_root) - local add_result, add_err = utils.exec(add_cmd) - - if add_err then - return - end - - -- Create a temporary commit - local commit_cmd = string.format('cd "%s" && git commit -m "%s" --allow-empty', git_root, commit_msg) - local commit_result, commit_err = utils.exec(commit_cmd) - - if commit_err and not commit_err:match 'nothing to commit' then - return - end - - -- Store the actual commit hash instead of 'HEAD' - local hash_cmd = string.format('cd "%s" && git rev-parse HEAD', git_root) - local commit_hash, hash_err = utils.exec(hash_cmd) - - if not hash_err and commit_hash and commit_hash ~= '' then - commit_hash = commit_hash:gsub('%s+', '') - M.baseline_commit = commit_hash - utils.write_file(baseline_file, commit_hash) - else - M.baseline_commit = 'HEAD' - utils.write_file(baseline_file, 'HEAD') - end + -- Pre-hook no longer creates baselines + -- Baselines are created on startup or through accept/reject + return end -- Post-tool-use hook: Create stash of Claude's changes and trigger diff review @@ -237,6 +187,58 @@ function M.test_inline_diff() inline_diff.show_inline_diff(bufnr, original_content, current_content) end +-- Create baseline on Neovim startup +function M.create_startup_baseline() + local utils = require 'nvim-claude.utils' + + -- Check if we're in a git repository + local git_root = utils.get_project_root() + if not git_root then + return + end + + -- Check if we already have a baseline + local baseline_file = '/tmp/claude-baseline-commit' + local existing_baseline = utils.read_file(baseline_file) + + -- If we have a valid baseline, keep it + if existing_baseline and existing_baseline ~= '' then + existing_baseline = existing_baseline:gsub('%s+', '') + local check_cmd = string.format('cd "%s" && git rev-parse --verify %s^{commit} 2>/dev/null', git_root, existing_baseline) + local check_result, check_err = utils.exec(check_cmd) + + if check_result and not check_err and check_result:match('^%x+') then + -- Baseline is valid + vim.notify('Claude baseline loaded: ' .. existing_baseline:sub(1, 7), vim.log.levels.INFO) + return + end + end + + -- Create new baseline of current state + local timestamp = os.time() + local commit_msg = string.format('claude-baseline-%d (startup)', timestamp) + + -- Stage all current changes + local add_cmd = string.format('cd "%s" && git add -A', git_root) + utils.exec(add_cmd) + + -- Create baseline commit + local commit_cmd = string.format('cd "%s" && git commit -m "%s" --allow-empty', git_root, commit_msg) + local commit_result, commit_err = utils.exec(commit_cmd) + + if not commit_err or commit_err:match('nothing to commit') then + -- Get the commit hash + local hash_cmd = string.format('cd "%s" && git rev-parse HEAD', git_root) + local commit_hash, hash_err = utils.exec(hash_cmd) + + if not hash_err and commit_hash then + commit_hash = commit_hash:gsub('%s+', '') + utils.write_file(baseline_file, commit_hash) + vim.notify('Claude baseline created: ' .. commit_hash:sub(1, 7), vim.log.levels.INFO) + end + end +end + -- Manual hook testing function M.test_hooks() vim.notify('=== Testing nvim-claude hooks ===', vim.log.levels.INFO)