Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 25 Sep 2021 08:41:35 GMT
From:      Piotr Pawel Stefaniak <pstef@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: dbabfd7e2676 - stable/13 - diff3: implement --strip-trailing-cr
Message-ID:  <202109250841.18P8fZxB018177@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by pstef:

URL: https://cgit.FreeBSD.org/src/commit/?id=dbabfd7e26763ac295ed0ee8bc77f3f3b58b6d03

commit dbabfd7e26763ac295ed0ee8bc77f3f3b58b6d03
Author:     Piotr Pawel Stefaniak <pstef@FreeBSD.org>
AuthorDate: 2021-08-21 00:28:58 +0000
Commit:     Piotr Pawel Stefaniak <pstef@FreeBSD.org>
CommitDate: 2021-09-25 08:34:39 +0000

    diff3: implement --strip-trailing-cr
    
    (cherry picked from commit 54a3415cb69e1ea0e6533ca3fc168deba64f249f)
---
 usr.bin/diff3/diff3.1             | 3 +++
 usr.bin/diff3/diff3.c             | 9 ++++++++-
 usr.bin/diff3/tests/1cr.txt       | 6 ++++++
 usr.bin/diff3/tests/Makefile      | 1 +
 usr.bin/diff3/tests/diff3_test.sh | 3 +++
 5 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/usr.bin/diff3/diff3.1 b/usr.bin/diff3/diff3.1
index cc4bdf1c8836..22e04b6af5e8 100644
--- a/usr.bin/diff3/diff3.1
+++ b/usr.bin/diff3/diff3.1
@@ -40,6 +40,7 @@
 .Nm diff3
 .Op Fl 3aEeiXx
 .Op Fl Fl diff-program Ar program
+.Op Fl Fl strip-trailing-cr
 .Op Fl L | Fl Fl label Ar label1
 .Op Fl L | Fl Fl label Ar label2
 .Op Fl L | Fl Fl label Ar label3
@@ -125,6 +126,8 @@ Use
 instead of the default
 .Xr diff 1
 to compare files.
+.It Fl Fl strip-trailing-cr
+Strip trailing carriage return on input files.
 .El
 .Pp
 The
diff --git a/usr.bin/diff3/diff3.c b/usr.bin/diff3/diff3.c
index 18daa54b2360..122243a9448f 100644
--- a/usr.bin/diff3/diff3.c
+++ b/usr.bin/diff3/diff3.c
@@ -264,6 +264,12 @@ get_line(FILE *b, size_t *n)
 	if ((len = getline(&buf, &bufsize, b)) < 0)
 		return (NULL);
 
+	if (strip_cr && len >= 2 && strcmp("\r\n", &(buf[len - 2])) == 0) {
+		buf[len - 2] = '\n';
+		buf[len - 1] = '\0';
+		len--;
+	}
+
 	if (n != NULL)
 		*n = len;
 
@@ -599,7 +605,7 @@ main(int argc, char **argv)
 	char *labels[] = { NULL, NULL, NULL };
 	const char *diffprog = DIFF_PATH;
 	char *file1, *file2, *file3;
-	char *diffargv[6];
+	char *diffargv[7];
 	int diffargc = 0;
 	int fd13[2], fd23[2];
 	int pd13, pd23;
@@ -657,6 +663,7 @@ main(int argc, char **argv)
 			break;
 		case STRIPCR_OPT:
 			strip_cr = 1;
+			diffargv[diffargc++] = __DECONST(char *, "--strip-trailing-cr");
 			break;
 		}
 	}
diff --git a/usr.bin/diff3/tests/1cr.txt b/usr.bin/diff3/tests/1cr.txt
new file mode 100644
index 000000000000..c24bf241e68a
--- /dev/null
+++ b/usr.bin/diff3/tests/1cr.txt
@@ -0,0 +1,6 @@
+This is a more complete test
+which multiple lines
+and a few typ0s to fix
+also I plan to add few lines
+    and to remove
+other lines
diff --git a/usr.bin/diff3/tests/Makefile b/usr.bin/diff3/tests/Makefile
index dd75be1ca02a..2ec7fbaab564 100644
--- a/usr.bin/diff3/tests/Makefile
+++ b/usr.bin/diff3/tests/Makefile
@@ -6,6 +6,7 @@ ATF_TESTS_SH=	diff3_test
 
 ${PACKAGE}FILES+=	\
 	1.txt \
+	1cr.txt \
 	2.txt \
 	3.txt \
 	1.out \
diff --git a/usr.bin/diff3/tests/diff3_test.sh b/usr.bin/diff3/tests/diff3_test.sh
index 6c479de2e9c0..f4b5adf95700 100755
--- a/usr.bin/diff3/tests/diff3_test.sh
+++ b/usr.bin/diff3/tests/diff3_test.sh
@@ -8,6 +8,9 @@ diff3_body()
 	atf_check -o file:$(atf_get_srcdir)/1.out \
 		diff3 $(atf_get_srcdir)/1.txt $(atf_get_srcdir)/2.txt $(atf_get_srcdir)/3.txt
 
+	atf_check -o file:$(atf_get_srcdir)/1.out \
+		diff3 --strip-trailing-cr $(atf_get_srcdir)/1cr.txt $(atf_get_srcdir)/2.txt $(atf_get_srcdir)/3.txt
+
 	atf_check -o file:$(atf_get_srcdir)/2.out \
 		diff3 -e $(atf_get_srcdir)/1.txt $(atf_get_srcdir)/2.txt $(atf_get_srcdir)/3.txt
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202109250841.18P8fZxB018177>