diff --git a/.ots/prev-commit.txt b/.ots/prev-commit.txt index 2fe79b2..9687ed2 100644 --- a/.ots/prev-commit.txt +++ b/.ots/prev-commit.txt @@ -1 +1 @@ -4a6f5ed0c12315b0bc8a0fa5815ada1bd20e5963 +46aded7b9582bbed673843e2cf8a3f8fa742ad91 diff --git a/.ots/proof.ots b/.ots/proof.ots index 8542c60..14d73c1 100644 Binary files a/.ots/proof.ots and b/.ots/proof.ots differ diff --git a/backfill-proofs.sh b/backfill-proofs.sh new file mode 100755 index 0000000..6fbc16e --- /dev/null +++ b/backfill-proofs.sh @@ -0,0 +1,97 @@ +#!/bin/bash +# backfill-proofs.sh - Generate/upgrade OpenTimestamp proofs for all commits +# Usage: backfill-proofs.sh [repository-path] + +set -e + +REPO_PATH="${1:-.}" +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 + +# Find the generate-proof.sh script +GENERATE_SCRIPT="" + +# Check if generate-proof.sh is in the same directory as this script +if [ -f "$SCRIPT_DIR/generate-proof.sh" ]; then + GENERATE_SCRIPT="$SCRIPT_DIR/generate-proof.sh" +# Check in repository root +elif [ -f "generate-proof.sh" ]; then + GENERATE_SCRIPT="./generate-proof.sh" +# Check if skill is installed globally +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 "Error: generate-proof.sh not found" >&2 + exit 1 +fi + +chmod +x "$GENERATE_SCRIPT" + +# Ensure .ots directory exists +mkdir -p .ots + +echo "[ots-backfill] Scanning commit history..." + +# Get all commit hashes (oldest to newest) +COMMITS=$(git rev-list --reverse HEAD) +TOTAL=$(echo "$COMMITS" | wc -l) +CURRENT=0 + +# Track if any proofs were updated +UPDATED=0 + +for COMMIT in $COMMITS; do + CURRENT=$((CURRENT + 1)) + echo "[ots-backfill] Processing commit $CURRENT/$TOTAL: ${COMMIT:0:8}" + + # Check if proof exists for this commit + PROOF_FILE=".ots/${COMMIT}.ots" + + if [ -f "$PROOF_FILE" ]; then + # Try to upgrade existing proof + echo " Upgrading existing proof..." + if ots upgrade "$PROOF_FILE" 2>/dev/null; then + echo " ✓ Upgraded to attested" + UPDATED=$((UPDATED + 1)) + else + echo " - Still pending (no upgrade available)" + fi + else + # Generate new proof for this commit + echo " Generating new proof..." + "$GENERATE_SCRIPT" "$COMMIT" "$PROOF_FILE" >/dev/null 2>&1 + echo " ✓ Generated proof" + UPDATED=$((UPDATED + 1)) + fi +done + +# Create/update latest proof symlink +LATEST_COMMIT=$(git rev-parse HEAD) +if [ -f ".ots/${LATEST_COMMIT}.ots" ]; then + cp ".ots/${LATEST_COMMIT}.ots" ".ots/proof.ots" + echo "[ots-backfill] Updated .ots/proof.ots for latest commit" +fi + +# Save previous commit chain info +echo "[ots-backfill] Saving commit chain..." +git rev-list HEAD | while read COMMIT; do + PREV=$(git rev-parse ${COMMIT}^1 2>/dev/null || echo "") + if [ -n "$PREV" ]; then + echo "$COMMIT:$PREV" >> ".ots/commit-chain.txt" + fi +done + +echo "" +echo "[ots-backfill] Complete! Processed $TOTAL commits, updated $UPDATED proofs" +echo "" +echo "Proofs stored in: .ots/" +echo " - Individual proofs: .ots/.ots" +echo " - Latest proof: .ots/proof.ots" +echo " - Commit chain: .ots/commit-chain.txt" diff --git a/install-ots-hook.sh b/install-ots-hook.sh index ff425a5..06c7f63 100755 --- a/install-ots-hook.sh +++ b/install-ots-hook.sh @@ -1,10 +1,11 @@ #!/bin/bash # install-ots-hook.sh - Install OpenTimestamp post-commit hook -# Usage: install-ots-hook.sh [repository-path] +# 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" @@ -18,7 +19,7 @@ fi HOOKS_DIR="$(git rev-parse --git-dir)/hooks" POST_COMMIT_HOOK="$HOOKS_DIR/post-commit" -echo "Installing OpenTimestamp hook in: $REPO_PATH" +echo "Installing OpenTimestamp hooks in: $REPO_PATH" # Create the post-commit hook cat > "$POST_COMMIT_HOOK" << 'HOOK_SCRIPT' @@ -66,10 +67,23 @@ HOOK_SCRIPT 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 generate-proof.sh is accessible (copy to repo root or install skill globally)" +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 hook works" +echo "3. Make a test commit to verify the hooks work" echo "" -echo "To uninstall, run: rm $POST_COMMIT_HOOK" +echo "To uninstall:" +echo " rm $POST_COMMIT_HOOK" +if [ "$ENABLE_BACKFILL" = "backfill" ]; then + echo " rm $PRE_COMMIT_HOOK" +fi diff --git a/pre-commit-backfill b/pre-commit-backfill new file mode 100755 index 0000000..3ff9a7d --- /dev/null +++ b/pre-commit-backfill @@ -0,0 +1,32 @@ +#!/bin/bash +# Pre-commit hook: backfill proofs for all commits before creating new commit +# This ensures all historical commits have attested proofs + +set -e + +# Get the directory where this hook is installed +HOOKS_DIR="$(git rev-parse --git-dir)/hooks" +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +# Find the backfill-proofs.sh script +BACKFILL_SCRIPT="" + +if [ -f "$SCRIPT_DIR/backfill-proofs.sh" ]; then + BACKFILL_SCRIPT="$SCRIPT_DIR/backfill-proofs.sh" +elif [ -f ".git/hooks/backfill-proofs.sh" ]; then + BACKFILL_SCRIPT=".git/hooks/backfill-proofs.sh" +elif [ -f "backfill-proofs.sh" ]; then + BACKFILL_SCRIPT="./backfill-proofs.sh" +elif [ -f "$HOME/.openclaw/workspace/skills/git-ots-hook/scripts/backfill-proofs.sh" ]; then + BACKFILL_SCRIPT="$HOME/.openclaw/workspace/skills/git-ots-hook/scripts/backfill-proofs.sh" +else + # Silently skip if backfill script not found + exit 0 +fi + +chmod +x "$BACKFILL_SCRIPT" + +echo "[ots] Backfilling proofs before commit..." +"$BACKFILL_SCRIPT" . + +echo "[ots] Ready to commit" diff --git a/test4.txt b/test4.txt new file mode 100644 index 0000000..6a11e80 --- /dev/null +++ b/test4.txt @@ -0,0 +1 @@ +# Test pre-commit backfill