#!/bin/bash # Ship The Sovereign Cloud - Repository Management Script # Manages upstream repository cloning and updating for Cloudron packaging # Runs on HOST (not in container) - uses host git commands set -euo pipefail # Script configuration SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PACKAGE_LIST="${SCRIPT_DIR}/PackageList.txt" TARGET_DIR="${SCRIPT_DIR}/Docker" WORKSPACE_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Logging functions log_info() { echo -e "${BLUE}[INFO]${NC} $1" } log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } # Check prerequisites check_prerequisites() { log_info "Checking prerequisites..." if ! command -v git &> /dev/null; then log_error "git is not installed or not in PATH" exit 1 fi if [[ ! -f "$PACKAGE_LIST" ]]; then log_error "PackageList.txt not found at: $PACKAGE_LIST" exit 1 fi log_success "Prerequisites check passed" } # Create target directory setup_workspace() { log_info "Setting up workspace..." mkdir -p "$TARGET_DIR" log_success "Workspace ready at: $TARGET_DIR" } # Extract repository URLs from PackageList.txt get_repo_urls() { # Extract URLs, ignoring comments and empty lines grep -v '^#' "$PACKAGE_LIST" | grep -v '^$' | grep -v '^[[:space:]]*$' || true } # Clone or update a single repository manage_single_repo() { local repo_url="$1" local repo_name local repo_path # Extract repository name from URL repo_name=$(basename "$repo_url" .git) repo_path="$TARGET_DIR/$repo_name" log_info "Processing: $repo_name" if [[ -d "$repo_path/.git" ]]; then # Repository exists, update it log_info "Updating existing repository: $repo_name" cd "$repo_path" # Fetch latest changes if git fetch --all --prune; then # Try to fast-forward merge if git pull --ff-only; then log_success "Updated: $repo_name" else log_warning "Could not fast-forward $repo_name (may have local changes)" log_info "Repository $repo_name is up to date but has diverged" fi else log_warning "Failed to fetch updates for $repo_name" fi else # Repository doesn't exist, clone it log_info "Cloning new repository: $repo_name" cd "$TARGET_DIR" if git clone --depth 1 "$repo_url" "$repo_name"; then log_success "Cloned: $repo_name" else log_error "Failed to clone: $repo_url" return 1 fi fi } # Main execution function main() { log_info "Ship The Sovereign Cloud - Repository Manager" log_info "==============================================" check_prerequisites setup_workspace # Get all repository URLs local repo_urls mapfile -t repo_urls < <(get_repo_urls) if [[ ${#repo_urls[@]} -eq 0 ]]; then log_warning "No repositories found in PackageList.txt" exit 0 fi log_info "Found ${#repo_urls[@]} repositories to manage" # Process each repository local success_count=0 local error_count=0 for repo_url in "${repo_urls[@]}"; do if [[ -n "$repo_url" ]]; then if manage_single_repo "$repo_url"; then ((success_count++)) else ((error_count++)) fi fi done # Summary log_info "==============================================" log_info "Repository management complete" log_success "Successfully processed: $success_count" if [[ $error_count -gt 0 ]]; then log_error "Failed: $error_count" fi log_info "Total repositories: ${#repo_urls[@]}" # Show workspace status log_info "Workspace location: $TARGET_DIR" log_info "Repository count: $(find "$TARGET_DIR" -maxdepth 1 -type d -name ".git" | wc -l)" } # Handle script arguments case "${1:-}" in "help"|"-h"|"--help") echo "Ship The Sovereign Cloud - Repository Manager" echo "" echo "Usage: $0 [command]" echo "" echo "Commands:" echo " (no args) - Clone missing repos and update existing ones" echo " help - Show this help message" echo "" echo "This script reads from PackageList.txt and manages repositories" echo "in the Docker/ subdirectory." exit 0 ;; "") main ;; *) log_error "Unknown command: $1" log_info "Use '$0 help' for usage information" exit 1 ;; esac