git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [BUG] `git diff` treats an unchanged line as modified
@ 2019-12-24 17:50 Hao Lee
  2019-12-24 19:34 ` Junio C Hamano
  0 siblings, 1 reply; 3+ messages in thread
From: Hao Lee @ 2019-12-24 17:50 UTC (permalink / raw)
  To: git

Here are two files `old` and `new`.

The content of `old` file:

```



kasan_poison_slab(page);//

shuffle = shuffle_freelist(s, page);//

if (!shuffle) {
for_each_object_idx(p, idx, s, start, page->objects) {
setup_object(s, page, p);
if (likely(idx < page->objects))
```

The content of `new` file:

```



kasan_poison_slab(page);

shuffle = shuffle_freelist(s, page);
```

When using git to compare these two files, the result is:

```
diff --git a/old b/new
index 474ab07..d432a67 100644
--- a/old
+++ b/new
@@ -1,11 +1,6 @@



-       kasan_poison_slab(page);//
-
-       shuffle = shuffle_freelist(s, page);//
-
-       if (!shuffle) {
-               for_each_object_idx(p, idx, s, start, page->objects) {
-                       setup_object(s, page, p);
-                       if (likely(idx < page->objects))
+       kasan_poison_slab(page);
+
+       shuffle = shuffle_freelist(s, page);
```

However, the Linux diff command gives the following result:

```
--- old 2019-12-25 00:56:40.000000000 +0800
+++ new 2019-12-25 01:12:48.000000000 +0800
@@ -1,11 +1,6 @@



- kasan_poison_slab(page);//
+ kasan_poison_slab(page);

- shuffle = shuffle_freelist(s, page);//
-
- if (!shuffle) {
- for_each_object_idx(p, idx, s, start, page->objects) {
- setup_object(s, page, p);
- if (likely(idx < page->objects))
+ shuffle = shuffle_freelist(s, page);
```

I think the latter is correct because the fifth line is not changed
and it's still a line which only contains a whitespace.

Steps to Reproduce:

curl -L https://git.io/JeFNy | base64 -d > file.tgz
tar -xf file.tgz
git diff --no-index old new

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [BUG] `git diff` treats an unchanged line as modified
  2019-12-24 17:50 [BUG] `git diff` treats an unchanged line as modified Hao Lee
@ 2019-12-24 19:34 ` Junio C Hamano
  2019-12-25 15:26   ` Hao Lee
  0 siblings, 1 reply; 3+ messages in thread
From: Junio C Hamano @ 2019-12-24 19:34 UTC (permalink / raw)
  To: Hao Lee; +Cc: git

Hao Lee <haolee.swjtu@gmail.com> writes:

> I think the latter is correct because the fifth line is not changed
> and it's still a line which only contains a whitespace.

As long as lines that begin with "-" and " " match what the 'old'
file has, and replacing them with lines that begin with " " and
"+" in the 'old' file yields the 'new' file, any patch is "correct".

IOW, there is no *single* diff output that is correct.

Among many "correct" diff output, there are ones that readers find
them easier to understand and those that look suboptimal.  The
differences are often personal tastes.

"git diff" has options to tweak heuristics it uses to choose among
many "correct" diff outputs, like --patience, --histogram, etc.
Perhaps using one of them would produce one that match your taste
better?


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [BUG] `git diff` treats an unchanged line as modified
  2019-12-24 19:34 ` Junio C Hamano
@ 2019-12-25 15:26   ` Hao Lee
  0 siblings, 0 replies; 3+ messages in thread
From: Hao Lee @ 2019-12-25 15:26 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Wed, 25 Dec 2019 at 03:34, Junio C Hamano <gitster@pobox.com> wrote:
> IOW, there is no *single* diff output that is correct.
>
> Among many "correct" diff output, there are ones that readers find
> them easier to understand and those that look suboptimal.  The
> differences are often personal tastes.

Thanks, this explanation is clear for me.

> "git diff" has options to tweak heuristics it uses to choose among
> many "correct" diff outputs, like --patience, --histogram, etc.
> Perhaps using one of them would produce one that match your taste
> better?
>

After understanding the correctness of diff algorithm, the default
algorithm also suits my needs now, although the patience algorithm is
more understandable in some special circumstances.

Regards,
Hao Lee

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-12-25 15:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-24 17:50 [BUG] `git diff` treats an unchanged line as modified Hao Lee
2019-12-24 19:34 ` Junio C Hamano
2019-12-25 15:26   ` Hao Lee

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).