How does git detect that a file has been modified?

后端 未结 4 653
借酒劲吻你
借酒劲吻你 2020-12-01 03:36

How does git detect a file modification so fast?

Does it hash every file in the repo and compare SHA1s? This would take a lot of time, wouldn\'t it?

Or does

4条回答
  •  执念已碎
    2020-12-01 03:55

    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()]

提交回复
热议问题