Search code examples
gitalgorithmtextreplace

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


I made web application to edit diagram (like draw.io) using konva. Now I can import/export text file corresponding to diagram and I planning to implement undo/redo. For now I think I can store many version of text file to represent undo/redo state but it use a lot of resource. I think it more efficient to store undo/redo as text lines addition/deletion as git does. when I commit change I saw git can compare and detect which line I add or delete (modify count as delete and add new line). I wish I can use same technique to my project as well. Anyone know the algorithm name and how it work?

For example:

enter image description here


Solution

  • What you are looking at is typically called a 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).