tags:

views:

252

answers:

4

I don't have the knowledge to look at the source code of git.

How does git detect a file modification so fast ? Does he hash every file in the repo and compare SHA1 ? This should take a lot of time.

Or does he compare atime, ctime or mtime ?

+2  A: 

Well I would hazard a guess that it's using a combination of stat() calls to work out what looks like it might have changed, then in turn actually tying to ascertain using it's diff'ing engine that this is the case.

You can see the code for the diff engine here to get some idea. I traced through the codebase to be sure that the status command does indeed call down into this code (it looks like a lot of stuff does!) and actually all this makes a lot of sense when you know that Git performs pretty badly on Windows where it is using an emulation layer to perform these POSIX type calls: it's an order of magnitude slower to do a git status on that platform.

Anyway, short of reading all the code from top to bottom (which I may later if I have time!) thats as far as I can take you for now...maybe someone can be more definitive if they have worked with the codebase.

Note: another possible speedup comes from judicious use of inline functions where it clearly makes sense, you can see this clearly in the headers.

[edit: see here for an explanation of stat()]

jkp
care to explain what `stat()` is/does?
hasen j
@hansen j: updated with a reference to the stat() man page.
jkp
+3  A: 

There's an initial mtime check for reports like "git status", but when the final commit is computed, mtimes don't matter... it's the SHA1 that matters.

Randal Schwartz
@Randal: I don't think this is true, it always does a diff: http://gist.github.com/240775. If only mtime was used for `git status` you would see modifications in the paste I did.
jkp
+1  A: 

Depending on platform, you should be able to find out what syscalls Git uses to figure out its status. Try strace git status on Linux, truss git status on SunOS, or the seemingly DTrace-based tool that Apple ships with its Developer Tools on Mac OS X.

Max A.