|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Exit immediately if a command exits with a non-zero status |
| 4 | +set -e |
| 5 | + |
| 6 | +# Color codes |
| 7 | +RED=$(tput setaf 1) |
| 8 | +GREEN=$(tput setaf 2) |
| 9 | +RESET=$(tput sgr0) |
| 10 | + |
| 11 | +# Log file path |
| 12 | +LOG_FILE="archive.log" |
| 13 | + |
| 14 | +# # Enable the line below to write output/error to the log file. |
| 15 | +exec > "$LOG_FILE" 2>&1 |
| 16 | + |
| 17 | +echo "Starting the archive process at $(date)" |
| 18 | + |
| 19 | +# [UPDATE_SUBMODULE] is used to update submodule or not. Default is "false". |
| 20 | +UPDATE_SUBMODULE=$1 |
| 21 | + |
| 22 | +if [ -z "$UPDATE_SUBMODULE" ]; then |
| 23 | + UPDATE_SUBMODULE="false" |
| 24 | +fi |
| 25 | + |
| 26 | +if [[ "${UPDATE_SUBMODULE}" == "true" ]]; then |
| 27 | + # Ensure all submodules are updated recursively |
| 28 | + echo "Updating submodules recursively..." |
| 29 | + git submodule update --init --recursive |
| 30 | + |
| 31 | + echo "Updated submodules successfully." |
| 32 | +fi |
| 33 | + |
| 34 | +# Create a temporary directory for the archive |
| 35 | +TEMP_DIR=$(mktemp -d) |
| 36 | +echo "Created temporary directory: $TEMP_DIR" |
| 37 | + |
| 38 | +# Archive the main repository |
| 39 | +echo "Archiving main repository..." |
| 40 | +git archive HEAD | tar -x -C "$TEMP_DIR" |
| 41 | +echo "Main repository archived successfully." |
| 42 | + |
| 43 | +# Archive submodules and their nested submodules |
| 44 | +echo "Archiving submodules and nested submodules..." |
| 45 | +git submodule foreach ' |
| 46 | + echo "Archiving submodule: $sm_path" |
| 47 | +
|
| 48 | + # Create the submodule directory structure inside the temporary folder |
| 49 | + mkdir -p '"$TEMP_DIR"'/$sm_path |
| 50 | +
|
| 51 | + # Archive the submodule into the appropriate folder in the temporary directory |
| 52 | + (cd $toplevel/$sm_path && git archive HEAD) | tar -x -C '"$TEMP_DIR"'/$sm_path |
| 53 | +
|
| 54 | + # Check if this submodule has nested submodules and archive them as well |
| 55 | + if [ -f "$toplevel/$sm_path/.gitmodules" ]; then |
| 56 | + echo "Found nested submodules in $sm_path, archiving them..." |
| 57 | +
|
| 58 | + SUBMODULE='"$TEMP_DIR"'/$sm_path |
| 59 | +
|
| 60 | + # (cd "$toplevel/$sm_path" && git submodule update --init --recursive) |
| 61 | + (cd "$toplevel/$sm_path" && git submodule foreach " |
| 62 | + echo \"Archiving nested submodule: \$sm_path\"; |
| 63 | + mkdir -p "$SUBMODULE"/\$sm_path; |
| 64 | + (cd \$toplevel/\$sm_path && git archive HEAD) | tar -x -C "$SUBMODULE"/\$sm_path; |
| 65 | + ") |
| 66 | + fi |
| 67 | +' |
| 68 | + |
| 69 | +# Zip the entire content of the temporary directory |
| 70 | +echo "Creating release.zip in the current directory..." |
| 71 | +cd "$TEMP_DIR" || exit 1 |
| 72 | +zip -r "$OLDPWD/release.zip" . 2>/dev/null # Suppress permission warnings |
| 73 | +echo "Zip archive created: release.zip." |
| 74 | +open $OLDPWD |
| 75 | + |
| 76 | +# Clean up |
| 77 | +echo "Cleaning up temporary files..." |
| 78 | +cd .. |
| 79 | +rm -rf "$TEMP_DIR" |
| 80 | + |
| 81 | +echo "${GREEN}✅ Archive release.zip created successfully.${RESET}" |
| 82 | +echo "${GREEN}Archive process completed at $(date)${RESET}" |
0 commit comments