Skip to content

Instantly share code, notes, and snippets.

@fakenickels
Created November 27, 2023 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fakenickels/a9bdaaa40c055ce2e8d6e816ed4079bc to your computer and use it in GitHub Desktop.
Save fakenickels/a9bdaaa40c055ce2e8d6e816ed4079bc to your computer and use it in GitHub Desktop.
#!/bin/bash
# Ensure we're in a git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
echo "This is not a git repository."
exit 1
fi
# Fetch the latest changes from the remote repository
git fetch origin
# Get diff output for each file
while read -r file; do
echo "File: $file"
echo "-----------------"
# Get blame for each line in the diff output
git diff origin/master... -- "$file" | grep -E '^\+' | cut -c2- | while read -r line; do
# Use blame to find the commit for the current line
# Extract commit SHA and commit message
blame_info=$(git blame -L "/$line/",+1 "$file" 2>/dev/null | awk '{print $1}')
if [ -n "$blame_info" ]; then
# Extract commit SHA and commit message
commit_msg=$(git log -1 --format="%s" "$blame_info" 2>/dev/null)
echo "$blame_info - $commit_msg: $line"
else
echo "Blame not found for line: $line"
fi
done
echo ""
done < <(git diff --name-only origin/master...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment