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