-3

Suppose I have file a.txt, b.txt and c.txt:

a.txt:

Hello, I like cake.

b.txt:

Hello, I like turtles.

c.txt:

go away, I don't like you

I suspect the difference between a.txt and b.txt is quantifiable. For example, one could say that characters 15-18 are deleted, and the string turtles is inserted.

Now, I would like to calculate this change and then apply it to c.txt, which would result in:

go away, I donturtlesike you

I believe git uses an analogous approach to track changes in a repository, apply stashes and so forth. I am looking for something with that level of generality. What is this called in software engineering?

5
  • I don't know if this has a specific name, but I think what you are looking for can be done with Pythons difflib. This library can used to detect differences between lists of strings and you could process them in the way you are describing.
    – Tyberius
    Jan 19 at 22:25
  • I looked into difflib but it dorsnt look like applying changes to a new file is possible
    – user32882
    Jan 20 at 6:03
  • While there isn't a function to directly apply the diffs to a new file, I don't think it would be too difficult to make one that processes the diffs produced by this library.
    – Tyberius
    Jan 20 at 14:54
  • 3
    "one could say that characters 15-18 are deleted, and the string turtles is inserted" - one could also say that the characters 15-17 are deleted, the string turtl is added before the (original) character 18, and the string s is added after the (original) character 18. Which is correct?
    – jfaccioni
    Jan 20 at 16:18
  • All of them. But for each given methodology or algorithm, the assumptions would remain consistent .
    – user32882
    Jan 20 at 17:41

1 Answer 1

2

I believe git uses an analogous approach to track changes in a repository, apply stashes and so forth

Misbelieving detected. Git (and all other VCSes) operates with objects "whole string" (with context around it), not separate chars or words inside strings. In short: diff between a.txt and b.txt is:

  • String "Hello, I like cake." deleted
  • String "Hello, I like turtles." added
2
  • Oh that's good to know... but this is more of a comment rather than an answer. I'm still quite sure it should be possible to quantify the diff in the way I originally stated. Perhaps that's not quite what git does, but then I'm not limited to git here
    – user32882
    Jan 19 at 19:26
  • Mhh, not so sure. Git has various options to display diffs, some with context, some without. Git supports various merge algorithms, these all must consider context by nature. Git's internal data model sometimes uses diff algorithms for delta compression, but these don't have to consider context as the base file is always known. (A Git commit should be understood as a snapshot of your project state, not as a description of a change).
    – amon
    Jan 20 at 14:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.