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

Re: a problem with git diff

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Apr 16, 2020, 22:55 UTC
Message-ID
<20200416225502.GJ2751707@camp.crustytoothpaste.net>
In-Reply-To
<65cbee30-fa37-1422-98f2-2c2b5aa094bd@maurel.de>
On 2020-04-16 at 16:43:40, Guy Maurel wrote:
Show 13 quoted lines
> Hello!
> 
> Using the appended two files:
>   git diff combine.cpp-2020-04-16-A combine.cpp-2020-04-16-R > git-diff.diff-Y
> 
> doesn't show the same differences as with:
>   meld combine.cpp-2020-04-16-R combine.cpp-2020-04-16-A
> 
> Have a look at git-diff.diff-Y at line 210:
> -static void process_returns(void)
> +static void mark_lvalue(chunk_t *pc)
> 
> which is NOT correct.

It looks like there's some additional code that gets inserted before the process_returns function. In this case, the diff is accurate in that applying it to the old file will result in the new file, but it's not very helpful.

The reason is that the default diff algorithm, myers, looks for common lines and finds them in the blank lines in both process_returns and the new code above it. It then writes the diff as a set of deletions of the lines in process_returns and an addition of the lines in the new code, plus a final addition of the process_returns function. While correct, this is, as you noted, confusing.

If you're looking for a more helpful output, you can use --diff-algorithm=patience (or diff.algorithm=patience), which results in a diff output that more logically matches what most humans would like to see. You can also use the histogram diff algorithm, which is based on patience but has some additional heuristics. Which one produces better output differs depending on the circumstance.

-- 
brian m. carlson: Houston, Texas, US
OpenPGP: https://keybase.io/bk2204
Previous: Guy MaurelNext: brian m. carlson
Message 2 of 5 in “a problem with git diff”
  1. Guy MaurelApr 16, 2020
  2. brian m. carlsonApr 16, 2020
  3. brian m. carlsonApr 17, 2020
  4. Junio C HamanoApr 17, 2020
  5. Pratyush YadavApr 22, 2020

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.