Files
my-vault/.scripts/memory/install-hook.sh
T

37 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
VAULT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
HOOK_FILE="$VAULT_DIR/.git/hooks/post-commit"
mkdir -p "$(dirname "$HOOK_FILE")"
touch "$HOOK_FILE"
chmod +x "$HOOK_FILE"
if grep -q "memory async index hook begin" "$HOOK_FILE"; then
echo "[memory] post-commit hook 已存在,跳过。"
exit 0
fi
cat >> "$HOOK_FILE" <<'HOOK'
# --- memory async index hook begin ---
run_memory_async_index() {
local vault_dir changes_file log_file
vault_dir="$(git rev-parse --show-toplevel 2>/dev/null || true)"
[[ -n "$vault_dir" ]] || return 0
log_file="$vault_dir/.memory-sync.log"
changes_file="$vault_dir/.memory-changes-$(date +%s)-$$.txt"
git diff-tree --no-commit-id --name-status -r -M --diff-filter=ACDMRT HEAD -- '*.md' > "$changes_file" 2>/dev/null || true
[[ -s "$changes_file" ]] || { rm -f "$changes_file"; return 0; }
nohup uv run --project "$vault_dir/.scripts/memory" python "$vault_dir/.scripts/memory/incremental_ingest.py" \
--changes-file "$changes_file" >> "$log_file" 2>&1 &
}
run_memory_async_index
# --- memory async index hook end ---
HOOK
echo "[memory] post-commit hook 已安装。"