@@ -323,12 +323,13 @@ static unsigned long line_all_diff(struct sline *sline, unsigned long all_mask)
323323 return different ;
324324}
325325
326- static void make_hunks (struct sline * sline , unsigned long cnt ,
326+ static int make_hunks (struct sline * sline , unsigned long cnt ,
327327 int num_parent , int dense )
328328{
329329 unsigned long all_mask = (1UL <<num_parent ) - 1 ;
330330 unsigned long mark = (1UL <<num_parent );
331331 unsigned long i ;
332+ int has_interesting = 0 ;
332333
333334 i = 0 ;
334335 while (i < cnt ) {
@@ -344,21 +345,23 @@ static void make_hunks(struct sline *sline, unsigned long cnt,
344345 j = (i + context < cnt ) ? i + context : cnt ;
345346 while (i < j )
346347 sline [i ++ ].flag |= mark ;
348+ has_interesting = 1 ;
347349 continue ;
348350 }
349351 i ++ ;
350352 }
351353 if (!dense )
352- return ;
354+ return has_interesting ;
353355
354356 /* Look at each hunk, and if we have changes from only one
355357 * parent, or the changes are the same from all but one
356358 * parent, mark that uninteresting.
357359 */
360+ has_interesting = 0 ;
358361 i = 0 ;
359362 while (i < cnt ) {
360363 int j , hunk_end , same , diff ;
361- unsigned long same_diff , all_diff , this_diff ;
364+ unsigned long same_diff , all_diff ;
362365 while (i < cnt && !(sline [i ].flag & mark ))
363366 i ++ ;
364367 if (cnt <= i )
@@ -387,8 +390,11 @@ static void make_hunks(struct sline *sline, unsigned long cnt,
387390 for (j = i ; j < hunk_end ; j ++ )
388391 sline [j ].flag &= ~mark ;
389392 }
393+ else
394+ has_interesting = 1 ;
390395 i = hunk_end ;
391396 }
397+ return has_interesting ;
392398}
393399
394400static void dump_sline (struct sline * sline , int cnt , int num_parent )
@@ -437,13 +443,13 @@ static void dump_sline(struct sline *sline, int cnt, int num_parent)
437443 }
438444}
439445
440- static void show_combined_diff (struct path_list * elem , int num_parent ,
441- int dense )
446+ static int show_combined_diff (struct path_list * elem , int num_parent ,
447+ int dense , const char * header , int show_empty )
442448{
443449 unsigned long size , cnt , lno ;
444450 char * result , * cp , * ep ;
445451 struct sline * sline ; /* survived lines */
446- int i ;
452+ int i , show_hunks , shown_header = 0 ;
447453 char ourtmp [TMPPATHLEN ];
448454
449455 /* Read the result of merge first */
@@ -479,9 +485,21 @@ static void show_combined_diff(struct path_list *elem, int num_parent,
479485 for (i = 0 ; i < num_parent ; i ++ )
480486 combine_diff (elem -> parent_sha1 [i ], ourtmp , sline , cnt , i );
481487
482- make_hunks (sline , cnt , num_parent , dense );
488+ show_hunks = make_hunks (sline , cnt , num_parent , dense );
483489
484- dump_sline (sline , cnt , num_parent );
490+ if (header && (show_hunks || show_empty )) {
491+ shown_header ++ ;
492+ puts (header );
493+ }
494+ if (show_hunks ) {
495+ printf ("diff --%s " , dense ? "cc" : "combined" );
496+ if (quote_c_style (elem -> path , NULL , NULL , 0 ))
497+ quote_c_style (elem -> path , NULL , stdout , 0 );
498+ else
499+ printf ("%s" , elem -> path );
500+ putchar ('\n' );
501+ dump_sline (sline , cnt , num_parent );
502+ }
485503 unlink (ourtmp );
486504 free (result );
487505
@@ -496,6 +514,7 @@ static void show_combined_diff(struct path_list *elem, int num_parent,
496514 }
497515 }
498516 free (sline );
517+ return shown_header ;
499518}
500519
501520int diff_tree_combined_merge (const unsigned char * sha1 ,
@@ -535,17 +554,12 @@ int diff_tree_combined_merge(const unsigned char *sha1,
535554 num_paths ++ ;
536555 }
537556 if (num_paths || show_empty_merge ) {
538- puts (header );
539557 for (p = paths ; p ; p = p -> next ) {
540558 if (!p -> len )
541559 continue ;
542- printf ("diff --%s " , dense ? "cc" : "combined" );
543- if (quote_c_style (p -> path , NULL , NULL , 0 ))
544- quote_c_style (p -> path , NULL , stdout , 0 );
545- else
546- printf ("%s" , p -> path );
547- putchar ('\n' );
548- show_combined_diff (p , num_parent , dense );
560+ if (show_combined_diff (p , num_parent , dense , header ,
561+ show_empty_merge ))
562+ header = NULL ;
549563 }
550564 }
551565
0 commit comments