Skip to content

Instantly share code, notes, and snippets.

@airborne-commando
Created March 13, 2026 06:21
Show Gist options
  • Select an option

  • Save airborne-commando/6710d5a6e44512932fb64aa87d765f16 to your computer and use it in GitHub Desktop.

Select an option

Save airborne-commando/6710d5a6e44512932fb64aa87d765f16 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Script to clean all radicle repositories with improved parsing
# Usage: ./radicle-clean-all.sh [--force] [--log-file FILE]
LOG_FILE="radicle-clean-$(date +%Y%m%d-%H%M%S).log"
FORCE_MODE=false
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--force)
FORCE_MODE=true
shift
;;
--log-file)
LOG_FILE="$2"
shift 2
;;
*)
echo "Unknown option: $1"
echo "Usage: $0 [--force] [--log-file FILE]"
exit 1
;;
esac
done
# Function to log messages
log_message() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
# Check if rad command is available
if ! command -v rad &> /dev/null; then
log_message "ERROR: 'rad' command not found"
exit 1
fi
log_message "Starting radicle repository cleaning"
# Debug: Show raw output
log_message "Raw rad ls output:"
rad ls 2>/dev/null | while read -r line; do
log_message " $line"
done
# Get list of repositories - improved parsing
log_message "Parsing repository list..."
# Method 1: Extract RIDs directly from the output
REPOS=$(rad ls 2>/dev/null | grep -o 'rad:[a-zA-Z0-9]\+' | sort -u)
if [ -z "$REPOS" ]; then
log_message "WARNING: No repositories found with Method 1. Trying alternative parsing..."
# Method 2: More aggressive parsing
REPOS=$(rad ls 2>/dev/null | grep -E 'rad:[a-zA-Z0-9]{40,}' | awk '{for(i=1;i<=NF;i++) if($i ~ /^rad:/) print $i}')
fi
if [ -z "$REPOS" ]; then
log_message "ERROR: Still no repositories found. Debugging output:"
rad ls 2>/dev/null | while read -r line; do
log_message "LINE: $line"
done
exit 1
fi
REPO_COUNT=$(echo "$REPOS" | wc -l)
log_message "Found $REPO_COUNT repositories to clean"
# Show repositories
log_message "Repositories found:"
echo "$REPOS" | while read -r repo; do
if [ -n "$repo" ]; then
log_message " - $repo"
fi
done
# Ask for confirmation if not in force mode
if [ "$FORCE_MODE" = false ]; then
echo ""
echo "Repositories to be cleaned:"
echo "$REPOS" | while read -r repo; do
if [ -n "$repo" ]; then
echo " - $repo"
fi
done
echo ""
read -p "Proceed with cleaning? (y/N) " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
log_message "Operation cancelled by user"
exit 0
fi
fi
# Clean repositories
SUCCESS=0
FAILED=0
echo "$REPOS" | while read -r repo; do
if [ -n "$repo" ]; then
log_message "Cleaning $repo"
if rad clean "$repo" --no-confirm 2>>"$LOG_FILE"; then
log_message "✓ Successfully cleaned $repo"
((SUCCESS++))
else
log_message "✗ Failed to clean $repo"
((FAILED++))
fi
fi
done
log_message "Cleaning complete - Success: $SUCCESS, Failed: $FAILED"
echo ""
echo "Log file saved to: $LOG_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment