@@ -359,7 +359,7 @@ static int
359359do_git_diff_no_hunks (FILE * f , char * * header , unsigned int num_headers ,
360360 int match , char * * line , size_t * linelen ,
361361 unsigned long * linenum , unsigned long start_linenum ,
362- char * status , const char * bestname , const char * patchname ,
362+ char status , const char * bestname , const char * patchname ,
363363 int * orig_file_exists , int * new_file_exists ,
364364 enum git_diff_type git_type )
365365{
@@ -386,16 +386,6 @@ do_git_diff_no_hunks (FILE *f, char **header, unsigned int num_headers,
386386 break ;
387387 }
388388
389- /* Update status based on file existence (do this early so returns below have correct status) */
390- if (status != NULL && mode != mode_filter && show_status &&
391- orig_file_exists != NULL && new_file_exists != NULL ) {
392- if (!* orig_file_exists )
393- * status = '+' ;
394- else if (!* new_file_exists )
395- * status = '-' ;
396- /* else: keep existing '!' value for modifications */
397- }
398-
399389 /* If this diff matches the filter, display it */
400390 if (match ) {
401391 if (mode == mode_filter ) {
@@ -404,7 +394,7 @@ do_git_diff_no_hunks (FILE *f, char **header, unsigned int num_headers,
404394 fputs (header [i ], stdout );
405395 } else if (mode == mode_list && !displayed_filename ) {
406396 if (!show_status ) {
407- display_filename (start_linenum , * status ,
397+ display_filename (start_linenum , status ,
408398 bestname , patchname );
409399 }
410400 displayed_filename = 1 ;
@@ -465,7 +455,7 @@ static int
465455do_unified (FILE * f , char * * header , unsigned int num_headers ,
466456 int match , char * * line ,
467457 size_t * linelen , unsigned long * linenum ,
468- unsigned long start_linenum , char * status ,
458+ unsigned long start_linenum , char status ,
469459 const char * bestname , const char * patchname ,
470460 int * orig_file_exists , int * new_file_exists )
471461{
@@ -679,9 +669,28 @@ do_unified (FILE *f, char **header, unsigned int num_headers,
679669 !regexecs (regex , num_regex , * line + 1 , 0 , NULL , 0 )) {
680670 if (output_matching == output_none ) {
681671 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+ }
682691 displayed_filename = 1 ;
683692 display_filename (start_linenum ,
684- * status , bestname ,
693+ display_status , bestname ,
685694 patchname );
686695 }
687696
@@ -756,24 +765,14 @@ do_unified (FILE *f, char **header, unsigned int num_headers,
756765 * new_file_exists = 0 ;
757766 }
758767
759- /* Update status based on final file existence after empty file processing */
760- if (status != NULL && mode != mode_filter && show_status &&
761- orig_file_exists != NULL && new_file_exists != NULL ) {
762- if (!* orig_file_exists )
763- * status = '+' ;
764- else if (!* new_file_exists )
765- * status = '-' ;
766- /* else: keep existing '!' value for modifications */
767- }
768-
769768 return ret ;
770769}
771770
772771static int
773772do_context (FILE * f , char * * header , unsigned int num_headers ,
774773 int match , char * * line ,
775774 size_t * linelen , unsigned long * linenum ,
776- unsigned long start_linenum , char * status ,
775+ unsigned long start_linenum , char status ,
777776 const char * bestname , const char * patchname ,
778777 int * orig_file_exists , int * new_file_exists )
779778{
@@ -1038,9 +1037,28 @@ do_context (FILE *f, char **header, unsigned int num_headers,
10381037 0 , NULL , 0 )) {
10391038 if (output_matching == output_none ) {
10401039 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+ }
10411059 displayed_filename = 1 ;
10421060 display_filename (start_linenum ,
1043- * status ,
1061+ display_status ,
10441062 bestname ,
10451063 patchname );
10461064 }
@@ -1170,16 +1188,6 @@ do_context (FILE *f, char **header, unsigned int num_headers,
11701188 * new_file_exists = 0 ;
11711189 }
11721190
1173- /* Update status based on final file existence after empty file processing */
1174- if (status != NULL && mode != mode_filter && show_status &&
1175- orig_file_exists != NULL && new_file_exists != NULL ) {
1176- if (!* orig_file_exists )
1177- * status = '+' ;
1178- else if (!* new_file_exists )
1179- * status = '-' ;
1180- /* else: keep existing '!' value for modifications */
1181- }
1182-
11831191 return ret ;
11841192}
11851193
@@ -1210,7 +1218,7 @@ static int filterdiff (FILE *f, const char *patchname)
12101218 int (* do_diff ) (FILE * , char * * , unsigned int ,
12111219 int , char * * , size_t * ,
12121220 unsigned long * , unsigned long ,
1213- char * , const char * , const char * ,
1221+ char , const char * , const char * ,
12141222 int * , int * );
12151223
12161224 orig_file_exists = 0 ; // shut gcc up
@@ -1405,13 +1413,19 @@ static int filterdiff (FILE *f, const char *patchname)
14051413 /* Process the git diff (it will handle filename display) */
14061414 result = do_git_diff_no_hunks (f , header , num_headers ,
14071415 match , & line , & linelen , & linenum ,
1408- start_linenum , & status , p , patchname ,
1416+ start_linenum , status , p , patchname ,
14091417 & orig_file_exists , & new_file_exists ,
14101418 git_type );
14111419
14121420 /* Print filename with status if in list mode and matches */
1413- if (match && show_status && mode == mode_list )
1421+ if (match && show_status && mode == mode_list ) {
1422+ if (!orig_file_exists )
1423+ status = '+' ;
1424+ else if (!new_file_exists )
1425+ status = '-' ;
1426+
14141427 display_filename (start_linenum , status , p , patchname );
1428+ }
14151429
14161430 /* Clean up */
14171431 free (git_old_name );
@@ -1500,13 +1514,19 @@ static int filterdiff (FILE *f, const char *patchname)
15001514 result = do_diff (f , header , num_headers ,
15011515 match , & line ,
15021516 & linelen , & linenum ,
1503- start_linenum , & status , p , patchname ,
1517+ start_linenum , status , p , patchname ,
15041518 & orig_file_exists , & new_file_exists );
15051519
15061520 // print if it matches.
1507- if (match && show_status && mode == mode_list )
1521+ if (match && show_status && mode == mode_list ) {
1522+ if (!orig_file_exists )
1523+ status = '+' ;
1524+ else if (!new_file_exists )
1525+ status = '-' ;
1526+
15081527 display_filename (start_linenum , status ,
15091528 p , patchname );
1529+ }
15101530
15111531 switch (result ) {
15121532 case EOF :
@@ -1617,6 +1637,8 @@ const char * syntax_str =
16171637#endif
16181638" - E , -- empty - files - as - absent (lsdiff )\n "
16191639" treat empty files as absent (lsdiff )\n "
1640+ " -- empty - files - as - absent (grepdiff )\n "
1641+ " treat empty files as absent (grepdiff )\n "
16201642" - f FILE , -- file = FILE (grepdiff )\n "
16211643" read regular expressions from FILE (grepdiff )\n "
16221644" -- filter run as 'filterdiff' (grepdiff , patchview , lsdiff )\n "
@@ -1840,7 +1862,7 @@ int main (int argc, char *argv[])
18401862 {"remove-timestamps" , 0 , 0 , 1000 + 'r' },
18411863 {"with-filename" , 0 , 0 , 'H' },
18421864 {"no-filename" , 0 , 0 , 'h' },
1843- {"empty-files-as-absent" , 0 , 0 , 'E' },
1865+ {"empty-files-as-absent" , 0 , 0 , 1000 + 'E' },
18441866 {"number-files" , 0 , 0 , 'N' },
18451867 {"clean" , 0 , 0 , 1000 + 'c' },
18461868 {"strip-match" , 1 , 0 , 'p' },
@@ -1853,7 +1875,7 @@ int main (int argc, char *argv[])
18531875 {"strip-match" , 1 , 0 , 'p' },
18541876 {"status" , 0 , 0 , 's' },
18551877 {"extended-regexp" , 0 , 0 , 'E' },
1856- {"empty-files-as-removed" , 0 , 0 , 'E' },
1878+ {"empty-files-as-removed" , 0 , 0 , 1000 + 'E' },
18571879 {"file" , 1 , 0 , 'f' },
18581880 {"in-place" , 0 , 0 , 1000 + 'w' },
18591881 {"git-prefixes" , 1 , 0 , 1000 + 'G' },
@@ -1883,6 +1905,12 @@ int main (int argc, char *argv[])
18831905 empty_files_as_absent = 1 ;
18841906 else syntax (1 );
18851907 break ;
1908+ case 1000 + 'E' :
1909+ /* Long form --empty-files-as-absent or --empty-files-as-removed */
1910+ if (mode == mode_grep || mode == mode_list )
1911+ empty_files_as_absent = 1 ;
1912+ else syntax (1 );
1913+ break ;
18861914 case 'f' :
18871915 if (mode == mode_grep ) {
18881916 regex_file_specified = 1 ;
0 commit comments