How does git blame determine who edited a line of a file?
Asked Answered
B

2

10

Using

git blame file

will show all info about each line, for example who add this line in which commit, and when, but as far as I know, Git will add a completely new object every time you change a file. So where does Git store such info about each line?

Bloodhound answered 12/7, 2013 at 5:9 Comment(2)
@Cupcake thanks for your revision, I will read this link until I never make the same mistake stackoverflow.com/posts/17607784/revisionsBloodhound
Don't be too hard on yourself William, the question just needed a little cleanup. Trust me, I've seen questions that were far, far worse :/Friedafriedberg
P
7

As @mvp said, "it doesn't". To answer your comment though—that is, "the flow of this process"—it works very roughly like a series of git diffs, starting with the most recent version of the file, and working backwards until every line has an assigned origin.

Suppose you have a short file with just four lines, and it is the most recent (i.e., the version in HEAD). Suppose further that git diff shows that in revision HEAD~1 there were only the first three lines, and I added that last (fourth) line. Then the "blame" for line 4 would be mine: it was not there in the previous version, and it was there in the current, so I must have added it.

That leaves the problem of figuring out who to "blame" for those three lines. So now git must diff HEAD~1 against HEAD~2. If those three lines all appear exactly as is in HEAD~2—which might be the case if, for instance, the change from HEAD~2 to HEAD~1 was simply to delete some lines—then we must keep going further back in history.

At some point, though, git diff will show that someone added line 1, line 2, and/or line 3 (in some previous version), possibly while deleting some other line(s); or in the worst case, git will reach a "root commit": a commit with no parents. In any case, whoever committed the commit(s) that caused those lines to appear, must be the one to blame.

Pinkerton answered 12/7, 2013 at 11:13 Comment(0)
G
3

Where does Git store such info about each line?

Git does NOT store this info anywhere, at least not explicitly. Every time you run git blame file, this info is dynamically computed from objects stored in git object store. If you are interested in knowing how exactly this is done, you can read git blame.c source code.

Gummosis answered 12/7, 2013 at 5:59 Comment(1)
so march code and in C, I just know a little Ruby, can you summarize that code with human readable description? like this: first it read this info from this object, and that info from that object, and add them together, and wallah!, you get that you want! I don't want to know the low level details, just the flow of this processBloodhound

© 2022 - 2024 — McMap. All rights reserved.