From nobody Wed Dec 14 01:33:30 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4NWyYl18jWz4kNGs; Wed, 14 Dec 2022 01:33:31 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4NWyYl0gYVz49Dq; Wed, 14 Dec 2022 01:33:31 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1670981611; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=qo5eI7WYbapsH+49dmLABieMQN0Zzk69qXyxU9j0/LU=; b=I+5guBBn2MfyMTtm18Nue91Y+IlLvwWw7Vlhn/Rn4MmQlIXfdtj8qssRs1edzmaI8kmVGW f/gWPnBtrC2mUhfV1GWnAbeD0wu0CDL+Kyu8N0iTD8ceGPKD8hwZc5yvtCfCg5x1mUbtBE NB4Z5xVSQhy96zXCEPN/dUnA4lK5VRPwuQTqNPYGvlO06GHxHTz1RthtiDx1YrYhO5R9ex d+0pNR0CocTuN+w4rIKYHEQtZUSd/3rqIjcp5zy8YTsk/BhV19wGarxi8SLLjFC0PZDjcu ZtY9G6MGECVRnXkSOPwJhUZXEl5MC63sRXAiUJyMF/EWWUonVPLuYOqxInff7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1670981611; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=qo5eI7WYbapsH+49dmLABieMQN0Zzk69qXyxU9j0/LU=; b=EKiV5zqxjkLGSewLnR4JC/Xb7A8+sOuCp4uktbJcl7zLql4nf2rQkJRyLNrbT37m0TrMTr 6wvBoCCxZ3YbiNfsgPgwHwDdLBr8bVyamIsbgPeHhx6An7Ls8Z/bucS+JMrKKdWuIuKPJP iiUIAgbU0JmhnJupvbYBIlu5i5fimfcFU1p0cxwKZWbKM3vylZmX2PPuh2xcDC0DTD72fH mQKet40chLriTTP6tpzxvTpkpAP8dz31TtT8nIAD2AHNxYZ8V6Qp5dhtQD8vmM16RKlfSx vIHHUl3MNnfn8iHGbiLeTU3sOZBtDWmxN2zDtgQV4OmjMXBgw8+DnNA1Fy0Mow== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1670981611; a=rsa-sha256; cv=none; b=CvipjX4Ab2wep+yca9D4NdOVOItAu+T6/Steb8lRu6aCe74hczUiYt8VCvyg/WWor5FGan 9iAqP4/uSfhIg3cosW3qOD29cHsnywYLFDhpiyDUtk9eo9JZN5Ou67BIJ1G1VCSJJFbKGu NtLNo5RpGtNQ3Cg0YB9XFX/VhX/A5VjYAoIvJHvy68ZnY9yc1s49v+6su6uWMr8JRFCrSo HC+Lkv70rv/jZnMM1LL5PYHKMe0eIXjjUxqYpYiWu+PdphXRqRs6U8ATxSLGk3t68fCDvs iTC0HKWtJ8JApL1dFefEuC4CXhN/IHC2kXwGzqUbQ67n0F6+I7qOuSkGvOLajA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4NWyYk6v64zbkN; Wed, 14 Dec 2022 01:33:30 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 2BE1XU9D077124; Wed, 14 Dec 2022 01:33:30 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2BE1XUpZ077123; Wed, 14 Dec 2022 01:33:30 GMT (envelope-from git) Date: Wed, 14 Dec 2022 01:33:30 GMT Message-Id: <202212140133.2BE1XUpZ077123@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kyle Evans Subject: git: 8bf187f35b62 - main - diff: fix side-by-side output with tabbed input List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 8bf187f35b6298b7848c5ecf45b0b714327090d9 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=8bf187f35b6298b7848c5ecf45b0b714327090d9 commit 8bf187f35b6298b7848c5ecf45b0b714327090d9 Author: Kyle Evans AuthorDate: 2022-12-14 01:31:21 +0000 Commit: Kyle Evans CommitDate: 2022-12-14 01:31:21 +0000 diff: fix side-by-side output with tabbed input The previous logic conflated some things... in this block: - j: input characters rendered so far - nc: number of characters in the line - col: columns rendered so far - hw: column width ((h)ard (w)idth?) Comparing j to hw or col to nc are naturally wrong, as col and hw are limits on their respective counters and nc is already brought down to hw if the input line should be truncated to start with. Right now, we end up easily truncating lines with tabs in them as we count each tab for $tabwidth lines in the input line, but we really should only be accounting for them in the column count. The problem is most easily demonstrated by the two input files added for the tests, the two tabbed lines lose at least a word or two even though there's plenty of space left in the row for each side. Reviewed by: bapt, pstef Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D37676 --- usr.bin/diff/diffreg.c | 14 ++++++-------- usr.bin/diff/tests/Makefile | 2 ++ usr.bin/diff/tests/diff_test.sh | 19 +++++++++++++++++++ usr.bin/diff/tests/side_by_side_tabbed_a.in | 4 ++++ usr.bin/diff/tests/side_by_side_tabbed_b.in | 4 ++++ 5 files changed, 35 insertions(+), 8 deletions(-) diff --git a/usr.bin/diff/diffreg.c b/usr.bin/diff/diffreg.c index def8a4e05974..1247317951d3 100644 --- a/usr.bin/diff/diffreg.c +++ b/usr.bin/diff/diffreg.c @@ -1247,7 +1247,8 @@ fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile, int flags) printf(" "); } col = 0; - for (j = 0, lastc = '\0'; j < nc; j++, lastc = c) { + for (j = 0, lastc = '\0'; j < nc && (hw == 0 || col < hw); + j++, lastc = c) { c = getc(lb); if (flags & D_STRIPCR && c == '\r') { if ((c = getc(lb)) == '\n') @@ -1274,19 +1275,16 @@ fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile, int flags) if (flags & D_EXPANDTABS) { newcol = ((col / tabsize) + 1) * tabsize; do { - if (diff_format == D_SIDEBYSIDE) - j++; printf(" "); - } while (++col < newcol && j < nc); + } while (++col < newcol && col < hw); } else { if (diff_format == D_SIDEBYSIDE) { - if ((j + tabsize) > nc) { - printf("%*s", nc - j, ""); - j = col = nc; + if ((col + tabsize) > hw) { + printf("%*s", hw - col, ""); + col = hw; } else { printf("\t"); col += tabsize - 1; - j += tabsize - 1; } } else { printf("\t"); diff --git a/usr.bin/diff/tests/Makefile b/usr.bin/diff/tests/Makefile index fbfac0c8372d..9c4639fe9a98 100644 --- a/usr.bin/diff/tests/Makefile +++ b/usr.bin/diff/tests/Makefile @@ -12,6 +12,8 @@ ${PACKAGE}FILES+= \ input2.in \ input_c1.in \ input_c2.in \ + side_by_side_tabbed_a.in \ + side_by_side_tabbed_b.in \ simple.out \ simple_e.out \ simple_n.out \ diff --git a/usr.bin/diff/tests/diff_test.sh b/usr.bin/diff/tests/diff_test.sh index e0968706acfb..fc82d1431a8d 100755 --- a/usr.bin/diff/tests/diff_test.sh +++ b/usr.bin/diff/tests/diff_test.sh @@ -7,6 +7,7 @@ atf_test_case header_ns atf_test_case ifdef atf_test_case group_format atf_test_case side_by_side +atf_test_case side_by_side_tabbed atf_test_case brief_format atf_test_case b230049 atf_test_case stripcr_o @@ -145,6 +146,23 @@ side_by_side_body() diff -W 65 -y --suppress-common-lines A B } +side_by_side_tabbed_body() +{ + file_a=$(atf_get_srcdir)/side_by_side_tabbed_a.in + file_b=$(atf_get_srcdir)/side_by_side_tabbed_b.in + + atf_check -o save:diffout -s not-exit:0 \ + diff -y ${file_a} ${file_b} + atf_check -o save:diffout_expanded -s not-exit:0 \ + diff -yt ${file_a} ${file_b} + + atf_check -o not-empty grep -Ee 'file A.+file B' diffout + atf_check -o not-empty grep -Ee 'file A.+file B' diffout_expanded + + atf_check -o not-empty grep -Ee 'tabs.+tabs' diffout + atf_check -o not-empty grep -Ee 'tabs.+tabs' diffout_expanded +} + brief_format_body() { atf_check mkdir A B @@ -343,6 +361,7 @@ atf_init_test_cases() atf_add_test_case ifdef atf_add_test_case group_format atf_add_test_case side_by_side + atf_add_test_case side_by_side_tabbed atf_add_test_case brief_format atf_add_test_case b230049 atf_add_test_case stripcr_o diff --git a/usr.bin/diff/tests/side_by_side_tabbed_a.in b/usr.bin/diff/tests/side_by_side_tabbed_a.in new file mode 100644 index 000000000000..3198d29f9203 --- /dev/null +++ b/usr.bin/diff/tests/side_by_side_tabbed_a.in @@ -0,0 +1,4 @@ +This + is my test file A + it has tabs +Thanks diff --git a/usr.bin/diff/tests/side_by_side_tabbed_b.in b/usr.bin/diff/tests/side_by_side_tabbed_b.in new file mode 100644 index 000000000000..6698182a8c5d --- /dev/null +++ b/usr.bin/diff/tests/side_by_side_tabbed_b.in @@ -0,0 +1,4 @@ +This + is my test file B + it has tabs +Thanks