Skip to content

Commit fd8fa78

Browse files
jupblbampcode-com
andcommitted
refactor: move git hook content to dedicated shell template file
- Extract post-commit hook shell script to lua/zoekt/templates/post-commit-hook.sh - Update hook.lua to read template from file instead of embedding it - Use {{INDEX_PATH}} placeholder for dynamic index path substitution - Update tests to ensure runtime path includes plugin directory for template access Co-authored-by: Amp <[email protected]> Amp-Thread-ID: https://ampcode.com/threads/T-b6730e29-eb49-4ea9-9bea-b8c3ae74a841
1 parent 5e00ca2 commit fd8fa78

File tree

3 files changed

+72
-28
lines changed

3 files changed

+72
-28
lines changed

lua/zoekt/hook.lua

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,50 @@ local M = {}
22
local config = require('zoekt.config')
33
local utils = require('zoekt.utils')
44

5+
-- Get the path to the hook template
6+
local function get_hook_template_path()
7+
-- Try to find the template in the runtime path
8+
local template_paths = vim.api.nvim_get_runtime_file(
9+
'lua/zoekt/templates/post-commit-hook.sh',
10+
false
11+
)
12+
13+
if #template_paths > 0 then
14+
return template_paths[1]
15+
end
16+
17+
-- Fallback: try to find it relative to this file
18+
local source = debug.getinfo(1, 'S').source
19+
if source:sub(1, 1) == '@' then
20+
source = source:sub(2)
21+
end
22+
local dir = vim.fn.fnamemodify(source, ':p:h')
23+
local template_path = dir .. '/templates/post-commit-hook.sh'
24+
25+
-- Check if file exists
26+
if vim.fn.filereadable(template_path) == 1 then
27+
return template_path
28+
end
29+
30+
error('Could not find hook template file')
31+
end
32+
533
-- Create the post-commit hook content
634
local function create_hook_content()
735
local index_path = config.get_option('index_path')
36+
local template_path = get_hook_template_path()
37+
38+
-- Read the template file
39+
local file = io.open(template_path, 'r')
40+
if not file then
41+
error('Could not read hook template file: ' .. template_path)
42+
end
43+
44+
local hook_content = file:read('*all')
45+
file:close()
846

9-
-- Create a standalone shell script that doesn't depend on Neovim
10-
local hook_content = [[#!/usr/bin/env sh
11-
# Auto-generated by zoekt.nvim
12-
# This hook updates the Zoekt index after each commit
13-
14-
# Get the repository root
15-
REPO_ROOT="$(git rev-parse --show-toplevel)"
16-
17-
# Check if zoekt-git-index is available
18-
if ! command -v zoekt-git-index >/dev/null 2>&1; then
19-
echo "Warning: zoekt-git-index not found in PATH. Skipping index update."
20-
exit 0
21-
fi
22-
23-
# Get the current branch name
24-
BRANCH="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"
25-
26-
# Index the repository
27-
echo "Updating Zoekt index..."
28-
# Index HEAD
29-
zoekt-git-index -index "]] .. index_path .. [[" -branches HEAD "$REPO_ROOT"
30-
# Also index the current branch if we're on a branch (not detached HEAD)
31-
if [ "$BRANCH" != "HEAD" ]; then
32-
zoekt-git-index -index "]] .. index_path .. [[" -branches "$BRANCH" "$REPO_ROOT"
33-
fi
34-
35-
exit 0
36-
]]
47+
-- Replace the placeholder with the actual index path
48+
hook_content = hook_content:gsub('{{INDEX_PATH}}', index_path)
3749

3850
return hook_content
3951
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env sh
2+
# Auto-generated by zoekt.nvim
3+
# This hook updates the Zoekt index after each commit
4+
5+
# Get the repository root
6+
REPO_ROOT="$(git rev-parse --show-toplevel)"
7+
8+
# Check if zoekt-git-index is available
9+
if ! command -v zoekt-git-index >/dev/null 2>&1; then
10+
echo "Warning: zoekt-git-index not found in PATH. Skipping index update."
11+
exit 0
12+
fi
13+
14+
# Get the current branch name
15+
BRANCH="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"
16+
17+
# Index the repository
18+
echo "Updating Zoekt index..."
19+
# Index HEAD
20+
zoekt-git-index -index "{{INDEX_PATH}}" -branches HEAD "$REPO_ROOT"
21+
# Also index the current branch if we're on a branch (not detached HEAD)
22+
if [ "$BRANCH" != "HEAD" ]; then
23+
zoekt-git-index -index "{{INDEX_PATH}}" -branches "$BRANCH" "$REPO_ROOT"
24+
fi
25+
26+
exit 0

tests/zoekt_hook_spec.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ describe('zoekt.hook', function()
99
-- Store original CWD
1010
original_cwd = vim.fn.getcwd()
1111

12+
-- Ensure the plugin directory is in the runtime path for template loading
13+
local plugin_dir = vim.fn.fnamemodify(original_cwd, ':p')
14+
if not vim.tbl_contains(vim.opt.rtp:get(), plugin_dir) then
15+
vim.opt.rtp:append(plugin_dir)
16+
end
17+
1218
-- Clear module cache
1319
package.loaded['zoekt.hook'] = nil
1420
package.loaded['zoekt.config'] = nil

0 commit comments

Comments
 (0)