@@ -4700,52 +4700,22 @@ int check_todo_list_from_file(struct repository *r)
4700
4700
return res ;
4701
4701
}
4702
4702
4703
- static int rewrite_file (const char * path , const char * buf , size_t len )
4704
- {
4705
- int rc = 0 ;
4706
- int fd = open (path , O_WRONLY | O_TRUNC );
4707
- if (fd < 0 )
4708
- return error_errno (_ ("could not open '%s' for writing" ), path );
4709
- if (write_in_full (fd , buf , len ) < 0 )
4710
- rc = error_errno (_ ("could not write to '%s'" ), path );
4711
- if (close (fd ) && !rc )
4712
- rc = error_errno (_ ("could not close '%s'" ), path );
4713
- return rc ;
4714
- }
4715
-
4716
4703
/* skip picking commits whose parents are unchanged */
4717
- static int skip_unnecessary_picks (struct repository * r , struct object_id * output_oid )
4704
+ static int skip_unnecessary_picks (struct repository * r ,
4705
+ struct todo_list * todo_list ,
4706
+ struct object_id * base_oid )
4718
4707
{
4719
- const char * todo_file = rebase_path_todo ();
4720
- struct strbuf buf = STRBUF_INIT ;
4721
- struct todo_list todo_list = TODO_LIST_INIT ;
4722
4708
struct object_id * parent_oid ;
4723
- int fd , i ;
4724
-
4725
- if (!read_oneliner (& buf , rebase_path_onto (), 0 ))
4726
- return error (_ ("could not read 'onto'" ));
4727
- if (get_oid (buf .buf , output_oid )) {
4728
- strbuf_release (& buf );
4729
- return error (_ ("need a HEAD to fixup" ));
4730
- }
4731
- strbuf_release (& buf );
4732
-
4733
- if (strbuf_read_file_or_whine (& todo_list .buf , todo_file ) < 0 )
4734
- return -1 ;
4735
- if (todo_list_parse_insn_buffer (r , todo_list .buf .buf , & todo_list ) < 0 ) {
4736
- todo_list_release (& todo_list );
4737
- return -1 ;
4738
- }
4709
+ int i ;
4739
4710
4740
- for (i = 0 ; i < todo_list . nr ; i ++ ) {
4741
- struct todo_item * item = todo_list . items + i ;
4711
+ for (i = 0 ; i < todo_list -> nr ; i ++ ) {
4712
+ struct todo_item * item = todo_list -> items + i ;
4742
4713
4743
4714
if (item -> command >= TODO_NOOP )
4744
4715
continue ;
4745
4716
if (item -> command != TODO_PICK )
4746
4717
break ;
4747
4718
if (parse_commit (item -> commit )) {
4748
- todo_list_release (& todo_list );
4749
4719
return error (_ ("could not parse commit '%s'" ),
4750
4720
oid_to_hex (& item -> commit -> object .oid ));
4751
4721
}
@@ -4754,42 +4724,26 @@ static int skip_unnecessary_picks(struct repository *r, struct object_id *output
4754
4724
if (item -> commit -> parents -> next )
4755
4725
break ; /* merge commit */
4756
4726
parent_oid = & item -> commit -> parents -> item -> object .oid ;
4757
- if (!oideq (parent_oid , output_oid ))
4727
+ if (!oideq (parent_oid , base_oid ))
4758
4728
break ;
4759
- oidcpy (output_oid , & item -> commit -> object .oid );
4729
+ oidcpy (base_oid , & item -> commit -> object .oid );
4760
4730
}
4761
4731
if (i > 0 ) {
4762
- int offset = get_item_line_offset (& todo_list , i );
4763
4732
const char * done_path = rebase_path_done ();
4764
4733
4765
- fd = open (done_path , O_CREAT | O_WRONLY | O_APPEND , 0666 );
4766
- if (fd < 0 ) {
4767
- error_errno (_ ("could not open '%s' for writing" ),
4768
- done_path );
4769
- todo_list_release (& todo_list );
4770
- return -1 ;
4771
- }
4772
- if (write_in_full (fd , todo_list .buf .buf , offset ) < 0 ) {
4734
+ if (todo_list_write_to_file (r , todo_list , done_path , NULL , NULL , i , 0 )) {
4773
4735
error_errno (_ ("could not write to '%s'" ), done_path );
4774
- todo_list_release (& todo_list );
4775
- close (fd );
4776
4736
return -1 ;
4777
4737
}
4778
- close (fd );
4779
4738
4780
- if (rewrite_file (rebase_path_todo (), todo_list .buf .buf + offset ,
4781
- todo_list .buf .len - offset ) < 0 ) {
4782
- todo_list_release (& todo_list );
4783
- return -1 ;
4784
- }
4739
+ MOVE_ARRAY (todo_list -> items , todo_list -> items + i , todo_list -> nr - i );
4740
+ todo_list -> nr -= i ;
4741
+ todo_list -> current = 0 ;
4785
4742
4786
- todo_list .current = i ;
4787
- if (is_fixup (peek_command (& todo_list , 0 )))
4788
- record_in_rewritten (output_oid , peek_command (& todo_list , 0 ));
4743
+ if (is_fixup (peek_command (todo_list , 0 )))
4744
+ record_in_rewritten (base_oid , peek_command (todo_list , 0 ));
4789
4745
}
4790
4746
4791
- todo_list_release (& todo_list );
4792
-
4793
4747
return 0 ;
4794
4748
}
4795
4749
@@ -4860,6 +4814,11 @@ int complete_action(struct repository *r, struct replay_opts *opts, unsigned fla
4860
4814
return -1 ;
4861
4815
}
4862
4816
4817
+ if (opts -> allow_ff && skip_unnecessary_picks (r , & new_todo , & oid )) {
4818
+ todo_list_release (& new_todo );
4819
+ return error (_ ("could not skip unnecessary pick commands" ));
4820
+ }
4821
+
4863
4822
if (todo_list_write_to_file (r , & new_todo , todo_file , NULL , NULL , -1 ,
4864
4823
flags & ~(TODO_LIST_SHORTEN_IDS ))) {
4865
4824
todo_list_release (& new_todo );
@@ -4868,9 +4827,6 @@ int complete_action(struct repository *r, struct replay_opts *opts, unsigned fla
4868
4827
4869
4828
todo_list_release (& new_todo );
4870
4829
4871
- if (opts -> allow_ff && skip_unnecessary_picks (r , & oid ))
4872
- return error (_ ("could not skip unnecessary pick commands" ));
4873
-
4874
4830
if (checkout_onto (opts , onto_name , oid_to_hex (& oid ), orig_head ))
4875
4831
return -1 ;
4876
4832
0 commit comments