@@ -1136,7 +1136,8 @@ trim_context (FILE *f /* positioned at start of @@ line */,
11361136 fwrite (line , (size_t ) got , 1 , out );
11371137 continue ;
11381138 }
1139- print_color (out , type , "%s" , line );
1139+ print_color (out , type , "%.*s" , (int ) got - 1 , line );
1140+ fputc ('\n' , out );
11401141 }
11411142 }
11421143
@@ -1442,29 +1443,36 @@ index_patch_generic (FILE *patch_file, struct file_list **file_list, int need_sk
14421443
14431444 /* For patch2, we need to handle the @@ line and skip content */
14441445 if (need_skip_content ) {
1445- if (getline (& line , & linelen , patch_file ) == -1 ) {
1446+ int found = 0 ;
1447+
1448+ while (!found &&
1449+ getline (& line , & linelen , patch_file ) > 0 ) {
1450+ if (strncmp (line , "@@ " , 3 ))
1451+ continue ;
1452+
1453+ p = strchr (line + 3 , '+' );
1454+ if (!p )
1455+ continue ;
1456+
1457+ p = strchr (p , ',' );
1458+ if (p ) {
1459+ /* Like '@@ -1,3 +1,3 @@' */
1460+ p ++ ;
1461+ skip = strtoul (p , & end , 10 );
1462+ if (p == end )
1463+ continue ;
1464+ } else
1465+ /* Like '@@ -1 +1 @@' */
1466+ skip = 1 ;
1467+ found = 1 ;
1468+ }
1469+
1470+ if (!found ) {
14461471 free (names [0 ]);
14471472 free (names [1 ]);
14481473 break ;
14491474 }
14501475
1451- if (strncmp (line , "@@ " , 3 ))
1452- goto try_next ;
1453-
1454- p = strchr (line + 3 , '+' );
1455- if (!p )
1456- goto try_next ;
1457- p = strchr (p , ',' );
1458- if (p ) {
1459- /* Like '@@ -1,3 +1,3 @@' */
1460- p ++ ;
1461- skip = strtoul (p , & end , 10 );
1462- if (p == end )
1463- goto try_next ;
1464- } else
1465- /* Like '@@ -1 +1 @@' */
1466- skip = 1 ;
1467-
14681476 add_to_list (file_list , best_name (2 , names ), pos );
14691477
14701478 while (skip -- ) {
@@ -1480,18 +1488,33 @@ index_patch_generic (FILE *patch_file, struct file_list **file_list, int need_sk
14801488 add_to_list (file_list , best_name (2 , names ), pos );
14811489 }
14821490
1483- try_next :
14841491 free (names [0 ]);
14851492 free (names [1 ]);
14861493 }
14871494
14881495 if (line )
14891496 free (line );
14901497
1498+ /* Return success if the file is empty, has indexed files, or
1499+ * has no --- lines at all (merge commits, mode-only changes,
1500+ * notes-only commits, binary-only patches, etc.). */
14911501 if (file_is_empty || * file_list )
14921502 return 0 ;
1493- else
1494- return 1 ;
1503+
1504+ /* Check if the patch has any file-header --- lines (as
1505+ * opposed to commit-message separators or b4 tracking).
1506+ * If not, it simply has no diffable content. */
1507+ line = NULL ;
1508+ linelen = 0 ;
1509+ rewind (patch_file );
1510+ while (getline (& line , & linelen , patch_file ) != -1 )
1511+ if (!strncmp (line , "--- a/" , 6 ) ||
1512+ !strncmp (line , "--- /dev/null" , 13 )) {
1513+ free (line );
1514+ return 1 ;
1515+ }
1516+ free (line );
1517+ return 0 ;
14951518}
14961519
14971520static int
@@ -2113,11 +2136,15 @@ interdiff (FILE *p1, FILE *p2, const char *patch1, const char *patch2)
21132136
21142137 names [0 ] = filename_from_header (line + 4 );
21152138
2116- if (getline (& line , & linelen , p1 ) == -1 )
2139+ if (getline (& line , & linelen , p1 ) == -1 ) {
2140+ free (names [0 ]);
21172141 break ;
2142+ }
21182143
2119- if (strncmp (line , "+++ " , 4 ))
2144+ if (strncmp (line , "+++ " , 4 )) {
2145+ free (names [0 ]);
21202146 continue ;
2147+ }
21212148
21222149 names [1 ] = filename_from_header (line + 4 );
21232150
@@ -2150,8 +2177,21 @@ interdiff (FILE *p1, FILE *p2, const char *patch1, const char *patch2)
21502177 free (p );
21512178 }
21522179
2153- if (!file_is_empty && !patch_found )
2154- no_patch (patch1 );
2180+ if (!file_is_empty && !patch_found ) {
2181+ /* Don't warn for patches with no file-header --- lines
2182+ * (merge commits, mode-only, binary-only, etc.). */
2183+ int has_diff_content = 0 ;
2184+
2185+ rewind (p1 );
2186+ while (getline (& line , & linelen , p1 ) != -1 )
2187+ if (!strncmp (line , "--- a/" , 6 ) ||
2188+ !strncmp (line , "--- /dev/null" , 13 )) {
2189+ has_diff_content = 1 ;
2190+ break ;
2191+ }
2192+ if (has_diff_content )
2193+ no_patch (patch1 );
2194+ }
21552195
21562196 copy_residue (p2 , mode == mode_flip ? flip1 : stdout );
21572197
0 commit comments