Re: [libgit2] Blame
- Thomas Rast
- 2013-03-05 @ 09:00
Carlos Martin Nieto <firstname.lastname@example.org> writes:
> Ignacio Casal Quinteiro <email@example.com> writes:
>> do any of you know what would be the best way to implement the blame
>> for a file?
> First of all, blame is going be somwhat painful to implement. In its
> basic/naïve form, you can put each line in the file in a list and go
> down the history, asking for a diff for each commit until all of its
> lines are blamed. When you look at a diff, you take at the '+' lines and
> blame the . You'd need to remember to update the line numbers in your
> list with the information from the chunk header, so you don't
> missatribute lines that are all the same, next to each other and
> added in different commits (think braces, for loops etc).
> Once that works, you can try to implement copy/move detection. I believe
> support for this is either in the current diff code or will soon
> come, so you'll be spared much of this uglyness. Once you decide that
> a block has been copied, you need adjust the data structures to blame
> those lines in their original location.
Note that there are actually two parts to the copy/move detection.
git-blame also looks for other blame opportunities *within* the file if
you specify at least -M, as you can easily test by rearranging the order
IIRC you can achieve this by running a diff between the added parts
(which would otherwise be blamed to the current commit) and the
preimage, and applying some heuristics to look for consecutive runs and
such. In any case it's a mess.