@@ -95,9 +95,19 @@ static const char note_template[] =
95
95
struct note_data {
96
96
int given ;
97
97
int use_editor ;
98
+ char * edit_path ;
98
99
struct strbuf buf ;
99
100
};
100
101
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
+
101
111
static int list_each_note (const unsigned char * object_sha1 ,
102
112
const unsigned char * note_sha1 , char * note_path ,
103
113
void * cb_data )
@@ -153,17 +163,15 @@ static void create_note(const unsigned char *object, struct note_data *d,
153
163
int append_only , const unsigned char * prev ,
154
164
unsigned char * result )
155
165
{
156
- char * path = NULL ;
157
-
158
166
if (d -> use_editor || !d -> given ) {
159
167
int fd ;
160
168
struct strbuf buf = STRBUF_INIT ;
161
169
162
170
/* 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 );
165
173
if (fd < 0 )
166
- die_errno (_ ("could not create file '%s'" ), path );
174
+ die_errno (_ ("could not create file '%s'" ), d -> edit_path );
167
175
168
176
if (d -> given )
169
177
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,
181
189
strbuf_release (& buf );
182
190
strbuf_reset (& d -> buf );
183
191
184
- if (launch_editor (path , & d -> buf , NULL )) {
192
+ if (launch_editor (d -> edit_path , & d -> buf , NULL )) {
185
193
die (_ ("Please supply the note contents using either -m or -F option" ));
186
194
}
187
195
stripspace (& d -> buf , 1 );
@@ -208,17 +216,12 @@ static void create_note(const unsigned char *object, struct note_data *d,
208
216
} else {
209
217
if (write_sha1_file (d -> buf .buf , d -> buf .len , blob_type , result )) {
210
218
error (_ ("unable to write note object" ));
211
- if (path )
219
+ if (d -> edit_path )
212
220
error (_ ("The note contents have been left in %s" ),
213
- path );
221
+ d -> edit_path );
214
222
exit (128 );
215
223
}
216
224
}
217
-
218
- if (path ) {
219
- unlink_or_warn (path );
220
- free (path );
221
- }
222
225
}
223
226
224
227
static 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)
402
405
unsigned char object [20 ], new_note [20 ];
403
406
char logmsg [100 ];
404
407
const unsigned char * note ;
405
- struct note_data d = { 0 , 0 , STRBUF_INIT };
408
+ struct note_data d = { 0 , 0 , NULL , STRBUF_INIT };
406
409
struct option options [] = {
407
410
{ OPTION_CALLBACK , 'm' , "message" , & d , N_ ("message" ),
408
411
N_ ("note contents as a string" ), PARSE_OPT_NONEG ,
@@ -447,6 +450,7 @@ static int add(int argc, const char **argv, const char *prefix)
447
450
* therefore still in argv[0-1].
448
451
*/
449
452
argv [0 ] = "edit" ;
453
+ free_note_data (& d );
450
454
free_notes (t );
451
455
return append_edit (argc , argv , prefix );
452
456
}
@@ -460,6 +464,7 @@ static int add(int argc, const char **argv, const char *prefix)
460
464
}
461
465
462
466
create_note (object , & d , 0 , note , new_note );
467
+ free_note_data (& d );
463
468
464
469
if (is_null_sha1 (new_note ))
465
470
remove_note (t , object );
@@ -471,7 +476,6 @@ static int add(int argc, const char **argv, const char *prefix)
471
476
commit_notes (t , logmsg );
472
477
out :
473
478
free_notes (t );
474
- strbuf_release (& d .buf );
475
479
return retval ;
476
480
}
477
481
@@ -559,7 +563,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
559
563
const unsigned char * note ;
560
564
char logmsg [100 ];
561
565
const char * const * usage ;
562
- struct note_data d = { 0 , 0 , STRBUF_INIT };
566
+ struct note_data d = { 0 , 0 , NULL , STRBUF_INIT };
563
567
struct option options [] = {
564
568
{ OPTION_CALLBACK , 'm' , "message" , & d , N_ ("message" ),
565
569
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)
600
604
note = get_note (t , object );
601
605
602
606
create_note (object , & d , !edit , note , new_note );
607
+ free_note_data (& d );
603
608
604
609
if (is_null_sha1 (new_note ))
605
610
remove_note (t , object );
@@ -610,7 +615,6 @@ static int append_edit(int argc, const char **argv, const char *prefix)
610
615
is_null_sha1 (new_note ) ? "removed" : "added" , argv [0 ]);
611
616
commit_notes (t , logmsg );
612
617
free_notes (t );
613
- strbuf_release (& d .buf );
614
618
return 0 ;
615
619
}
616
620
0 commit comments