Skip to content

Instantly share code, notes, and snippets.

@djch
Last active July 11, 2024 01:56
Show Gist options
  • Save djch/4688b2c41a7f59e5d1abeeefe0fd968a to your computer and use it in GitHub Desktop.
Save djch/4688b2c41a7f59e5d1abeeefe0fd968a to your computer and use it in GitHub Desktop.
A shell script to merge updated writebook rails source code with an existing git repo of a previous version
#!/bin/bash
# WARNING
# =======
#
# I'm not responsible for any accidental loss or damage to your code if you run this.
# It works for me, in my setup, but that doesn't mean it will definitely work for you.
#
# Check if the source and destination directories are provided
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: $0 /path/to/source_folder /path/to/repo_folder"
exit 1
fi
# Define variables
SOURCE_DIR="$1"
DEST_DIR="$2"
# Exclude patterns
EXCLUDE_PATTERNS=(
'.git'
'.gitignore'
'.ruby-version'
'Gemfile.lock'
'*.sqlite3'
'storage'
'log'
'tmp'
)
# Build the rsync exclude options
RSYNC_EXCLUDES=""
for PATTERN in "${EXCLUDE_PATTERNS[@]}"; do
RSYNC_EXCLUDES="$RSYNC_EXCLUDES --exclude=$PATTERN"
done
# Perform a dry run first and capture the output
echo "Performing dry run of rsync to ensure correct exclusions..."
DRY_RUN_OUTPUT=$(rsync -nav --delete --ignore-existing $RSYNC_EXCLUDES "$SOURCE_DIR/" "$DEST_DIR/")
# Display the dry run output
echo "$DRY_RUN_OUTPUT"
# Check if there are any files marked for deletion
DELETE_LIST=$(echo "$DRY_RUN_OUTPUT" | grep '^deleting ')
if [ -n "$DELETE_LIST" ]; then
echo "The following files are marked for deletion in the destination directory:"
echo "$DELETE_LIST"
read -p "Do you want to continue with these deletions? (y/n): " CONFIRM
if [[ "$CONFIRM" != "y" ]]; then
echo "Aborting operation."
exit 1
fi
fi
# Perform the actual rsync operation
echo "Performing actual rsync..."
rsync -a --delete $RSYNC_EXCLUDES "$SOURCE_DIR/" "$DEST_DIR/"
echo "Writebook files have been merged into $DEST_DIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment