22#include "diff.h"
33
44static int cached_only = 0 ;
5- static int diff_output_format = DIFF_FORMAT_RAW ;
6- static int diff_line_termination = '\n' ;
75static int match_nonexisting = 0 ;
8- static int detect_rename = 0 ;
9- static int find_copies_harder = 0 ;
10- static int diff_setup_opt = 0 ;
11- static int diff_score_opt = 0 ;
12- static const char * pickaxe = NULL ;
13- static int pickaxe_opts = 0 ;
14- static int diff_break_opt = -1 ;
15- static const char * orderfile = NULL ;
16- static const char * diff_filter = NULL ;
6+ static struct diff_options diff_options ;
177
188/* A file entry went away or appeared */
19- static void show_file (const char * prefix , struct cache_entry * ce , unsigned char * sha1 , unsigned int mode )
9+ static void show_file (const char * prefix ,
10+ struct cache_entry * ce ,
11+ unsigned char * sha1 , unsigned int mode )
2012{
21- diff_addremove (prefix [0 ], ntohl (mode ), sha1 , ce -> name , NULL );
13+ diff_addremove (& diff_options , prefix [0 ], ntohl (mode ),
14+ sha1 , ce -> name , NULL );
2215}
2316
24- static int get_stat_data (struct cache_entry * ce , unsigned char * * sha1p , unsigned int * modep )
17+ static int get_stat_data (struct cache_entry * ce ,
18+ unsigned char * * sha1p , unsigned int * modep )
2519{
2620 unsigned char * sha1 = ce -> sha1 ;
2721 unsigned int mode = ce -> ce_mode ;
@@ -77,13 +71,13 @@ static int show_modified(struct cache_entry *old,
7771
7872 oldmode = old -> ce_mode ;
7973 if (mode == oldmode && !memcmp (sha1 , old -> sha1 , 20 ) &&
80- !find_copies_harder )
74+ !diff_options . find_copies_harder )
8175 return 0 ;
8276
8377 mode = ntohl (mode );
8478 oldmode = ntohl (oldmode );
8579
86- diff_change (oldmode , mode ,
80+ diff_change (& diff_options , oldmode , mode ,
8781 old -> sha1 , sha1 , old -> name , NULL );
8882 return 0 ;
8983}
@@ -127,7 +121,7 @@ static int diff_cache(struct cache_entry **ac, int entries, const char **pathspe
127121 break ;
128122 /* fallthru */
129123 case 3 :
130- diff_unmerge (ce -> name );
124+ diff_unmerge (& diff_options , ce -> name );
131125 break ;
132126
133127 default :
@@ -168,7 +162,7 @@ static const char diff_cache_usage[] =
168162"[<common diff options>] <tree-ish> [<path>...]"
169163COMMON_DIFF_OPTIONS_HELP ;
170164
171- int main (int argc , char * * argv )
165+ int main (int argc , const char * * argv )
172166{
173167 const char * tree_name = NULL ;
174168 unsigned char sha1 [20 ];
@@ -180,8 +174,10 @@ int main(int argc, char **argv)
180174 int allow_options = 1 ;
181175 int i ;
182176
177+ diff_setup (& diff_options );
183178 for (i = 1 ; i < argc ; i ++ ) {
184179 const char * arg = argv [i ];
180+ int diff_opt_cnt ;
185181
186182 if (!allow_options || * arg != '-' ) {
187183 if (tree_name )
@@ -198,60 +194,15 @@ int main(int argc, char **argv)
198194 /* We accept the -r flag just to look like git-diff-tree */
199195 continue ;
200196 }
201- /* We accept the -u flag as a synonym for "-p" */
202- if (!strcmp (arg , "-p" ) || !strcmp (arg , "-u" )) {
203- diff_output_format = DIFF_FORMAT_PATCH ;
204- continue ;
205- }
206- if (!strncmp (arg , "-B" , 2 )) {
207- if ((diff_break_opt = diff_scoreopt_parse (arg )) == -1 )
208- usage (diff_cache_usage );
209- continue ;
210- }
211- if (!strncmp (arg , "-M" , 2 )) {
212- detect_rename = DIFF_DETECT_RENAME ;
213- if ((diff_score_opt = diff_scoreopt_parse (arg )) == -1 )
214- usage (diff_cache_usage );
215- continue ;
216- }
217- if (!strncmp (arg , "-C" , 2 )) {
218- detect_rename = DIFF_DETECT_COPY ;
219- if ((diff_score_opt = diff_scoreopt_parse (arg )) == -1 )
220- usage (diff_cache_usage );
221- continue ;
222- }
223- if (!strcmp (arg , "--find-copies-harder" )) {
224- find_copies_harder = 1 ;
225- continue ;
226- }
227- if (!strcmp (arg , "-z" )) {
228- diff_line_termination = 0 ;
229- continue ;
230- }
231- if (!strcmp (arg , "--name-only" )) {
232- diff_output_format = DIFF_FORMAT_NAME ;
233- continue ;
234- }
235- if (!strcmp (arg , "-R" )) {
236- diff_setup_opt |= DIFF_SETUP_REVERSE ;
237- continue ;
238- }
239- if (!strncmp (arg , "-S" , 2 )) {
240- pickaxe = arg + 2 ;
241- continue ;
242- }
243- if (!strncmp (arg , "--diff-filter=" , 14 )) {
244- diff_filter = arg + 14 ;
245- continue ;
246- }
247- if (!strncmp (arg , "-O" , 2 )) {
248- orderfile = arg + 2 ;
249- continue ;
250- }
251- if (!strcmp (arg , "--pickaxe-all" )) {
252- pickaxe_opts = DIFF_PICKAXE_ALL ;
197+ diff_opt_cnt = diff_opt_parse (& diff_options , argv + i ,
198+ argc - i );
199+ if (diff_opt_cnt < 0 )
200+ usage (diff_cache_usage );
201+ else if (diff_opt_cnt ) {
202+ i += diff_opt_cnt - 1 ;
253203 continue ;
254204 }
205+
255206 if (!strcmp (arg , "-m" )) {
256207 match_nonexisting = 1 ;
257208 continue ;
@@ -265,17 +216,14 @@ int main(int argc, char **argv)
265216
266217 pathspec = get_pathspec (prefix , argv + i );
267218
268- if (find_copies_harder && detect_rename != DIFF_DETECT_COPY )
219+ if (diff_setup_done ( & diff_options ) < 0 )
269220 usage (diff_cache_usage );
270221
271222 if (!tree_name || get_sha1 (tree_name , sha1 ))
272223 usage (diff_cache_usage );
273224
274225 read_cache ();
275226
276- /* The rest is for paths restriction. */
277- diff_setup (diff_setup_opt );
278-
279227 mark_merge_entries ();
280228
281229 tree = read_object_with_reference (sha1 , "tree" , & size , NULL );
@@ -286,11 +234,7 @@ int main(int argc, char **argv)
286234
287235 ret = diff_cache (active_cache , active_nr , pathspec );
288236
289- diffcore_std (pathspec ,
290- detect_rename , diff_score_opt ,
291- pickaxe , pickaxe_opts ,
292- diff_break_opt ,
293- orderfile , diff_filter );
294- diff_flush (diff_output_format , diff_line_termination );
237+ diffcore_std (& diff_options );
238+ diff_flush (& diff_options );
295239 return ret ;
296240}
0 commit comments