Skip to content

Instantly share code, notes, and snippets.

@mcdurdin
Created June 6, 2023 07:22
Show Gist options
  • Save mcdurdin/dfdcd6defa3937ef1fcab8b8730d3688 to your computer and use it in GitHub Desktop.
Save mcdurdin/dfdcd6defa3937ef1fcab8b8730d3688 to your computer and use it in GitHub Desktop.
Script to search git diffs, returning only lines that have changed, and the corresponding files
#!/usr/bin/env bash
# Adapted from https://stackoverflow.com/a/50569950/1836776
if [[ $1 == '--help' ]]; then
script=`basename "$0"`
echo "USAGE"
echo " $script term [git diff parameters]"
echo
echo "DESCRIPTION"
echo " Searches git diff results for <term>, returning only lines that have changed"
echo
echo "EXAMPLES"
echo " - Search all changes from master in current branch for 'TODO':"
echo " $script TODO master..."
echo
echo " - As above, with pagination (color support):"
echo " $script TODO master... | less -r"
exit 1
fi
colored=$'(\e\[[0-9;]*[a-zA-Z])'
marker="^$colored+diff"
pattern="^$colored+.*(\+|\-).*$1"
shift
# Yeah, okay, both sed and awk for maximum dork here, feel free to combine!
git diff --color $* | sed -rn -e "/$marker/! H; /$marker/ ba; $ ba; b; :a; x; /$pattern/ p" | awk "/\+\+\+/{f = \$2 \":\\n\"}; /$pattern/ {print f \$0; f = \"\"}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment