git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Can I convince the diff algorithm to behave better?

From
TRTom Ritter <tom@ritter.vg>
Date
Mar 3, 2021, 02:03 UTC
Message-ID
<CA+cU71=FfReSG411Feo=vmkw4MdK4KDgokP1jH6uwOkC_0AbYA@mail.gmail.com>
(For a specific, nuanced, and personal definition of better...)

I have a frequent behavior that arises when I am copy/pasting chunks of code, typically in tests. Here is an example:

My Original code:
def function():
   line 1
   line 2
   line 3
   line 4
   line 5
   line 6

-------------------------------- I add, after it:

def function2():
   line 1
   line 2
   line 3
   line 4
   line 5
   line 6

-------------------------------- My diff is:

+   line 3
+   line 4
+   line 5
+   line 6
+
+def function2():
+   line 1
+   line 2

-------------------------------- I'd like my diff to be

+
+def function2():
+   line 1
+   line 2
+   line 3
+   line 4
+   line 5
+   line 6

Obviously there's nothing incorrect about the former diff, I just wish it was the latter rather than the former.

I know that git includes four diff algorithms; in my testing patience or histogram exacerbated the problem; and none of them improved upon it. If anyone has suggestions I'd be curious to know if there's anything that could be done...

Thanks, -tom

Next: Thomas Braun
Message 1 of 4 in “Can I convince the diff algorithm to behave better?”
  1. Tom RitterMar 3, 2021
  2. Thomas BraunMar 3, 2021
  3. Jonathan TanMar 3, 2021
  4. Christian CouderMar 4, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.