Files
Server-scripts/generatePatchesByFile.sh
2026-09-14 12:43:38 +02:00

99 lines
2.5 KiB
Bash
Executable File

#!/bin/bash
commit1=$1
commit2=$2
dir=$3
patches_dir=$4
CHECK=1
APPLY=1
if [ -z "$commit1" ] || [ -z "$commit2" ] || [ -z "$dir" ]; then
echo "Usage: $0 <commit1> <commit2> <directory> [patches_dir]"
exit 1
fi
# Default patches directory if not provided
if [ -z "$patches_dir" ]; then
patches_dir="$(pwd)/patches"
elif [[ "$patches_dir" != /* ]]; then
patches_dir="$(pwd)/${patches_dir}patches"
fi
if [[ "$commit2" == "HEAD" ]]; then
commit2=""
fi
echo "Diffing $dir from $commit1 to ${commit2:-HEAD}..."
echo "Patches will be saved in: $patches_dir"
# Save current directory
orig_dir="$(pwd)"
# Get git root directory relative to $dir
git_root=$(git -C "$dir" rev-parse --show-toplevel) || { echo "Not a git repo: $dir"; exit 1; }
# Compute relative path of $dir from git root
relative_dir=$(realpath --relative-to="$git_root" "$dir")
echo "Relative path of $dir from git root: $relative_dir"
# Change to git root directory
cd "$git_root" || { echo "Failed to cd to git root of $dir"; exit 1; }
echo "Go to $git_root"
mkdir -p "$patches_dir"
# Load list of changed files into an array
mapfile -t diff_files < <(git diff --name-only $commit1..$commit -- $relative_dir )
for file in "${diff_files[@]}"; do
git diff $commit1..$commit2 --stat -- "$file"
done
for file in "${diff_files[@]}"; do
echo $file
git diff $commit1..$commit2 -- "$file" > "$patches_dir/$(basename "$file").patch"
done
for patch in "$patches_dir"/*.patch; do
echo $patch
for patch in "$patches_dir"/*.patch; do
sed -i 's|trixel3_software/||g' "$patch"
done
done
# Get git root directory relative to $dir
git_root_patches_dir=$(git -C "$patches_dir" rev-parse --show-toplevel) || { echo "Not a git repo: $patches_dir"; exit 1; }
# Compute relative path of $dir from git root
relative_dir_patches_dir=$(realpath --relative-to="$git_root_patches_dir" "$patches_dir")
echo "Relative path of $patches_dir from git root: $relative_dir_patches_dir"
# Change to git root directory
cd "$git_root_patches_dir" || { echo "Failed to cd to git root of $patches_dir"; exit 1; }
echo "Go to $git_root_patches_dir"
if [[ $CHECK -eq 1 ]]; then
# check patches
echo "CHECK Patches"
for patch in "$patches_dir"/*.patch; do
echo $patch
git apply --check "$patch"
done
fi
if [[ $APPLY -eq 1 ]]; then
# apply patches
echo "APPLY Patches"
for patch in "$patches_dir"/*.patch; do
if git apply "$patch"; then
echo "Applied $patch successfully."
rm "$patch"
else
echo "Failed to apply $patch. Skipping."
fi
done
fi
# Return to original directory
cd "$orig_dir" || exit 1