如何检测文本文件的添加和删除(如git)

How to detect addition and deletion of text file (like git)

我制作了使用 konva 编辑图表的 Web 应用程序(如 draw.io)。 现在我可以 import/export 与图表对应的文本文件并且我计划实施 undo/redo。 现在我认为我可以存储许多版本的文本文件来表示 undo/redo 状态,但它使用大量资源。我认为将 undo/redo 存储为文本行 addition/deletion 与 git 一样更有效率。当我提交更改时,我看到 git 可以比较和检测我添加或删除的行(将计数修改为删除并添加新行)。我希望我也可以在我的项目中使用相同的技术。有人知道算法名称及其工作原理吗?

例如:

您正在查看的通常称为 diff. You can generate one using the diff command line, and you will find some language modules that can do this (e.g., Python's difflib)。