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

Re: weird diff output?

From
Stefan Beller <sbeller@google.com>
Date
Mar 29, 2016, 18:16 UTC
Message-ID
<CAGZ79kZiiOgxh6vMDnaJ_b+VVGrFBfGzZukTN6OEBxUV9-2vQw@mail.gmail.com>
In-Reply-To
<xmqqzithxj8l.fsf@gitster.mtv.corp.google.com>
On Tue, Mar 29, 2016 at 10:54 AM, Junio C Hamano <gitster@pobox.com> wrote:
Show 44 quoted lines
> Stefan Beller <sbeller@google.com> writes:
>
>> I thought this is an optimization for C code where you have a diff like:
>>
>>     int existingStuff1(..) {
>>     ...
>>     }
>>     +
>>     + int foo(..) {
>>     +...
>>     +}
>>
>>     int existingStuff2(...) {
>>     ...
>>
>> Note that the closing '}' could be taken from the method existingStuff1 instead
>> of correctly closing foo.
>
> That is a less optimal output.  Another possible output would be
> like so:
>
>       int existingStuff1(..) {
>       ...
>       }
>
>      + int foo(..) {
>      +...
>      +}
>      +
>       int existingStuff2(...) {
>
> All three are valid output, and ...
>
>> So the correct heuristic really depends on what kind of text we
>> are diffing.
>
> ... this realization is correct.
>
> I have a feeling that any heuristic would be correct half of the
> time, including the ehuristic implemented in the current code.  The
> readers of patches have inherent bias.  They do not notice when the
> hunk is formed to match their expectation, but they will notice and
> remember when they see something less optimal.
>
We have 3 possible diffs:
1) closing brace and newline before the chunk
2) newline before, closing brace after the chunk
3) closing brace and newline after the chunk

For C code we may want to conclude that 3) is best. (appeals the bias of most people) 2 is slightly worse, whereas 1) is absolutely worst.

Now looking at the code Jacob found strange:
Show 8 quoted lines
>  cat > expect <<EOF
> + expected results ...
> + EOF
> +test_expect_failure  ... '
> + ...
> + '
> +
> +cat > expect <<EOF
This can be written in two ways:
1) "cat > expect <<EOF" before the diff chunk
2) "cat > expect <<EOF" after the diff chunk

We claim 1) is better than 2). This is different from the C code as now we want to have the same lines before not after.

To find a heuristic, which appeals both the C code and the shell code, we could take the empty line as a strong hint for the divider:

1) determine the amount of diff which is ambiguous, i.e. can
   go before or after the chunk.
2) Does the ambiguous part contain an empty line?
3) If not, I have no offer for you, stop.
4) divide the ambiguous chunk by the empty line,
5) put the lines *after* the empty line in front of the chunk
6) put the part before (including) the empty line after the
   chunk
7) Observe output:
Show 7 quoted lines
>       }
>
>      + int foo(..) {
>      +...
>      +}
>      +
>       int existingStuff2(...) {
Show 12 quoted lines
> test_expect_failure ... '
> existing test ...
> '
>
> + cat > expect <<EOF
> + expected results ...
> + EOF
> +test_expect_failure  ... '
> + ...
> + '
> +
> cat > expect <<EOF

This is what we want in both cases. And I would argue it would appease many other kinds of text as well, because an empty line is usually a strong indicator for any text that a different thing comes along. (Other programming languages, such as Java, C++ and any other C like language behaves that way; even when writing latex figures you'd rather want to break at new lines?)

Thanks, Stefan

Previous: Junio C HamanoNext: Jacob Keller
Message 4 of 27 in “weird diff output?”
  1. Jacob KellerMar 29, 2016
  2. Stefan BellerMar 29, 2016
  3. Junio C HamanoMar 29, 2016
  4. Stefan BellerMar 29, 2016
  5. Jacob KellerMar 29, 2016
  6. Junio C HamanoMar 30, 2016
  7. Jeff KingMar 30, 2016
  8. Stefan BellerMar 30, 2016
  9. Jacob KellerMar 30, 2016
  10. Jacob KellerMar 30, 2016
  11. Jacob KellerMar 30, 2016
  12. Stefan BellerMar 30, 2016
  13. Junio C HamanoApr 1, 2016
  14. Jeff KingMar 31, 2016
  15. Jacob KellerApr 6, 2016
  16. Stefan BellerApr 12, 2016
  17. Davide LibenziApr 14, 2016
  18. Jeff KingApr 14, 2016
  19. Stefan BellerApr 14, 2016
  20. Implement better chunk heuristics.Stefan Beller, Apr 15, 2016
  21. Jacob KellerApr 15, 2016
  22. Stefan BellerApr 15, 2016
  23. Jacob KellerApr 15, 2016
  24. Junio C HamanoApr 15, 2016
  25. Stefan BellerApr 15, 2016
  26. Jacob KellerApr 15, 2016
  27. Jeff KingApr 15, 2016

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.