@@ -2670,6 +2670,19 @@ int check_todo_list(void)
2670
2670
return res ;
2671
2671
}
2672
2672
2673
+ static int rewrite_file (const char * path , const char * buf , size_t len )
2674
+ {
2675
+ int rc = 0 ;
2676
+ int fd = open (path , O_WRONLY | O_TRUNC );
2677
+ if (fd < 0 )
2678
+ return error_errno (_ ("could not open '%s' for writing" ), path );
2679
+ if (write_in_full (fd , buf , len ) < 0 )
2680
+ rc = error_errno (_ ("could not write to '%s'" ), path );
2681
+ if (close (fd ) && !rc )
2682
+ rc = error_errno (_ ("could not close '%s'" ), path );
2683
+ return rc ;
2684
+ }
2685
+
2673
2686
/* skip picking commits whose parents are unchanged */
2674
2687
int skip_unnecessary_picks (void )
2675
2688
{
@@ -2742,29 +2755,11 @@ int skip_unnecessary_picks(void)
2742
2755
}
2743
2756
close (fd );
2744
2757
2745
- fd = open (rebase_path_todo (), O_WRONLY , 0666 );
2746
- if (fd < 0 ) {
2747
- error_errno (_ ("could not open '%s' for writing" ),
2748
- rebase_path_todo ());
2749
- todo_list_release (& todo_list );
2750
- return -1 ;
2751
- }
2752
- if (write_in_full (fd , todo_list .buf .buf + offset ,
2753
- todo_list .buf .len - offset ) < 0 ) {
2754
- error_errno (_ ("could not write to '%s'" ),
2755
- rebase_path_todo ());
2756
- close (fd );
2757
- todo_list_release (& todo_list );
2758
- return -1 ;
2759
- }
2760
- if (ftruncate (fd , todo_list .buf .len - offset ) < 0 ) {
2761
- error_errno (_ ("could not truncate '%s'" ),
2762
- rebase_path_todo ());
2758
+ if (rewrite_file (rebase_path_todo (), todo_list .buf .buf + offset ,
2759
+ todo_list .buf .len - offset ) < 0 ) {
2763
2760
todo_list_release (& todo_list );
2764
- close (fd );
2765
2761
return -1 ;
2766
2762
}
2767
- close (fd );
2768
2763
2769
2764
todo_list .current = i ;
2770
2765
if (is_fixup (peek_command (& todo_list , 0 )))
@@ -2949,15 +2944,7 @@ int rearrange_squash(void)
2949
2944
}
2950
2945
}
2951
2946
2952
- fd = open (todo_file , O_WRONLY );
2953
- if (fd < 0 )
2954
- res = error_errno (_ ("could not open '%s'" ), todo_file );
2955
- else if (write (fd , buf .buf , buf .len ) < 0 )
2956
- res = error_errno (_ ("could not write to '%s'" ), todo_file );
2957
- else if (ftruncate (fd , buf .len ) < 0 )
2958
- res = error_errno (_ ("could not truncate '%s'" ),
2959
- todo_file );
2960
- close (fd );
2947
+ res = rewrite_file (todo_file , buf .buf , buf .len );
2961
2948
strbuf_release (& buf );
2962
2949
}
2963
2950
0 commit comments