I\'m having trouble figuring out how to use git blame for getting the set of commits that ever touched a given range of lines. There are similar questi
I think this is what you want:
git rev-list HEAD -- foo.rb | (
while read rev; do
git blame -l -L 1000,+5 $rev -- foo.rb | cut -d ' ' -f 1
done;
) | awk '{ if (!h[$0]) { print $0; h[$0]=1 } }'
That'll output the the revision number for each commit that has an edit to the lines you've chosen.
Here are the steps:
The first part git rev-list HEAD -- foo.rb outputs all revisions in which the chosen file is edited.
Each of those revisions then goes into the second part, which takes each one and puts it into git blame -l -L 1000,+5 $rev -- foo.rb | cut -d ' ' -f 1. This is a two-part command.
git blame -l -L 1000,+5 $rev -- foo.rb outputs the blame for the chosen lines. By feeding it the revision number, we are telling it to start from that commit and go from there, rather than starting at the head.cut -d ' ' -f 1 gives us the first column (the revision number) of the blame output. awk '{ if (!h[$0]) { print $0; h[$0]=1 } }' takes out non-adjacent duplicate lines while maintaining the order they appeared in. See http://jeetworks.org/node/94 for more info about this command. You could add a last step here to get prettier output. Pipe everything into xargs -L 1 git log --oneline -1 and get the corresponding commit message for the list of revisions. I had a weird issue using this last step where I had to keep pressing next every few revisions that were output. I'm not sure why that was, which is why I didn't include it in my solution.