From patchwork Thu Aug 17 22:29:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Junio C Hamano X-Patchwork-Id: 13357053 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3EA8EC678DC for ; Thu, 17 Aug 2023 22:30:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355742AbjHQWaT (ORCPT ); Thu, 17 Aug 2023 18:30:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355732AbjHQW3z (ORCPT ); Thu, 17 Aug 2023 18:29:55 -0400 Received: from pb-smtp2.pobox.com (pb-smtp2.pobox.com [64.147.108.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92AC43595 for ; Thu, 17 Aug 2023 15:29:53 -0700 (PDT) Received: from pb-smtp2.pobox.com (unknown [127.0.0.1]) by pb-smtp2.pobox.com (Postfix) with ESMTP id AF9E91A824D; Thu, 17 Aug 2023 18:29:52 -0400 (EDT) (envelope-from gitster@pobox.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=pobox.com; h=from:to :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=sasl; bh=H5/BOkdbStAc/BYk39dViL9QO aHXf/vMIGXnBlM23nM=; b=U6RX0uMPUXZzS1MArAec9OyItvM7SRCrQDboFvcso j0besz4+Y/3Hj/SlnFN/3Qp9fxDOWSiU1StG+SIoa09hXkJjpjJsvoV+My8wS+vy bEGUu2ohNUSdRzy3S4QMQJP1u5TxR7gljbiA447WXDQ7mRySPBSSlNYUzHS5PjlX zo= Received: from pb-smtp2.nyi.icgroup.com (unknown [127.0.0.1]) by pb-smtp2.pobox.com (Postfix) with ESMTP id A776E1A824C; Thu, 17 Aug 2023 18:29:52 -0400 (EDT) (envelope-from gitster@pobox.com) Received: from pobox.com (unknown [34.83.58.166]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pb-smtp2.pobox.com (Postfix) with ESMTPSA id 1C5CC1A824B; Thu, 17 Aug 2023 18:29:52 -0400 (EDT) (envelope-from gitster@pobox.com) From: Junio C Hamano To: git@vger.kernel.org Subject: [PATCH v2 1/5] diff: --dirstat leakfix Date: Thu, 17 Aug 2023 15:29:45 -0700 Message-ID: <20230817222949.3835424-2-gitster@pobox.com> X-Mailer: git-send-email 2.42.0-rc2 In-Reply-To: <20230817222949.3835424-1-gitster@pobox.com> References: <20230817222949.3835424-1-gitster@pobox.com> MIME-Version: 1.0 X-Pobox-Relay-ID: 9547C696-3D4D-11EE-B079-25B3960A682E-77302942!pb-smtp2.pobox.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The dirstat_dir structure holds a list of files that had "damages" and is used to summarize the change by directory. It was allocated, used, and then left behind, leaking. Signed-off-by: Junio C Hamano --- diff.c | 14 ++++++++++++-- t/t4047-diff-dirstat.sh | 2 ++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/diff.c b/diff.c index 648f6717a5..03d0cfc700 100644 --- a/diff.c +++ b/diff.c @@ -2977,6 +2977,7 @@ static void show_dirstat(struct diff_options *options) unsigned long changed; struct dirstat_dir dir; struct diff_queue_struct *q = &diff_queued_diff; + struct dirstat_file *to_free; dir.files = NULL; dir.alloc = 0; @@ -3060,13 +3061,17 @@ static void show_dirstat(struct diff_options *options) dir.nr++; } + to_free = dir.files; + /* This can happen even with many files, if everything was renames */ if (!changed) - return; + goto free_return; /* Show all directories with more than x% of the changes */ QSORT(dir.files, dir.nr, dirstat_compare); gather_dirstat(options, &dir, changed, "", 0); +free_return: + free(to_free); } static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *options) @@ -3074,6 +3079,7 @@ static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *o int i; unsigned long changed; struct dirstat_dir dir; + struct dirstat_file *to_free; if (data->nr == 0) return; @@ -3104,13 +3110,17 @@ static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *o dir.nr++; } + to_free = dir.files; + /* This can happen even with many files, if everything was renames */ if (!changed) - return; + goto free_return; /* Show all directories with more than x% of the changes */ QSORT(dir.files, dir.nr, dirstat_compare); gather_dirstat(options, &dir, changed, "", 0); +free_return: + free(to_free); } static void free_diffstat_file(struct diffstat_file *f) diff --git a/t/t4047-diff-dirstat.sh b/t/t4047-diff-dirstat.sh index 7fec2cb9cd..70224c3da1 100755 --- a/t/t4047-diff-dirstat.sh +++ b/t/t4047-diff-dirstat.sh @@ -1,6 +1,8 @@ #!/bin/sh test_description='diff --dirstat tests' + +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh # set up two commits where the second commit has these files From patchwork Thu Aug 17 22:29:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Junio C Hamano X-Patchwork-Id: 13357054 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 82CD1C678DF for ; Thu, 17 Aug 2023 22:30:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355745AbjHQWaU (ORCPT ); Thu, 17 Aug 2023 18:30:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36522 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355734AbjHQW37 (ORCPT ); Thu, 17 Aug 2023 18:29:59 -0400 Received: from pb-smtp21.pobox.com (pb-smtp21.pobox.com [173.228.157.53]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5EDF3590 for ; Thu, 17 Aug 2023 15:29:57 -0700 (PDT) Received: from pb-smtp21.pobox.com (unknown [127.0.0.1]) by pb-smtp21.pobox.com (Postfix) with ESMTP id 9373B377CB; Thu, 17 Aug 2023 18:29:57 -0400 (EDT) (envelope-from gitster@pobox.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=pobox.com; h=from:to :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=sasl; bh=mNbnbnacm4FQet3iaLcy/AVc8 Q1/OhqyYLRtkqX4Rak=; b=TueJMsBSQ3rWNJNp8+L8Qc1wTcIeXrrMw+cV/s74P SfpUxQ9pjVXeZxSQ0F5t/w3MCMF1G4ANGGKtD7iIlQVtVKXNBCHIyqraRo6R87i5 WeeuValczmi/NXHDVG1xcUQCSCzGH+K9P1mnjZs9kKdmQI6r7kVswX/2X83ObiTj bY= Received: from pb-smtp21.sea.icgroup.com (unknown [127.0.0.1]) by pb-smtp21.pobox.com (Postfix) with ESMTP id 8D2A3377CA; Thu, 17 Aug 2023 18:29:57 -0400 (EDT) (envelope-from gitster@pobox.com) Received: from pobox.com (unknown [34.83.58.166]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pb-smtp21.pobox.com (Postfix) with ESMTPSA id C5056377C9; Thu, 17 Aug 2023 18:29:53 -0400 (EDT) (envelope-from gitster@pobox.com) From: Junio C Hamano To: git@vger.kernel.org Subject: [PATCH v2 2/5] diff: move the fallback "--exit-code" code down Date: Thu, 17 Aug 2023 15:29:46 -0700 Message-ID: <20230817222949.3835424-3-gitster@pobox.com> X-Mailer: git-send-email 2.42.0-rc2 In-Reply-To: <20230817222949.3835424-1-gitster@pobox.com> References: <20230817222949.3835424-1-gitster@pobox.com> MIME-Version: 1.0 X-Pobox-Relay-ID: 96496F9A-3D4D-11EE-BD9B-A19503B9AAD1-77302942!pb-smtp21.pobox.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org When "--exit-code" is asked and the code cannot just answer by comparing the object names on both sides but need to inspect and compare the contents, there are two ways that the result is found out. Some output modes, like "--stat" and "--patch", inherently have to inspect the contents in order to _show_ the differences in the way they do. The codepaths for these modes set the .found_changes bit as they compute what to show. However, other output modes do not need to inspect the contents to show the differences in the way they do. The most notable example is "--quiet", which does not need to compute any output. When they are asked to report "--exit-code", they run the codepaths for the "--patch" output with their output redirected to "/dev/null", only to set the .found_changes bit. Currently, this fallback invocation of "--patch" output is done after the "--stat" output format and its friends and before the "--patch" and internal callback logic. Move it to the end of the sequence to clarify the fallback status of this code block. Signed-off-by: Junio C Hamano --- diff.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/diff.c b/diff.c index 03d0cfc700..ce9c8272c7 100644 --- a/diff.c +++ b/diff.c @@ -6555,6 +6555,21 @@ void diff_flush(struct diff_options *options) separator++; } + if (output_format & DIFF_FORMAT_PATCH) { + if (separator) { + emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0, 0); + if (options->stat_sep) + /* attach patch instead of inline */ + emit_diff_symbol(options, DIFF_SYMBOL_STAT_SEP, + NULL, 0, 0); + } + + diff_flush_patch_all_file_pairs(options); + } + + if (output_format & DIFF_FORMAT_CALLBACK) + options->format_callback(q, options, options->format_callback_data); + if (output_format & DIFF_FORMAT_NO_OUTPUT && options->flags.exit_with_status && options->flags.diff_from_contents) { @@ -6576,21 +6591,6 @@ void diff_flush(struct diff_options *options) } } - if (output_format & DIFF_FORMAT_PATCH) { - if (separator) { - emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0, 0); - if (options->stat_sep) - /* attach patch instead of inline */ - emit_diff_symbol(options, DIFF_SYMBOL_STAT_SEP, - NULL, 0, 0); - } - - diff_flush_patch_all_file_pairs(options); - } - - if (output_format & DIFF_FORMAT_CALLBACK) - options->format_callback(q, options, options->format_callback_data); - for (i = 0; i < q->nr; i++) diff_free_filepair(q->queue[i]); free_queue: From patchwork Thu Aug 17 22:29:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Junio C Hamano X-Patchwork-Id: 13357055 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 72752C6FD22 for ; Thu, 17 Aug 2023 22:30:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355747AbjHQWaV (ORCPT ); Thu, 17 Aug 2023 18:30:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36526 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355737AbjHQW37 (ORCPT ); Thu, 17 Aug 2023 18:29:59 -0400 Received: from pb-smtp1.pobox.com (pb-smtp1.pobox.com [64.147.108.70]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 975103595 for ; Thu, 17 Aug 2023 15:29:58 -0700 (PDT) Received: from pb-smtp1.pobox.com (unknown [127.0.0.1]) by pb-smtp1.pobox.com (Postfix) with ESMTP id D6B4D1990FD; Thu, 17 Aug 2023 18:29:57 -0400 (EDT) (envelope-from gitster@pobox.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=pobox.com; h=from:to :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=sasl; bh=a67xFPWNuHCFn5UKzYQwy8Y5l ikGece0KAp5A9R5vQE=; b=gwYYNCQxmrzvdA+cqVrv8JCXIYlUnXrBwG/gy7sFr pbM+H65Wl1tmaV4Khf0N2zH+Eaow0ixq6WXhaZQz0jmtOQByIjy2L/NcylNISiiH nDnYa/+LSbtfizy6bmYP3HrQXB5zD/RLV7QGbsrnU+POQRoyD2gpIRZKa6SwqPMD Y4= Received: from pb-smtp1.nyi.icgroup.com (unknown [127.0.0.1]) by pb-smtp1.pobox.com (Postfix) with ESMTP id CF3FF1990FB; Thu, 17 Aug 2023 18:29:57 -0400 (EDT) (envelope-from gitster@pobox.com) Received: from pobox.com (unknown [34.83.58.166]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pb-smtp1.pobox.com (Postfix) with ESMTPSA id 3FC3D1990FA; Thu, 17 Aug 2023 18:29:57 -0400 (EDT) (envelope-from gitster@pobox.com) From: Junio C Hamano To: git@vger.kernel.org Subject: [PATCH v2 3/5] diff: mode-only change should be noticed by "--patch -w --exit-code" Date: Thu, 17 Aug 2023 15:29:47 -0700 Message-ID: <20230817222949.3835424-4-gitster@pobox.com> X-Mailer: git-send-email 2.42.0-rc2 In-Reply-To: <20230817222949.3835424-1-gitster@pobox.com> References: <20230817222949.3835424-1-gitster@pobox.com> MIME-Version: 1.0 X-Pobox-Relay-ID: 98586232-3D4D-11EE-91CA-78DCEB2EC81B-77302942!pb-smtp1.pobox.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The codepath to notice the content-level changes, taking certaion no-op changes like "ignore whitespace" into account, forgot that a mode-only change is still a change. This resulted in $ git diff --patch --exit-code -w to exit with status 0 even when there is such a mode-only change, breaking both "--patch" and "--quiet" output formats. Teach the builtin_diff() codepath that creation and deletion as well as mode changes are all interesting changes. Note that the test specifically checks removal of an empty file, because if there is anything in the preimage (i.e. the removed file is not empty), the removal would still trigger textual patch output and the codepath for that does update .found_changes bit to report that it found an interesting change. We need to make sure that the .found_changes bit is set even without triggering textual patch output. Signed-off-by: Junio C Hamano --- diff.c | 3 +++ t/t4015-diff-whitespace.sh | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/diff.c b/diff.c index ce9c8272c7..de18364902 100644 --- a/diff.c +++ b/diff.c @@ -3501,18 +3501,21 @@ static void builtin_diff(const char *name_a, strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, meta, two->mode, reset); if (xfrm_msg) strbuf_addstr(&header, xfrm_msg); + o->found_changes = 1; must_show_header = 1; } else if (lbl[1][0] == '/') { strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, meta, one->mode, reset); if (xfrm_msg) strbuf_addstr(&header, xfrm_msg); + o->found_changes = 1; must_show_header = 1; } else { if (one->mode != two->mode) { strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, meta, one->mode, reset); strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, meta, two->mode, reset); + o->found_changes = 1; must_show_header = 1; } if (xfrm_msg) diff --git a/t/t4015-diff-whitespace.sh b/t/t4015-diff-whitespace.sh index f3e20dd5bb..943ad252d4 100755 --- a/t/t4015-diff-whitespace.sh +++ b/t/t4015-diff-whitespace.sh @@ -1,7 +1,7 @@ #!/bin/sh # # Copyright (c) 2006 Johannes E. Schindelin -# +# Copyright (c) 2023 Google LLC test_description='Test special whitespace in diff engine. @@ -11,6 +11,39 @@ TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh . "$TEST_DIRECTORY"/lib-diff.sh +for opts in --patch --quiet -s +do + + test_expect_success "status with $opts (different)" ' + echo foo >x && + git add x && + echo bar >x && + test_expect_code 1 git diff -w $opts --exit-code x + ' + + test_expect_success "status with $opts (mode differs)" ' + test_when_finished "git update-index --chmod=-x x" && + echo foo >x && + git add x && + git update-index --chmod=+x x && + test_expect_code 1 git diff -w $opts --exit-code x + ' + + test_expect_success "status with $opts (removing an empty file)" ' + : >x && + git add x && + rm x && + test_expect_code 1 git diff -w $opts --exit-code -- x + ' + + test_expect_success "status with $opts (different but equivalent)" ' + echo foo >x && + git add x && + echo " foo" >x && + git diff -w $opts --exit-code x + ' +done + test_expect_success "Ray Lehtiniemi's example" ' cat <<-\EOF >x && do { From patchwork Thu Aug 17 22:29:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Junio C Hamano X-Patchwork-Id: 13357056 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9434BC7112F for ; Thu, 17 Aug 2023 22:30:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355749AbjHQWaV (ORCPT ); Thu, 17 Aug 2023 18:30:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355738AbjHQWaH (ORCPT ); Thu, 17 Aug 2023 18:30:07 -0400 Received: from pb-smtp20.pobox.com (pb-smtp20.pobox.com [173.228.157.52]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3DA5030DA for ; Thu, 17 Aug 2023 15:30:03 -0700 (PDT) Received: from pb-smtp20.pobox.com (unknown [127.0.0.1]) by pb-smtp20.pobox.com (Postfix) with ESMTP id DAD6521588; Thu, 17 Aug 2023 18:30:02 -0400 (EDT) (envelope-from gitster@pobox.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=pobox.com; h=from:to :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=sasl; bh=d3jHmn1eL3Y5F+8rwj8DGJg4Y ACZBGRMycmuvYISa1Y=; b=sAz9inqusq13u00WWxvzqFBp8H9RU5vsC4VIfRQlM uOVWP/BP8xbURW1VgjCR1RsK5jLI5UcxPVkGJxWkvsWWErdOQ0yLPSNKIBc9p9wJ dpqlMC7ECZvJVp0OlYRjWfUdE2h4FyOJCXGU/TlhL415MxgOEV13yuRa0Y4ZipfO XY= Received: from pb-smtp20.sea.icgroup.com (unknown [127.0.0.1]) by pb-smtp20.pobox.com (Postfix) with ESMTP id D477E21587; Thu, 17 Aug 2023 18:30:02 -0400 (EDT) (envelope-from gitster@pobox.com) Received: from pobox.com (unknown [34.83.58.166]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pb-smtp20.pobox.com (Postfix) with ESMTPSA id EE61621582; Thu, 17 Aug 2023 18:29:58 -0400 (EDT) (envelope-from gitster@pobox.com) From: Junio C Hamano To: git@vger.kernel.org Subject: [PATCH v2 4/5] diff: teach "--stat -w --exit-code" to notice differences Date: Thu, 17 Aug 2023 15:29:48 -0700 Message-ID: <20230817222949.3835424-5-gitster@pobox.com> X-Mailer: git-send-email 2.42.0-rc2 In-Reply-To: <20230817222949.3835424-1-gitster@pobox.com> References: <20230817222949.3835424-1-gitster@pobox.com> MIME-Version: 1.0 X-Pobox-Relay-ID: 995DFE44-3D4D-11EE-95D2-F515D2CDFF5E-77302942!pb-smtp20.pobox.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org When options like "-w" is used while "--exit-code" option is in effect, instead of the usual "do we have any filepair whose preimage and postimage have different ?" check, we need to compare the contents of the blobs, taking into account that certain changes are considered no-op. With the previous step, we taught "--patch" codepath to set the .found_changes bit correctly, even for a change that only affects the mode and not object. The "--stat" codepath, however, did not set the .found_changes bit at all. This lead to $ git diff --stat -w --exit-code for a change that does have an outout to exit with status 0. Set the bit by inspecting the list of paths the diffstat output is given for (a mode-only change will still appear as a "0-line added 0-line deleted" change) to fix it. Signed-off-by: Junio C Hamano --- diff.c | 1 + t/t4015-diff-whitespace.sh | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/diff.c b/diff.c index de18364902..7213237675 100644 --- a/diff.c +++ b/diff.c @@ -6905,6 +6905,7 @@ void compute_diffstat(struct diff_options *options, if (check_pair_status(p)) diff_flush_stat(p, options, diffstat); } + options->found_changes = !!diffstat->nr; } void diff_addremove(struct diff_options *options, diff --git a/t/t4015-diff-whitespace.sh b/t/t4015-diff-whitespace.sh index 943ad252d4..355d96aa14 100755 --- a/t/t4015-diff-whitespace.sh +++ b/t/t4015-diff-whitespace.sh @@ -11,7 +11,7 @@ TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh . "$TEST_DIRECTORY"/lib-diff.sh -for opts in --patch --quiet -s +for opts in --patch --quiet -s --stat --shortstat --dirstat=lines do test_expect_success "status with $opts (different)" ' From patchwork Thu Aug 17 22:29:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Junio C Hamano X-Patchwork-Id: 13357057 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AA7D4C7112B for ; Thu, 17 Aug 2023 22:30:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355751AbjHQWaW (ORCPT ); Thu, 17 Aug 2023 18:30:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355739AbjHQWaH (ORCPT ); Thu, 17 Aug 2023 18:30:07 -0400 Received: from pb-smtp2.pobox.com (pb-smtp2.pobox.com [64.147.108.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A2523590 for ; Thu, 17 Aug 2023 15:30:06 -0700 (PDT) Received: from pb-smtp2.pobox.com (unknown [127.0.0.1]) by pb-smtp2.pobox.com (Postfix) with ESMTP id 6BC611A8254; Thu, 17 Aug 2023 18:30:05 -0400 (EDT) (envelope-from gitster@pobox.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=pobox.com; h=from:to :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=sasl; bh=KZ9bVplMqjBwPPZwqZPiKerMb gbCvdYG3lvFDXPxXVg=; b=tRR2nmfWvxQEE1x4ToAqbNiClDvsGdAM9JlyVkqjW jXiFszBVt3fBmBLMDLBsnnjkfHDQrZ52Pbgzfpd5SY9Lhp0+JO7w0PLc6IoOPggn kEyOWUFeTVjyFnOCiTTzMgo0oW6j72ux7EEl3VTQkZchjG7nzLdE+wA1OEg6lqT4 p4= Received: from pb-smtp2.nyi.icgroup.com (unknown [127.0.0.1]) by pb-smtp2.pobox.com (Postfix) with ESMTP id 1B0E11A8251; Thu, 17 Aug 2023 18:30:05 -0400 (EDT) (envelope-from gitster@pobox.com) Received: from pobox.com (unknown [34.83.58.166]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pb-smtp2.pobox.com (Postfix) with ESMTPSA id 5C4FE1A824E; Thu, 17 Aug 2023 18:30:03 -0400 (EDT) (envelope-from gitster@pobox.com) From: Junio C Hamano To: git@vger.kernel.org Subject: [PATCH v2 5/5] diff: teach "--name-status" and friends to honor "--exit-code -w" Date: Thu, 17 Aug 2023 15:29:49 -0700 Message-ID: <20230817222949.3835424-6-gitster@pobox.com> X-Mailer: git-send-email 2.42.0-rc2 In-Reply-To: <20230817222949.3835424-1-gitster@pobox.com> References: <20230817222949.3835424-1-gitster@pobox.com> MIME-Version: 1.0 X-Pobox-Relay-ID: 9C042DEE-3D4D-11EE-907B-25B3960A682E-77302942!pb-smtp2.pobox.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org We have fallback code that is used when "-s -w --exit-code" options are used together to compute the exit code, because the exit code must take the whitespace-ignoring comparison into account over the contents, but with "-s", the normal codepath does not even have to look at the contents at all. The fallback code simply runs a "git diff --patch" while sending the output to "/dev/null". The codepaths for some other output modes, like "--name-status" and "--raw", share the same trait as "-s" in that they do not look at the contents for their output generation. Extend the fallback code to cover these output modes as well. Note that they may still not be correct in that a path whose contents have no differences other than whitespace changes would still show up in the "diff -w --name-only --exit-code" output, even though the exit status may say there is no differences. Arguably this is better than status quo, even though it still may be wrong. Signed-off-by: Junio C Hamano --- diff.c | 23 +++++++++++++++++++---- t/t4015-diff-whitespace.sh | 3 ++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/diff.c b/diff.c index 7213237675..3bb9d11bfe 100644 --- a/diff.c +++ b/diff.c @@ -6573,13 +6573,28 @@ void diff_flush(struct diff_options *options) if (output_format & DIFF_FORMAT_CALLBACK) options->format_callback(q, options, options->format_callback_data); - if (output_format & DIFF_FORMAT_NO_OUTPUT && + if (((output_format & DIFF_FORMAT_NO_OUTPUT) || + /* these compute .found_changes properly */ + !(output_format & (DIFF_FORMAT_DIFFSTAT| + DIFF_FORMAT_SHORTSTAT| + DIFF_FORMAT_NUMSTAT| + DIFF_FORMAT_DIRSTAT| + DIFF_FORMAT_PATCH))) && options->flags.exit_with_status && options->flags.diff_from_contents) { /* - * run diff_flush_patch for the exit status. setting - * options->file to /dev/null should be safe, because we - * aren't supposed to produce any output anyway. + * We need to inspect the contents, not just object + * names, to determine the exit status, but the usual + * processing for the output format specified does not + * have to work with and does not look at the + * contents. Run an extra and silent "diff --patch" + * but discard the output to /dev/null, so that we + * would set the .found_changes bit correctly. + * + * We can safely close and discard the original output + * file here, since all that is left to do from this + * point is to return (we don't do the FORMAT_PATCH + * thing below). */ diff_free_file(options); options->file = xfopen("/dev/null", "w"); diff --git a/t/t4015-diff-whitespace.sh b/t/t4015-diff-whitespace.sh index 355d96aa14..412d20181c 100755 --- a/t/t4015-diff-whitespace.sh +++ b/t/t4015-diff-whitespace.sh @@ -11,7 +11,8 @@ TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh . "$TEST_DIRECTORY"/lib-diff.sh -for opts in --patch --quiet -s --stat --shortstat --dirstat=lines +for opts in --patch --quiet -s --stat --shortstat --dirstat=lines \ + --name-only --raw --name-status --summary do test_expect_success "status with $opts (different)" '