From e63c5e5882f7a1dff65f66ad02092941607ef202 Mon Sep 17 00:00:00 2001 From: Samir Ladoui Date: Mon, 9 Sep 2024 12:57:34 +0100 Subject: [PATCH] improve clone and pull scripts --- .gitignore | 5 ++-- clone.sh | 55 ++++++++++++++++++++++++++++++++++++-------- pull.sh | 67 ++++++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 97 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index e3695b424..f522c0579 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ __pycache__/ *.py[cod] *$py.class +.history # C extensions *.so @@ -114,8 +115,6 @@ venv.bak/ # Rope project settings .ropeproject - - # mypy .mypy_cache/ .dmypy.json @@ -124,5 +123,5 @@ dmypy.json # Pyre type checker .pyre/ -# github action +# github action .github/workflows/*yaml diff --git a/clone.sh b/clone.sh index 741c06f46..572676339 100644 --- a/clone.sh +++ b/clone.sh @@ -4,6 +4,9 @@ USERNAME=$1 TOKEN=$2 +# Retry limit for failed clone attempts +RETRY_LIMIT=3 + # Branch types and their corresponding directories declare -A branch_dirs=( ["dev"]="test" @@ -72,6 +75,33 @@ branches=( "master_openeducat_erp-14.0.1.0" ) +# Error log file +error_log="clone_error_log.txt" > "$error_log" # Clear log file at the start + +# Clone branch function with retry mechanism +clone_branch() { + local branch=$1 + local full_path=$2 + local attempt=1 + + while [ $attempt -le $RETRY_LIMIT ]; do + echo "Cloning $branch into $full_path (Attempt $attempt/$RETRY_LIMIT)..." + git clone --depth=1 -b "$branch" "https://$USERNAME:$TOKEN@github.com/expsa/odex25-standard-modules.git" "$full_path" + + if [ $? -eq 0 ]; then + echo "Successfully cloned $branch into $full_path" + return 0 + else + echo "Failed to clone $branch (Attempt $attempt)." + attempt=$((attempt + 1)) + fi + done + + # If the retry limit is reached, log the failure + echo "Failed to clone $branch after $RETRY_LIMIT attempts." >> "$error_log" + return 1 +} + # Loop through each branch for branch in "${branches[@]}"; do # Extract the prefix (dev, preprod, master) and the branch name @@ -84,17 +114,22 @@ for branch in "${branches[@]}"; do # Full path for the folder inside the corresponding directory full_path="$base_dir/$folder_name" + # Skip cloning if the folder already exists + if [ -d "$full_path" ]; then + echo "Skipping $branch: already cloned in $full_path" + continue + fi + # Create the directory if it doesn't exist mkdir -p "$full_path" - # Clone the specific branch into the designated folder using token-based authentication - git clone --depth=1 -b "$branch" "https://$USERNAME:$TOKEN@github.com/expsa/odex25-standard-modules.git" "$full_path" - - # Check if the clone was successful - if [ $? -eq 0 ]; then - echo "Successfully cloned $branch into $full_path" - else - echo "Failed to clone $branch" - exit 1 - fi + # Clone the specific branch with error handling + clone_branch "$branch" "$full_path" done + +# Check if any errors were logged +if [ -s "$error_log" ]; then + echo "Some branches failed to clone. Check $error_log for details." +else + echo "All branches cloned successfully." +fi diff --git a/pull.sh b/pull.sh index 55f7bf6be..9f928bbe0 100644 --- a/pull.sh +++ b/pull.sh @@ -3,6 +3,9 @@ # Branch to process (test, preprod, master) BRANCH=$1 +# Retry limit for failed pull attempts +RETRY_LIMIT=3 + # Branch types and their corresponding directories declare -A branch_dirs=( ["dev"]="test" @@ -71,6 +74,40 @@ branches=( "master_openeducat_erp-14.0.1.0" ) +# Error log file +error_log="pull_error_log.txt" > "$error_log" # Clear log file at the start + +# Function to pull code with retries +pull_branch() { + local branch=$1 + local full_path=$2 + local attempt=1 + + while [ $attempt -le $RETRY_LIMIT ]; do + echo "Pulling $branch into $full_path (Attempt $attempt/$RETRY_LIMIT)..." + + # Navigate to the directory and pull the latest changes + cd "$full_path" || { echo "Failed to access $full_path"; exit 1; } + git pull origin "$branch" + local pull_status=$? + + # Return to the previous directory + cd - || exit 1 + + if [ $pull_status -eq 0 ]; then + echo "Successfully pulled $branch into $full_path" + return 0 + else + echo "Failed to pull $branch (Attempt $attempt)." + attempt=$((attempt + 1)) + fi + done + + # If retry limit is reached, log the failure + echo "Failed to pull $branch after $RETRY_LIMIT attempts." >> "$error_log" + return 1 +} + # Loop through each branch for branch in "${branches[@]}"; do prefix="${branch%%_*}" @@ -80,24 +117,20 @@ for branch in "${branches[@]}"; do folder_name="${branch#*_}" full_path="$BRANCH/$folder_name" - # Create the directory if it doesn't exist - mkdir -p "$full_path" - - # Navigate to the directory - cd "$full_path" || { echo "Failed to access $full_path"; exit 1; } - - # Pull the latest changes - git pull origin "$branch" - - # Check if the pull was successful - if [ $? -eq 0 ]; then - echo "Successfully pulled $branch into $full_path" - else - echo "Failed to pull $branch" - exit 1 + # Skip the branch if the directory does not exist + if [ ! -d "$full_path" ]; then + echo "Skipping $branch: $full_path does not exist" + continue fi - # Return to the previous directory - cd - > /dev/null + # Pull the branch with error handling + pull_branch "$branch" "$full_path" fi done + +# Check if any errors were logged +if [ -s "$error_log" ]; then + echo "Some branches failed to pull. Check $error_log for details." +else + echo "All branches pulled successfully." +fi