threads / bug / 52516

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

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

## tl;dr

3 messages between Dec 24, 2019 and Dec 25, 2019.

replies: 2people: 2as markdown or json

Hao Lee· Dec 24, 2019, 17:50 UTC · lore
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· Dec 24, 2019, 19:34 UTC · re: Hao Lee · lore

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

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· Dec 25, 2019, 15:26 UTC · re: Junio C Hamano · lore

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

On Wed, 25 Dec 2019 at 03:34, Junio C Hamano <gitster@pobox.com> wrote:
Show 5 quoted lines
> 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.
Show 5 quoted lines
> "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

← back to recent threads