@@ -4404,6 +4404,15 @@ static struct lock_file lock_file;
44044404#define INACCURATE_EOF (1<<0)
44054405#define RECOUNT (1<<1)
44064406
4407+ /*
4408+ * Try to apply a patch.
4409+ *
4410+ * Returns:
4411+ * -128 if a bad error happened (like patch unreadable)
4412+ * -1 if patch did not apply and user cannot deal with it
4413+ * 0 if the patch applied
4414+ * 1 if the patch did not apply but user might fix it
4415+ */
44074416static int apply_patch (struct apply_state * state ,
44084417 int fd ,
44094418 const char * filename ,
@@ -4413,6 +4422,7 @@ static int apply_patch(struct apply_state *state,
44134422 struct strbuf buf = STRBUF_INIT ; /* owns the patch text */
44144423 struct patch * list = NULL , * * listp = & list ;
44154424 int skipped_patch = 0 ;
4425+ int res = 0 ;
44164426
44174427 state -> patch_input_file = filename ;
44184428 read_patch_file (& buf , fd );
@@ -4445,8 +4455,11 @@ static int apply_patch(struct apply_state *state,
44454455 offset += nr ;
44464456 }
44474457
4448- if (!list && !skipped_patch )
4449- die (_ ("unrecognized input" ));
4458+ if (!list && !skipped_patch ) {
4459+ error (_ ("unrecognized input" ));
4460+ res = -128 ;
4461+ goto end ;
4462+ }
44504463
44514464 if (state -> whitespace_error && (state -> ws_error_action == die_on_ws_error ))
44524465 state -> apply = 0 ;
@@ -4455,21 +4468,23 @@ static int apply_patch(struct apply_state *state,
44554468 if (state -> update_index && state -> newfd < 0 )
44564469 state -> newfd = hold_locked_index (state -> lock_file , 1 );
44574470
4458- if (state -> check_index ) {
4459- if (read_cache () < 0 )
4460- die (_ ("unable to read index file" ));
4471+ if (state -> check_index && read_cache () < 0 ) {
4472+ error (_ ("unable to read index file" ));
4473+ res = -128 ;
4474+ goto end ;
44614475 }
44624476
44634477 if ((state -> check || state -> apply ) &&
44644478 check_patch_list (state , list ) < 0 &&
4465- !state -> apply_with_reject )
4466- exit (1 );
4479+ !state -> apply_with_reject ) {
4480+ res = -1 ;
4481+ goto end ;
4482+ }
44674483
44684484 if (state -> apply && write_out_results (state , list )) {
4469- if (state -> apply_with_reject )
4470- exit (1 );
44714485 /* with --3way, we still need to write the index out */
4472- return 1 ;
4486+ res = state -> apply_with_reject ? -1 : 1 ;
4487+ goto end ;
44734488 }
44744489
44754490 if (state -> fake_ancestor )
@@ -4484,10 +4499,11 @@ static int apply_patch(struct apply_state *state,
44844499 if (state -> summary )
44854500 summary_patch_list (list );
44864501
4502+ end :
44874503 free_patch_list (list );
44884504 strbuf_release (& buf );
44894505 string_list_clear (& state -> fn_table , 0 );
4490- return 0 ;
4506+ return res ;
44914507}
44924508
44934509static void git_apply_config (void )
@@ -4628,6 +4644,7 @@ static int apply_all_patches(struct apply_state *state,
46284644 int options )
46294645{
46304646 int i ;
4647+ int res ;
46314648 int errs = 0 ;
46324649 int read_stdin = 1 ;
46334650
@@ -4636,7 +4653,10 @@ static int apply_all_patches(struct apply_state *state,
46364653 int fd ;
46374654
46384655 if (!strcmp (arg , "-" )) {
4639- errs |= apply_patch (state , 0 , "<stdin>" , options );
4656+ res = apply_patch (state , 0 , "<stdin>" , options );
4657+ if (res < 0 )
4658+ goto end ;
4659+ errs |= res ;
46404660 read_stdin = 0 ;
46414661 continue ;
46424662 } else if (0 < state -> prefix_length )
@@ -4649,12 +4669,19 @@ static int apply_all_patches(struct apply_state *state,
46494669 die_errno (_ ("can't open patch '%s'" ), arg );
46504670 read_stdin = 0 ;
46514671 set_default_whitespace_mode (state );
4652- errs |= apply_patch (state , fd , arg , options );
4672+ res = apply_patch (state , fd , arg , options );
4673+ if (res < 0 )
4674+ goto end ;
4675+ errs |= res ;
46534676 close (fd );
46544677 }
46554678 set_default_whitespace_mode (state );
4656- if (read_stdin )
4657- errs |= apply_patch (state , 0 , "<stdin>" , options );
4679+ if (read_stdin ) {
4680+ res = apply_patch (state , 0 , "<stdin>" , options );
4681+ if (res < 0 )
4682+ goto end ;
4683+ errs |= res ;
4684+ }
46584685
46594686 if (state -> whitespace_error ) {
46604687 if (state -> squelch_whitespace_errors &&
@@ -4690,6 +4717,9 @@ static int apply_all_patches(struct apply_state *state,
46904717 }
46914718
46924719 return !!errs ;
4720+
4721+ end :
4722+ exit (res == -1 ? 1 : 128 );
46934723}
46944724
46954725int cmd_apply (int argc , const char * * argv , const char * prefix )
0 commit comments