Hello,
I have made the following discovery and also know a simple workaround in the shell how to avoid it, without changing diffstat's source code.
However, in case you're considering fixing this issue inside the diffstat code... here are my findings:
OS Release:
Output:
NAME="openSUSE Tumbleweed"
# VERSION="20211228"
ID="opensuse-tumbleweed"
ID_LIKE="opensuse suse"
VERSION_ID="20211228"
PRETTY_NAME="openSUSE Tumbleweed"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:opensuse:tumbleweed:20211228"
BUG_REPORT_URL="https://bugs.opensuse.org"
HOME_URL="https://www.opensuse.org/"
DOCUMENTATION_URL="https://en.opensuse.org/Portal:Tumbleweed"
LOGO="distributor-logo-Tumbleweed"
Diff version:
Output:
diff (GNU diffutils) 3.8
Packaged by openSUSE
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Paul Eggert, Mike Haertel, David Hayes,
Richard Stallman, and Len Tower.
Diffstat version:
Output:
Setup the test playground:
mkdir -p ~/tmp/diffstat_tests
cd ~/tmp/diffstat_tests
mkdir A B
touch A/old_file.txt
touch B/new_file.txt
touch A/common_file.txt
touch B/common_file.txt
ls -lR > A/common_file.txt
ls -lR > B/common_file.txt
Regular diff run:
Output:
diff -ur A/common_file.txt B/common_file.txt
--- A/common_file.txt 2022-01-01 14:51:14.228516014 +0100
+++ B/common_file.txt 2022-01-01 14:51:19.656661982 +0100
@@ -4,10 +4,10 @@
./A:
Permissions Size User Date Modified Name
-.rw------- 0 paxsali 2022-01-01 14:51 common_file.txt
+.rw------- 467 paxsali 2022-01-01 14:51 common_file.txt
.rw------- 0 paxsali 2022-01-01 14:49 old_file.txt
./B:
Permissions Size User Date Modified Name
-.rw------- 0 paxsali 2022-01-01 14:50 common_file.txt
+.rw------- 188 paxsali 2022-01-01 14:51 common_file.txt
.rw------- 0 paxsali 2022-01-01 14:49 new_file.txt
Only in B: new_file.txt
Only in A: old_file.txt
Pipe output through diffstat:
diff -ur A B | diffstat -C -K
Output:
A/old_file.txt |deleted
B/common_file.txt | 4 ++--
B/new_file.txt |added
3 files changed, 2 insertions(+), 2 deletions(-), 1 file added, 1 file removed
Notice the output is correct and the added and deleted files are correctly identified.
Regular diff, but with trailing slashes:
Output:
diff -ur A/common_file.txt B/common_file.txt
--- A/common_file.txt 2022-01-01 14:51:14.228516014 +0100
+++ B/common_file.txt 2022-01-01 14:51:19.656661982 +0100
@@ -4,10 +4,10 @@
./A:
Permissions Size User Date Modified Name
-.rw------- 0 paxsali 2022-01-01 14:51 common_file.txt
+.rw------- 467 paxsali 2022-01-01 14:51 common_file.txt
.rw------- 0 paxsali 2022-01-01 14:49 old_file.txt
./B:
Permissions Size User Date Modified Name
-.rw------- 0 paxsali 2022-01-01 14:50 common_file.txt
+.rw------- 188 paxsali 2022-01-01 14:51 common_file.txt
.rw------- 0 paxsali 2022-01-01 14:49 new_file.txt
Only in B/: new_file.txt
Only in A/: old_file.txt
Pipe diff with trailing slashes through diffstat:
diff -ur A/ B/ | diffstat -C -K
Output:
A//old_file.txt |deleted
B//new_file.txt |deleted
B/common_file.txt | 4 ++--
3 files changed, 2 insertions(+), 2 deletions(-), 2 files removed
Now the output is A) unusual, because of the double-slashes in the filenames and B) incorrect, since both files are labeled deleted.
Workaround is to remove trailing slashes on "Only in DIR/: FILE" lines.
Both of the following commands produce the same output (including any combination of A/ B, A B/):
diff -ur A/ B/ | sed '/^Only in /s/\/:/:/' | diffstat -C -K
diff -ur A B | sed '/^Only in /s/\/:/:/' | diffstat -C -K
Output:
A/old_file.txt |deleted
B/common_file.txt | 4 ++--
B/new_file.txt |added
3 files changed, 2 insertions(+), 2 deletions(-), 1 file added, 1 file removed
I'll consider providing a patch, but I'm not a C programmer, so I'd have to read up and test some things out first.
Please consider patching this on the diffstat side, because I can see some legitimate ambiguity on the question of where to address this issue.
It may as well have been addressed over at the guys from https://savannah.gnu.org/projects/diffutils/ to the original diff utility.
However, IMHO it may be more appropriate to fix this here rather than in diff itself.
Thanks. And happy new year, btw.