#!/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 bash -c " # 日志轮转:超过 5MB 保留最后 1000 行 if [[ -f '$log_file' ]] && [[ \$(wc -c < '$log_file') -gt 5242880 ]]; then tail -n 1000 '$log_file' > '$log_file.tmp' && mv '$log_file.tmp' '$log_file' fi uv run --project '$vault_dir/.scripts/memory' python '$vault_dir/.scripts/memory/incremental_ingest.py' \ --changes-file '$changes_file' >> '$log_file' 2>&1 exit_code=\$? if [[ \$exit_code -ne 0 ]]; then echo \"\$(date '+%Y-%m-%d %H:%M:%S') [ERROR] incremental_ingest 退出码=\$exit_code\" >> '$log_file' fi rm -f '$changes_file' " & } run_memory_async_index # --- memory async index hook end --- HOOK echo "[memory] post-commit hook 已安装。"