@@ -95,9 +95,19 @@ static const char note_template[] =
9595struct note_data {
9696 int given ;
9797 int use_editor ;
98+ char * edit_path ;
9899 struct strbuf buf ;
99100};
100101
102+ static void free_note_data (struct note_data * d )
103+ {
104+ if (d -> edit_path ) {
105+ unlink_or_warn (d -> edit_path );
106+ free (d -> edit_path );
107+ }
108+ strbuf_release (& d -> buf );
109+ }
110+
101111static int list_each_note (const unsigned char * object_sha1 ,
102112 const unsigned char * note_sha1 , char * note_path ,
103113 void * cb_data )
@@ -153,17 +163,15 @@ static void create_note(const unsigned char *object, struct note_data *d,
153163 int append_only , const unsigned char * prev ,
154164 unsigned char * result )
155165{
156- char * path = NULL ;
157-
158166 if (d -> use_editor || !d -> given ) {
159167 int fd ;
160168 struct strbuf buf = STRBUF_INIT ;
161169
162170 /* write the template message before editing: */
163- path = git_pathdup ("NOTES_EDITMSG" );
164- fd = open (path , O_CREAT | O_TRUNC | O_WRONLY , 0600 );
171+ d -> edit_path = git_pathdup ("NOTES_EDITMSG" );
172+ fd = open (d -> edit_path , O_CREAT | O_TRUNC | O_WRONLY , 0600 );
165173 if (fd < 0 )
166- die_errno (_ ("could not create file '%s'" ), path );
174+ die_errno (_ ("could not create file '%s'" ), d -> edit_path );
167175
168176 if (d -> given )
169177 write_or_die (fd , d -> buf .buf , d -> buf .len );
@@ -181,7 +189,7 @@ static void create_note(const unsigned char *object, struct note_data *d,
181189 strbuf_release (& buf );
182190 strbuf_reset (& d -> buf );
183191
184- if (launch_editor (path , & d -> buf , NULL )) {
192+ if (launch_editor (d -> edit_path , & d -> buf , NULL )) {
185193 die (_ ("Please supply the note contents using either -m or -F option" ));
186194 }
187195 stripspace (& d -> buf , 1 );
@@ -208,17 +216,12 @@ static void create_note(const unsigned char *object, struct note_data *d,
208216 } else {
209217 if (write_sha1_file (d -> buf .buf , d -> buf .len , blob_type , result )) {
210218 error (_ ("unable to write note object" ));
211- if (path )
219+ if (d -> edit_path )
212220 error (_ ("The note contents have been left in %s" ),
213- path );
221+ d -> edit_path );
214222 exit (128 );
215223 }
216224 }
217-
218- if (path ) {
219- unlink_or_warn (path );
220- free (path );
221- }
222225}
223226
224227static int parse_msg_arg (const struct option * opt , const char * arg , int unset )
@@ -402,7 +405,7 @@ static int add(int argc, const char **argv, const char *prefix)
402405 unsigned char object [20 ], new_note [20 ];
403406 char logmsg [100 ];
404407 const unsigned char * note ;
405- struct note_data d = { 0 , 0 , STRBUF_INIT };
408+ struct note_data d = { 0 , 0 , NULL , STRBUF_INIT };
406409 struct option options [] = {
407410 { OPTION_CALLBACK , 'm' , "message" , & d , N_ ("message" ),
408411 N_ ("note contents as a string" ), PARSE_OPT_NONEG ,
@@ -447,6 +450,7 @@ static int add(int argc, const char **argv, const char *prefix)
447450 * therefore still in argv[0-1].
448451 */
449452 argv [0 ] = "edit" ;
453+ free_note_data (& d );
450454 free_notes (t );
451455 return append_edit (argc , argv , prefix );
452456 }
@@ -460,6 +464,7 @@ static int add(int argc, const char **argv, const char *prefix)
460464 }
461465
462466 create_note (object , & d , 0 , note , new_note );
467+ free_note_data (& d );
463468
464469 if (is_null_sha1 (new_note ))
465470 remove_note (t , object );
@@ -471,7 +476,6 @@ static int add(int argc, const char **argv, const char *prefix)
471476 commit_notes (t , logmsg );
472477out :
473478 free_notes (t );
474- strbuf_release (& d .buf );
475479 return retval ;
476480}
477481
@@ -559,7 +563,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
559563 const unsigned char * note ;
560564 char logmsg [100 ];
561565 const char * const * usage ;
562- struct note_data d = { 0 , 0 , STRBUF_INIT };
566+ struct note_data d = { 0 , 0 , NULL , STRBUF_INIT };
563567 struct option options [] = {
564568 { OPTION_CALLBACK , 'm' , "message" , & d , N_ ("message" ),
565569 N_ ("note contents as a string" ), PARSE_OPT_NONEG ,
@@ -600,6 +604,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
600604 note = get_note (t , object );
601605
602606 create_note (object , & d , !edit , note , new_note );
607+ free_note_data (& d );
603608
604609 if (is_null_sha1 (new_note ))
605610 remove_note (t , object );
@@ -610,7 +615,6 @@ static int append_edit(int argc, const char **argv, const char *prefix)
610615 is_null_sha1 (new_note ) ? "removed" : "added" , argv [0 ]);
611616 commit_notes (t , logmsg );
612617 free_notes (t );
613- strbuf_release (& d .buf );
614618 return 0 ;
615619}
616620
0 commit comments