Rename hooks to standard git hook names
- Rename post-commit-ots → post-commit - Rename pre-commit-ots → pre-commit - Remove legacy post-commit and pre-commit symlinks - Update install.sh and documentation - Simplified: only 2 hook files with standard names Hooks are now named exactly as git expects them, making manual installation more intuitive.
This commit is contained in:
parent
b4e2eb3c12
commit
948e7d9d70
8 changed files with 44 additions and 291 deletions
|
|
@ -1,24 +1,19 @@
|
|||
#!/bin/bash
|
||||
# Git OpenTimestamp Pre-Commit Backfill Hook
|
||||
# Self-contained - no external dependencies
|
||||
# Upgrades all historical proofs before each new commit
|
||||
# Git OpenTimestamp Pre-Commit Backfill Hook (ots CLI version)
|
||||
# Requires: opentimestamps-client (pipx install opentimestamps-client)
|
||||
|
||||
set -e
|
||||
|
||||
# Configuration
|
||||
OUTPUT_DIR=".ots"
|
||||
STATUS_CACHE="$OUTPUT_DIR/.attestation-cache"
|
||||
|
||||
# Ensure output directory exists
|
||||
mkdir -p "$OUTPUT_DIR"
|
||||
|
||||
# Initialize cache file if it doesn't exist
|
||||
# Initialize cache
|
||||
if [ ! -f "$STATUS_CACHE" ]; then
|
||||
echo "# Attestation status cache" > "$STATUS_CACHE"
|
||||
echo "# Format: commit-hash:status:timestamp" >> "$STATUS_CACHE"
|
||||
echo "# Format: commit-hash:status:timestamp" > "$STATUS_CACHE"
|
||||
fi
|
||||
|
||||
# Function to get cached status
|
||||
get_cached_status() {
|
||||
local commit="$1"
|
||||
local cache_line=$(grep "^$commit:" "$STATUS_CACHE" 2>/dev/null | tail -1)
|
||||
|
|
@ -26,9 +21,7 @@ get_cached_status() {
|
|||
local status=$(echo "$cache_line" | cut -d: -f2)
|
||||
local timestamp=$(echo "$cache_line" | cut -d: -f3)
|
||||
local now=$(date +%s)
|
||||
local age=$((now - timestamp))
|
||||
# Cache valid for 1 hour (3600 seconds)
|
||||
if [ "$age" -lt 3600 ]; then
|
||||
if [ "$((now - timestamp))" -lt 3600 ]; then
|
||||
echo "$status"
|
||||
return 0
|
||||
fi
|
||||
|
|
@ -36,19 +29,15 @@ get_cached_status() {
|
|||
return 1
|
||||
}
|
||||
|
||||
# Function to cache status
|
||||
cache_status() {
|
||||
local commit="$1"
|
||||
local status="$2"
|
||||
local now=$(date +%s)
|
||||
echo "$commit:$status:$now" >> "$STATUS_CACHE"
|
||||
echo "$commit:$status:$(date +%s)" >> "$STATUS_CACHE"
|
||||
}
|
||||
|
||||
# Function to generate proof using ots CLI
|
||||
generate_proof() {
|
||||
local hash="$1"
|
||||
local output="$2"
|
||||
|
||||
local temp_file=$(mktemp)
|
||||
local temp_ots="${temp_file}.ots"
|
||||
|
||||
|
|
@ -59,20 +48,17 @@ generate_proof() {
|
|||
mv "$temp_ots" "$output"
|
||||
rm -f "$temp_file"
|
||||
return 0
|
||||
else
|
||||
rm -f "$temp_file"
|
||||
return 1
|
||||
fi
|
||||
rm -f "$temp_file"
|
||||
return 1
|
||||
}
|
||||
|
||||
# Function to check if proof is attested
|
||||
is_attested() {
|
||||
local proof_file="$1"
|
||||
local pending_count=$(ots info "$proof_file" 2>&1 | grep -c "PendingAttestation" || echo "0")
|
||||
[ "$pending_count" -eq 0 ]
|
||||
}
|
||||
|
||||
# Function to upgrade proof
|
||||
upgrade_proof() {
|
||||
local proof_file="$1"
|
||||
ots upgrade "$proof_file" 2>/dev/null
|
||||
|
|
@ -80,7 +66,6 @@ upgrade_proof() {
|
|||
|
||||
echo "[ots] Backfilling proofs..."
|
||||
|
||||
# Get all commit hashes (oldest to newest)
|
||||
COMMITS=$(git rev-list --reverse HEAD)
|
||||
TOTAL=$(echo "$COMMITS" | wc -l)
|
||||
CURRENT=0
|
||||
|
|
@ -90,47 +75,35 @@ for COMMIT in $COMMITS; do
|
|||
CURRENT=$((CURRENT + 1))
|
||||
PROOF_FILE="$OUTPUT_DIR/${COMMIT}.ots"
|
||||
|
||||
# Skip verbose output for brevity
|
||||
if [ $CURRENT -le 3 ] || [ $CURRENT -eq $TOTAL ]; then
|
||||
echo "[ots] Processing commit $CURRENT/$TOTAL: ${COMMIT:0:8}"
|
||||
echo "[ots] Processing $CURRENT/$TOTAL: ${COMMIT:0:8}"
|
||||
elif [ $CURRENT -eq 4 ]; then
|
||||
echo "[ots] ... processing remaining commits ..."
|
||||
echo "[ots] ... processing remaining ..."
|
||||
fi
|
||||
|
||||
if [ -f "$PROOF_FILE" ]; then
|
||||
# Check cached status first
|
||||
CACHED_STATUS=$(get_cached_status "$COMMIT" || echo "")
|
||||
|
||||
if [ "$CACHED_STATUS" = "attested" ]; then
|
||||
continue
|
||||
fi
|
||||
[ "$CACHED_STATUS" = "attested" ] && continue
|
||||
|
||||
# Check if already attested
|
||||
if is_attested "$PROOF_FILE"; then
|
||||
cache_status "$COMMIT" "attested"
|
||||
continue
|
||||
fi
|
||||
|
||||
# Cache as pending
|
||||
cache_status "$COMMIT" "pending"
|
||||
|
||||
# Skip upgrade if cache is fresh (< 10 min old)
|
||||
CACHE_LINE=$(grep "^$COMMIT:" "$STATUS_CACHE" | tail -1)
|
||||
CACHE_TIME=$(echo "$CACHE_LINE" | cut -d: -f3)
|
||||
NOW=$(date +%s)
|
||||
CACHE_AGE=$((NOW - CACHE_TIME))
|
||||
CACHE_AGE=$(($(date +%s) - CACHE_TIME))
|
||||
|
||||
if [ "$CACHE_AGE" -lt 600 ]; then
|
||||
continue
|
||||
fi
|
||||
[ "$CACHE_AGE" -lt 600 ] && continue
|
||||
|
||||
# Try to upgrade
|
||||
if upgrade_proof "$PROOF_FILE"; then
|
||||
cache_status "$COMMIT" "attested"
|
||||
UPDATED=$((UPDATED + 1))
|
||||
fi
|
||||
else
|
||||
# Generate new proof
|
||||
if generate_proof "$COMMIT" "$PROOF_FILE"; then
|
||||
cache_status "$COMMIT" "pending"
|
||||
UPDATED=$((UPDATED + 1))
|
||||
|
|
@ -138,19 +111,15 @@ for COMMIT in $COMMITS; do
|
|||
fi
|
||||
done
|
||||
|
||||
# Update latest proof symlink
|
||||
# Update latest proof
|
||||
LATEST_COMMIT=$(git rev-parse HEAD)
|
||||
if [ -f "$OUTPUT_DIR/${LATEST_COMMIT}.ots" ]; then
|
||||
cp "$OUTPUT_DIR/${LATEST_COMMIT}.ots" "$OUTPUT_DIR/proof.ots"
|
||||
fi
|
||||
[ -f "$OUTPUT_DIR/${LATEST_COMMIT}.ots" ] && cp "$OUTPUT_DIR/${LATEST_COMMIT}.ots" "$OUTPUT_DIR/proof.ots"
|
||||
|
||||
# Save commit chain
|
||||
rm -f "$OUTPUT_DIR/commit-chain.txt"
|
||||
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" >> "$OUTPUT_DIR/commit-chain.txt"
|
||||
fi
|
||||
[ -n "$PREV" ] && echo "$COMMIT:$PREV" >> "$OUTPUT_DIR/commit-chain.txt"
|
||||
done
|
||||
|
||||
echo "[ots] Backfill complete: $UPDATED proofs updated"
|
||||
echo "[ots] Backfill complete: $UPDATED updated"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue