git diff如何判断一行是否被修改或添加?

huangapple go评论70阅读模式
英文:

How does git diff tell if a line has been modified or added?

问题

在两个文件之间,git diff或任何差异工具如何确定哪些行已添加、修改或删除?

是否存在任何边缘情况,可能无法识别编辑行的正确状态?

编辑:这个问题更关注差异比较发生的实际方法。

英文:

Given two files how does git diff or any diff tool tell which lines have been added, modified or deleted?

Are there any edge cases where it may fail to recognise the correct status of the edited line?

EDIT: This question is more concerned with the actual method of how the diffing happens

答案1

得分: 4

Git存储快照:在选择的任意两个快照中,Git将要比较的文件提取到一个临时区域,然后在这两个文件上运行一个“差异引擎”。这个引擎的结果应该是一系列指令,当应用时,可以将左侧文件转换为右侧文件。

Git有几个内置的差异引擎,Git称之为:

  • myers:这使用了Eugene W Myers的算法,经过一些轻微的修改。关于Myers差异有很多StackOverflow的问题和答案:链接链接链接

  • minimal:与myers相同,但没有对分治部分进行一些修改,而是在非常长的编辑脚本开始表现不好时加快速度而不是更加精简。从某种意义上说,这是真正的Myers算法。

  • patience:Bram Cohen的算法;参见https://stackoverflow.com/q/2817255/1256452的被接受答案。

  • histogrampatience的修改,旨在避免在像只包含大括号的行这样的情况下意外同步。

它还可以运行您选择的外部差异引擎。

1 或至少,它希望比较的文件。如果您比较的是实际上不在提交中的文件,它们可能已经被提取。

2 Git主要在内存中执行此操作,但在运行外部差异时,确实使用临时文件。

英文:

Git stores snapshots: given any two snapshots of your choice, Git extracts the files you'd like compared<sup>1</sup> to a temporary area<sup>2</sup> and then runs a "diff engine" on the two files. The result of this engine is, or should be, a series of instructions that, when applied, will convert the left-side file to the right-side file.

Git has several built-in diff engines, which Git calls:

It can also run your chosen external diff engine.


<sup>1</sup>Or at least, the ones it wants to compare. If you are comparing files that are not actually in commits, they may already be extracted.

<sup>2</sup>Git mostly does this in-memory, but when running an external diff, really does use temporary files.

答案2

得分: 0

Git 保存了仓库中每个单独文件的当前提交版本。如果文件被修改,那么就会生成已提交的版本,并与你的文件的当前版本进行比较。这与在硬盘上对比两个文件没有区别。如果 diff 工具可以对比两个普通文件,那么它也可以对比 Git 生成的版本和你拥有的版本。

除非存在某些非常罕见的 bug,否则 diff 工具应该能够捕捉到任何差异,因为它逐行比较两个文件。

英文:

git keeps the current committed version of every single file in the repo. If a file is modified, then the committed version is generated and compared with the current version of your file. It is then the same as diff'ing two files on the hard drive. If the diff tool can diff two ordinary files, then it can diff the version that git produced and the version that you have.

Unless if there is some really obscure bug, the diff tool should be able to catch any difference, as the diff tool compares the two files line by line.

huangapple
  • 本文由 发表于 2020年1月6日 22:34:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定