Skip to content

Commit 5fab49c

Browse files
committed
Fix grepdiff -s/--status to show correct file status indicators
The grepdiff -s option was incorrectly showing '!' (modification) for all matching files, regardless of whether they were additions, deletions, or modifications. The issue was that grepdiff calls display_filename() from inside do_unified() and do_context() when it finds a matching line, but the status variable was always initialized to '!' and never updated based on file existence. This fix calculates the correct status (+/-/!) inside do_unified() and do_context() just before calling display_filename(), using: - orig_file_exists/new_file_exists to check if files exist - orig_is_empty/new_is_empty to check for empty files when --empty-files-as-absent is used The fix applies to both unified and context diff formats. All 194 tests pass (188 pass + 6 expected failures). Assisted-by: Claude Code
1 parent 398eafa commit 5fab49c

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

src/filterdiff.c

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,28 @@ do_unified (FILE *f, char **header, unsigned int num_headers,
669669
!regexecs (regex, num_regex, *line + 1, 0, NULL, 0)) {
670670
if (output_matching == output_none) {
671671
if (!displayed_filename) {
672+
char display_status = status;
673+
/* Update status based on file existence for grepdiff -s */
674+
if (show_status) {
675+
int orig_absent = orig_file_exists && !*orig_file_exists;
676+
int new_absent = new_file_exists && !*new_file_exists;
677+
678+
/* Check for empty files if --empty-files-as-absent is set */
679+
if (empty_files_as_absent) {
680+
if (orig_file_exists && *orig_file_exists && orig_is_empty)
681+
orig_absent = 1;
682+
if (new_file_exists && *new_file_exists && new_is_empty)
683+
new_absent = 1;
684+
}
685+
686+
if (orig_absent)
687+
display_status = '+';
688+
else if (new_absent)
689+
display_status = '-';
690+
}
672691
displayed_filename = 1;
673692
display_filename (start_linenum,
674-
status, bestname,
693+
display_status, bestname,
675694
patchname);
676695
}
677696

@@ -1018,9 +1037,28 @@ do_context (FILE *f, char **header, unsigned int num_headers,
10181037
0, NULL, 0)) {
10191038
if (output_matching == output_none) {
10201039
if (!displayed_filename) {
1040+
char display_status = status;
1041+
/* Update status based on file existence for grepdiff -s */
1042+
if (show_status) {
1043+
int orig_absent = orig_file_exists && !*orig_file_exists;
1044+
int new_absent = new_file_exists && !*new_file_exists;
1045+
1046+
/* Check for empty files if --empty-files-as-absent is set */
1047+
if (empty_files_as_absent) {
1048+
if (orig_file_exists && *orig_file_exists && orig_is_empty)
1049+
orig_absent = 1;
1050+
if (new_file_exists && *new_file_exists && new_is_empty)
1051+
new_absent = 1;
1052+
}
1053+
1054+
if (orig_absent)
1055+
display_status = '+';
1056+
else if (new_absent)
1057+
display_status = '-';
1058+
}
10211059
displayed_filename = 1;
10221060
display_filename(start_linenum,
1023-
status,
1061+
display_status,
10241062
bestname,
10251063
patchname);
10261064
}

0 commit comments

Comments
 (0)