@@ -336,15 +336,9 @@ static void squash_message(struct commit *commit, struct commit_list *remotehead
336336 struct rev_info rev ;
337337 struct strbuf out = STRBUF_INIT ;
338338 struct commit_list * j ;
339- const char * filename ;
340- int fd ;
341339 struct pretty_print_context ctx = {0 };
342340
343341 printf (_ ("Squash commit -- not updating HEAD\n" ));
344- filename = git_path_squash_msg ();
345- fd = open (filename , O_WRONLY | O_CREAT , 0666 );
346- if (fd < 0 )
347- die_errno (_ ("Could not write to '%s'" ), filename );
348342
349343 init_revisions (& rev , NULL );
350344 rev .ignore_merges = 1 ;
@@ -371,10 +365,7 @@ static void squash_message(struct commit *commit, struct commit_list *remotehead
371365 oid_to_hex (& commit -> object .oid ));
372366 pretty_print_commit (& ctx , commit , & out );
373367 }
374- if (write_in_full (fd , out .buf , out .len ) != out .len )
375- die_errno (_ ("Writing SQUASH_MSG" ));
376- if (close (fd ))
377- die_errno (_ ("Finishing SQUASH_MSG" ));
368+ write_file_buf (git_path_squash_msg (), out .buf , out .len );
378369 strbuf_release (& out );
379370}
380371
@@ -756,18 +747,6 @@ static void add_strategies(const char *string, unsigned attr)
756747
757748}
758749
759- static void write_merge_msg (struct strbuf * msg )
760- {
761- const char * filename = git_path_merge_msg ();
762- int fd = open (filename , O_WRONLY | O_CREAT , 0666 );
763- if (fd < 0 )
764- die_errno (_ ("Could not open '%s' for writing" ),
765- filename );
766- if (write_in_full (fd , msg -> buf , msg -> len ) != msg -> len )
767- die_errno (_ ("Could not write to '%s'" ), filename );
768- close (fd );
769- }
770-
771750static void read_merge_msg (struct strbuf * msg )
772751{
773752 const char * filename = git_path_merge_msg ();
@@ -801,7 +780,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)
801780 strbuf_addch (& msg , '\n' );
802781 if (0 < option_edit )
803782 strbuf_commented_addf (& msg , _ (merge_editor_comment ), comment_line_char );
804- write_merge_msg ( & msg );
783+ write_file_buf ( git_path_merge_msg (), msg . buf , msg . len );
805784 if (run_commit_hook (0 < option_edit , get_index_file (), "prepare-commit-msg" ,
806785 git_path_merge_msg (), "merge" , NULL ))
807786 abort_commit (remoteheads , NULL );
@@ -964,8 +943,6 @@ static int setup_with_upstream(const char ***argv)
964943
965944static void write_merge_state (struct commit_list * remoteheads )
966945{
967- const char * filename ;
968- int fd ;
969946 struct commit_list * j ;
970947 struct strbuf buf = STRBUF_INIT ;
971948
@@ -979,26 +956,14 @@ static void write_merge_state(struct commit_list *remoteheads)
979956 }
980957 strbuf_addf (& buf , "%s\n" , oid_to_hex (oid ));
981958 }
982- filename = git_path_merge_head ();
983- fd = open (filename , O_WRONLY | O_CREAT , 0666 );
984- if (fd < 0 )
985- die_errno (_ ("Could not open '%s' for writing" ), filename );
986- if (write_in_full (fd , buf .buf , buf .len ) != buf .len )
987- die_errno (_ ("Could not write to '%s'" ), filename );
988- close (fd );
959+ write_file_buf (git_path_merge_head (), buf .buf , buf .len );
989960 strbuf_addch (& merge_msg , '\n' );
990- write_merge_msg ( & merge_msg );
961+ write_file_buf ( git_path_merge_msg (), merge_msg . buf , merge_msg . len );
991962
992- filename = git_path_merge_mode ();
993- fd = open (filename , O_WRONLY | O_CREAT | O_TRUNC , 0666 );
994- if (fd < 0 )
995- die_errno (_ ("Could not open '%s' for writing" ), filename );
996963 strbuf_reset (& buf );
997964 if (fast_forward == FF_NO )
998965 strbuf_addf (& buf , "no-ff" );
999- if (write_in_full (fd , buf .buf , buf .len ) != buf .len )
1000- die_errno (_ ("Could not write to '%s'" ), filename );
1001- close (fd );
966+ write_file_buf (git_path_merge_mode (), buf .buf , buf .len );
1002967}
1003968
1004969static int default_edit_option (void )
0 commit comments