Git能够真正追踪从一个文件到另一个文件的单个函数的移动吗?如果是这样,怎么样? [英] Can Git really track the movement of a single function from 1 file to another? If so, how?

查看:158
本文介绍了Git能够真正追踪从一个文件到另一个文件的单个函数的移动吗?如果是这样,怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有几次,我遇到过这样的说法:如果将单个函数从一个文件移动到另一个文件,Git可以跟踪它。例如,此条目说,Linus说,如果你将一个函数从一个文件移动到另一个文件,Git会告诉你整个移动过程中单个函数的历史记录。



但是我有点意识到Git的一些内置设计,我不明白这是如何实现的。所以我想知道......这是一个正确的说法吗?如果是这样,这怎么可能?



我的理解是Git会将每个文件的内容存储为Blob,并且每个Blob具有全局唯一标识,这些标识来自SHA它的内容和大小的散列。 Git然后将文件夹表示为树。任何文件名信息属于树,而不是Blob,所以文件重命名例如显示为对Tree的更改,而不是Blob。

所以如果我有一个名为foo的文件,其中包含20个函数,还有一个名为bar的文件,其中包含5个函数,我将其中一个函数从foo移动到bar(分别导致19和6),怎么能Git检测到我将该函数从一个文件移动到另一个文件?



据我了解,这会导致2个新的blob存在(一个用于修改foo,另一个用于修改栏)。我意识到可以计算出差异来显示函数从一个文件移动到另一个文件。但是我不明白这个函数的历史如何可能与bar相关而不是foo(不是自动的,无论如何)。



如果Git实际上看起来像单个文件的内部,并计算每个函数的(这将是疯狂的/不可行的,因为你必须知道如何解析任何可能的语言),然后我可以看到这可能是怎么可能的。



所以......这个陈述是否正确?如果这是正确的,那么我的理解是什么?

解决方案

这个功能是通过git blame提供的-C < -C>

-C选项可以让git试图在正在审阅的文件中添加或删除大块文本与在相同变更集中修改的文件之间找到匹配。其他-CC或-CCC扩展搜索。在手册页中输入git help blame。



在git blame -C中尝试自己的测试回购,您会看到刚刚移动的代码块起源于它所属的原始文件。


Several times, I have come across the statement that, if you move a single function from one file to another file, Git can track it. For example, this entry says, "Linus says that if you move a function from one file to another, Git will tell you the history of that single function across the move."

But I have a little bit of awareness of some of Git's under-the-hood design, and I don't see how this is possible. So I'm wondering ... is this is a correct statement? And if so, how is this possible?

My understanding is that Git stores each file's contents as a Blob, and each Blob has a globally unique identity which arises from the SHA hash of its contents and size. Git then represents folders as Trees. Any filename information belongs to the Tree, not to the Blob, so a file rename for example shows up as a change to a Tree, not to a Blob.

So if I have a file called "foo" with 20 functions in it, and a file called "bar" with 5 functions in it, and I move one of the functions from foo into bar (resulting in 19 and 6, respectively), how can Git detect that I moved that function from one file to another?

From my understanding, this would cause 2 new blobs to exist (one for the modified foo and one for the modified bar). I realize a diff could be calculated to show that the function was moved from one file to the other. But I don't see how history about the function could possibly become associated with bar instead of foo (not automatically, anyway).

If Git were to actually look inside of single files, and compute a blob per function (which would be crazy / infeasible, because you'd have to know how to parse any possible language), then I could see how this might be possible.

So ... is the statement correct or not? And if it is correct, then what is lacking in my understanding?

解决方案

This functionality is provided through git blame -C

The -C option drives git into trying to find matches between addition or deletion of chunks of text in the file being reviewed and the files modified in the same changesets. Additional -CC, or -CCC extend the search. Type git help blame for the manual page.

Try for yourself in a test repo with git blame -C and you'll see that the block of code that you just moved is originated in the original file where it belonged to.

这篇关于Git能够真正追踪从一个文件到另一个文件的单个函数的移动吗?如果是这样,怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆