Earlier,
b55e6d36 (diff: ensure consistent diff behavior with
ignore options, 2025-08-08) introduced "dry-run" mode to the
diff machinery so that content-based diff filtering (like
ignoring space changes or those that match -I<regex>) can first
try to produce a patch without emitting any output to see if
under the given diff filtering condition we would get any output
lines, and a new helper function diff_flush_patch_quietly() was
introduced to use the mode to see an individual filepair needs
to be shown.
However, the solution was not complete. When files are deleted,
file modes change, or there are unmerged entries in the index,
dry-run mode still produces output because we overlooked these
conditions, and as a result, dry-run mode was not quiet.
To fix this, return early in emit_diff_symbol_from_struct() if
we are in dry-run mode. This function will be called by all the
emit functions to output the results. Returning early can avoid
diff output when files are deleted or file modes are changed.
Stop print message in dry-run mode if we have unmerged entries
in index. Discard output of external diff tool in dry-run mode.
Signed-off-by: Lidong Yan <yldhome2d2@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
int len = eds->len;
unsigned flags = eds->flags;
+ if (o->dry_run)
+ return;
+
switch (s) {
case DIFF_SYMBOL_NO_LF_EOF:
context = diff_get_color_opt(o, DIFF_CONTEXT);
{
struct child_process cmd = CHILD_PROCESS_INIT;
struct diff_queue_struct *q = &diff_queued_diff;
- int quiet = !(o->output_format & DIFF_FORMAT_PATCH);
+ int quiet = !(o->output_format & DIFF_FORMAT_PATCH) || o->dry_run;
int rc;
/*
p->status == DIFF_STATUS_RENAMED)
o->found_changes = 1;
} else {
- fprintf(o->file, "* Unmerged path %s\n", name);
+ if (!o->dry_run)
+ fprintf(o->file, "* Unmerged path %s\n", name);
o->found_changes = 1;
}
}
test_grep ! "file1" actual
'
+test_expect_success 'diff -I<regex>: ignore all content changes' '
+ test_when_finished "git rm -f file1 file2 file3" &&
+ : >file1 &&
+ git add file1 &&
+ : >file2 &&
+ git add file2 &&
+ : >file3 &&
+ git add file3 &&
+
+ rm -f file1 file2 &&
+ mkdir file2 &&
+ echo "A" >file3 &&
+ A_hash=$(git hash-object -w file3) &&
+ echo "B" >file3 &&
+ B_hash=$(git hash-object -w file3) &&
+ cat <<-EOF | git update-index --index-info &&
+ 100644 $A_hash 1 file3
+ 100644 $B_hash 2 file3
+ EOF
+
+ test_diff_no_content_changes () {
+ git diff $1 --ignore-blank-lines -I".*" >actual &&
+ test_line_count = 3 actual &&
+ test_grep "file1" actual &&
+ test_grep "file2" actual &&
+ test_grep "file3" actual &&
+ test_grep ! "diff --git" actual
+ } &&
+ test_diff_no_content_changes "--raw" &&
+ test_diff_no_content_changes "--name-only" &&
+ test_diff_no_content_changes "--name-status" &&
+
+ : >actual &&
+ test_must_fail git diff --quiet -I".*" >actual &&
+ test_must_be_empty actual
+'
+
# check_prefix <patch> <src> <dst>
# check only lines with paths to avoid dependency on exact oid/contents
check_prefix () {