Skip to content

Instantly share code, notes, and snippets.

@luskan
Last active March 6, 2025 13:55
Show Gist options
  • Save luskan/9a24539b0f2de7076a31a29160ca2368 to your computer and use it in GitHub Desktop.
Save luskan/9a24539b0f2de7076a31a29160ca2368 to your computer and use it in GitHub Desktop.
Script created with the help of o3-mini, it searches git repo for a commit with specified text, allows to find change also in files deleted/moved long times ago. Example use: `find_commit_diff.sh --search overrideAamCriticalParameters` it will also print progress and allows to do reverse search - default is search from newest commits to oldest.
#!/bin/bash
# find_commit_diff.sh
# Usage: ./find_commit_diff.sh --search <string> [--reverse]
# --search The search string (e.g. "overrideAamCriticalParameters")
# --reverse Process commits from oldest to newest (optional)
# If no parameters are provided, show help
if [ $# -eq 0 ]; then
echo "Usage: $0 --search <string> [--reverse]"
exit 1
fi
SEARCH_STRING=""
REVERSE_FLAG=""
# Parse parameters
while [[ $# -gt 0 ]]; do
case "$1" in
--search)
shift
if [ -z "$1" ]; then
echo "Error: --search requires an argument."
exit 1
fi
SEARCH_STRING="$1"
shift
;;
--reverse)
REVERSE_FLAG="--reverse"
shift
;;
--help|-h)
echo "Usage: $0 --search <string> [--reverse]"
exit 0
;;
*)
echo "Unknown parameter: $1"
echo "Usage: $0 --search <string> [--reverse]"
exit 1
;;
esac
done
if [ -z "$SEARCH_STRING" ]; then
echo "Error: search string not provided."
echo "Usage: $0 --search <string> [--reverse]"
exit 1
fi
# Determine the revision list command based on the reverse flag.
ITERATION_CMD="git rev-list --all $REVERSE_FLAG"
# Count total commits for progress calculation
TOTAL_COMMITS=$(git rev-list --all $REVERSE_FLAG | wc -l)
PROCESSED=0
LAST_UPDATE=0
# Iterate through commits using process substitution to avoid subshell issues.
while read -r commit; do
PROCESSED=$((PROCESSED+1))
CURRENT_TIME=$(date +%s)
# Update progress every 3 seconds.
if [ $((CURRENT_TIME - LAST_UPDATE)) -ge 3 ]; then
# Calculate percentage (using awk for float arithmetic)
PERCENT=$(awk "BEGIN {printf \"%.2f\", ($PROCESSED/$TOTAL_COMMITS)*100}")
# Get the commit date (using git show for that commit)
COMMIT_DATE=$(git show -s --format=%ci "$commit")
# Print progress on one line (carriage return returns to start of line)
echo -ne "\rProgress: $PERCENT% - Last Commit Date: $COMMIT_DATE"
LAST_UPDATE=$CURRENT_TIME
fi
# Get added lines from the diff that contain the search string
MATCHES=$(git show "$commit" --unified=0 | grep '^+' | grep -v '^+++' | grep "$SEARCH_STRING")
if [ -n "$MATCHES" ]; then
echo -e "\nCommit: $commit"
echo "$MATCHES"
echo "--------------------------------"
fi
done < <(git rev-list --all $REVERSE_FLAG)
# Clear progress line at end
echo -ne "\r"
echo "Done scanning $TOTAL_COMMITS commits."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment