@@ -194,6 +194,27 @@ static inline const char *am_path(const struct am_state *state, const char *path
194194 return mkpath ("%s/%s" , state -> dir , path );
195195}
196196
197+ /**
198+ * For convenience to call write_file()
199+ */
200+ static int write_state_text (const struct am_state * state ,
201+ const char * name , const char * string )
202+ {
203+ return write_file (am_path (state , name ), "%s" , string );
204+ }
205+
206+ static int write_state_count (const struct am_state * state ,
207+ const char * name , int value )
208+ {
209+ return write_file (am_path (state , name ), "%d" , value );
210+ }
211+
212+ static int write_state_bool (const struct am_state * state ,
213+ const char * name , int value )
214+ {
215+ return write_state_text (state , name , value ? "t" : "f" );
216+ }
217+
197218/**
198219 * If state->quiet is false, calls fprintf(fp, fmt, ...), and appends a newline
199220 * at the end.
@@ -363,7 +384,7 @@ static void write_author_script(const struct am_state *state)
363384 sq_quote_buf (& sb , state -> author_date );
364385 strbuf_addch (& sb , '\n' );
365386
366- write_file ( am_path ( state , "author-script" ), 1 , "%s " , sb .buf );
387+ write_state_text ( state , "author-script" , sb .buf );
367388
368389 strbuf_release (& sb );
369390}
@@ -1001,13 +1022,10 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
10011022 if (state -> rebasing )
10021023 state -> threeway = 1 ;
10031024
1004- write_file (am_path (state , "threeway" ), 1 , state -> threeway ? "t" : "f" );
1005-
1006- write_file (am_path (state , "quiet" ), 1 , state -> quiet ? "t" : "f" );
1007-
1008- write_file (am_path (state , "sign" ), 1 , state -> signoff ? "t" : "f" );
1009-
1010- write_file (am_path (state , "utf8" ), 1 , state -> utf8 ? "t" : "f" );
1025+ write_state_bool (state , "threeway" , state -> threeway );
1026+ write_state_bool (state , "quiet" , state -> quiet );
1027+ write_state_bool (state , "sign" , state -> signoff );
1028+ write_state_bool (state , "utf8" , state -> utf8 );
10111029
10121030 switch (state -> keep ) {
10131031 case KEEP_FALSE :
@@ -1023,9 +1041,8 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
10231041 die ("BUG: invalid value for state->keep" );
10241042 }
10251043
1026- write_file (am_path (state , "keep" ), 1 , "%s" , str );
1027-
1028- write_file (am_path (state , "messageid" ), 1 , state -> message_id ? "t" : "f" );
1044+ write_state_text (state , "keep" , str );
1045+ write_state_bool (state , "messageid" , state -> message_id );
10291046
10301047 switch (state -> scissors ) {
10311048 case SCISSORS_UNSET :
@@ -1040,24 +1057,23 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
10401057 default :
10411058 die ("BUG: invalid value for state->scissors" );
10421059 }
1043-
1044- write_file (am_path (state , "scissors" ), 1 , "%s" , str );
1060+ write_state_text (state , "scissors" , str );
10451061
10461062 sq_quote_argv (& sb , state -> git_apply_opts .argv , 0 );
1047- write_file ( am_path ( state , "apply-opt" ), 1 , "%s " , sb .buf );
1063+ write_state_text ( state , "apply-opt" , sb .buf );
10481064
10491065 if (state -> rebasing )
1050- write_file ( am_path ( state , "rebasing" ), 1 , "%s " , "" );
1066+ write_state_text ( state , "rebasing" , "" );
10511067 else
1052- write_file ( am_path ( state , "applying" ), 1 , "%s " , "" );
1068+ write_state_text ( state , "applying" , "" );
10531069
10541070 if (!get_sha1 ("HEAD" , curr_head )) {
1055- write_file ( am_path ( state , "abort-safety" ), 1 , "%s " , sha1_to_hex (curr_head ));
1071+ write_state_text ( state , "abort-safety" , sha1_to_hex (curr_head ));
10561072 if (!state -> rebasing )
10571073 update_ref ("am" , "ORIG_HEAD" , curr_head , NULL , 0 ,
10581074 UPDATE_REFS_DIE_ON_ERR );
10591075 } else {
1060- write_file ( am_path ( state , "abort-safety" ), 1 , "%s " , "" );
1076+ write_state_text ( state , "abort-safety" , "" );
10611077 if (!state -> rebasing )
10621078 delete_ref ("ORIG_HEAD" , NULL , 0 );
10631079 }
@@ -1067,9 +1083,8 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
10671083 * session is in progress, they should be written last.
10681084 */
10691085
1070- write_file (am_path (state , "next" ), 1 , "%d" , state -> cur );
1071-
1072- write_file (am_path (state , "last" ), 1 , "%d" , state -> last );
1086+ write_state_count (state , "next" , state -> cur );
1087+ write_state_count (state , "last" , state -> last );
10731088
10741089 strbuf_release (& sb );
10751090}
@@ -1102,12 +1117,12 @@ static void am_next(struct am_state *state)
11021117 unlink (am_path (state , "original-commit" ));
11031118
11041119 if (!get_sha1 ("HEAD" , head ))
1105- write_file ( am_path ( state , "abort-safety" ), 1 , "%s " , sha1_to_hex (head ));
1120+ write_state_text ( state , "abort-safety" , sha1_to_hex (head ));
11061121 else
1107- write_file ( am_path ( state , "abort-safety" ), 1 , "%s " , "" );
1122+ write_state_text ( state , "abort-safety" , "" );
11081123
11091124 state -> cur ++ ;
1110- write_file ( am_path ( state , "next" ), 1 , "%d " , state -> cur );
1125+ write_state_count ( state , "next" , state -> cur );
11111126}
11121127
11131128/**
@@ -1480,8 +1495,7 @@ static int parse_mail_rebase(struct am_state *state, const char *mail)
14801495 write_commit_patch (state , commit );
14811496
14821497 hashcpy (state -> orig_commit , commit_sha1 );
1483- write_file (am_path (state , "original-commit" ), 1 , "%s" ,
1484- sha1_to_hex (commit_sha1 ));
1498+ write_state_text (state , "original-commit" , sha1_to_hex (commit_sha1 ));
14851499
14861500 return 0 ;
14871501}
@@ -1783,7 +1797,7 @@ static void am_run(struct am_state *state, int resume)
17831797 refresh_and_write_cache ();
17841798
17851799 if (index_has_changes (& sb )) {
1786- write_file ( am_path ( state , "dirtyindex" ) , 1 , "t" );
1800+ write_state_bool ( state , "dirtyindex" , 1 );
17871801 die (_ ("Dirty index: cannot apply patches (dirty: %s)" ), sb .buf );
17881802 }
17891803
0 commit comments