Collection-Scripts/generate_diffs.sh

47 lines
1.1 KiB
Bash

#!/bin/bash
# Function to handle Ctrl+C
handle_sigint()
{
echo "Interrupt received. Exiting script..."
exit 1
}
# Trap SIGINT (Ctrl+C) and call handle_sigint()
trap 'handle_sigint' INT
# Check if commit_hash.txt exists
if [ -f commit_hash.txt ]
then
read -p "commit_hash.txt found. Do you want to use it (y/n)? " answer
if [ "$answer" != "${answer#[Yy]}" ]
then
commit_hash=$(cat commit_hash.txt)
fi
fi
# If commit_hash is empty, ask the user for a commit hash
if [ -z "$commit_hash" ]
then
read -p "Please enter a commit hash: " commit_hash
fi
# If the commit hash is still empty (no input given), exit the script
if [ -z "$commit_hash" ]
then
echo "No commit hash given. Exiting script..."
exit 1
fi
latest_commit_hash=$(git rev-parse HEAD)
# Get the diff and write it to diffs.txt
git diff ${commit_hash}~1..${latest_commit_hash} > diffs.txt
# Always grab the latest commit hash and write to commit_hash.txt
echo ${latest_commit_hash} > commit_hash.txt
echo "Diffs and latest commit hash have been written to diffs.txt and commit_hash.txt, respectively."
echo "To apply the diffs, run: git apply diffs.txt"