git-timestamps/hooks/install.sh
2026-03-08 00:36:44 +01:00

93 lines
2.7 KiB
Bash
Executable file

#!/bin/bash
# Git OpenTimestamp Hooks Installer
# Detects available tools and installs matching hooks
set -e
REPO_PATH="${1:-.}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$REPO_PATH"
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"
MODE=""
# Detect available tools
if command -v ots &> /dev/null; then
MODE="ots"
echo "Detected: ots CLI"
elif command -v node &> /dev/null && node -e "require('@opentimestamps/ots')" &> /dev/null 2>&1; then
MODE="node"
echo "Detected: @opentimestamps/ots (Node.js)"
echo "Note: Node version creates local proofs only (no calendar submission)"
echo ""
# Install node package locally if not already present
if [ ! -d "node_modules/@opentimestamps" ]; then
echo "Installing @opentimestamps/ots locally..."
npm install @opentimestamps/ots
echo "✓ Package installed"
fi
else
echo "Error: Neither ots CLI nor @opentimestamps/ots found" >&2
echo "Install one of:" >&2
echo " pipx install opentimestamps-client (recommended)" >&2
echo " npm install @opentimestamps/ots (local proofs only)" >&2
exit 1
fi
echo ""
echo "Installing $MODE hooks to: $HOOKS_DIR"
# Copy appropriate hooks
if [ "$MODE" = "ots" ]; then
cp "$SCRIPT_DIR/post-commit-ots" "$HOOKS_DIR/post-commit"
cp "$SCRIPT_DIR/pre-commit-ots" "$HOOKS_DIR/pre-commit"
else
cp "$SCRIPT_DIR/post-commit-node" "$HOOKS_DIR/post-commit"
cp "$SCRIPT_DIR/pre-commit-node" "$HOOKS_DIR/pre-commit"
fi
chmod +x "$HOOKS_DIR/post-commit" "$HOOKS_DIR/pre-commit"
echo "✓ Post-commit hook installed"
echo "✓ Pre-commit backfill hook installed"
# Setup .gitignore
GITIGNORE=".gitignore"
if [ ! -f "$GITIGNORE" ]; then
cat > "$GITIGNORE" << 'EOF'
.ots/.attestation-cache
node_modules/
EOF
echo "✓ Created .gitignore"
elif ! grep -q ".ots/.attestation-cache" "$GITIGNORE"; then
echo ".ots/.attestation-cache" >> "$GITIGNORE"
if [ "$MODE" = "node" ] && ! grep -q "node_modules/" "$GITIGNORE"; then
echo "node_modules/" >> "$GITIGNORE"
fi
echo "✓ Updated .gitignore"
fi
echo ""
echo "Mode: $MODE"
if [ "$MODE" = "ots" ]; then
echo " - Full Bitcoin attestation via calendars"
echo " - Proofs submitted to remote calendars"
else
echo " - Local proofs only (no calendar submission)"
echo " - For full attestation, use: pipx install opentimestamps-client"
fi
echo ""
echo "Next steps:"
echo "1. Make a test commit"
echo "2. Commit proofs: git add .ots/ && git commit -m 'Add OpenTimestamp proofs'"
echo ""
echo "To uninstall:"
echo " rm $HOOKS_DIR/post-commit $HOOKS_DIR/pre-commit"