1212#include "builtin.h"
1313#include "tag.h"
1414#include "reflog-walk.h"
15+ #include "patch-ids.h"
1516
1617static int default_show_root = 1 ;
1718
@@ -333,25 +334,12 @@ static int reopen_stdout(struct commit *commit, int nr, int keep_subject)
333334
334335}
335336
336- static int get_patch_id (struct commit * commit , struct diff_options * options ,
337- unsigned char * sha1 )
338- {
339- if (commit -> parents )
340- diff_tree_sha1 (commit -> parents -> item -> object .sha1 ,
341- commit -> object .sha1 , "" , options );
342- else
343- diff_root_tree_sha1 (commit -> object .sha1 , "" , options );
344- diffcore_std (options );
345- return diff_flush_patch_id (options , sha1 );
346- }
347-
348- static void get_patch_ids (struct rev_info * rev , struct diff_options * options , const char * prefix )
337+ static void get_patch_ids (struct rev_info * rev , struct patch_ids * ids , const char * prefix )
349338{
350339 struct rev_info check_rev ;
351340 struct commit * commit ;
352341 struct object * o1 , * o2 ;
353342 unsigned flags1 , flags2 ;
354- unsigned char sha1 [20 ];
355343
356344 if (rev -> pending .nr != 2 )
357345 die ("Need exactly one range." );
@@ -364,10 +352,7 @@ static void get_patch_ids(struct rev_info *rev, struct diff_options *options, co
364352 if ((flags1 & UNINTERESTING ) == (flags2 & UNINTERESTING ))
365353 die ("Not a range." );
366354
367- diff_setup (options );
368- options -> recursive = 1 ;
369- if (diff_setup_done (options ) < 0 )
370- die ("diff_setup_done failed" );
355+ init_patch_ids (ids );
371356
372357 /* given a range a..b get all patch ids for b..a */
373358 init_revisions (& check_rev , prefix );
@@ -382,8 +367,7 @@ static void get_patch_ids(struct rev_info *rev, struct diff_options *options, co
382367 if (commit -> parents && commit -> parents -> next )
383368 continue ;
384369
385- if (!get_patch_id (commit , options , sha1 ))
386- created_object (sha1 , xcalloc (1 , sizeof (struct object )));
370+ add_commit_patch_id (commit , ids );
387371 }
388372
389373 /* reset for next revision walk */
@@ -421,7 +405,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
421405 int ignore_if_in_upstream = 0 ;
422406 int thread = 0 ;
423407 const char * in_reply_to = NULL ;
424- struct diff_options patch_id_opts ;
408+ struct patch_ids ids ;
425409 char * add_signoff = NULL ;
426410 char message_id [1024 ];
427411 char ref_message_id [1024 ];
@@ -559,22 +543,19 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
559543 }
560544
561545 if (ignore_if_in_upstream )
562- get_patch_ids (& rev , & patch_id_opts , prefix );
546+ get_patch_ids (& rev , & ids , prefix );
563547
564548 if (!use_stdout )
565549 realstdout = fdopen (dup (1 ), "w" );
566550
567551 prepare_revision_walk (& rev );
568552 while ((commit = get_revision (& rev )) != NULL ) {
569- unsigned char sha1 [20 ];
570-
571553 /* ignore merges */
572554 if (commit -> parents && commit -> parents -> next )
573555 continue ;
574556
575557 if (ignore_if_in_upstream &&
576- !get_patch_id (commit , & patch_id_opts , sha1 ) &&
577- lookup_object (sha1 ))
558+ has_commit_patch_id (commit , & ids ))
578559 continue ;
579560
580561 nr ++ ;
@@ -629,6 +610,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
629610 fclose (stdout );
630611 }
631612 free (list );
613+ if (ignore_if_in_upstream )
614+ free_patch_ids (& ids );
632615 return 0 ;
633616}
634617
@@ -651,7 +634,7 @@ static const char cherry_usage[] =
651634int cmd_cherry (int argc , const char * * argv , const char * prefix )
652635{
653636 struct rev_info revs ;
654- struct diff_options patch_id_opts ;
637+ struct patch_ids ids ;
655638 struct commit * commit ;
656639 struct commit_list * list = NULL ;
657640 const char * upstream ;
@@ -697,7 +680,7 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
697680 return 0 ;
698681 }
699682
700- get_patch_ids (& revs , & patch_id_opts , prefix );
683+ get_patch_ids (& revs , & ids , prefix );
701684
702685 if (limit && add_pending_commit (limit , & revs , UNINTERESTING ))
703686 die ("Unknown commit %s" , limit );
@@ -713,12 +696,10 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
713696 }
714697
715698 while (list ) {
716- unsigned char sha1 [20 ];
717699 char sign = '+' ;
718700
719701 commit = list -> item ;
720- if (!get_patch_id (commit , & patch_id_opts , sha1 ) &&
721- lookup_object (sha1 ))
702+ if (has_commit_patch_id (commit , & ids ))
722703 sign = '-' ;
723704
724705 if (verbose ) {
@@ -736,5 +717,6 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
736717 list = list -> next ;
737718 }
738719
720+ free_patch_ids (& ids );
739721 return 0 ;
740722}
0 commit comments