Skip to content

Instantly share code, notes, and snippets.

@aheissenberger
Last active October 5, 2022 13:13
Show Gist options
  • Save aheissenberger/08da82b860c9aab7ee23a60af546ff14 to your computer and use it in GitHub Desktop.
Save aheissenberger/08da82b860c9aab7ee23a60af546ff14 to your computer and use it in GitHub Desktop.
patch creation with diff from files where a copy of the file with the extension ".old" exists, there is a patch file per found file
#/bin/bash
#set -x
APP_NAME=$(basename -- "$0")
if [ -z "$1" ]; then
echo -e "\n$APP_NAME [base dir (Default:.)] [base search dir (Default:".")] [search glob (Default:**/*.php.old)] [patches dir (Default:patches]"
echo -e "\tThe base search and patches dir are relative to basedir!"
echo -e "\tExpample: $APP_NAME: wordpress web/wp patches"
exit
fi
BASE_START="$(pwd)"
BASE_DIR="${BASE_START}"
[ ! -z "$1" ] && [ "." != "$1" ] && BASE_DIR="${BASE_DIR}/$1"
BASE_SEARCH_PATH=""
BASE_SEARCH_DIR="${BASE_DIR}"
[ ! -z "$2" ] && [ "." != "$2" ] && BASE_SEARCH_DIR="${BASE_SEARCH_DIR}/$2"
[ ! -z "$2" ] && [ "." != "$2" ] && BASE_SEARCH_PATH="$2/"
BASE_SEARCH_GLOB=${3:-"**/*.php.old"}
BASE_PATCH_DIR="${BASE_DIR}/${4:-"patches"}"
ERROR="FALSE"
if [ ! -d "${BASE_DIR}" ]; then
echo "Base directory ""${BASE_DIR}"" not found!"
ERROR="TRUE"
fi
if [ ! -d "${BASE_SEARCH_DIR}" ]; then
echo "Search directory ""${BASE_DIR}"" not found!"
ERROR="TRUE"
fi
if [ ! -d "${BASE_PATCH_DIR}" ]; then
echo "Patches directory ""${BASE_PATCH_DIR}"" not found!"
ERROR="TRUE"
fi
[ "$ERROR" = "TRUE" ] && exit
cd "${BASE_SEARCH_DIR}"
trap "cd ${BASE_START}" EXIT
shopt -s globstar
for fullfile in $BASE_SEARCH_GLOB; do # Whitespace-safe and recursive
# echo "fullfile:$fullfile"
fileName=$(basename -- "$fullfile")
fileDir=$(dirname -- "$fullfile")
origFileName="${fileName%.*}"
if [ -f "${BASE_SEARCH_DIR}/${fileDir}/${origFileName}" ]; then
patchFileName="${BASE_SEARCH_PATH}${fileDir}/${origFileName}"
patchFileNameFixed="${BASE_PATCH_DIR}/${patchFileName//[^a-zA-Z0-9]/-}.patch"
#echo "FF:${fullfile} - FN:${fullfile} - PN:${patchFileNameFixed}"
echo "${fileDir}/${origFileName}"
cd "${BASE_DIR}"
diff -Naur "${BASE_SEARCH_PATH}${fileDir}/${fileName}" "${BASE_SEARCH_PATH}${fileDir}/${origFileName}" > "${patchFileNameFixed}"
cd "${BASE_SEARCH_DIR}"
else
echo "Error: Original file \"${fileDir}/${origFileName}\" not found!"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment