# [BUG] `git diff` treats an unchanged line as modified

3 messages from 2019-12-24 to 2019-12-25. Participants: Hao Lee, Junio C Hamano.
Thread: https://gitlist.dev/t/52516

## Hao Lee, 2019-12-24 17:50

Subject: [BUG] `git diff` treats an unchanged line as modified
Message-ID: <CA+PpKPm0_zXr978Mw8h7owHBChx-uqVCdjrtuh45uOS-A_0qrA@mail.gmail.com>
URL: https://gitlist.dev/e/CA%2BPpKPm0_zXr978Mw8h7owHBChx-uqVCdjrtuh45uOS-A_0qrA%40mail.gmail.com

```
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

```

## Junio C Hamano, 2019-12-24 19:34

Subject: Re: [BUG] `git diff` treats an unchanged line as modified
Message-ID: <xmqqy2v14iwc.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqqy2v14iwc.fsf%40gitster-ct.c.googlers.com
In-Reply-To: <CA+PpKPm0_zXr978Mw8h7owHBChx-uqVCdjrtuh45uOS-A_0qrA@mail.gmail.com>

```
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?


```

## Hao Lee, 2019-12-25 15:26

Subject: Re: [BUG] `git diff` treats an unchanged line as modified
Message-ID: <CA+PpKPnCrSciiNinC9g6BVX7DHXPDq8k2zegK+-9Yg2CPzF_bQ@mail.gmail.com>
URL: https://gitlist.dev/e/CA%2BPpKPnCrSciiNinC9g6BVX7DHXPDq8k2zegK%2B-9Yg2CPzF_bQ%40mail.gmail.com
In-Reply-To: <xmqqy2v14iwc.fsf@gitster-ct.c.googlers.com>

```
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

```
