From 989e99f87623d0abdac125aaae9a6253070475ce Mon Sep 17 00:00:00 2001 From: zolinthecow Date: Thu, 10 Jul 2025 01:37:39 -0700 Subject: [PATCH] claude-baseline-1752136659 --- LICENSE.md | 6 ++ lua/nvim-claude/hooks.lua | 26 +++---- lua/nvim-claude/inline-diff.lua | 117 ++++++++++++++++++++++++++++---- 3 files changed, 117 insertions(+), 32 deletions(-) diff --git a/LICENSE.md b/LICENSE.md index 8878f15b..5b096aed 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -81,3 +81,9 @@ AUTOMATIC INLINE DIFF: This should appear automatically without any prompts! NEW EDIT: This line is different from the baseline and should trigger inline diff! RESTART TEST: Testing if inline diff works after Neovim restart! + +BASELINE FIX TEST: This should properly update baseline when accepted! + +DEBUG BASELINE: This edit includes debugging to see why baseline commits fail! + +AFTER RESTART: Testing baseline commit creation with debug output! diff --git a/lua/nvim-claude/hooks.lua b/lua/nvim-claude/hooks.lua index a3560f51..7a067227 100644 --- a/lua/nvim-claude/hooks.lua +++ b/lua/nvim-claude/hooks.lua @@ -13,8 +13,6 @@ end -- Pre-tool-use hook: Create baseline commit only if one doesn't exist function M.pre_tool_use_hook() - - local utils = require 'nvim-claude.utils' -- Check if we're in a git repository @@ -30,11 +28,14 @@ function M.pre_tool_use_hook() 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', git_root, existing_baseline) + 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 not check_err then - return + -- 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 @@ -47,7 +48,7 @@ function M.pre_tool_use_hook() local add_result, add_err = utils.exec(add_cmd) if add_err then - return + return end -- Create a temporary commit @@ -55,7 +56,7 @@ function M.pre_tool_use_hook() local commit_result, commit_err = utils.exec(commit_cmd) if commit_err and not commit_err:match 'nothing to commit' then - return + return end -- Store the actual commit hash instead of 'HEAD' @@ -70,23 +71,15 @@ function M.pre_tool_use_hook() M.baseline_commit = 'HEAD' utils.write_file(baseline_file, 'HEAD') end - end -- Post-tool-use hook: Create stash of Claude's changes and trigger diff review function M.post_tool_use_hook() - - - - -- Run directly without vim.schedule for testing local utils = require 'nvim-claude.utils' - -- Refresh all buffers to show Claude's changes vim.cmd 'checktime' - - -- Check if Claude made any changes local git_root = utils.get_project_root() @@ -109,7 +102,6 @@ function M.post_tool_use_hook() table.insert(modified_files, file) end end - -- Get the baseline commit reference first local baseline_ref = utils.read_file '/tmp/claude-baseline-commit' @@ -146,7 +138,6 @@ function M.post_tool_use_hook() -- Get the original content (from baseline) local baseline_cmd = string.format('cd "%s" && git show %s:%s 2>/dev/null', git_root, baseline_ref or 'HEAD', file) local original_content, orig_err = utils.exec(baseline_cmd) - if not orig_err and original_content then -- Get current content @@ -156,7 +147,6 @@ function M.post_tool_use_hook() -- Show inline diff inline_diff.show_inline_diff(buf, original_content, current_content) opened_inline = true - -- Switch to that buffer if it's not the current one if buf ~= vim.api.nvim_get_current_buf() then diff --git a/lua/nvim-claude/inline-diff.lua b/lua/nvim-claude/inline-diff.lua index 49e0a4bf..5346c910 100644 --- a/lua/nvim-claude/inline-diff.lua +++ b/lua/nvim-claude/inline-diff.lua @@ -355,11 +355,53 @@ function M.reject_current_hunk(bufnr) -- Revert the hunk by applying original content M.revert_hunk_changes(bufnr, hunk) - -- Update baseline to the state after rejection - local current_lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) - local current_content = table.concat(current_lines, '\n') - M.original_content[bufnr] = current_content - vim.notify('Baseline updated after rejection', vim.log.levels.INFO) + -- Create a new baseline commit with the rejected changes reverted + local utils = require('nvim-claude.utils') + local git_root = utils.get_project_root() + + if git_root then + -- Save the buffer to ensure changes are on disk + vim.api.nvim_buf_call(bufnr, function() + if vim.bo.modified then + vim.cmd('write') + vim.notify('Buffer saved', vim.log.levels.INFO) + else + vim.notify('Buffer already saved', vim.log.levels.INFO) + end + end) + + -- Stage only the current file (now with hunk reverted) + local file_path = vim.api.nvim_buf_get_name(bufnr) + local relative_path = file_path:gsub('^' .. git_root .. '/', '') + + -- Create a new baseline commit with only this file + local timestamp = os.time() + local commit_msg = string.format('claude-baseline-%d (rejected changes)', timestamp) + + -- Use git commit with only the specific file + local commit_cmd = string.format('cd "%s" && git add "%s" && git commit -m "%s" -- "%s"', + git_root, relative_path, commit_msg, relative_path) + local commit_result, commit_err = utils.exec(commit_cmd) + + if not commit_err or commit_err:match('nothing to commit') then + -- Get the new 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+', '') + -- Update the baseline file + utils.write_file('/tmp/claude-baseline-commit', commit_hash) + + -- Update in-memory baseline + local current_lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) + local current_content = table.concat(current_lines, '\n') + M.original_content[bufnr] = current_content + + vim.notify('Baseline commit created after rejection: ' .. commit_hash:sub(1, 7), vim.log.levels.INFO) + end + end + end -- Remove this hunk from the diff data since it's rejected table.remove(diff_data.hunks, diff_data.current_hunk) @@ -525,18 +567,65 @@ end -- Update baseline content after accepting a hunk function M.update_baseline_after_accept(bufnr, hunk) - -- Get current content (which now includes the accepted change) - local current_lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) - local current_content = table.concat(current_lines, '\n') + local utils = require('nvim-claude.utils') + local git_root = utils.get_project_root() + + if not git_root then + vim.notify('Not in a git repository', vim.log.levels.ERROR) + return + end + + -- Save the buffer to ensure changes are on disk + vim.api.nvim_buf_call(bufnr, function() + if vim.bo.modified then + vim.cmd('write') + vim.notify('Buffer saved', vim.log.levels.INFO) + else + vim.notify('Buffer already saved', vim.log.levels.INFO) + end + end) + + -- Stage only the current file + local file_path = vim.api.nvim_buf_get_name(bufnr) + local relative_path = file_path:gsub('^' .. git_root .. '/', '') - vim.notify('DEBUG: Updating baseline for buffer ' .. bufnr, vim.log.levels.INFO) - vim.notify('DEBUG: New baseline content length: ' .. #current_content, vim.log.levels.INFO) + -- Create a new baseline commit with only this file + local timestamp = os.time() + local commit_msg = string.format('claude-baseline-%d (accepted changes)', timestamp) - -- Update the stored original content to match current content - -- This way, future diffs will compare against this new baseline - M.original_content[bufnr] = current_content + -- Use git commit with only the specific file + local commit_cmd = string.format('cd "%s" && git add "%s" && git commit -m "%s" -- "%s"', + git_root, relative_path, commit_msg, relative_path) + local commit_result, commit_err = utils.exec(commit_cmd) - vim.notify('Baseline updated to include accepted changes', vim.log.levels.INFO) + vim.notify('Commit command: ' .. commit_cmd, vim.log.levels.INFO) + vim.notify('Commit result: ' .. (commit_result or 'nil'), vim.log.levels.INFO) + + if commit_result and (commit_result:match('1 file changed') or commit_result:match('create mode')) then + -- Commit was successful + 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+', '') + -- Update the baseline file + utils.write_file('/tmp/claude-baseline-commit', commit_hash) + + -- Update in-memory baseline + local current_lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) + local current_content = table.concat(current_lines, '\n') + M.original_content[bufnr] = current_content + + vim.notify('Baseline commit created: ' .. commit_hash:sub(1, 7), vim.log.levels.INFO) + else + vim.notify('Failed to get commit hash: ' .. (hash_err or 'unknown'), vim.log.levels.ERROR) + end + else + vim.notify('Failed to create baseline commit', vim.log.levels.ERROR) + if commit_err then + vim.notify('Error: ' .. commit_err, vim.log.levels.ERROR) + end + end end -- Test keymap functionality