Skip to content

Instantly share code, notes, and snippets.

@jcaxmacher
Created September 23, 2020 20:04
Show Gist options
  • Save jcaxmacher/ea972b336474bcfef52a73b77a4a7c06 to your computer and use it in GitHub Desktop.
Save jcaxmacher/ea972b336474bcfef52a73b77a4a7c06 to your computer and use it in GitHub Desktop.
Git blame and comment in every file
#!/bin/bash
for file in $(git ls-files); do
commits=`git blame $file | cut -f1 -d' ' | sort -u`
count=`git blame $file | cut -f1 -d' ' | sort -u | wc -l`
comments=""
for commit in $commits; do
newcommit=`echo $commit | sed -e 's/^\^//g'`
user=`git show -s --format='%an' $newcommit`
dt=`git show -s --format=%ci $newcommit`
message=`git log --format=%B -n 1 $newcommit`
lines=`git blame $file | grep "^$commit" | cut -d '(' -f 2- | cut -d ')' -f 1 | grep -o '[^ ]*$'`
newlines=""
for line in $lines; do
newlines="$newlines,$((line + count))"
done
comments="${comments}# $dt - $user - $message - lines:$newlines\n"
done
echo -e "${comments}$(cat $file)" > $file
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment