git-timestamps/install-ots-hook.sh
2026-03-07 16:28:51 +01:00

89 lines
2.7 KiB
Bash
Executable file

#!/bin/bash
# install-ots-hook.sh - Install OpenTimestamp post-commit hook
# Usage: install-ots-hook.sh [repository-path] [backfill]
set -e
REPO_PATH="${1:-.}"
ENABLE_BACKFILL="${2:-}" # Second arg: "backfill" to enable pre-commit backfill hook
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$REPO_PATH"
# Verify we're in a git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
echo "Error: not a git repository" >&2
exit 1
fi
HOOKS_DIR="$(git rev-parse --git-dir)/hooks"
POST_COMMIT_HOOK="$HOOKS_DIR/post-commit"
echo "Installing OpenTimestamp hooks in: $REPO_PATH"
# Create the post-commit hook
cat > "$POST_COMMIT_HOOK" << 'HOOK_SCRIPT'
#!/bin/bash
# Post-commit hook: generate OpenTimestamp proof for each commit
set -e
# Get the commit hash
COMMIT_HASH=$(git rev-parse HEAD)
# Find the generate-proof.sh script
# Try relative paths first, then check common locations
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
GENERATE_SCRIPT=""
# Check if generate-proof.sh is in the same directory as this hook
if [ -f "$SCRIPT_DIR/generate-proof.sh" ]; then
GENERATE_SCRIPT="$SCRIPT_DIR/generate-proof.sh"
# Check in .git/hooks directory
elif [ -f ".git/hooks/generate-proof.sh" ]; then
GENERATE_SCRIPT=".git/hooks/generate-proof.sh"
# Check in repository root
elif [ -f "generate-proof.sh" ]; then
GENERATE_SCRIPT="./generate-proof.sh"
# Check if skill is installed globally (common OpenClaw path)
elif [ -f "$HOME/.openclaw/workspace/skills/git-ots-hook/scripts/generate-proof.sh" ]; then
GENERATE_SCRIPT="$HOME/.openclaw/workspace/skills/git-ots-hook/scripts/generate-proof.sh"
else
echo "Warning: generate-proof.sh not found, skipping OpenTimestamp proof" >&2
exit 0
fi
# Make sure the script is executable
chmod +x "$GENERATE_SCRIPT"
# Generate the proof
echo "[ots] Generating proof for commit: $COMMIT_HASH"
"$GENERATE_SCRIPT" "$COMMIT_HASH"
echo "[ots] Proof generated successfully"
HOOK_SCRIPT
# Make the hook executable
chmod +x "$POST_COMMIT_HOOK"
echo "✓ Post-commit hook installed successfully"
# Optionally install pre-commit backfill hook
if [ "$ENABLE_BACKFILL" = "backfill" ]; then
PRE_COMMIT_HOOK="$HOOKS_DIR/pre-commit"
cp "$SCRIPT_DIR/pre-commit-backfill" "$PRE_COMMIT_HOOK"
chmod +x "$PRE_COMMIT_HOOK"
echo "✓ Pre-commit backfill hook installed (will backfill proofs before each commit)"
fi
echo ""
echo "Next steps:"
echo "1. Ensure scripts are accessible (copy to repo root or install skill globally)"
echo "2. Verify ots CLI or @opentimestamps/ots is installed"
echo "3. Make a test commit to verify the hooks work"
echo ""
echo "To uninstall:"
echo " rm $POST_COMMIT_HOOK"
if [ "$ENABLE_BACKFILL" = "backfill" ]; then
echo " rm $PRE_COMMIT_HOOK"
fi