205 lines
6.2 KiB
Bash

#!/bin/bash
VERSION="v0.9.2.0"
# Function to check if a URL is valid
validate_url() {
local url=$1
if [[ -z "$url" ]]; then
echo "❌ Error: URL cannot be empty."
exit 1
fi
if [[ ! "$url" =~ ^https?:// ]]; then
echo "❌ Error: Invalid URL format: $url"
exit 1
fi
# Blacklist specific URLs
if [[ "$url" == "https://hub.allspice.io/AllSpice-Demos/AllSpice-Loves-SVN-Monorepos.git" || "$url" == "https://hub.allspice.io/AllSpice-Demos/AllSpice-Loves-SVN.git" ]]; then
echo "❌ Error: This repository URL is not allowed: $url"
exit 1
fi
}
# Define LOGFILE location
LOGFILE="$USERPROFILE/allspice/${REPO_NAME}_git_svn_clone.log"
# Initialize a static index variable
declare -g index=0
# Function to log messages with a header
log_header() {
local text="$1"
local color="$2"
local timestamp
timestamp=$(date +"%Y-%m-%d_%H:%M:%S")
# Define color codes
local RESET="\033[0m"
local RED="\033[31m"
local GREEN="\033[32m"
local YELLOW="\033[33m"
local BLUE="\033[34m"
local MAGENTA="\033[35m"
local CYAN="\033[36m"
local WHITE="\033[37m"
# Choose color
case "$color" in
red) color_code=$RED ;;
green) color_code=$GREEN ;;
yellow) color_code=$YELLOW ;;
blue) color_code=$BLUE ;;
magenta) color_code=$MAGENTA ;;
cyan) color_code=$CYAN ;;
white) color_code=$WHITE ;;
*) color_code=$RESET ;; # Default to no color
esac
# Format and print message
local message="${BLUE}[$timestamp],$VERSION, #${index}, ${color_code}$text${RESET}"
echo -e "${message}"
echo "$message" >> "$LOGFILE"
# Increment index
((index++))
}
log_header "AllSpice + SVN deepclone into a monorepo" cyan
log_header "v0.9.1.1" cyan
# Prompt user for ALLSPICE_URL
log_header "Enter the AllSpice URL" yellow
read ALLSPICE_URL
log_header "AllSpice URL: $ALLSPICE_URL" magenta
validate_url "$ALLSPICE_URL"
# Prompt user for SVN_URL
log_header "Enter the SVN URL" yellow
read SVN_URL
log_header "SVN URL: $SVN_URL" magenta
validate_url "$SVN_URL"
# Extract repository name from ALLSPICE_URL
REPO_NAME=$(echo $ALLSPICE_URL | awk -F'/' '{print $NF}' | sed 's/.git//')
log_header "Extracted Repository Name: $REPO_NAME" magenta
log_header "Repo directory: $USERPROFILE/allspice/$REPO_NAME" magenta
# Define log file
LOGFILE="$USERPROFILE/allspice/$REPO_NAME_git_svn_clone.log"
log_header "Cloning AllSpice repository" cyan
log_header "AllSpice URL: $ALLSPICE_URL" magenta
# Run git clone and capture output
git clone "$ALLSPICE_URL" "$USERPROFILE/allspice/$REPO_NAME" &> "$LOGFILE"
# Check if the command was successful
if [ $? -eq 0 ]; then
log_header "Successfully cloned the AllSpice repository!" green
log_header "Repository is located at: $USERPROFILE/allspice/$REPO_NAME" magenta
else
log_header "Error: Failed to clone the AllSpice repository." red
log_header "Please check the AllSpice URL and try again." yellow
log_header "Check the log for details: $LOGFILE" magenta
exit 1 # Exit the script with an error code
fi
# Run git svn clone and capture output
log_header "Cloning the SVN repository" cyan
git svn clone -r HEAD "$SVN_URL" "$USERPROFILE/allspice/$REPO_NAME"
# Check if the command was successful
if [ $? -eq 0 ]; then
log_header "Successfully cloned the SVN repository!" green
log_header "Repository is located at: $USERPROFILE/allspice/$REPO_NAME" magenta
else
log_header "Error: Failed to clone the SVN repository." red
log_header "Please check the SVN URL and try again." yellow
log_header "Check the log for details: $LOGFILE" magenta
exit 1 # Exit the script with an error code
fi
# Change to the repo directory
cd $USERPROFILE/allspice/$REPO_NAME
# Pull all branches from AllSpice
log_header "Pulling all branches from AllSpice" cyan
git fetch --all
# Check out git-svn branch that should have SVN history
# Turn off detached head advice temporarily
git config --local advice.detachedHead false
# Ensure git-svn is up to date before rebasing anything
log_header "Checking out git-svn branch" cyan
git checkout -b git-svn refs/remotes/git-svn # Create locally if missing
log_header "Switching to git-svn branch" cyan
git checkout refs/heads/git-svn
log_header "Pushing git-svn to remote" cyan
git push --set-upstream origin git-svn
log_header "Rebasing git-svn" cyan
git svn rebase # Make sure it's aligned with SVN
git config --local advice.detachedHead false
# Remove develop branch
log_header "Removing local develop branch" cyan
git branch -D refs/heads/develop
log_header "Removing remote develop branch" cyan
git push origin --delete develop
log_header "Checking out main branch" cyan
git checkout main
log_header "Rebasing main branch, ( git-svn -> main )" cyan
git rebase refs/heads/git-svn
log_header "Checking out git-svn branch" cyan
git checkout refs/heads/git-svn
log_header "Adding *.bat to .gitignore" cyan
grep -qxF "*.bat" .gitignore || echo "*.bat" >> .gitignore
sort -u -o .gitignore .gitignore
if git diff --quiet -- .gitignore; then
: # Replace with if debug
# echo "No changes in .gitignore. Skipping commit."
else
git add .gitignore
git commit -m "Update .gitignore"
git push
log_header "Added *.bat to .gitignore" cyan
fi
log_header "force push main to AllSpice" cyan
git push --force origin main
log_header "Checking out git-svn branch" cyan
git checkout git-svn
git push
log_header "Set core.hooksPath to .git/hooks" cyan
git config --local core.hooksPath .git/hooks
log_header "Downloading pre-push hook" cyan
curl -L -o $USERPROFILE/allspice/$REPO_NAME/.git/hooks/pre-push https://hub.allspice.io/AllSpice-Demos/AllSpice-Loves-SVN-Monorepos/raw/branch/main/.allspice/pre-push
log_header "Making pre-push hook executable" cyan
chmod +x $USERPROFILE/allspice/$REPO_NAME/.git/hooks/pre-push
log_header "Downloading fetch-svn.bat to $USERPROFILE/allspice/$REPO_NAME" cyan
curl -L -o $USERPROFILE/allspice/$REPO_NAME/fetch-svn.bat https://hub.allspice.io/AllSpice-Demos/AllSpice-Loves-SVN-Monorepos/raw/branch/main/.allspice/fetch-svn.bat
log_header "Finished cloning AllSpice + SVN repos, switching shell to bash" green
git status
echo "$USERPROFILE/allspice/$REPO_NAME" > "$USERPROFILE/allspice/latest-repo.txt"