11#include "cache.h"
2+ #include "diff.h"
23
34static int cached_only = 0 ;
5+ static int generate_patch = 0 ;
46static int line_termination = '\n' ;
57
68/* A file entry went away or appeared */
79static void show_file (const char * prefix , struct cache_entry * ce )
810{
9- printf ("%s%o\t%s\t%s\t%s%c" , prefix , ntohl (ce -> ce_mode ), "blob" ,
10- sha1_to_hex (ce -> sha1 ), ce -> name , line_termination );
11+ if (generate_patch )
12+ diff_addremove (prefix [0 ], ntohl (ce -> ce_mode ),
13+ ce -> sha1 , ce -> name , NULL );
14+ else
15+ printf ("%s%06o\tblob\t%s\t%s%c" , prefix , ntohl (ce -> ce_mode ),
16+ sha1_to_hex (ce -> sha1 ), ce -> name ,
17+ line_termination );
1118}
1219
1320static int show_modified (struct cache_entry * old , struct cache_entry * new )
@@ -35,11 +42,15 @@ static int show_modified(struct cache_entry *old, struct cache_entry *new)
3542 if (mode == oldmode && !memcmp (sha1 , old -> sha1 , 20 ))
3643 return 0 ;
3744
38- strcpy (old_sha1_hex , sha1_to_hex (old -> sha1 ));
39- printf ("*%o->%o\t%s\t%s->%s\t%s%c" , oldmode , mode ,
40- "blob" ,
41- old_sha1_hex , sha1_to_hex (sha1 ),
42- old -> name , line_termination );
45+ if (generate_patch )
46+ diff_change (oldmode , mode ,
47+ old -> sha1 , sha1 , old -> name , NULL );
48+ else {
49+ strcpy (old_sha1_hex , sha1_to_hex (old -> sha1 ));
50+ printf ("*%06o->%06o\tblob\t%s->%s\t%s%c" , oldmode , mode ,
51+ old_sha1_hex , sha1_to_hex (sha1 ),
52+ old -> name , line_termination );
53+ }
4354 return 0 ;
4455}
4556
@@ -67,7 +78,10 @@ static int diff_cache(struct cache_entry **ac, int entries)
6778 }
6879 /* Otherwise we fall through to the "unmerged" case */
6980 case 3 :
70- printf ("U %s%c" , ce -> name , line_termination );
81+ if (generate_patch )
82+ diff_unmerge (ce -> name );
83+ else
84+ printf ("U %s%c" , ce -> name , line_termination );
7185 break ;
7286
7387 default :
@@ -102,7 +116,8 @@ static void mark_merge_entries(void)
102116 }
103117}
104118
105- static char * diff_cache_usage = "diff-cache [-r] [-z] [--cached] <tree sha1>" ;
119+ static char * diff_cache_usage =
120+ "diff-cache [-r] [-z] [-p] [--cached] <tree sha1>" ;
106121
107122int main (int argc , char * * argv )
108123{
@@ -119,6 +134,10 @@ int main(int argc, char **argv)
119134 /* We accept the -r flag just to look like diff-tree */
120135 continue ;
121136 }
137+ if (!strcmp (arg , "-p" )) {
138+ generate_patch = 1 ;
139+ continue ;
140+ }
122141 if (!strcmp (arg , "-z" )) {
123142 line_termination = '\0' ;
124143 continue ;
0 commit comments